You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1 line
16 KiB
1 line
16 KiB
{"ast":null,"code":"import { createVNode, isVNode, render } from 'vue';\nimport '../../../utils/index.mjs';\nimport MessageBoxConstructor from './index.mjs';\nimport { isString, isFunction, hasOwn, isObject } from '@vue/shared';\nimport { isElement, isUndefined } from '../../../utils/types.mjs';\nimport { debugWarn } from '../../../utils/error.mjs';\nimport { isClient } from '@vueuse/core';\nconst messageInstance = /* @__PURE__ */new Map();\nconst getAppendToElement = props => {\n let appendTo = document.body;\n if (props.appendTo) {\n if (isString(props.appendTo)) {\n appendTo = document.querySelector(props.appendTo);\n }\n if (isElement(props.appendTo)) {\n appendTo = props.appendTo;\n }\n if (!isElement(appendTo)) {\n debugWarn(\"ElMessageBox\", \"the appendTo option is not an HTMLElement. Falling back to document.body.\");\n appendTo = document.body;\n }\n }\n return appendTo;\n};\nconst initInstance = (props, container, appContext = null) => {\n const vnode = createVNode(MessageBoxConstructor, props, isFunction(props.message) || isVNode(props.message) ? {\n default: isFunction(props.message) ? props.message : () => props.message\n } : null);\n vnode.appContext = appContext;\n render(vnode, container);\n getAppendToElement(props).appendChild(container.firstElementChild);\n return vnode.component;\n};\nconst genContainer = () => {\n return document.createElement(\"div\");\n};\nconst showMessage = (options, appContext) => {\n const container = genContainer();\n options.onVanish = () => {\n render(null, container);\n messageInstance.delete(vm);\n };\n options.onAction = action => {\n const currentMsg = messageInstance.get(vm);\n let resolve;\n if (options.showInput) {\n resolve = {\n value: vm.inputValue,\n action\n };\n } else {\n resolve = action;\n }\n if (options.callback) {\n options.callback(resolve, instance.proxy);\n } else {\n if (action === \"cancel\" || action === \"close\") {\n if (options.distinguishCancelAndClose && action !== \"cancel\") {\n currentMsg.reject(\"close\");\n } else {\n currentMsg.reject(\"cancel\");\n }\n } else {\n currentMsg.resolve(resolve);\n }\n }\n };\n const instance = initInstance(options, container, appContext);\n const vm = instance.proxy;\n for (const prop in options) {\n if (hasOwn(options, prop) && !hasOwn(vm.$props, prop)) {\n vm[prop] = options[prop];\n }\n }\n vm.visible = true;\n return vm;\n};\nfunction MessageBox(options, appContext = null) {\n if (!isClient) return Promise.reject();\n let callback;\n if (isString(options) || isVNode(options)) {\n options = {\n message: options\n };\n } else {\n callback = options.callback;\n }\n return new Promise((resolve, reject) => {\n const vm = showMessage(options, appContext != null ? appContext : MessageBox._context);\n messageInstance.set(vm, {\n options,\n callback,\n resolve,\n reject\n });\n });\n}\nconst MESSAGE_BOX_VARIANTS = [\"alert\", \"confirm\", \"prompt\"];\nconst MESSAGE_BOX_DEFAULT_OPTS = {\n alert: {\n closeOnPressEscape: false,\n closeOnClickModal: false\n },\n confirm: {\n showCancelButton: true\n },\n prompt: {\n showCancelButton: true,\n showInput: true\n }\n};\nMESSAGE_BOX_VARIANTS.forEach(boxType => {\n ;\n MessageBox[boxType] = messageBoxFactory(boxType);\n});\nfunction messageBoxFactory(boxType) {\n return (message, title, options, appContext) => {\n let titleOrOpts = \"\";\n if (isObject(title)) {\n options = title;\n titleOrOpts = \"\";\n } else if (isUndefined(title)) {\n titleOrOpts = \"\";\n } else {\n titleOrOpts = title;\n }\n return MessageBox(Object.assign({\n title: titleOrOpts,\n message,\n type: \"\",\n ...MESSAGE_BOX_DEFAULT_OPTS[boxType]\n }, options, {\n boxType\n }), appContext);\n };\n}\nMessageBox.close = () => {\n messageInstance.forEach((_, vm) => {\n vm.doClose();\n });\n messageInstance.clear();\n};\nMessageBox._context = null;\nexport { MessageBox as default };","map":{"version":3,"names":["messageInstance","Map","getAppendToElement","props","appendTo","document","body","isString","querySelector","isElement","debugWarn","initInstance","container","appContext","vnode","createVNode","MessageBoxConstructor","isFunction","message","isVNode","default","render","appendChild","firstElementChild","component","genContainer","createElement","showMessage","options","onVanish","delete","vm","onAction","action","currentMsg","get","resolve","showInput","value","inputValue","callback","instance","proxy","distinguishCancelAndClose","reject","prop","hasOwn","$props","visible","MessageBox","isClient","Promise","_context","set","MESSAGE_BOX_VARIANTS","MESSAGE_BOX_DEFAULT_OPTS","alert","closeOnPressEscape","closeOnClickModal","confirm","showCancelButton","prompt","forEach","boxType","messageBoxFactory","title","titleOrOpts","isObject","isUndefined","Object","assign","type","close","_","doClose","clear"],"sources":["../../../../../../packages/components/message-box/src/messageBox.ts"],"sourcesContent":["import { createVNode, render } from 'vue'\nimport {\n debugWarn,\n hasOwn,\n isClient,\n isElement,\n isFunction,\n isObject,\n isString,\n isUndefined,\n isVNode,\n} from '@element-plus/utils'\nimport MessageBoxConstructor from './index.vue'\n\nimport type { AppContext, ComponentPublicInstance, VNode } from 'vue'\nimport type {\n Action,\n Callback,\n ElMessageBoxOptions,\n ElMessageBoxShortcutMethod,\n IElMessageBox,\n MessageBoxData,\n MessageBoxState,\n} from './message-box.type'\n\n// component default merge props & data\n\nconst messageInstance = new Map<\n ComponentPublicInstance<{ doClose: () => void }>, // marking doClose as function\n {\n options: any\n callback: Callback | undefined\n resolve: (res: any) => void\n reject: (reason?: any) => void\n }\n>()\n\nconst getAppendToElement = (props: any): HTMLElement => {\n let appendTo: HTMLElement | null = document.body\n if (props.appendTo) {\n if (isString(props.appendTo)) {\n appendTo = document.querySelector<HTMLElement>(props.appendTo)\n }\n if (isElement(props.appendTo)) {\n appendTo = props.appendTo\n }\n\n // should fallback to default value with a warning\n if (!isElement(appendTo)) {\n debugWarn(\n 'ElMessageBox',\n 'the appendTo option is not an HTMLElement. Falling back to document.body.'\n )\n appendTo = document.body\n }\n }\n return appendTo\n}\n\nconst initInstance = (\n props: any,\n container: HTMLElement,\n appContext: AppContext | null = null\n) => {\n const vnode = createVNode(\n MessageBoxConstructor,\n props,\n isFunction(props.message) || isVNode(props.message)\n ? {\n default: isFunction(props.message)\n ? props.message\n : () => props.message,\n }\n : null\n )\n vnode.appContext = appContext\n render(vnode, container)\n getAppendToElement(props).appendChild(container.firstElementChild!)\n return vnode.component\n}\n\nconst genContainer = () => {\n return document.createElement('div')\n}\n\nconst showMessage = (options: any, appContext?: AppContext | null) => {\n const container = genContainer()\n // Adding destruct method.\n // when transition leaves emitting `vanish` evt. so that we can do the clean job.\n options.onVanish = () => {\n // not sure if this causes mem leak, need proof to verify that.\n // maybe calling out like 1000 msg-box then close them all.\n render(null, container)\n messageInstance.delete(vm) // Remove vm to avoid mem leak.\n // here we were suppose to call document.body.removeChild(container.firstElementChild)\n // but render(null, container) did that job for us. so that we do not call that directly\n }\n\n options.onAction = (action: Action) => {\n const currentMsg = messageInstance.get(vm)!\n let resolve: Action | { value: string; action: Action }\n if (options.showInput) {\n resolve = { value: vm.inputValue, action }\n } else {\n resolve = action\n }\n if (options.callback) {\n options.callback(resolve, instance.proxy)\n } else {\n if (action === 'cancel' || action === 'close') {\n if (options.distinguishCancelAndClose && action !== 'cancel') {\n currentMsg.reject('close')\n } else {\n currentMsg.reject('cancel')\n }\n } else {\n currentMsg.resolve(resolve)\n }\n }\n }\n\n const instance = initInstance(options, container, appContext)!\n\n // This is how we use message box programmably.\n // Maybe consider releasing a template version?\n // get component instance like v2.\n const vm = instance.proxy as ComponentPublicInstance<\n {\n visible: boolean\n doClose: () => void\n } & MessageBoxState\n >\n\n for (const prop in options) {\n if (hasOwn(options, prop) && !hasOwn(vm.$props, prop)) {\n vm[prop as keyof ComponentPublicInstance] = options[prop]\n }\n }\n\n // change visibility after everything is settled\n vm.visible = true\n return vm\n}\n\nasync function MessageBox(\n options: ElMessageBoxOptions,\n appContext?: AppContext | null\n): Promise<MessageBoxData>\nfunction MessageBox(\n options: ElMessageBoxOptions | string | VNode,\n appContext: AppContext | null = null\n): Promise<{ value: string; action: Action } | Action> {\n if (!isClient) return Promise.reject()\n let callback: Callback | undefined\n if (isString(options) || isVNode(options)) {\n options = {\n message: options,\n }\n } else {\n callback = options.callback\n }\n\n return new Promise((resolve, reject) => {\n const vm = showMessage(\n options,\n appContext ?? (MessageBox as IElMessageBox)._context\n )\n // collect this vm in order to handle upcoming events.\n messageInstance.set(vm, {\n options,\n callback,\n resolve,\n reject,\n })\n })\n}\n\nconst MESSAGE_BOX_VARIANTS = ['alert', 'confirm', 'prompt'] as const\nconst MESSAGE_BOX_DEFAULT_OPTS: Record<\n typeof MESSAGE_BOX_VARIANTS[number],\n Partial<ElMessageBoxOptions>\n> = {\n alert: { closeOnPressEscape: false, closeOnClickModal: false },\n confirm: { showCancelButton: true },\n prompt: { showCancelButton: true, showInput: true },\n}\n\nMESSAGE_BOX_VARIANTS.forEach((boxType) => {\n ;(MessageBox as IElMessageBox)[boxType] = messageBoxFactory(\n boxType\n ) as ElMessageBoxShortcutMethod\n})\n\nfunction messageBoxFactory(boxType: typeof MESSAGE_BOX_VARIANTS[number]) {\n return (\n message: string | VNode,\n title: string | ElMessageBoxOptions,\n options?: ElMessageBoxOptions,\n appContext?: AppContext | null\n ) => {\n let titleOrOpts = ''\n if (isObject(title)) {\n options = title as ElMessageBoxOptions\n titleOrOpts = ''\n } else if (isUndefined(title)) {\n titleOrOpts = ''\n } else {\n titleOrOpts = title as string\n }\n\n return MessageBox(\n Object.assign(\n {\n title: titleOrOpts,\n message,\n type: '',\n ...MESSAGE_BOX_DEFAULT_OPTS[boxType],\n },\n options,\n {\n boxType,\n }\n ),\n appContext\n )\n }\n}\n\nMessageBox.close = () => {\n // instance.setupInstall.doClose()\n // instance.setupInstall.state.visible = false\n\n messageInstance.forEach((_, vm) => {\n vm.doClose()\n })\n\n messageInstance.clear()\n}\n;(MessageBox as IElMessageBox)._context = null\n\nexport default MessageBox as IElMessageBox\n"],"mappings":";;;;;;;AAaA,MAAMA,eAAe,kBAAmB,IAAIC,GAAG,EAAE;AACjD,MAAMC,kBAAkB,GAAIC,KAAK,IAAK;EACpC,IAAIC,QAAQ,GAAGC,QAAQ,CAACC,IAAI;EAC5B,IAAIH,KAAK,CAACC,QAAQ,EAAE;IAClB,IAAIG,QAAQ,CAACJ,KAAK,CAACC,QAAQ,CAAC,EAAE;MAC5BA,QAAQ,GAAGC,QAAQ,CAACG,aAAa,CAACL,KAAK,CAACC,QAAQ,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