function-call.mjs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
  2. import { extend, inBrowser } from "../utils/index.mjs";
  3. import { mountComponent, usePopupState } from "../utils/mount-component.mjs";
  4. import Dialog from "./Dialog.mjs";
  5. let instance;
  6. const DEFAULT_OPTIONS = {
  7. title: "",
  8. width: "",
  9. theme: null,
  10. message: "",
  11. overlay: true,
  12. callback: null,
  13. teleport: "body",
  14. className: "",
  15. allowHtml: false,
  16. lockScroll: true,
  17. transition: void 0,
  18. beforeClose: null,
  19. overlayClass: "",
  20. overlayStyle: void 0,
  21. messageAlign: "",
  22. cancelButtonText: "",
  23. cancelButtonColor: null,
  24. cancelButtonDisabled: false,
  25. confirmButtonText: "",
  26. confirmButtonColor: null,
  27. confirmButtonDisabled: false,
  28. showConfirmButton: true,
  29. showCancelButton: false,
  30. closeOnPopstate: true,
  31. closeOnClickOverlay: false
  32. };
  33. let currentOptions = extend({}, DEFAULT_OPTIONS);
  34. function initInstance() {
  35. const Wrapper = {
  36. setup() {
  37. const {
  38. state,
  39. toggle
  40. } = usePopupState();
  41. return () => _createVNode(Dialog, _mergeProps(state, {
  42. "onUpdate:show": toggle
  43. }), null);
  44. }
  45. };
  46. ({
  47. instance
  48. } = mountComponent(Wrapper));
  49. }
  50. function showDialog(options) {
  51. if (!inBrowser) {
  52. return Promise.resolve(void 0);
  53. }
  54. return new Promise((resolve, reject) => {
  55. if (!instance) {
  56. initInstance();
  57. }
  58. instance.open(extend({}, currentOptions, options, {
  59. callback: (action) => {
  60. (action === "confirm" ? resolve : reject)(action);
  61. }
  62. }));
  63. });
  64. }
  65. const setDialogDefaultOptions = (options) => {
  66. extend(currentOptions, options);
  67. };
  68. const resetDialogDefaultOptions = () => {
  69. currentOptions = extend({}, DEFAULT_OPTIONS);
  70. };
  71. const showConfirmDialog = (options) => showDialog(extend({
  72. showCancelButton: true
  73. }, options));
  74. const closeDialog = () => {
  75. if (instance) {
  76. instance.toggle(false);
  77. }
  78. };
  79. export {
  80. closeDialog,
  81. resetDialogDefaultOptions,
  82. setDialogDefaultOptions,
  83. showConfirmDialog,
  84. showDialog
  85. };