jellyfin-web/dashboard-ui/scripts/notifications.js

196 lines
5.9 KiB
JavaScript
Raw Normal View History

2013-07-06 14:23:32 -07:00
(function ($, document, Dashboard) {
var getNotificationsSummaryPromise;
function getNotificationsSummary() {
getNotificationsSummaryPromise = getNotificationsSummaryPromise || ApiClient.getNotificationSummary(Dashboard.getCurrentUserId());
2013-07-06 14:23:32 -07:00
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;
2013-12-27 09:18:42 -07:00
var html = '<div data-role="popup" class="notificationsFlyout" style="min-width:250px;margin-top:30px;margin-right:20px;" data-theme="a">';
2013-07-06 14:23:32 -07:00
2013-12-24 11:37:29 -07:00
html += '<a href="#" data-rel="back" data-role="button" data-theme="b" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>';
2013-07-06 14:23:32 -07:00
2013-12-24 11:37:29 -07:00
html += '<div class="ui-bar-a" style="text-align:center;">';
2013-07-06 22:41:28 -07:00
html += '<h3 style="margin: .5em 0;">Notifications</h3>';
2013-07-06 14:23:32 -07:00
html += '</div>';
2013-12-24 11:37:29 -07:00
html += '<div data-role="content" style="padding: 0;">';
2013-07-06 14:23:32 -07:00
html += '<p class="notificationsFlyoutlist">Loading...';
html += '</p>';
2013-12-27 09:18:42 -07:00
html += '<p style="display:none;" class="btnMarkReadContainer"><button class="btnMarkRead" type="button" data-icon="check" data-mini="true" data-theme="b">Mark these read</button></p>';
2013-07-06 14:23:32 -07:00
html += '</div>';
html += '</div>';
$(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");
getNotificationsSummaryPromise = null;
updateNotificationCount();
2013-07-06 14:23:32 -07:00
});
});
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) {
2013-07-06 19:01:14 -07:00
elem.html('<p style="padding:.5em 1em;">No unread notifications.</p>');
2013-07-06 14:23:32 -07:00
$('.btnMarkReadContainer').hide();
return;
}
if (notifications.filter(function (n) {
2013-07-06 14:23:32 -07:00
return !n.IsRead;
2013-07-06 14:23:32 -07:00
}).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');
2013-07-06 14:23:32 -07:00
}
function getNotificationHtml(notification) {
var html = '';
var cssClass = notification.IsRead ? "flyoutNotification" : "flyoutNotification unreadFlyoutNotification";
html += '<div data-notificationid="' + notification.Id + '" class="' + cssClass + '">';
html += '<div class="notificationImage">';
html += getImageHtml(notification);
html += '</div>';
html += '<div class="notificationContent">';
2014-02-28 21:12:56 -07:00
html += '<p style="margin: .4em 0 .25em;" class="notificationName">' + notification.Name + '</p>';
2013-07-06 14:23:32 -07:00
2014-02-28 21:12:56 -07:00
html += '<p style="margin: .25em 0;">' + humane_date(notification.Date) + '</p>';
2013-07-06 14:23:32 -07:00
if (notification.Description) {
2014-02-28 21:12:56 -07:00
html += '<p style="margin: .25em 0;">' + notification.Description + '</p>';
2013-07-06 14:23:32 -07:00
}
if (notification.Url) {
2014-02-28 21:12:56 -07:00
html += '<p style="margin: .25em 0;"><a href="' + notification.Url + '" target="_blank">More information</a></p>';
}
2013-07-06 14:23:32 -07:00
html += '</div>';
2013-07-06 16:08:10 -07:00
2013-07-06 14:23:32 -07:00
html += '</div>';
return html;
}
2013-07-06 14:23:32 -07:00
function getImageHtml(notification) {
2013-07-06 14:23:32 -07:00
if (notification.Level == "Error") {
return '<div class="imgNotification imgNotificationError"><div class="imgNotificationInner imgNotificationIcon"></div></div>';
}
if (notification.Level == "Warning") {
return '<div class="imgNotification imgNotificationWarning"><div class="imgNotificationInner imgNotificationIcon"></div></div>';
}
2013-07-06 14:23:32 -07:00
return '<div class="imgNotification imgNotificationNormal"><div class="imgNotificationInner imgNotificationIcon"></div></div>';
}
$(Dashboard).on('interiorheaderrendered', function (e, header, user) {
if (!user || $('.notificationsButton', header).length) {
return;
}
2013-07-06 22:41:28 -07:00
$('<a class="imageLink btnNotifications" href="#" title="Notifications">0</a>').insertAfter($('.btnCurrentUser', header)).on('click', showNotificationsFlyout);
2013-07-06 14:23:32 -07:00
updateNotificationCount();
});
$(ApiClient).on("websocketmessage", function (e, msg) {
if (msg.MessageType === "NotificationUpdated" || msg.MessageType === "NotificationAdded" || msg.MessageType === "NotificationsMarkedRead") {
getNotificationsSummaryPromise = null;
updateNotificationCount();
}
});
})(jQuery, document, Dashboard);