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

208 lines
5.7 KiB
JavaScript
Raw Normal View History

2016-01-19 20:02:14 -07:00
(function ($, document, Dashboard) {
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
2014-05-30 12:23:56 -07:00
self.getNotificationsSummary = function () {
2013-07-06 14:23:32 -07:00
2015-05-18 15:23:03 -07:00
var apiClient = window.ApiClient;
2014-10-25 11:32:58 -07:00
if (!apiClient) {
return;
}
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
2014-05-30 12:23:56 -07:00
self.updateNotificationCount = function () {
2013-07-06 14:23:32 -07:00
2014-07-13 14:03:57 -07:00
if (!Dashboard.getCurrentUserId()) {
return;
}
2015-09-21 18:05:33 -07:00
if (!window.ApiClient) {
return;
}
2014-10-25 11:32:58 -07:00
var promise = self.getNotificationsSummary();
if (!promise) {
return;
}
2015-12-14 08:43:03 -07:00
promise.then(function (summary) {
2013-07-06 14:23:32 -07:00
2014-06-21 22:52:31 -07:00
var item = $('.btnNotificationsInner').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
2014-05-30 12:23:56 -07:00
self.markNotificationsRead = function (ids, callback) {
2015-12-14 08:43:03 -07:00
ApiClient.markNotificationsRead(Dashboard.getCurrentUserId(), ids, true).then(function () {
2013-07-06 14:23:32 -07:00
self.getNotificationsSummaryPromise = null;
2013-07-06 14:23:32 -07:00
self.updateNotificationCount();
2014-10-04 11:05:24 -07:00
if (callback) {
callback();
}
});
};
2015-05-31 14:07:44 -07:00
self.showNotificationsList = function (startIndex, limit, elem) {
2015-05-31 14:07:44 -07:00
refreshNotifications(startIndex, limit, elem, true);
};
}
2015-05-31 14:07:44 -07:00
function refreshNotifications(startIndex, limit, elem, showPaging) {
2013-07-06 14:23:32 -07:00
2015-05-18 15:23:03 -07:00
var apiClient = window.ApiClient;
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
if (apiClient) {
2015-12-14 08:43:03 -07:00
return apiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).then(function (result) {
2013-07-06 14:23:32 -07:00
2015-05-31 14:07:44 -07:00
listUnreadNotifications(result.Notifications, result.TotalRecordCount, startIndex, limit, elem, showPaging);
2014-10-25 11:32:58 -07:00
});
}
2013-07-06 14:23:32 -07:00
}
2015-05-31 14:07:44 -07:00
function listUnreadNotifications(list, totalRecordCount, startIndex, limit, elem, showPaging) {
2013-07-06 14:23:32 -07:00
if (!totalRecordCount) {
2014-05-30 12:23:56 -07:00
elem.html('<p style="padding:.5em 1em;">' + Globalize.translate('LabelNoUnreadNotifications') + '</p>');
2014-10-04 11:05:24 -07:00
2013-07-06 14:23:32 -07:00
return;
}
Notifications.total = totalRecordCount;
2013-07-06 14:23:32 -07:00
var html = '';
if (totalRecordCount > limit && showPaging === true) {
var query = { StartIndex: startIndex, Limit: limit };
2013-07-06 14:23:32 -07:00
2015-06-18 11:29:44 -07:00
html += LibraryBrowser.getQueryPagingHtml({
startIndex: query.StartIndex,
limit: query.Limit,
totalRecordCount: totalRecordCount,
showLimit: false,
updatePageSizeSetting: false
});
}
2015-12-14 08:43:03 -07:00
require(['humanedate', 'paper-fab', 'paper-item-body', 'paper-icon-item'], function () {
for (var i = 0, length = list.length; i < length; i++) {
2015-12-14 08:43:03 -07:00
var notification = list[i];
2013-07-06 14:23:32 -07:00
2015-12-14 08:43:03 -07:00
html += getNotificationHtml(notification);
2013-07-06 14:23:32 -07:00
2015-12-14 08:43:03 -07:00
}
2013-07-06 14:23:32 -07:00
2015-12-14 08:43:03 -07:00
elem.html(html).trigger('create');
});
2013-07-06 14:23:32 -07:00
}
function getNotificationHtml(notification) {
2015-09-22 09:06:27 -07:00
var itemHtml = '';
2015-01-19 22:19:13 -07:00
if (notification.Url) {
2015-09-22 09:06:27 -07:00
itemHtml += '<a class="clearLink" href="' + notification.Url + '" target="_blank">';
}
2015-09-22 09:06:27 -07:00
itemHtml += '<paper-icon-item>';
2013-07-06 16:08:10 -07:00
2015-09-29 10:35:23 -07:00
if (notification.Level == "Error") {
2015-10-26 11:55:46 -07:00
itemHtml += '<paper-fab mini class="" style="background:#cc3333;" icon="error" item-icon></paper-fab>';
2015-09-29 10:35:23 -07:00
} else {
2015-10-27 08:53:50 -07:00
itemHtml += '<paper-fab mini class="blue" icon="dvr" item-icon></paper-fab>';
2015-09-29 10:35:23 -07:00
}
2013-07-06 14:23:32 -07:00
2015-09-22 09:06:27 -07:00
itemHtml += '<paper-item-body three-line>';
2015-09-22 09:06:27 -07:00
itemHtml += '<div>';
itemHtml += notification.Name;
itemHtml += '</div>';
2013-07-06 14:23:32 -07:00
2015-09-22 09:06:27 -07:00
itemHtml += '<div secondary>';
itemHtml += humane_date(notification.Date);
itemHtml += '</div>';
2013-07-06 14:23:32 -07:00
2015-09-22 09:06:27 -07:00
if (notification.Description) {
itemHtml += '<div secondary>';
itemHtml += notification.Description;
itemHtml += '</div>';
2013-07-06 14:23:32 -07:00
}
2015-09-22 09:06:27 -07:00
itemHtml += '</paper-item-body>';
2013-07-06 14:23:32 -07:00
2015-09-22 09:06:27 -07:00
itemHtml += '</paper-icon-item>';
2015-09-22 09:06:27 -07:00
if (notification.Url) {
itemHtml += '</a>';
}
2013-07-06 14:23:32 -07:00
2015-09-22 09:06:27 -07:00
return itemHtml;
2013-07-06 14:23:32 -07:00
}
window.Notifications = new notifications();
2015-09-21 18:05:33 -07:00
var needsRefresh = true;
2013-07-06 14:23:32 -07:00
2015-05-19 12:15:40 -07:00
function onWebSocketMessage(e, msg) {
if (msg.MessageType === "NotificationUpdated" || msg.MessageType === "NotificationAdded" || msg.MessageType === "NotificationsMarkedRead") {
2013-07-06 14:23:32 -07:00
2015-05-19 12:15:40 -07:00
Notifications.getNotificationsSummaryPromise = null;
2013-07-06 14:23:32 -07:00
2015-05-19 12:15:40 -07:00
Notifications.updateNotificationCount();
}
}
2013-07-06 14:23:32 -07:00
2015-05-19 12:15:40 -07:00
function initializeApiClient(apiClient) {
2015-12-23 10:46:01 -07:00
Events.off(apiClient, "websocketmessage", onWebSocketMessage);
Events.on(apiClient, "websocketmessage", onWebSocketMessage);
2014-10-25 11:32:58 -07:00
}
2015-12-14 08:43:03 -07:00
if (window.ApiClient) {
initializeApiClient(window.ApiClient);
}
2013-07-06 14:23:32 -07:00
2015-12-23 10:46:01 -07:00
Events.on(ConnectionManager, 'apiclientcreated', function (e, apiClient) {
2015-12-14 08:43:03 -07:00
initializeApiClient(apiClient);
});
2015-09-21 18:05:33 -07:00
2015-12-14 08:43:03 -07:00
Events.on(ConnectionManager, 'localusersignedin', function () {
needsRefresh = true;
});
2015-09-21 18:05:33 -07:00
2015-12-14 08:43:03 -07:00
Events.on(ConnectionManager, 'localusersignedout', function () {
needsRefresh = true;
2015-09-21 18:05:33 -07:00
});
2015-09-24 10:08:10 -07:00
pageClassOn('pageshow', "type-interior", function () {
2015-09-21 18:05:33 -07:00
if (needsRefresh) {
Notifications.updateNotificationCount();
}
2014-10-25 11:32:58 -07:00
});
2013-07-06 14:23:32 -07:00
2016-01-19 20:02:14 -07:00
})(jQuery, document, Dashboard);