function-call.mjs 2.1 KB

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