notificationHelper.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. var abp = abp || {};
  2. abp.userNotificationHelper = abp.userNotificationHelper || {};
  3. abp.userNotificationHelper.getUrl = function (userNotification) {
  4. switch (userNotification.notification.notificationName) {
  5. default:
  6. return '#';
  7. }
  8. //switch (userNotification.notification.notificationName) {
  9. // case 'App.NewUserRegistered':
  10. // return '/AppAreaName/users?filterText=' + userNotification.notification.data.properties.emailAddress;
  11. // case 'App.NewTenantRegistered':
  12. // return '/AppAreaName/tenants?filterText=' + userNotification.notification.data.properties.tenancyName;
  13. // case 'App.DownloadInvalidImportUsers':
  14. // return '/File/DownloadTempFile?fileToken=' + userNotification.notification.data.properties.fileToken +
  15. // '&fileType=' + userNotification.notification.data.properties.fileType +
  16. // '&fileName=' + userNotification.notification.data.properties.fileName;
  17. // //Add your custom notification names to navigate to a URL when user clicks to a notification.
  18. //}
  19. };
  20. abp.userNotificationHelper.getUiIconBySeverity = function (severity) {
  21. switch (severity) {
  22. case abp.notifications.severity.SUCCESS:
  23. return 'fas fa-check text-success';
  24. case abp.notifications.severity.WARN:
  25. return 'fas fa-exclamation text-warning';
  26. case abp.notifications.severity.ERROR:
  27. return 'fas fa-bolt text-danger';
  28. case abp.notifications.severity.FATAL:
  29. return 'fas fa-bomb text-danger';
  30. case abp.notifications.severity.INFO:
  31. default:
  32. return 'fas fa-info text-primary';
  33. }
  34. };
  35. abp.userNotificationHelper.format = function (userNotification, truncateText) {
  36. moment.locale(lang);
  37. var formatted = {
  38. userNotificationId: userNotification.id,
  39. text: abp.notifications.getFormattedMessageFromUserNotification(userNotification),
  40. time: moment(userNotification.notification.creationTime).format("YYYY-MM-DD HH:mm:ss"),
  41. icon: abp.userNotificationHelper.getUiIconBySeverity(userNotification.notification.severity),
  42. state: abp.notifications.getUserNotificationStateAsString(userNotification.state),
  43. data: userNotification.notification.data,
  44. url: abp.userNotificationHelper.getUrl(userNotification),
  45. isUnread: userNotification.state === abp.notifications.userNotificationState.UNREAD,
  46. timeAgo: moment(userNotification.notification.creationTime).fromNow()
  47. };
  48. if (truncateText || truncateText === undefined) {
  49. formatted.text = abp.utils.truncateStringWithPostfix(formatted.text, 100);
  50. }
  51. return formatted;
  52. };
  53. abp.userNotificationHelper.show = function (userNotification) {
  54. //Application notification
  55. abp.notifications.showUiNotifyForUserNotification(userNotification, {
  56. 'onclick': function () {
  57. //Take action when user clicks to live toastr notification
  58. var url = getUrl(userNotification);
  59. if (url) {
  60. location.href = url;
  61. }
  62. }
  63. });
  64. //Desktop notification
  65. //Push.create("新消息", {
  66. // body:abp.userNotificationHelper.format(userNotification).text,
  67. // icon: abp.appPath + 'Content/image/logo.png',
  68. // timeout: 10000,
  69. // onClick: function () {
  70. // window.focus();
  71. // this.close();
  72. // }
  73. //});
  74. };
  75. abp.userNotificationHelper.setAllAsRead = function (callback) {
  76. $.iwbAjax4({
  77. url: abp.appUrl + 'Notification/SetAllNotificationsAsRead',
  78. success: function () {
  79. abp.event.trigger('app.notifications.refresh');
  80. callback && callback();
  81. }
  82. });
  83. };
  84. abp.userNotificationHelper.setAsRead = function (userNotificationId, callback) {
  85. $.iwbAjax4({
  86. url: abp.appUrl + 'Notification/SetNotificationAsRead',
  87. data: { id: userNotificationId },
  88. success: function () {
  89. abp.event.trigger('app.notifications.read', userNotificationId);
  90. callback && callback(userNotificationId);
  91. }
  92. });
  93. };
  94. $(function () {
  95. //Notification handler
  96. function bindNotificationEvents() {
  97. $('#SetAllNotificationsAsRead').click(function (e) {
  98. e.preventDefault();
  99. e.stopPropagation();
  100. abp.userNotificationHelper.setAllAsRead(function () {
  101. loadNotifications();
  102. });
  103. });
  104. $('.set-read').click(function (e) {
  105. e.preventDefault();
  106. e.stopPropagation();
  107. var notificationId = $(this).closest('.notification-item').find('.no-read').attr("data-notification-id");
  108. if (notificationId) {
  109. abp.userNotificationHelper.setAsRead(notificationId, function () {
  110. loadNotifications();
  111. });
  112. }
  113. });
  114. $('div.notification-url').click(function () {
  115. var url = $(this).attr('data-url');
  116. document.location.href = url;
  117. });
  118. }
  119. function loadNotifications() {
  120. $.iwbAjax4({
  121. url: abp.appUrl + 'Notification/getUserNotifications',
  122. data: { maxResultCount: 5 },
  123. success: function (result) {
  124. result.notifications = [];
  125. result.unreadMessageExists = result.unreadCount > 0;
  126. $.each(result.items, function (index, item) {
  127. result.notifications.push(abp.userNotificationHelper.format(item));
  128. });
  129. var $li = $('#header_notification_bar');
  130. var template = $('#headerNotificationBarTemplate').html();
  131. Mustache.parse(template);
  132. var rendered = Mustache.render(template, result);
  133. $li.html(rendered);
  134. bindNotificationEvents();
  135. if (result.unreadCount > 0) {
  136. $.blinkTitle.stop();
  137. $.blinkTitle.start();
  138. } else {
  139. $.blinkTitle.stop();
  140. }
  141. }
  142. });
  143. }
  144. abp.event.on('abp.notifications.received', function (userNotification) {
  145. abp.userNotificationHelper.show(userNotification);
  146. loadNotifications();
  147. });
  148. abp.event.on('app.notifications.refresh', function () {
  149. loadNotifications();
  150. });
  151. abp.event.on('app.notifications.read', function () {
  152. loadNotifications();
  153. });
  154. loadNotifications();
  155. });