(function ($, document, Dashboard, LibraryBrowser) { function notifications() { var self = this; self.getNotificationsSummaryPromise = null; self.total = 0; self.getNotificationsSummary = function() { self.getNotificationsSummaryPromise = self.getNotificationsSummaryPromise || ApiClient.getNotificationSummary(Dashboard.getCurrentUserId()); return self.getNotificationsSummaryPromise; }; self.updateNotificationCount = function() { self.getNotificationsSummary().done(function(summary) { var item = $('.btnNotifications').removeClass('levelNormal').removeClass('levelWarning').removeClass('levelError').html(summary.UnreadCount); if (summary.UnreadCount) { item.addClass('level' + summary.MaxUnreadNotificationLevel); } }); }; self.showNotificationsFlyout = function() { var context = this; var html = '
'; html += 'Close'; html += '
'; html += '

Notifications

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

Loading...'; html += '

'; 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(); self.markNotificationsRead(ids, function() { $('.notificationsFlyout').popup("close"); }); }).on("click", ".btnNotificationList", function(e) { e.preventDefault(); Dashboard.navigate("notificationlist.html"); }); self.isFlyout = true; var startIndex = 0; var limit = 4; var elem = $('.notificationsFlyoutlist'); var markReadButton = $('.btnMarkReadContainer'); refreshNotifications(startIndex, limit, elem, markReadButton, false); }; self.markNotificationsRead = function(ids, callback) { ApiClient.markNotificationsRead(Dashboard.getCurrentUserId(), ids, true).done(function() { self.getNotificationsSummaryPromise = null; self.updateNotificationCount(); callback(); }); }; self.showNotificationsList = function (startIndex, limit, elem, btn) { refreshNotifications(startIndex, limit, elem, btn, true); }; } function refreshNotifications(startIndex, limit, elem, btn, showPaging) { ApiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).done(function (result) { listUnreadNotifications(result.Notifications, result.TotalRecordCount, startIndex, limit, elem, btn, showPaging); }); } function listUnreadNotifications(list, totalRecordCount, startIndex, limit, elem, btn, showPaging) { if (!totalRecordCount) { elem.html('

No unread notifications.

'); btn.hide(); return; } Notifications.total = totalRecordCount; if (list.filter(function (n) { return !n.IsRead; }).length) { btn.show(); } else { btn.hide(); } var html = ''; if (totalRecordCount > limit && showPaging === true) { var query = { StartIndex: startIndex, Limit: limit }; html += LibraryBrowser.getPagingHtml(query, totalRecordCount, false, limit, false); } for (var i = 0, length = list.length; i < length; i++) { var notification = list[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 '
'; } window.Notifications = new notifications(); $(Dashboard).on('interiorheaderrendered', function (e, header, user) { if (!user || $('.notificationsButton', header).length) { return; } $('0').insertAfter($('.btnCurrentUser', header)).on('click', Notifications.showNotificationsFlyout); Notifications.updateNotificationCount(); }); $(ApiClient).on("websocketmessage", function (e, msg) { if (msg.MessageType === "NotificationUpdated" || msg.MessageType === "NotificationAdded" || msg.MessageType === "NotificationsMarkedRead") { Notifications.getNotificationsSummaryPromise = null; Notifications.updateNotificationCount(); } }); })(jQuery, document, Dashboard, LibraryBrowser);