{"version":3,"file":"messageBox.js","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(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\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\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"],"names":["isString","isElement","debugWarn","createVNode","MessageBoxConstructor","isFunction","isVNode","render","hasOwn","isClient","isObject","isUndefined"],"mappings":";;;;;;;;;;;;AAaA,MAAM,eAAe,mBAAmB,IAAI,GAAG,EAAE,CAAC;AAClD,MAAM,kBAAkB,GAAG,CAAC,KAAK,KAAK;AACtC,EAAE,IAAI,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC;AAC/B,EAAE,IAAI,KAAK,CAAC,QAAQ,EAAE;AACtB,IAAI,IAAIA,eAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE;AAClC,MAAM,QAAQ,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;AACxD,KAAK;AACL,IAAI,IAAIC,eAAS,CAA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