HookNotification.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. exports.getUuid = getUuid;
  8. var _vue = require("vue");
  9. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  10. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  11. var _Notice = _interopRequireDefault(require("./Notice"));
  12. var _transition = require("../_util/transition");
  13. var _classNames = _interopRequireDefault(require("../_util/classNames"));
  14. var _Portal = _interopRequireDefault(require("../_util/Portal"));
  15. let seed = 0;
  16. const now = Date.now();
  17. function getUuid() {
  18. const id = seed;
  19. seed += 1;
  20. return `rcNotification_${now}_${id}`;
  21. }
  22. const Notification = (0, _vue.defineComponent)({
  23. name: 'HookNotification',
  24. inheritAttrs: false,
  25. props: ['prefixCls', 'transitionName', 'animation', 'maxCount', 'closeIcon', 'hashId', 'remove', 'notices', 'getStyles', 'getClassName', 'onAllRemoved', 'getContainer'],
  26. setup(props, _ref) {
  27. let {
  28. attrs,
  29. slots
  30. } = _ref;
  31. const hookRefs = new Map();
  32. const notices = (0, _vue.computed)(() => props.notices);
  33. const transitionProps = (0, _vue.computed)(() => {
  34. let name = props.transitionName;
  35. if (!name && props.animation) {
  36. switch (typeof props.animation) {
  37. case 'string':
  38. name = props.animation;
  39. break;
  40. case 'function':
  41. name = props.animation().name;
  42. break;
  43. case 'object':
  44. name = props.animation.name;
  45. break;
  46. default:
  47. name = `${props.prefixCls}-fade`;
  48. break;
  49. }
  50. }
  51. return (0, _transition.getTransitionGroupProps)(name);
  52. });
  53. const remove = key => props.remove(key);
  54. const placements = (0, _vue.ref)({});
  55. (0, _vue.watch)(notices, () => {
  56. const nextPlacements = {};
  57. // init placements with animation
  58. Object.keys(placements.value).forEach(placement => {
  59. nextPlacements[placement] = [];
  60. });
  61. props.notices.forEach(config => {
  62. const {
  63. placement = 'topRight'
  64. } = config.notice;
  65. if (placement) {
  66. nextPlacements[placement] = nextPlacements[placement] || [];
  67. nextPlacements[placement].push(config);
  68. }
  69. });
  70. placements.value = nextPlacements;
  71. });
  72. const placementList = (0, _vue.computed)(() => Object.keys(placements.value));
  73. return () => {
  74. var _a;
  75. const {
  76. prefixCls,
  77. closeIcon = (_a = slots.closeIcon) === null || _a === void 0 ? void 0 : _a.call(slots, {
  78. prefixCls
  79. })
  80. } = props;
  81. const noticeNodes = placementList.value.map(placement => {
  82. var _a, _b;
  83. const noticesForPlacement = placements.value[placement];
  84. const classes = (_a = props.getClassName) === null || _a === void 0 ? void 0 : _a.call(props, placement);
  85. const styles = (_b = props.getStyles) === null || _b === void 0 ? void 0 : _b.call(props, placement);
  86. const noticeNodesForPlacement = noticesForPlacement.map((_ref2, index) => {
  87. let {
  88. notice,
  89. holderCallback
  90. } = _ref2;
  91. const updateMark = index === notices.value.length - 1 ? notice.updateMark : undefined;
  92. const {
  93. key,
  94. userPassKey
  95. } = notice;
  96. const {
  97. content
  98. } = notice;
  99. const noticeProps = (0, _extends2.default)((0, _extends2.default)((0, _extends2.default)({
  100. prefixCls,
  101. closeIcon: typeof closeIcon === 'function' ? closeIcon({
  102. prefixCls
  103. }) : closeIcon
  104. }, notice), notice.props), {
  105. key,
  106. noticeKey: userPassKey || key,
  107. updateMark,
  108. onClose: noticeKey => {
  109. var _a;
  110. remove(noticeKey);
  111. (_a = notice.onClose) === null || _a === void 0 ? void 0 : _a.call(notice);
  112. },
  113. onClick: notice.onClick
  114. });
  115. if (holderCallback) {
  116. return (0, _vue.createVNode)("div", {
  117. "key": key,
  118. "class": `${prefixCls}-hook-holder`,
  119. "ref": div => {
  120. if (typeof key === 'undefined') {
  121. return;
  122. }
  123. if (div) {
  124. hookRefs.set(key, div);
  125. holderCallback(div, noticeProps);
  126. } else {
  127. hookRefs.delete(key);
  128. }
  129. }
  130. }, null);
  131. }
  132. return (0, _vue.createVNode)(_Notice.default, (0, _objectSpread2.default)((0, _objectSpread2.default)({}, noticeProps), {}, {
  133. "class": (0, _classNames.default)(noticeProps.class, props.hashId)
  134. }), {
  135. default: () => [typeof content === 'function' ? content({
  136. prefixCls
  137. }) : content]
  138. });
  139. });
  140. const className = {
  141. [prefixCls]: 1,
  142. [`${prefixCls}-${placement}`]: 1,
  143. [attrs.class]: !!attrs.class,
  144. [props.hashId]: true,
  145. [classes]: !!classes
  146. };
  147. function onAfterLeave() {
  148. var _a;
  149. if (noticesForPlacement.length > 0) {
  150. return;
  151. }
  152. Reflect.deleteProperty(placements.value, placement);
  153. (_a = props.onAllRemoved) === null || _a === void 0 ? void 0 : _a.call(props);
  154. }
  155. return (0, _vue.createVNode)("div", {
  156. "key": placement,
  157. "class": className,
  158. "style": attrs.style || styles || {
  159. top: '65px',
  160. left: '50%'
  161. }
  162. }, [(0, _vue.createVNode)(_vue.TransitionGroup, (0, _objectSpread2.default)((0, _objectSpread2.default)({
  163. "tag": "div"
  164. }, transitionProps.value), {}, {
  165. "onAfterLeave": onAfterLeave
  166. }), {
  167. default: () => [noticeNodesForPlacement]
  168. })]);
  169. });
  170. return (0, _vue.createVNode)(_Portal.default, {
  171. "getContainer": props.getContainer
  172. }, {
  173. default: () => [noticeNodes]
  174. });
  175. };
  176. }
  177. });
  178. var _default = exports.default = Notification;