142 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			142 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| Object.defineProperty(exports, '__esModule', { value: true });
 | |
| 
 | |
| var vue = require('vue');
 | |
| var index = require('./index.js');
 | |
| var core = require('@vueuse/core');
 | |
| var shared = require('@vue/shared');
 | |
| var types = require('../../../utils/types.js');
 | |
| 
 | |
| const messageInstance = /* @__PURE__ */ new Map();
 | |
| const getAppendToElement = (props) => {
 | |
|   let appendTo = document.body;
 | |
|   if (props.appendTo) {
 | |
|     if (shared.isString(props.appendTo)) {
 | |
|       appendTo = document.querySelector(props.appendTo);
 | |
|     }
 | |
|     if (types.isElement(props.appendTo)) {
 | |
|       appendTo = props.appendTo;
 | |
|     }
 | |
|     if (!types.isElement(appendTo)) {
 | |
|       appendTo = document.body;
 | |
|     }
 | |
|   }
 | |
|   return appendTo;
 | |
| };
 | |
| const initInstance = (props, container, appContext = null) => {
 | |
|   const vnode = vue.createVNode(index["default"], props, shared.isFunction(props.message) || vue.isVNode(props.message) ? {
 | |
|     default: shared.isFunction(props.message) ? props.message : () => props.message
 | |
|   } : null);
 | |
|   vnode.appContext = appContext;
 | |
|   vue.render(vnode, container);
 | |
|   getAppendToElement(props).appendChild(container.firstElementChild);
 | |
|   return vnode.component;
 | |
| };
 | |
| const genContainer = () => {
 | |
|   return document.createElement("div");
 | |
| };
 | |
| const showMessage = (options, appContext) => {
 | |
|   const container = genContainer();
 | |
|   options.onVanish = () => {
 | |
|     vue.render(null, container);
 | |
|     messageInstance.delete(vm);
 | |
|   };
 | |
|   options.onAction = (action) => {
 | |
|     const currentMsg = messageInstance.get(vm);
 | |
|     let resolve;
 | |
|     if (options.showInput) {
 | |
|       resolve = { value: vm.inputValue, action };
 | |
|     } else {
 | |
|       resolve = action;
 | |
|     }
 | |
|     if (options.callback) {
 | |
|       options.callback(resolve, instance.proxy);
 | |
|     } else {
 | |
|       if (action === "cancel" || action === "close") {
 | |
|         if (options.distinguishCancelAndClose && action !== "cancel") {
 | |
|           currentMsg.reject("close");
 | |
|         } else {
 | |
|           currentMsg.reject("cancel");
 | |
|         }
 | |
|       } else {
 | |
|         currentMsg.resolve(resolve);
 | |
|       }
 | |
|     }
 | |
|   };
 | |
|   const instance = initInstance(options, container, appContext);
 | |
|   const vm = instance.proxy;
 | |
|   for (const prop in options) {
 | |
|     if (shared.hasOwn(options, prop) && !shared.hasOwn(vm.$props, prop)) {
 | |
|       if (prop === "closeIcon" && shared.isObject(options[prop])) {
 | |
|         vm[prop] = vue.markRaw(options[prop]);
 | |
|       } else {
 | |
|         vm[prop] = options[prop];
 | |
|       }
 | |
|     }
 | |
|   }
 | |
|   vm.visible = true;
 | |
|   return vm;
 | |
| };
 | |
| function MessageBox(options, appContext = null) {
 | |
|   if (!core.isClient)
 | |
|     return Promise.reject();
 | |
|   let callback;
 | |
|   if (shared.isString(options) || vue.isVNode(options)) {
 | |
|     options = {
 | |
|       message: options
 | |
|     };
 | |
|   } else {
 | |
|     callback = options.callback;
 | |
|   }
 | |
|   return new Promise((resolve, reject) => {
 | |
|     const vm = showMessage(options, appContext != null ? appContext : MessageBox._context);
 | |
|     messageInstance.set(vm, {
 | |
|       options,
 | |
|       callback,
 | |
|       resolve,
 | |
|       reject
 | |
|     });
 | |
|   });
 | |
| }
 | |
| const MESSAGE_BOX_VARIANTS = ["alert", "confirm", "prompt"];
 | |
| const MESSAGE_BOX_DEFAULT_OPTS = {
 | |
|   alert: { closeOnPressEscape: false, closeOnClickModal: false },
 | |
|   confirm: { showCancelButton: true },
 | |
|   prompt: { showCancelButton: true, showInput: true }
 | |
| };
 | |
| MESSAGE_BOX_VARIANTS.forEach((boxType) => {
 | |
|   MessageBox[boxType] = messageBoxFactory(boxType);
 | |
| });
 | |
| function messageBoxFactory(boxType) {
 | |
|   return (message, title, options, appContext) => {
 | |
|     let titleOrOpts = "";
 | |
|     if (shared.isObject(title)) {
 | |
|       options = title;
 | |
|       titleOrOpts = "";
 | |
|     } else if (types.isUndefined(title)) {
 | |
|       titleOrOpts = "";
 | |
|     } else {
 | |
|       titleOrOpts = title;
 | |
|     }
 | |
|     return MessageBox(Object.assign({
 | |
|       title: titleOrOpts,
 | |
|       message,
 | |
|       type: "",
 | |
|       ...MESSAGE_BOX_DEFAULT_OPTS[boxType]
 | |
|     }, options, {
 | |
|       boxType
 | |
|     }), appContext);
 | |
|   };
 | |
| }
 | |
| MessageBox.close = () => {
 | |
|   messageInstance.forEach((_, vm) => {
 | |
|     vm.doClose();
 | |
|   });
 | |
|   messageInstance.clear();
 | |
| };
 | |
| MessageBox._context = null;
 | |
| 
 | |
| exports["default"] = MessageBox;
 | |
| //# sourceMappingURL=messageBox.js.map
 |