608c897de8eb4cd150fbdfeafb1fd634336d86ddfc357237df771432ae47f370dbae6cf7339d454d9d1326610a88b864845c4c75502fa57a47cdd2a4023967 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { isVNode, createVNode, render } from 'vue';
  2. import NotificationConstructor from './notification2.mjs';
  3. import { notificationTypes } from './notification.mjs';
  4. import { isClient } from '@vueuse/core';
  5. import { isString, isFunction } from '@vue/shared';
  6. import { isElement, isUndefined } from '../../../utils/types.mjs';
  7. const notifications = {
  8. "top-left": [],
  9. "top-right": [],
  10. "bottom-left": [],
  11. "bottom-right": []
  12. };
  13. const GAP_SIZE = 16;
  14. let seed = 1;
  15. const notify = function(options = {}, context) {
  16. if (!isClient)
  17. return { close: () => void 0 };
  18. if (isString(options) || isVNode(options)) {
  19. options = { message: options };
  20. }
  21. const position = options.position || "top-right";
  22. let verticalOffset = options.offset || 0;
  23. notifications[position].forEach(({ vm: vm2 }) => {
  24. var _a;
  25. verticalOffset += (((_a = vm2.el) == null ? void 0 : _a.offsetHeight) || 0) + GAP_SIZE;
  26. });
  27. verticalOffset += GAP_SIZE;
  28. const id = `notification_${seed++}`;
  29. const userOnClose = options.onClose;
  30. const props = {
  31. ...options,
  32. offset: verticalOffset,
  33. id,
  34. onClose: () => {
  35. close(id, position, userOnClose);
  36. }
  37. };
  38. let appendTo = document.body;
  39. if (isElement(options.appendTo)) {
  40. appendTo = options.appendTo;
  41. } else if (isString(options.appendTo)) {
  42. appendTo = document.querySelector(options.appendTo);
  43. }
  44. if (!isElement(appendTo)) {
  45. appendTo = document.body;
  46. }
  47. const container = document.createElement("div");
  48. const vm = createVNode(NotificationConstructor, props, isFunction(props.message) ? props.message : isVNode(props.message) ? () => props.message : null);
  49. vm.appContext = isUndefined(context) ? notify._context : context;
  50. vm.props.onDestroy = () => {
  51. render(null, container);
  52. };
  53. render(vm, container);
  54. notifications[position].push({ vm });
  55. appendTo.appendChild(container.firstElementChild);
  56. return {
  57. close: () => {
  58. vm.component.exposed.visible.value = false;
  59. }
  60. };
  61. };
  62. notificationTypes.forEach((type) => {
  63. notify[type] = (options = {}, appContext) => {
  64. if (isString(options) || isVNode(options)) {
  65. options = {
  66. message: options
  67. };
  68. }
  69. return notify({ ...options, type }, appContext);
  70. };
  71. });
  72. function close(id, position, userOnClose) {
  73. const orientedNotifications = notifications[position];
  74. const idx = orientedNotifications.findIndex(({ vm: vm2 }) => {
  75. var _a;
  76. return ((_a = vm2.component) == null ? void 0 : _a.props.id) === id;
  77. });
  78. if (idx === -1)
  79. return;
  80. const { vm } = orientedNotifications[idx];
  81. if (!vm)
  82. return;
  83. userOnClose == null ? void 0 : userOnClose(vm);
  84. const removedHeight = vm.el.offsetHeight;
  85. const verticalPos = position.split("-")[0];
  86. orientedNotifications.splice(idx, 1);
  87. const len = orientedNotifications.length;
  88. if (len < 1)
  89. return;
  90. for (let i = idx; i < len; i++) {
  91. const { el, component } = orientedNotifications[i].vm;
  92. const pos = Number.parseInt(el.style[verticalPos], 10) - removedHeight - GAP_SIZE;
  93. component.props.offset = pos;
  94. }
  95. }
  96. function closeAll() {
  97. for (const orientedNotifications of Object.values(notifications)) {
  98. orientedNotifications.forEach(({ vm }) => {
  99. vm.component.exposed.visible.value = false;
  100. });
  101. }
  102. }
  103. function updateOffsets(position = "top-right") {
  104. var _a, _b, _c, _d;
  105. let verticalOffset = ((_c = (_b = (_a = notifications[position][0]) == null ? void 0 : _a.vm.component) == null ? void 0 : _b.props) == null ? void 0 : _c.offset) || 0;
  106. for (const { vm } of notifications[position]) {
  107. vm.component.props.offset = verticalOffset;
  108. verticalOffset += (((_d = vm.el) == null ? void 0 : _d.offsetHeight) || 0) + GAP_SIZE;
  109. }
  110. }
  111. notify.closeAll = closeAll;
  112. notify.updateOffsets = updateOffsets;
  113. notify._context = null;
  114. export { close, closeAll, notify as default, updateOffsets };
  115. //# sourceMappingURL=notify.mjs.map