mirror of
https://github.com/jellyfin/jellyfin-web
synced 2025-03-30 19:56:21 +00:00
added a notifications service
This commit is contained in:
parent
95a8a246b9
commit
cfb3392c35
8 changed files with 411 additions and 4 deletions
191
dashboard-ui/scripts/notifications.js
Normal file
191
dashboard-ui/scripts/notifications.js
Normal file
|
@ -0,0 +1,191 @@
|
|||
(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 = '<div data-role="popup" class="notificationsFlyout" style="min-width:200px;margin-top:30px;margin-right:20px;" class="ui-corner-all">';
|
||||
|
||||
html += '<a href="#" data-rel="back" data-role="button" data-theme="a" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>';
|
||||
|
||||
html += '<div class="ui-corner-top ui-bar-a" style="text-align:center;">';
|
||||
html += '<h3 style="margin: .5em 0;">Notifications</h3>';
|
||||
html += '</div>';
|
||||
|
||||
html += '<div data-role="content" class="ui-corner-bottom ui-content" style="padding: 0;background: #f8f8f8;">';
|
||||
|
||||
html += '<p class="notificationsFlyoutlist">Loading...';
|
||||
|
||||
html += '</p>';
|
||||
|
||||
html += '<p style="display:none;" class="btnMarkReadContainer"><button class="btnMarkRead" type="button" data-icon="ok" data-mini="true">Mark these read</button></p>';
|
||||
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");
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
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('<p>No unread notifications.</p>');
|
||||
$('.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);
|
||||
}
|
||||
|
||||
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">';
|
||||
|
||||
html += '<p class="notificationName">' + notification.Name + '</p>';
|
||||
|
||||
html += '<p>' + humane_date(notification.Date) + '</p>';
|
||||
|
||||
if (notification.Description) {
|
||||
html += '<p>' + notification.Description + '</p>';
|
||||
}
|
||||
|
||||
html += '</div>';
|
||||
html += '</div>';
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
function getImageHtml(notification) {
|
||||
|
||||
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>';
|
||||
|
||||
}
|
||||
|
||||
return '<div class="imgNotification imgNotificationNormal"><div class="imgNotificationInner imgNotificationIcon"></div></div>';
|
||||
|
||||
}
|
||||
|
||||
$(Dashboard).on('interiorheaderrendered', function (e, header, user) {
|
||||
|
||||
if (!user || $('.notificationsButton', header).length) {
|
||||
return;
|
||||
}
|
||||
|
||||
userId = user.Id;
|
||||
|
||||
$('<a class="imageLink btnNotifications" href="#" title="Notifications">0</a>').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);
|
Loading…
Add table
Add a link
Reference in a new issue