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

215 lines
6.0 KiB
JavaScript
Raw Normal View History

2016-08-05 12:34:10 -07:00
define(['libraryBrowser', 'listViewStyle'], function (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
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
2016-03-17 20:39:59 -07:00
var btnNotificationsInner = document.querySelector('.btnNotificationsInner');
if (btnNotificationsInner) {
2013-07-06 14:23:32 -07:00
2016-03-17 20:39:59 -07:00
btnNotificationsInner.classList.remove('levelNormal');
btnNotificationsInner.classList.remove('levelWarning');
btnNotificationsInner.classList.remove('levelError');
btnNotificationsInner.innerHTML = summary.UnreadCount;
if (summary.UnreadCount) {
btnNotificationsInner.classList.add('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
2016-07-18 20:57:55 -07:00
html += libraryBrowser.getQueryPagingHtml({
2015-06-18 11:29:44 -07:00
startIndex: query.StartIndex,
limit: query.Limit,
totalRecordCount: totalRecordCount,
showLimit: false,
updatePageSizeSetting: false
});
}
2016-08-05 12:34:10 -07:00
require(['humanedate'], function () {
2015-12-14 08:43:03 -07:00
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">';
}
2016-08-05 12:34:10 -07:00
itemHtml += '<div class="listItem">';
2013-07-06 16:08:10 -07:00
2015-09-29 10:35:23 -07:00
if (notification.Level == "Error") {
2016-08-05 12:34:10 -07:00
itemHtml += '<i class="listItemIcon md-icon" style="background:#cc3333;">error</i>';
2015-09-29 10:35:23 -07:00
} else {
2016-08-05 12:34:10 -07:00
itemHtml += '<i class="listItemIcon md-icon">dvr</i>';
2015-09-29 10:35:23 -07:00
}
2013-07-06 14:23:32 -07:00
2016-08-05 12:34:10 -07:00
itemHtml += '<div class="listItemBody three-line">';
2016-08-05 12:34:10 -07:00
itemHtml += '<h3 class="listItemBodyText">';
2015-09-22 09:06:27 -07:00
itemHtml += notification.Name;
2016-08-05 12:34:10 -07:00
itemHtml += '</h3>';
2013-07-06 14:23:32 -07:00
2016-08-05 12:34:10 -07:00
itemHtml += '<div class="listItemBodyText secondary">';
2015-09-22 09:06:27 -07:00
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) {
2016-08-05 12:34:10 -07:00
itemHtml += '<div class="listItemBodyText secondary listItemBodyText-nowrap">';
2015-09-22 09:06:27 -07:00
itemHtml += notification.Description;
itemHtml += '</div>';
2013-07-06 14:23:32 -07:00
}
2016-08-05 12:34:10 -07:00
itemHtml += '</div>';
2013-07-06 14:23:32 -07:00
2016-08-05 12:34:10 -07:00
itemHtml += '</div>';
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-03-17 20:39:59 -07:00
});