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

238 lines
7.3 KiB
JavaScript
Raw Normal View History

(function ($, document, Dashboard, LibraryBrowser) {
2013-07-06 14:23:32 -07:00
function notifications() {
2013-07-06 14:23:32 -07:00
var self = this;
2013-07-06 14:23:32 -07:00
self.getNotificationsSummaryPromise = null;
2013-07-06 14:23:32 -07:00
self.total = 0;
2013-07-06 14:23:32 -07:00
self.getNotificationsSummary = function() {
2013-07-06 14:23:32 -07:00
self.getNotificationsSummaryPromise = self.getNotificationsSummaryPromise || ApiClient.getNotificationSummary(Dashboard.getCurrentUserId());
2013-07-06 14:23:32 -07:00
return self.getNotificationsSummaryPromise;
};
2013-07-06 14:23:32 -07:00
self.updateNotificationCount = function() {
2013-07-06 14:23:32 -07:00
self.getNotificationsSummary().done(function(summary) {
2013-07-06 14:23:32 -07:00
var item = $('.btnNotifications').removeClass('levelNormal').removeClass('levelWarning').removeClass('levelError').html(summary.UnreadCount);
2013-07-06 14:23:32 -07:00
if (summary.UnreadCount) {
item.addClass('level' + summary.MaxUnreadNotificationLevel);
}
});
};
2013-07-06 14:23:32 -07:00
self.showNotificationsFlyout = function() {
2013-07-06 14:23:32 -07:00
var context = this;
2013-07-06 14:23:32 -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
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
html += '<div class="ui-bar-a" style="text-align:center;">';
html += '<h3 style="margin: .5em 0;">Notifications</h3>';
html += '</div>';
2013-07-06 14:23:32 -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>';
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 += '<p class="btnNotificationListContainer"><button class="btnNotificationList" type="button" data-icon="check" data-mini="true" data-theme="b">View Notifications</button></p>';
2013-07-06 14:23:32 -07:00
html += '</div>';
2013-07-06 14:23:32 -07:00
html += '</div>';
2013-07-06 14:23:32 -07:00
$(document.body).append(html);
2013-07-06 14:23:32 -07:00
$('.notificationsFlyout').popup({ positionTo: context }).trigger('create').popup("open").on("popupafterclose", function() {
2013-07-06 14:23:32 -07:00
$(this).off("popupafterclose").remove();
2013-07-06 14:23:32 -07:00
}).on('click', '.btnMarkRead', function() {
2013-07-06 14:23:32 -07:00
var ids = $('.unreadFlyoutNotification').map(function() {
2013-07-06 14:23:32 -07:00
return this.getAttribute('data-notificationid');
2013-07-06 14:23:32 -07:00
}).get();
2013-07-06 14:23:32 -07:00
self.markNotificationsRead(ids, function() {
$('.notificationsFlyout').popup("close");
});
}).on("click", ".btnNotificationList", function(e) {
e.preventDefault();
Dashboard.navigate("notificationlist.html");
2013-07-06 14:23:32 -07:00
});
var startIndex = 0;
var limit = 5;
var elem = $('.notificationsFlyoutlist');
var markReadButton = $('.btnMarkReadContainer');
2013-07-06 14:23:32 -07:00
refreshNotifications(startIndex, limit, elem, markReadButton);
};
self.markNotificationsRead = function(ids, callback) {
ApiClient.markNotificationsRead(Dashboard.getCurrentUserId(), ids, true).done(function() {
2013-07-06 14:23:32 -07:00
self.getNotificationsSummaryPromise = null;
2013-07-06 14:23:32 -07:00
self.updateNotificationCount();
callback();
});
};
self.showNotificationsList = function(startIndex, limit, elem, btn) {
refreshNotifications(startIndex, limit, elem, btn);
};
}
function refreshNotifications(startIndex, limit, elem, btn) {
2013-07-06 14:23:32 -07:00
ApiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).done(function (result) {
listUnreadNotifications(result.Notifications, result.TotalRecordCount, startIndex, limit, elem, btn);
2013-07-06 14:23:32 -07:00
});
}
function listUnreadNotifications(list, totalRecordCount, startIndex, limit, elem, btn) {
2013-07-06 14:23:32 -07:00
if (!totalRecordCount) {
2013-07-06 19:01:14 -07:00
elem.html('<p style="padding:.5em 1em;">No unread notifications.</p>');
btn.hide();
2013-07-06 14:23:32 -07:00
return;
}
Notifications.total = totalRecordCount;
if (list.filter(function (n) {
2013-07-06 14:23:32 -07:00
return !n.IsRead;
2013-07-06 14:23:32 -07:00
}).length) {
btn.show();
2013-07-06 14:23:32 -07:00
} else {
btn.hide();
2013-07-06 14:23:32 -07:00
}
var html = '';
if (totalRecordCount > limit) {
var query = { StartIndex: startIndex, Limit: limit };
2013-07-06 14:23:32 -07:00
html += LibraryBrowser.getPagingHtml(query, totalRecordCount, false, limit, false);
}
for (var i = 0, length = list.length; i < length; i++) {
var notification = list[i];
2013-07-06 14:23:32 -07:00
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>';
}
window.Notifications = new notifications();
2013-07-06 14:23:32 -07:00
$(Dashboard).on('interiorheaderrendered', function (e, header, user) {
if (!user || $('.notificationsButton', header).length) {
return;
}
$('<a class="imageLink btnNotifications" href="#" title="Notifications">0</a>').insertAfter($('.btnCurrentUser', header)).on('click', Notifications.showNotificationsFlyout);
2013-07-06 14:23:32 -07:00
Notifications.updateNotificationCount();
2013-07-06 14:23:32 -07:00
});
$(ApiClient).on("websocketmessage", function (e, msg) {
if (msg.MessageType === "NotificationUpdated" || msg.MessageType === "NotificationAdded" || msg.MessageType === "NotificationsMarkedRead") {
Notifications.getNotificationsSummaryPromise = null;
2013-07-06 14:23:32 -07:00
Notifications.updateNotificationCount();
2013-07-06 14:23:32 -07:00
}
});
})(jQuery, document, Dashboard, LibraryBrowser);