var abp = abp || {}; abp.userNotificationHelper = abp.userNotificationHelper || {}; abp.userNotificationHelper.getUrl = function (userNotification) { switch (userNotification.notification.notificationName) { case 'KeyPointAlarm': case 'KeyPointExpire': var contractNo = userNotification.notification.data.contractNo; return "/LegalCase/ContractDetail/" + contractNo; 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 'fa fa-check bg-success'; case abp.notifications.severity.WARN: return 'fa fa-exclamation bg-warning'; case abp.notifications.severity.ERROR: return 'fa fa-bolt bg-danger'; case abp.notifications.severity.FATAL: return 'fa fa-bomb bg-danger'; case abp.notifications.severity.INFO: default: return 'fa fa-info bg-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 = abp.userNotificationHelper.getUrl(userNotification); if (url) { location.href = url; } } }); var text = abp.notifications.getFormattedMessageFromUserNotification(userNotification); //Desktop notification Push.create("系统通知", { body: text, icon: abp.appPath + 'Content/Image/logo.png', timeout: 6000, 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); } }); }; abp.notifications.messageFormatters["ContractService.CommonManager.Notifications.KeyPointNotificationData"] = function (userNotification) { var data = userNotification.notification.data; return '[{0}]合同的[{1}]关键点需要在[{2}]处理 立即处理{4}' .format(data.contractName, data.keyPointName, new Date(data.expireDate).format("yyyy-MM-dd"), data.isAlarm ? "text-iwb" : "text-danger", data.isAlarm ? '' : '(已过期)', abp.userNotificationHelper.getUrl(userNotification), userNotification.id ); } $(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').data("notification-id"); if (notificationId) { abp.userNotificationHelper.setAsRead(notificationId, function () { loadNotifications(); }); } }); $('div.notification-url').click(function () { var url = $(this).data('url'); var notificationId = $(this).closest('.notification-item').data("notification-id"); if (notificationId) { abp.userNotificationHelper.setAsRead(notificationId, function () { 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,0)); }); var $li = $('#header_notification_bar'); var rendered = NotificationsFormatter(result); $li.html(rendered); bindNotificationEvents(); if (result.unreadCount > 0) { $.blinkTitle.stop(); $.blinkTitle.start(); } else { $.blinkTitle.stop(); } } }); } function NotificationsFormatter(data) { var str = ''; str += '{0}'.format( data.unreadCount ? ''.format(data.unreadCount) : ''); str += '
'; return str; } function NotificationFormatter(data) { var str = ''; str += '