123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- var abp = abp || {};
- abp.userNotificationHelper = abp.userNotificationHelper || {};
- abp.userNotificationHelper.getUrl = function (userNotification) {
- switch (userNotification.notification.notificationName) {
- default:
- return '#';
- }
- //switch (userNotification.notification.notificationName) {
- // case 'App.NewUserRegistered':
- // return '/AppAreaName/users?filterText=' + userNotification.notification.data.properties.emailAddress;
- // case 'App.NewTenantRegistered':
- // return '/AppAreaName/tenants?filterText=' + userNotification.notification.data.properties.tenancyName;
- // case 'App.DownloadInvalidImportUsers':
- // return '/File/DownloadTempFile?fileToken=' + userNotification.notification.data.properties.fileToken +
- // '&fileType=' + userNotification.notification.data.properties.fileType +
- // '&fileName=' + userNotification.notification.data.properties.fileName;
- // //Add your custom notification names to navigate to a URL when user clicks to a notification.
- //}
- };
- abp.userNotificationHelper.getUiIconBySeverity = function (severity) {
- switch (severity) {
- case abp.notifications.severity.SUCCESS:
- return 'fas fa-check text-success';
- case abp.notifications.severity.WARN:
- return 'fas fa-exclamation text-warning';
- case abp.notifications.severity.ERROR:
- return 'fas fa-bolt text-danger';
- case abp.notifications.severity.FATAL:
- return 'fas fa-bomb text-danger';
- case abp.notifications.severity.INFO:
- default:
- return 'fas fa-info text-primary';
- }
- };
- abp.userNotificationHelper.format = function (userNotification, truncateText) {
- moment.locale(lang);
- var formatted = {
- userNotificationId: userNotification.id,
- text: abp.notifications.getFormattedMessageFromUserNotification(userNotification),
- time: moment(userNotification.notification.creationTime).format("YYYY-MM-DD HH:mm:ss"),
- icon: abp.userNotificationHelper.getUiIconBySeverity(userNotification.notification.severity),
- state: abp.notifications.getUserNotificationStateAsString(userNotification.state),
- data: userNotification.notification.data,
- url: abp.userNotificationHelper.getUrl(userNotification),
- isUnread: userNotification.state === abp.notifications.userNotificationState.UNREAD,
- timeAgo: moment(userNotification.notification.creationTime).fromNow()
- };
- if (truncateText || truncateText === undefined) {
- formatted.text = abp.utils.truncateStringWithPostfix(formatted.text, 100);
- }
- return formatted;
- };
- abp.userNotificationHelper.show = function (userNotification) {
- //Application notification
- abp.notifications.showUiNotifyForUserNotification(userNotification, {
- 'onclick': function () {
- //Take action when user clicks to live toastr notification
- var url = getUrl(userNotification);
- if (url) {
- location.href = url;
- }
- }
- });
- //Desktop notification
- //Push.create("新消息", {
- // body:abp.userNotificationHelper.format(userNotification).text,
- // icon: abp.appPath + 'Content/image/logo.png',
- // timeout: 10000,
- // onClick: function () {
- // window.focus();
- // this.close();
- // }
- //});
- };
- abp.userNotificationHelper.setAllAsRead = function (callback) {
- $.iwbAjax4({
- url: abp.appUrl + 'Notification/SetAllNotificationsAsRead',
- success: function () {
- abp.event.trigger('app.notifications.refresh');
- callback && callback();
- }
- });
- };
- abp.userNotificationHelper.setAsRead = function (userNotificationId, callback) {
- $.iwbAjax4({
- url: abp.appUrl + 'Notification/SetNotificationAsRead',
- data: { id: userNotificationId },
- success: function () {
- abp.event.trigger('app.notifications.read', userNotificationId);
- callback && callback(userNotificationId);
- }
- });
- };
- $(function () {
- //Notification handler
- function bindNotificationEvents() {
- $('#SetAllNotificationsAsRead').click(function (e) {
- e.preventDefault();
- e.stopPropagation();
- abp.userNotificationHelper.setAllAsRead(function () {
- loadNotifications();
- });
- });
- $('.set-read').click(function (e) {
- e.preventDefault();
- e.stopPropagation();
- var notificationId = $(this).closest('.notification-item').find('.no-read').attr("data-notification-id");
- if (notificationId) {
- abp.userNotificationHelper.setAsRead(notificationId, function () {
- loadNotifications();
- });
- }
- });
- $('div.notification-url').click(function () {
- var url = $(this).attr('data-url');
- document.location.href = url;
- });
- }
- function loadNotifications() {
- $.iwbAjax4({
- url: abp.appUrl + 'Notification/getUserNotifications',
- data: { maxResultCount: 5 },
- success: function (result) {
- result.notifications = [];
- result.unreadMessageExists = result.unreadCount > 0;
- $.each(result.items, function (index, item) {
- result.notifications.push(abp.userNotificationHelper.format(item));
- });
- var $li = $('#header_notification_bar');
- var template = $('#headerNotificationBarTemplate').html();
- Mustache.parse(template);
- var rendered = Mustache.render(template, result);
- $li.html(rendered);
- bindNotificationEvents();
- if (result.unreadCount > 0) {
- $.blinkTitle.stop();
- $.blinkTitle.start();
- } else {
- $.blinkTitle.stop();
- }
- }
- });
- }
- abp.event.on('abp.notifications.received', function (userNotification) {
- abp.userNotificationHelper.show(userNotification);
- loadNotifications();
- });
- abp.event.on('app.notifications.refresh', function () {
- loadNotifications();
- });
- abp.event.on('app.notifications.read', function () {
- loadNotifications();
- });
- loadNotifications();
- });
|