{"version":3,"file":"instance.mjs","sources":["../../../../../../packages/components/notification/src/instance.ts"],"sourcesContent":["import { createVNode, render } from 'vue'\r\nimport { isClient } from '@vueuse/core'\r\nimport { isVNode } from '@sgui-plus/utils/util'\r\nimport NotificationConstructor from './notification.vue'\r\nimport { notificationTypes } from './notification'\r\n\r\nimport type { ComponentPublicInstance, VNode } from 'vue'\r\nimport type {\r\n  NotificationOptions,\r\n  Notify,\r\n  NotifyFn,\r\n  NotificationQueue,\r\n  NotificationProps,\r\n} from './notification'\r\n\r\n// This should be a queue but considering there were `non-autoclosable` notifications.\r\nconst notifications: Record<\r\n  NotificationOptions['position'],\r\n  NotificationQueue\r\n> = {\r\n  'top-left': [],\r\n  'top-right': [],\r\n  'bottom-left': [],\r\n  'bottom-right': [],\r\n}\r\n\r\n// the gap size between each notification\r\nconst GAP_SIZE = 16\r\nlet seed = 1\r\n\r\nconst notify: NotifyFn & Partial<Notify> = function (options = {}) {\r\n  if (!isClient) return { close: () => undefined }\r\n\r\n  if (typeof options === 'string' || isVNode(options)) {\r\n    options = { description: options }\r\n  }\r\n\r\n  const position = options.position || 'top-right'\r\n\r\n  if (\r\n    !isVNode(options) &&\r\n    typeof options === 'object' &&\r\n    options.grouping &&\r\n    !isVNode(options.description) &&\r\n    notifications[position].length\r\n  ) {\r\n    const tempVm: any = notifications[position].find(\r\n      (item) =>\r\n        `${item.vm.props?.description ?? ''}` ===\r\n        `${(options as any).description ?? ''}`\r\n    )\r\n    if (tempVm) {\r\n      tempVm.vm.component!.props.repeatNum += 1\r\n      tempVm.vm.component!.props.type = options?.type\r\n      return {\r\n        close: () =>\r\n          ((\r\n            vm.component!.proxy as ComponentPublicInstance<{ visible: boolean }>\r\n          ).visible = false),\r\n      }\r\n    }\r\n  }\r\n\r\n  let verticalOffset = options.offset || 0\r\n  notifications[position].forEach(({ vm }) => {\r\n    verticalOffset += (vm.el?.offsetHeight || 0) + GAP_SIZE\r\n  })\r\n  verticalOffset += GAP_SIZE\r\n\r\n  const id = `notification_${seed++}`\r\n  const userOnClose = options.onClose\r\n  const props: Partial<NotificationProps> = {\r\n    // default options end\r\n    offset: verticalOffset,\r\n    ...options,\r\n    id,\r\n    onClose: () => {\r\n      close(id, position, userOnClose)\r\n    },\r\n  }\r\n\r\n  const appendTo: HTMLElement | null = document.body\r\n\r\n  const container = document.createElement('div')\r\n\r\n  const vm = createVNode(\r\n    NotificationConstructor,\r\n    props,\r\n    isVNode(props.description)\r\n      ? {\r\n          default: () => props.description,\r\n        }\r\n      : null\r\n  )\r\n\r\n  // clean notification element preventing mem leak\r\n  vm.props!.onDestroy = () => {\r\n    render(null, container)\r\n  }\r\n\r\n  // instances will remove this item when close function gets called. So we do not need to worry about it.\r\n  render(vm, container)\r\n  notifications[position].push({ vm })\r\n  appendTo.appendChild(container.firstElementChild!)\r\n\r\n  return {\r\n    // instead of calling the onClose function directly, setting this value so that we can have the full lifecycle\r\n    // for out component, so that all closing steps will not be skipped.\r\n    close: () => {\r\n      ;(\r\n        vm.component!.proxy as ComponentPublicInstance<{ visible: boolean }>\r\n      ).visible = false\r\n    },\r\n  }\r\n}\r\nnotificationTypes.forEach((type) => {\r\n  notify[type] = (options = {}) => {\r\n    if (typeof options === 'string' || isVNode(options)) {\r\n      options = {\r\n        description: options,\r\n      }\r\n    }\r\n    return notify({\r\n      ...options,\r\n      type,\r\n    })\r\n  }\r\n})\r\n\r\n/**\r\n * This function gets called when user click `x` button or press `esc` or the time reached its limitation.\r\n * Emitted by transition@before-leave event so that we can fetch the current notification.offsetHeight, if this was called\r\n * by @after-leave the DOM element will be removed from the page thus we can no longer fetch the offsetHeight.\r\n * @param {String} id notification id to be closed\r\n * @param {Position} position the positioning strategy\r\n * @param {Function} userOnClose the callback called when close passed by user\r\n */\r\nexport function close(\r\n  id: string,\r\n  position: NotificationOptions['position'],\r\n  userOnClose?: (vm: VNode) => void\r\n): void {\r\n  // maybe we can store the index when inserting the vm to notification list.\r\n  const orientedNotifications = notifications[position]\r\n  const idx = orientedNotifications.findIndex(\r\n    ({ vm }) => vm.component?.props.id === id\r\n  )\r\n  if (idx === -1) return\r\n  const { vm } = orientedNotifications[idx]\r\n  if (!vm) return\r\n  // calling user's on close function before notification gets removed from DOM.\r\n  userOnClose?.(vm)\r\n\r\n  // note that this is called @before-leave, that's why we were able to fetch this property.\r\n  const removedHeight = vm.el!.offsetHeight\r\n  const verticalPos = position.split('-')[0]\r\n  orientedNotifications.splice(idx, 1)\r\n  const len = orientedNotifications.length\r\n  if (len < 1) return\r\n  // starting from the removing item.\r\n  for (let i = idx; i < len; i++) {\r\n    // new position equals the current offsetTop minus removed height plus 16px(the gap size between each item)\r\n    const { el, component } = orientedNotifications[i].vm\r\n    const pos = parseInt(el!.style[verticalPos], 10) - removedHeight - GAP_SIZE\r\n    component!.props.offset = pos\r\n  }\r\n}\r\n\r\nexport function closeAll(): void {\r\n  // loop through all directions, close them at once.\r\n  for (const orientedNotifications of Object.values(notifications)) {\r\n    orientedNotifications.forEach(({ vm }) => {\r\n      // same as the previous close method, we'd like to make sure lifecycle gets handle properly.\r\n      ;(\r\n        vm.component!.proxy as ComponentPublicInstance<{ visible: boolean }>\r\n      ).visible = false\r\n    })\r\n  }\r\n}\r\n\r\nnotify.closeAll = closeAll\r\n\r\nexport default notify as Notify\r\n"],"names":[],"mappings":";;;;;;AAKA,MAAM,aAAa,GAAG;AACtB,EAAE,UAAU,EAAE,EAAE;AAChB,EAAE,WAAW,EAAE,EAAE;AACjB,EAAE,aAAa,EAAE,EAAE;AACnB,EAAE,cAAc,EAAE,EAAE;AACpB,CAAC,CAAC;AACF,MAAM,QAAQ,GAAG,EAAE,CAAC;AACpB,IAAI,IAAI,GAAG,CAAC,CAAC;AACR,MAAC,MAAM,GAAG,SAAS,OAAO,GAAG,EAAE,EAAE;AACtC,EAAE,IAAI,CAAC,QAAQ;AACf,IAAI,OAAO,EAAE,KAAK,EAAE,MAAM,KAAK,CAAC,EAAE,CAAC;AACnC,EAAE,IAAI,OAAO,OAAO,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