function-call.mjs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
  2. import { extend, isObject, inBrowser, withInstall } from "../utils/index.mjs";
  3. import { mountComponent, usePopupState } from "../utils/mount-component.mjs";
  4. import VanNotify from "./Notify.mjs";
  5. let timer;
  6. let instance;
  7. const parseOptions = (message) => isObject(message) ? message : {
  8. message
  9. };
  10. function initInstance() {
  11. ({
  12. instance
  13. } = mountComponent({
  14. setup() {
  15. const {
  16. state,
  17. toggle
  18. } = usePopupState();
  19. return () => _createVNode(VanNotify, _mergeProps(state, {
  20. "onUpdate:show": toggle
  21. }), null);
  22. }
  23. }));
  24. }
  25. function Notify(options) {
  26. if (!inBrowser) {
  27. return;
  28. }
  29. if (!instance) {
  30. initInstance();
  31. }
  32. options = extend({}, Notify.currentOptions, parseOptions(options));
  33. instance.open(options);
  34. clearTimeout(timer);
  35. if (options.duration > 0) {
  36. timer = window.setTimeout(Notify.clear, options.duration);
  37. }
  38. return instance;
  39. }
  40. const getDefaultOptions = () => ({
  41. type: "danger",
  42. color: void 0,
  43. message: "",
  44. onClose: void 0,
  45. onClick: void 0,
  46. onOpened: void 0,
  47. duration: 3e3,
  48. position: void 0,
  49. className: "",
  50. lockScroll: false,
  51. background: void 0
  52. });
  53. Notify.clear = () => {
  54. if (instance) {
  55. instance.toggle(false);
  56. }
  57. };
  58. Notify.currentOptions = getDefaultOptions();
  59. Notify.setDefaultOptions = (options) => {
  60. extend(Notify.currentOptions, options);
  61. };
  62. Notify.resetDefaultOptions = () => {
  63. Notify.currentOptions = getDefaultOptions();
  64. };
  65. Notify.Component = withInstall(VanNotify);
  66. Notify.install = (app) => {
  67. app.use(Notify.Component);
  68. app.config.globalProperties.$notify = Notify;
  69. };
  70. export {
  71. Notify
  72. };