useNotification.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = useNotification;
  7. var _vue = require("vue");
  8. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  9. var _HookNotification = _interopRequireWildcard(require("./HookNotification"));
  10. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
  11. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
  12. var __rest = void 0 && (void 0).__rest || function (s, e) {
  13. var t = {};
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  15. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  16. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  17. }
  18. return t;
  19. };
  20. const defaultGetContainer = () => document.body;
  21. let uniqueKey = 0;
  22. function mergeConfig() {
  23. const clone = {};
  24. for (var _len = arguments.length, objList = new Array(_len), _key = 0; _key < _len; _key++) {
  25. objList[_key] = arguments[_key];
  26. }
  27. objList.forEach(obj => {
  28. if (obj) {
  29. Object.keys(obj).forEach(key => {
  30. const val = obj[key];
  31. if (val !== undefined) {
  32. clone[key] = val;
  33. }
  34. });
  35. }
  36. });
  37. return clone;
  38. }
  39. function useNotification() {
  40. let rootConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  41. const {
  42. getContainer = defaultGetContainer,
  43. motion,
  44. prefixCls,
  45. maxCount,
  46. getClassName,
  47. getStyles,
  48. onAllRemoved
  49. } = rootConfig,
  50. shareConfig = __rest(rootConfig, ["getContainer", "motion", "prefixCls", "maxCount", "getClassName", "getStyles", "onAllRemoved"]);
  51. const notices = (0, _vue.shallowRef)([]);
  52. const notificationsRef = (0, _vue.shallowRef)();
  53. const add = (originNotice, holderCallback) => {
  54. const key = originNotice.key || (0, _HookNotification.getUuid)();
  55. const notice = (0, _extends2.default)((0, _extends2.default)({}, originNotice), {
  56. key
  57. });
  58. const noticeIndex = notices.value.map(v => v.notice.key).indexOf(key);
  59. const updatedNotices = notices.value.concat();
  60. if (noticeIndex !== -1) {
  61. updatedNotices.splice(noticeIndex, 1, {
  62. notice,
  63. holderCallback
  64. });
  65. } else {
  66. if (maxCount && notices.value.length >= maxCount) {
  67. notice.key = updatedNotices[0].notice.key;
  68. notice.updateMark = (0, _HookNotification.getUuid)();
  69. notice.userPassKey = key;
  70. updatedNotices.shift();
  71. }
  72. updatedNotices.push({
  73. notice,
  74. holderCallback
  75. });
  76. }
  77. notices.value = updatedNotices;
  78. };
  79. const removeNotice = removeKey => {
  80. notices.value = notices.value.filter(_ref => {
  81. let {
  82. notice: {
  83. key,
  84. userPassKey
  85. }
  86. } = _ref;
  87. const mergedKey = userPassKey || key;
  88. return mergedKey !== removeKey;
  89. });
  90. };
  91. const destroy = () => {
  92. notices.value = [];
  93. };
  94. const contextHolder = () => (0, _vue.createVNode)(_HookNotification.default, {
  95. "ref": notificationsRef,
  96. "prefixCls": prefixCls,
  97. "maxCount": maxCount,
  98. "notices": notices.value,
  99. "remove": removeNotice,
  100. "getClassName": getClassName,
  101. "getStyles": getStyles,
  102. "animation": motion,
  103. "hashId": rootConfig.hashId,
  104. "onAllRemoved": onAllRemoved,
  105. "getContainer": getContainer
  106. }, null);
  107. const taskQueue = (0, _vue.shallowRef)([]);
  108. // ========================= Refs =========================
  109. const api = {
  110. open: config => {
  111. const mergedConfig = mergeConfig(shareConfig, config);
  112. //@ts-ignore
  113. if (mergedConfig.key === null || mergedConfig.key === undefined) {
  114. //@ts-ignore
  115. mergedConfig.key = `vc-notification-${uniqueKey}`;
  116. uniqueKey += 1;
  117. }
  118. taskQueue.value = [...taskQueue.value, {
  119. type: 'open',
  120. config: mergedConfig
  121. }];
  122. },
  123. close: key => {
  124. taskQueue.value = [...taskQueue.value, {
  125. type: 'close',
  126. key
  127. }];
  128. },
  129. destroy: () => {
  130. taskQueue.value = [...taskQueue.value, {
  131. type: 'destroy'
  132. }];
  133. }
  134. };
  135. // ======================== Effect ========================
  136. (0, _vue.watch)(taskQueue, () => {
  137. // Flush task when node ready
  138. if (taskQueue.value.length) {
  139. taskQueue.value.forEach(task => {
  140. switch (task.type) {
  141. case 'open':
  142. // @ts-ignore
  143. add(task.config);
  144. break;
  145. case 'close':
  146. removeNotice(task.key);
  147. break;
  148. case 'destroy':
  149. destroy();
  150. break;
  151. }
  152. });
  153. taskQueue.value = [];
  154. }
  155. });
  156. // ======================== Return ========================
  157. return [api, contextHolder];
  158. }