{"version":3,"file":"method.mjs","sources":["../../../../../../packages/components/message/src/method.ts"],"sourcesContent":["import { createVNode, render } from 'vue'\nimport {\n debugWarn,\n isBoolean,\n isClient,\n isElement,\n isFunction,\n isNumber,\n isString,\n isVNode,\n} from '@element-plus/utils'\nimport { messageConfig } from '@element-plus/components/config-provider'\nimport MessageConstructor from './message.vue'\nimport { messageDefaults, messageTypes } from './message'\nimport { instances } from './instance'\n\nimport type { MessageContext } from './instance'\nimport type { AppContext } from 'vue'\nimport type {\n Message,\n MessageFn,\n MessageHandler,\n MessageOptions,\n MessageParams,\n MessageParamsNormalized,\n messageType,\n} from './message'\n\nlet seed = 1\n\n// TODO: Since Notify.ts is basically the same like this file. So we could do some encapsulation against them to reduce code duplication.\n\nconst normalizeOptions = (params?: MessageParams) => {\n const options: MessageOptions =\n !params || isString(params) || isVNode(params) || isFunction(params)\n ? { message: params }\n : params\n\n const normalized = {\n ...messageDefaults,\n ...options,\n }\n\n if (!normalized.appendTo) {\n normalized.appendTo = document.body\n } else if (isString(normalized.appendTo)) {\n let appendTo = document.querySelector(normalized.appendTo)\n\n // should fallback to default value with a warning\n if (!isElement(appendTo)) {\n debugWarn(\n 'ElMessage',\n 'the appendTo option is not an HTMLElement. Falling back to document.body.'\n )\n appendTo = document.body\n }\n\n normalized.appendTo = appendTo\n }\n\n // When grouping is configured globally,\n // if grouping is manually set when calling message individually and it is not equal to the default value,\n // the global configuration cannot override the current setting. default => false\n if (isBoolean(messageConfig.grouping) && !normalized.grouping) {\n normalized.grouping = messageConfig.grouping\n }\n if (isNumber(messageConfig.duration) && normalized.duration === 3000) {\n normalized.duration = messageConfig.duration\n }\n if (isNumber(messageConfig.offset) && normalized.offset === 16) {\n normalized.offset = messageConfig.offset\n }\n if (isBoolean(messageConfig.showClose) && !normalized.showClose) {\n normalized.showClose = messageConfig.showClose\n }\n\n return normalized as MessageParamsNormalized\n}\n\nconst closeMessage = (instance: MessageContext) => {\n const idx = instances.indexOf(instance)\n if (idx === -1) return\n\n instances.splice(idx, 1)\n const { handler } = instance\n handler.close()\n}\n\nconst createMessage = (\n { appendTo, ...options }: MessageParamsNormalized,\n context?: AppContext | null\n): MessageContext => {\n const id = `message_${seed++}`\n const userOnClose = options.onClose\n\n const container = document.createElement('div')\n\n const props = {\n ...options,\n // now the zIndex will be used inside the message.vue component instead of here.\n // zIndex: nextIndex() + options.zIndex\n id,\n onClose: () => {\n userOnClose?.()\n closeMessage(instance)\n },\n\n // clean message element preventing mem leak\n onDestroy: () => {\n // since the element is destroy, then the VNode should be collected by GC as well\n // we do not want cause any mem leak because we have returned vm as a reference to users\n // so that we manually set it to false.\n render(null, container)\n },\n }\n const vnode = createVNode(\n MessageConstructor,\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 = context || message._context\n\n render(vnode, container)\n // instances will remove this item when close function gets called. So we do not need to worry about it.\n appendTo.appendChild(container.firstElementChild!)\n\n const vm = vnode.component!\n\n const handler: MessageHandler = {\n // instead of calling the onClose function directly, setting this value so that we can have the full lifecycle\n // for out component, so that all closing steps will not be skipped.\n close: () => {\n vm.exposed!.visible.value = false\n },\n }\n\n const instance: MessageContext = {\n id,\n vnode,\n vm,\n handler,\n props: (vnode.component as any).props,\n }\n\n return instance\n}\n\nconst message: MessageFn &\n Partial & { _context: AppContext | null } = (\n options = {},\n context\n) => {\n if (!isClient) return { close: () => undefined }\n\n const normalized = normalizeOptions(options)\n\n if (normalized.grouping && instances.length) {\n const instance = instances.find(\n ({ vnode: vm }) => vm.props?.message === normalized.message\n )\n if (instance) {\n instance.props.repeatNum += 1\n instance.props.type = normalized.type\n return instance.handler\n }\n }\n\n if (isNumber(messageConfig.max) && instances.length >= messageConfig.max) {\n return { close: () => undefined }\n }\n\n const instance = createMessage(normalized, context)\n\n instances.push(instance)\n return instance.handler\n}\n\nmessageTypes.forEach((type) => {\n message[type] = (options = {}, appContext) => {\n const normalized = normalizeOptions(options)\n return message({ ...normalized, type }, appContext)\n }\n})\n\nexport function closeAll(type?: messageType): void {\n for (const instance of instances) {\n if (!type || type === instance.props.type) {\n instance.handler.close()\n }\n }\n}\n\nmessage.closeAll = closeAll\nmessage._context = null\n\nexport default message as Message\n"],"names":[],"mappings":";;;;;;;;;;;;AAeA,IAAI,IAAI,GAAG,CAAC,CAAC;AACb,MAAM,gBAAgB,GAAG,CAAC,MAAM,KAAK;AACrC,EAAE,MAAM,OAAO,GAAG,CAAC,MAAM,IAAI,QAAQ,CAAC,MAAM,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AACtH,EAAE,MAAM,UAAU,GAAG;AACrB,IAAI,GAAG,eAAe;AACtB,IAAI,GAAG,OAAO;AACd,GAAG,CAAC;AACJ,EAAE,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE;AAC5B,IAAI,UAAU,CAAC,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC;AACxC,GAAG,MAAM,IAAI,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE;AAC5C,IAAI,IAAI,QAAQ,GAAG,QAAQ,CAAC,aAAa,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;AAC/D,IAAI,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAE;AAC9B,MAAM,SAAS,CAAC,WAAW,EAAE,2EAA2E,CAAC,CAAC;AAC1G,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,CAAC;AAC/B,KAAK;AACL,IAAI,UAAU,CAAC,QAAQ,GAAG,QAAQ,CAAC;AACnC,GAAG;AACH,EAAE,IAAI,SAAS,CAAC,aAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,EAAE;AACjE,IAAI,UAAU,CAAC,QAAQ,GAAG,aAAa,CAAC,QAAQ,CAAC;AACjD,GAAG;A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