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 lines
14 KiB

{"ast":null,"code":"import { isVNode, createVNode, render } from 'vue';\nimport NotificationConstructor from './notification2.mjs';\nimport { notificationTypes } from './notification.mjs';\nimport { isClient } from '@vueuse/core';\nimport { isString, isFunction } from '@vue/shared';\nimport { isElement, isUndefined } from '../../../utils/types.mjs';\nimport { debugWarn } from '../../../utils/error.mjs';\nconst notifications = {\n \"top-left\": [],\n \"top-right\": [],\n \"bottom-left\": [],\n \"bottom-right\": []\n};\nconst GAP_SIZE = 16;\nlet seed = 1;\nconst notify = function (options = {}, context) {\n if (!isClient) return {\n close: () => void 0\n };\n if (isString(options) || isVNode(options)) {\n options = {\n message: options\n };\n }\n const position = options.position || \"top-right\";\n let verticalOffset = options.offset || 0;\n notifications[position].forEach(({\n vm: vm2\n }) => {\n var _a;\n verticalOffset += (((_a = vm2.el) == null ? void 0 : _a.offsetHeight) || 0) + GAP_SIZE;\n });\n verticalOffset += GAP_SIZE;\n const id = `notification_${seed++}`;\n const userOnClose = options.onClose;\n const props = {\n ...options,\n offset: verticalOffset,\n id,\n onClose: () => {\n close(id, position, userOnClose);\n }\n };\n let appendTo = document.body;\n if (isElement(options.appendTo)) {\n appendTo = options.appendTo;\n } else if (isString(options.appendTo)) {\n appendTo = document.querySelector(options.appendTo);\n }\n if (!isElement(appendTo)) {\n debugWarn(\"ElNotification\", \"the appendTo option is not an HTMLElement. Falling back to document.body.\");\n appendTo = document.body;\n }\n const container = document.createElement(\"div\");\n const vm = createVNode(NotificationConstructor, props, isFunction(props.message) ? props.message : isVNode(props.message) ? () => props.message : null);\n vm.appContext = isUndefined(context) ? notify._context : context;\n vm.props.onDestroy = () => {\n render(null, container);\n };\n render(vm, container);\n notifications[position].push({\n vm\n });\n appendTo.appendChild(container.firstElementChild);\n return {\n close: () => {\n vm.component.exposed.visible.value = false;\n }\n };\n};\nnotificationTypes.forEach(type => {\n notify[type] = (options = {}, appContext) => {\n if (isString(options) || isVNode(options)) {\n options = {\n message: options\n };\n }\n return notify({\n ...options,\n type\n }, appContext);\n };\n});\nfunction close(id, position, userOnClose) {\n const orientedNotifications = notifications[position];\n const idx = orientedNotifications.findIndex(({\n vm: vm2\n }) => {\n var _a;\n return ((_a = vm2.component) == null ? void 0 : _a.props.id) === id;\n });\n if (idx === -1) return;\n const {\n vm\n } = orientedNotifications[idx];\n if (!vm) return;\n userOnClose == null ? void 0 : userOnClose(vm);\n const removedHeight = vm.el.offsetHeight;\n const verticalPos = position.split(\"-\")[0];\n orientedNotifications.splice(idx, 1);\n const len = orientedNotifications.length;\n if (len < 1) return;\n for (let i = idx; i < len; i++) {\n const {\n el,\n component\n } = orientedNotifications[i].vm;\n const pos = Number.parseInt(el.style[verticalPos], 10) - removedHeight - GAP_SIZE;\n component.props.offset = pos;\n }\n}\nfunction closeAll() {\n for (const orientedNotifications of Object.values(notifications)) {\n orientedNotifications.forEach(({\n vm\n }) => {\n vm.component.exposed.visible.value = false;\n });\n }\n}\nnotify.closeAll = closeAll;\nnotify._context = null;\nexport { close, closeAll, notify as default };","map":{"version":3,"names":["notifications","GAP_SIZE","seed","notify","options","context","isClient","close","isString","isVNode","message","position","verticalOffset","offset","forEach","vm","vm2","_a","el","offsetHeight","id","userOnClose","onClose","props","appendTo","document","body","isElement","querySelector","debugWarn","container","createElement","createVNode","NotificationConstructor","isFunction","appContext","isUndefined","_context","onDestroy","render","push","appendChild","firstElementChild","component","exposed","visible","value","notificationTypes","type","orientedNotifications","idx","findIndex","removedHeight","verticalPos","split","splice","len","length","i","pos","Number","parseInt","style","closeAll","Object","values"],"sources":["../../../../../../packages/components/notification/src/notify.ts"],"sourcesContent":["import { createVNode, isVNode, render } from 'vue'\nimport {\n debugWarn,\n isClient,\n isElement,\n isFunction,\n isString,\n isUndefined,\n} from '@element-plus/utils'\nimport NotificationConstructor from './notification.vue'\nimport { notificationTypes } from './notification'\n\nimport type { Ref, VNode } from 'vue'\nimport type {\n NotificationOptions,\n NotificationProps,\n NotificationQueue,\n Notify,\n NotifyFn,\n} from './notification'\n\n// This should be a queue but considering there were `non-autoclosable` notifications.\nconst notifications: Record<\n NotificationOptions['position'],\n NotificationQueue\n> = {\n 'top-left': [],\n 'top-right': [],\n 'bottom-left': [],\n 'bottom-right': [],\n}\n\n// the gap size between each notification\nconst GAP_SIZE = 16\nlet seed = 1\n\nconst notify: NotifyFn & Partial<Notify> = function (options = {}, context) {\n if (!isClient) return { close: () => undefined }\n\n if (isString(options) || isVNode(options)) {\n options = { message: options }\n }\n\n const position = options.position || 'top-right'\n\n let verticalOffset = options.offset || 0\n notifications[position].forEach(({ vm }) => {\n verticalOffset += (vm.el?.offsetHeight || 0) + GAP_SIZE\n })\n verticalOffset += GAP_SIZE\n\n const id = `notification_${seed++}`\n const userOnClose = options.onClose\n const props: Partial<NotificationProps> = {\n ...options,\n offset: verticalOffset,\n id,\n onClose: () => {\n close(id, position, userOnClose)\n },\n }\n\n let appendTo: HTMLElement | null = document.body\n if (isElement(options.appendTo)) {\n appendTo = options.appendTo\n } else if (isString(options.appendTo)) {\n appendTo = document.querySelector(options.appendTo)\n }\n\n // should fallback to default value with a warning\n if (!isElement(appendTo)) {\n debugWarn(\n 'ElNotification',\n 'the appendTo option is not an HTMLElement. Falling back to document.body.'\n )\n appendTo = document.body\n }\n\n const container = document.createElement('div')\n\n const vm = createVNode(\n NotificationConstructor,\n props,\n isFunction(props.message)\n ? props.message\n : isVNode(props.message)\n ? () => props.message\n : null\n )\n vm.appContext = isUndefined(context) ? notify._context : context\n\n // clean notification element preventing mem leak\n vm.props!.onDestroy = () => {\n render(null, container)\n }\n\n // instances will remove this item when close function gets called. So we do not need to worry about it.\n render(vm, container)\n notifications[position].push({ vm })\n appendTo.appendChild(container.firstElementChild!)\n\n return {\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.component!.exposed as { visible: Ref<boolean> }).visible.value =\n false\n },\n }\n}\nnotificationTypes.forEach((type) => {\n notify[type] = (options = {}, appContext) => {\n if (isString(options) || isVNode(options)) {\n options = {\n message: options,\n }\n }\n return notify({ ...options, type }, appContext)\n }\n})\n\n/**\n * This function gets called when user click `x` button or press `esc` or the time reached its limitation.\n * Emitted by transition@before-leave event so that we can fetch the current notification.offsetHeight, if this was called\n * by @after-leave the DOM element will be removed from the page thus we can no longer fetch the offsetHeight.\n * @param {String} id notification id to be closed\n * @param {Position} position the positioning strategy\n * @param {Function} userOnClose the callback called when close passed by user\n */\nexport function close(\n id: string,\n position: NotificationOptions['position'],\n userOnClose?: (vm: VNode) => void\n): void {\n // maybe we can store the index when inserting the vm to notification list.\n const orientedNotifications = notifications[position]\n const idx = orientedNotifications.findIndex(\n ({ vm }) => vm.component?.props.id === id\n )\n if (idx === -1) return\n const { vm } = orientedNotifications[idx]\n if (!vm) return\n // calling user's on close function before notification gets removed from DOM.\n userOnClose?.(vm)\n\n // note that this is called @before-leave, that's why we were able to fetch this property.\n const removedHeight = vm.el!.offsetHeight\n const verticalPos = position.split('-')[0]\n orientedNotifications.splice(idx, 1)\n const len = orientedNotifications.length\n if (len < 1) return\n // starting from the removing item.\n for (let i = idx; i < len; i++) {\n // new position equals the current offsetTop minus removed height plus 16px(the gap size between each item)\n const { el, component } = orientedNotifications[i].vm\n const pos =\n Number.parseInt(el!.style[verticalPos], 10) - removedHeight - GAP_SIZE\n component!.props.offset = pos\n }\n}\n\nexport function closeAll(): void {\n // loop through all directions, close them at once.\n for (const orientedNotifications of Object.values(notifications)) {\n orientedNotifications.forEach(({ vm }) => {\n // same as the previous close method, we'd like to make sure lifecycle gets handle properly.\n ;(vm.component!.exposed as { visible: Ref<boolean> }).visible.value =\n false\n })\n }\n}\n\nnotify.closeAll = closeAll\nnotify._context = null\n\nexport default notify as Notify\n"],"mappings":";;;;;;;AAWA,MAAMA,aAAa,GAAG;EACpB,UAAU,EAAE,EAAE;EACd,WAAW,EAAE,EAAE;EACf,aAAa,EAA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