function-call.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
  2. import { extend, isObject, inBrowser } 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. const getDefaultOptions = () => ({
  26. type: "danger",
  27. color: void 0,
  28. message: "",
  29. onClose: void 0,
  30. onClick: void 0,
  31. onOpened: void 0,
  32. duration: 3e3,
  33. position: void 0,
  34. className: "",
  35. lockScroll: false,
  36. background: void 0
  37. });
  38. let currentOptions = getDefaultOptions();
  39. const closeNotify = () => {
  40. if (instance) {
  41. instance.toggle(false);
  42. }
  43. };
  44. function showNotify(options) {
  45. if (!inBrowser) {
  46. return;
  47. }
  48. if (!instance) {
  49. initInstance();
  50. }
  51. options = extend({}, currentOptions, parseOptions(options));
  52. instance.open(options);
  53. clearTimeout(timer);
  54. if (options.duration > 0) {
  55. timer = setTimeout(closeNotify, options.duration);
  56. }
  57. return instance;
  58. }
  59. const setNotifyDefaultOptions = (options) => extend(currentOptions, options);
  60. const resetNotifyDefaultOptions = () => {
  61. currentOptions = getDefaultOptions();
  62. };
  63. export {
  64. closeNotify,
  65. resetNotifyDefaultOptions,
  66. setNotifyDefaultOptions,
  67. showNotify
  68. };