UNPKG

ideaz-element

Version:

<p align="center"> <a href="" target="_blank" rel="noopener noreferrer"> <img src="./docs/public/logo.svg" alt="Ideaz Element" width="180" style="width: 180px;" /> </a> </p>

1 lines 7.44 kB
{"version":3,"file":"dialog.cjs","sources":["../../../../../packages/element/dialog/src/dialog.ts"],"sourcesContent":["import { createVNode, isVNode, render } from 'vue'\nimport { hasOwn } from '@vue/shared'\nimport { isClient } from '@vueuse/core'\nimport { isElement, isFunction, isObject, isString, isUndefined } from '@ideaz/utils'\nimport type { AppContext, ComponentPublicInstance, VNode } from 'vue'\nimport type { DialogProps } from './props'\nimport DialogConstructor from './index'\n\nexport type IDialogTipMethod = (\n message: string | VNode,\n title: string,\n options?: Partial<DialogProps>,\n appContext?: AppContext | null\n) => any\n\nexport interface IDialogTip {\n _context: AppContext | null\n\n (\n options: Partial<DialogProps>,\n appContext?: AppContext | null\n ): void\n\n normal: IDialogTipMethod\n\n warning: IDialogTipMethod\n\n danger: IDialogTipMethod\n\n info: IDialogTipMethod\n\n close: () => void\n}\n\nconst dialogInstance = new Map<\n ComponentPublicInstance<{ done: () => void, exposed: any }>, // marking doClose as function\n {\n options: any\n callback: any | undefined\n }\n>()\n\nfunction 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 // should fallback to default value with a warning\n if (!isElement(appendTo)) {\n console.warn(\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\nfunction initInstance(props: any, container: HTMLElement, appContext: AppContext | null = null) {\n const vnode = createVNode(\n DialogConstructor,\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\nfunction genContainer() {\n return document.createElement('div')\n}\n\nfunction showMessage(options: any, appContext?: AppContext | null) {\n const container = genContainer()\n const instance = initInstance(options, container, appContext)!\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 done: () => void\n } & any\n >\n\n // Adding destruct method.\n // when transition leaves emitting `vanish` evt. so that we can do the clean job.\n\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 dialogInstance.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 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 // change visibility after everything is settled\n instance.exposed!.isShowDialog.value = true\n return vm\n}\n\nfunction DialogTip(\n options: any | string | VNode,\n appContext: AppContext | null = null,\n) {\n if (!isClient)\n return\n let callback: any | undefined\n if (isString(options) || isVNode(options)) {\n options = {\n message: options,\n }\n }\n else {\n callback = options.callback\n }\n\n options.extend = true\n\n const vm = showMessage(\n options,\n appContext ?? (DialogTip as any)._context,\n )\n // collect this vm in order to handle upcoming events.\n dialogInstance.set(vm, {\n options,\n callback,\n })\n}\n\nfunction messageBoxFactory(type: typeof Dialog_VARIANTS[number]) {\n return (\n message: string | VNode,\n title: string,\n options?: any,\n appContext?: AppContext | null,\n ) => {\n let titleOrOpts = ''\n if (isObject(title) || isObject(message)) {\n options = title || message\n titleOrOpts = ''\n }\n else if (isUndefined(title)) {\n titleOrOpts = ''\n }\n else {\n titleOrOpts = title as string\n }\n\n return DialogTip(\n Object.assign(\n {\n title: titleOrOpts,\n message,\n },\n options,\n {\n type,\n },\n ),\n appContext,\n )\n }\n}\n\nconst Dialog_VARIANTS = ['normal', 'danger', 'warning', 'info'] as const\n\nDialog_VARIANTS.forEach((type) => {\n (DialogTip as IDialogTip)[type] = messageBoxFactory(\n type,\n ) as IDialogTipMethod\n})\n\nDialogTip.close = () => {\n dialogInstance.forEach((_, instance) => {\n instance.exposed.done()\n })\n\n dialogInstance.clear()\n}\n;(DialogTip as any)._context = null\n\nexport default DialogTip as IDialogTip\n"],"names":["dialogInstance","getAppendToElement","props","appendTo","is","initInstance","container","appContext","vnode","vue","index","genContainer","showMessage","options","instance","vm","prop","shared","DialogTip","index$1","callback","messageBoxFactory","type","message","title","titleOrOpts","fullscreen","Dialog_VARIANTS","_"],"mappings":"gkBAkCAA,EAAA,IAAA,IAQA,SAAAC,EAAAC,EAAA,CACE,IAAAC,EAAA,SAAA,KACA,OAAAD,EAAA,WACEE,EAAA,SAAAF,EAAA,QAAA,IACEC,EAAA,SAAA,cAAAD,EAAA,QAAA,GAEFE,EAAA,UAAAF,EAAA,QAAA,IACEC,EAAAD,EAAA,UAGFE,EAAA,UAAAD,CAAA,IACE,QAAA,KAAQ,eACN,2EACA,EAEFA,EAAA,SAAA,OAGJA,CACF,CAEA,SAAAE,EAAAH,EAAAI,EAAAC,EAAA,KAAA,CACE,MAAAC,EAAAC,EAAA,YAAcC,EACZR,EACAE,EAAA,WAAAF,EAAA,OAAA,GAAAO,EAAA,QAAAP,EAAA,OAAA,EAAA,CAEI,QAAAE,EAAA,WAAAF,EAAA,OAAA,EAAAA,EAAA,QAAA,IAAAA,EAAA,OAGkB,EAAA,IAElB,EAEN,OAAAM,EAAA,WAAAD,EACAE,EAAA,OAAAD,EAAAF,CAAA,EACAL,EAAAC,CAAA,EAAA,YAAAI,EAAA,iBAAA,EACAE,EAAA,SACF,CAEA,SAAAG,GAAA,CACE,OAAA,SAAA,cAAA,KAAA,CACF,CAEA,SAAAC,EAAAC,EAAAN,EAAA,CACE,MAAAD,EAAAK,EAAA,EACAG,EAAAT,EAAAQ,EAAAP,EAAAC,CAAA,EAIAQ,EAAAD,EAAA,MASAD,EAAA,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