(function ($, document, Dashboard) { var userId; var getNotificationsSummaryPromise; function getNotificationsSummary() { getNotificationsSummaryPromise = getNotificationsSummaryPromise || ApiClient.getNotificationSummary(userId); return getNotificationsSummaryPromise; } function updateNotificationCount() { getNotificationsSummary().done(function (summary) { var elem = $('.btnNotifications').removeClass('levelNormal').removeClass('levelWarning').removeClass('levelError').html(summary.UnreadCount); if (summary.UnreadCount) { elem.addClass('level' + summary.MaxUnreadNotificationLevel); } }); } function showNotificationsFlyout() { var context = this; var html = '
'; html += 'Close'; html += '
'; html += '

Notifications

'; html += '
'; html += '
'; html += '

Loading...'; html += '

'; html += ''; html += '
'; html += '
'; $(document.body).append(html); $('.notificationsFlyout').popup({ positionTo: context }).trigger('create').popup("open").on("popupafterclose", function () { $(this).off("popupafterclose").remove(); }).on('click', '.btnMarkRead', function () { var ids = $('.unreadFlyoutNotification').map(function () { return this.getAttribute('data-notificationid'); }).get(); ApiClient.markNotificationsRead(Dashboard.getCurrentUserId(), ids, true).done(function () { $('.notificationsFlyout').popup("close"); }); }); refreshFlyoutContents(); } function refreshFlyoutContents() { var limit = 5; var startIndex = 0; ApiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).done(function (result) { listUnreadNotifications(result.Notifications, result.TotalRecordCount, startIndex, limit); }); } function listUnreadNotifications(notifications, totalRecordCount, startIndex, limit) { var elem = $('.notificationsFlyoutlist'); if (!totalRecordCount) { elem.html('

No unread notifications.

'); $('.btnMarkReadContainer').hide(); return; } if (notifications.filter(function (n) { return !n.IsRead; }).length) { $('.btnMarkReadContainer').show(); } else { $('.btnMarkReadContainer').hide(); } var html = ''; for (var i = 0, length = notifications.length; i < length; i++) { var notification = notifications[i]; html += getNotificationHtml(notification); } elem.html(html).trigger('create'); } function getNotificationHtml(notification) { var html = ''; var cssClass = notification.IsRead ? "flyoutNotification" : "flyoutNotification unreadFlyoutNotification"; html += '
'; html += '
'; html += getImageHtml(notification); html += '
'; html += '
'; html += '

' + notification.Name + '

'; html += '

' + humane_date(notification.Date) + '

'; if (notification.Description) { html += '

' + notification.Description + '

'; } if (notification.Url) { html += '

More information

'; } html += '
'; html += '
'; return html; } function getImageHtml(notification) { if (notification.Level == "Error") { return '
'; } if (notification.Level == "Warning") { return '
'; } return '
'; } $(Dashboard).on('interiorheaderrendered', function (e, header, user) { if (!user || $('.notificationsButton', header).length) { return; } userId = user.Id; $('0').insertAfter($('.btnCurrentUser', header)).on('click', showNotificationsFlyout); updateNotificationCount(); }); $(ApiClient).on("websocketmessage", function (e, msg) { if (msg.MessageType === "NotificationUpdated" || msg.MessageType === "NotificationAdded" || msg.MessageType === "NotificationsMarkedRead") { getNotificationsSummaryPromise = null; updateNotificationCount(); } }); })(jQuery, document, Dashboard);