function-call.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  21. mod
  22. ));
  23. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  24. var stdin_exports = {};
  25. __export(stdin_exports, {
  26. Toast: () => Toast
  27. });
  28. module.exports = __toCommonJS(stdin_exports);
  29. var import_vue = require("vue");
  30. var import_vue2 = require("vue");
  31. var import_utils = require("../utils");
  32. var import_mount_component = require("../utils/mount-component");
  33. var import_Toast = __toESM(require("./Toast"));
  34. const defaultOptions = {
  35. icon: "",
  36. type: "text",
  37. message: "",
  38. className: "",
  39. overlay: false,
  40. onClose: void 0,
  41. onOpened: void 0,
  42. duration: 2e3,
  43. teleport: "body",
  44. iconSize: void 0,
  45. iconPrefix: void 0,
  46. position: "middle",
  47. transition: "van-fade",
  48. forbidClick: false,
  49. loadingType: void 0,
  50. overlayClass: "",
  51. overlayStyle: void 0,
  52. closeOnClick: false,
  53. closeOnClickOverlay: false
  54. };
  55. let queue = [];
  56. let allowMultiple = false;
  57. let currentOptions = (0, import_utils.extend)({}, defaultOptions);
  58. const defaultOptionsMap = /* @__PURE__ */ new Map();
  59. function parseOptions(message) {
  60. if ((0, import_utils.isObject)(message)) {
  61. return message;
  62. }
  63. return {
  64. message
  65. };
  66. }
  67. function createInstance() {
  68. const {
  69. instance,
  70. unmount
  71. } = (0, import_mount_component.mountComponent)({
  72. setup() {
  73. const message = (0, import_vue2.ref)("");
  74. const {
  75. open,
  76. state,
  77. close,
  78. toggle
  79. } = (0, import_mount_component.usePopupState)();
  80. const onClosed = () => {
  81. if (allowMultiple) {
  82. queue = queue.filter((item) => item !== instance);
  83. unmount();
  84. }
  85. };
  86. const render = () => {
  87. const attrs = {
  88. onClosed,
  89. "onUpdate:show": toggle
  90. };
  91. return (0, import_vue.createVNode)(import_Toast.default, (0, import_vue.mergeProps)(state, attrs), null);
  92. };
  93. (0, import_vue2.watch)(message, (val) => {
  94. state.message = val;
  95. });
  96. (0, import_vue2.getCurrentInstance)().render = render;
  97. return {
  98. open,
  99. clear: close,
  100. message
  101. };
  102. }
  103. });
  104. return instance;
  105. }
  106. function getInstance() {
  107. if (!queue.length || allowMultiple) {
  108. const instance = createInstance();
  109. queue.push(instance);
  110. }
  111. return queue[queue.length - 1];
  112. }
  113. function Toast(options = {}) {
  114. if (!import_utils.inBrowser) {
  115. return {};
  116. }
  117. const toast = getInstance();
  118. const parsedOptions = parseOptions(options);
  119. toast.open((0, import_utils.extend)({}, currentOptions, defaultOptionsMap.get(parsedOptions.type || currentOptions.type), parsedOptions));
  120. return toast;
  121. }
  122. const createMethod = (type) => (options) => Toast((0, import_utils.extend)({
  123. type
  124. }, parseOptions(options)));
  125. Toast.loading = createMethod("loading");
  126. Toast.success = createMethod("success");
  127. Toast.fail = createMethod("fail");
  128. Toast.clear = (all) => {
  129. var _a;
  130. if (queue.length) {
  131. if (all) {
  132. queue.forEach((toast) => {
  133. toast.clear();
  134. });
  135. queue = [];
  136. } else if (!allowMultiple) {
  137. queue[0].clear();
  138. } else {
  139. (_a = queue.shift()) == null ? void 0 : _a.clear();
  140. }
  141. }
  142. };
  143. function setDefaultOptions(type, options) {
  144. if (typeof type === "string") {
  145. defaultOptionsMap.set(type, options);
  146. } else {
  147. (0, import_utils.extend)(currentOptions, type);
  148. }
  149. }
  150. Toast.setDefaultOptions = setDefaultOptions;
  151. Toast.resetDefaultOptions = (type) => {
  152. if (typeof type === "string") {
  153. defaultOptionsMap.delete(type);
  154. } else {
  155. currentOptions = (0, import_utils.extend)({}, defaultOptions);
  156. defaultOptionsMap.clear();
  157. }
  158. };
  159. Toast.allowMultiple = (value = true) => {
  160. allowMultiple = value;
  161. };
  162. Toast.install = (app) => {
  163. app.use((0, import_utils.withInstall)(import_Toast.default));
  164. app.config.globalProperties.$toast = Toast;
  165. };