function-call.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. var __create = Object.create;
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __getProtoOf = Object.getPrototypeOf;
  6. var __hasOwnProp = Object.prototype.hasOwnProperty;
  7. var __export = (target, all) => {
  8. for (var name in all)
  9. __defProp(target, name, { get: all[name], enumerable: true });
  10. };
  11. var __copyProps = (to, from, except, desc) => {
  12. if (from && typeof from === "object" || typeof from === "function") {
  13. for (let key of __getOwnPropNames(from))
  14. if (!__hasOwnProp.call(to, key) && key !== except)
  15. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  16. }
  17. return to;
  18. };
  19. var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  20. // If the importer is in node compatibility mode or this is not an ESM
  21. // file that has been converted to a CommonJS file using a Babel-
  22. // compatible transform (i.e. "__esModule" has not been set), then set
  23. // "default" to the CommonJS "module.exports" for node compatibility.
  24. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  25. mod
  26. ));
  27. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  28. var stdin_exports = {};
  29. __export(stdin_exports, {
  30. allowMultipleToast: () => allowMultipleToast,
  31. closeToast: () => closeToast,
  32. resetToastDefaultOptions: () => resetToastDefaultOptions,
  33. setToastDefaultOptions: () => setToastDefaultOptions,
  34. showFailToast: () => showFailToast,
  35. showLoadingToast: () => showLoadingToast,
  36. showSuccessToast: () => showSuccessToast,
  37. showToast: () => showToast
  38. });
  39. module.exports = __toCommonJS(stdin_exports);
  40. var import_vue = require("vue");
  41. var import_utils = require("../utils");
  42. var import_mount_component = require("../utils/mount-component");
  43. var import_Toast = __toESM(require("./Toast"));
  44. const defaultOptions = {
  45. icon: "",
  46. type: "text",
  47. message: "",
  48. className: "",
  49. overlay: false,
  50. onClose: void 0,
  51. onOpened: void 0,
  52. duration: 2e3,
  53. teleport: "body",
  54. iconSize: void 0,
  55. iconPrefix: void 0,
  56. position: "middle",
  57. transition: "van-fade",
  58. forbidClick: false,
  59. loadingType: void 0,
  60. overlayClass: "",
  61. overlayStyle: void 0,
  62. closeOnClick: false,
  63. closeOnClickOverlay: false
  64. };
  65. let queue = [];
  66. let allowMultiple = false;
  67. let currentOptions = (0, import_utils.extend)({}, defaultOptions);
  68. const defaultOptionsMap = /* @__PURE__ */ new Map();
  69. function parseOptions(message) {
  70. if ((0, import_utils.isObject)(message)) {
  71. return message;
  72. }
  73. return {
  74. message
  75. };
  76. }
  77. function createInstance() {
  78. const {
  79. instance,
  80. unmount
  81. } = (0, import_mount_component.mountComponent)({
  82. setup() {
  83. const message = (0, import_vue.ref)("");
  84. const {
  85. open,
  86. state,
  87. close,
  88. toggle
  89. } = (0, import_mount_component.usePopupState)();
  90. const onClosed = () => {
  91. if (allowMultiple) {
  92. queue = queue.filter((item) => item !== instance);
  93. unmount();
  94. }
  95. };
  96. const render = () => {
  97. const attrs = {
  98. onClosed,
  99. "onUpdate:show": toggle
  100. };
  101. return (0, import_vue.createVNode)(import_Toast.default, (0, import_vue.mergeProps)(state, attrs), null);
  102. };
  103. (0, import_vue.watch)(message, (val) => {
  104. state.message = val;
  105. });
  106. (0, import_vue.getCurrentInstance)().render = render;
  107. return {
  108. open,
  109. close,
  110. message
  111. };
  112. }
  113. });
  114. return instance;
  115. }
  116. function getInstance() {
  117. if (!queue.length || allowMultiple) {
  118. const instance = createInstance();
  119. queue.push(instance);
  120. }
  121. return queue[queue.length - 1];
  122. }
  123. function showToast(options = {}) {
  124. if (!import_utils.inBrowser) {
  125. return {};
  126. }
  127. const toast = getInstance();
  128. const parsedOptions = parseOptions(options);
  129. toast.open((0, import_utils.extend)({}, currentOptions, defaultOptionsMap.get(parsedOptions.type || currentOptions.type), parsedOptions));
  130. return toast;
  131. }
  132. const createMethod = (type) => (options) => showToast((0, import_utils.extend)({
  133. type
  134. }, parseOptions(options)));
  135. const showLoadingToast = createMethod("loading");
  136. const showSuccessToast = createMethod("success");
  137. const showFailToast = createMethod("fail");
  138. const closeToast = (all) => {
  139. var _a;
  140. if (queue.length) {
  141. if (all) {
  142. queue.forEach((toast) => {
  143. toast.close();
  144. });
  145. queue = [];
  146. } else if (!allowMultiple) {
  147. queue[0].close();
  148. } else {
  149. (_a = queue.shift()) == null ? void 0 : _a.close();
  150. }
  151. }
  152. };
  153. function setToastDefaultOptions(type, options) {
  154. if (typeof type === "string") {
  155. defaultOptionsMap.set(type, options);
  156. } else {
  157. (0, import_utils.extend)(currentOptions, type);
  158. }
  159. }
  160. const resetToastDefaultOptions = (type) => {
  161. if (typeof type === "string") {
  162. defaultOptionsMap.delete(type);
  163. } else {
  164. currentOptions = (0, import_utils.extend)({}, defaultOptions);
  165. defaultOptionsMap.clear();
  166. }
  167. };
  168. const allowMultipleToast = (value = true) => {
  169. allowMultiple = value;
  170. };