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

248 lines
7.5 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
2014-05-30 12:23:56 -07:00
self.getNotificationsSummary = function () {
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
var apiClient = ConnectionManager.currentApiClient();
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;
}
2014-10-25 11:32:58 -07:00
var promise = self.getNotificationsSummary();
if (!promise) {
return;
}
promise.done(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.showNotificationsFlyout = function () {
2013-07-06 14:23:32 -07:00
2014-07-17 15:21:35 -07:00
var html = '<div data-role="panel" data-position="right" data-display="overlay" class="notificationsFlyout" data-position-fixed="true" data-theme="a">';
2013-07-06 14:23:32 -07:00
2014-07-17 15:21:35 -07:00
html += '<h1 style="margin: .25em 0;">';
html += '<span style="vertical-align:middle;">' + Globalize.translate('HeaderNotifications') + '</span>';
html += '<a data-role="button" data-inline="true" data-icon="arrow-r" href="notificationlist.html" data-iconpos="notext" style="vertical-align:middle;margin-left:.5em;">' + Globalize.translate('ButtonViewNotifications') + '</a>';
html += '</h1>';
2013-07-06 14:23:32 -07:00
2014-07-17 15:21:35 -07:00
html += '<div>';
2013-07-06 14:23:32 -07:00
2014-07-17 15:21:35 -07:00
html += '<div class="notificationsFlyoutlist">Loading...';
2014-07-17 15:21:35 -07:00
html += '</div>';
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').panel({}).trigger('create').panel("open").on("panelclose", function () {
2013-07-06 14:23:32 -07:00
2014-07-17 15:21:35 -07:00
$(this).off("panelclose").remove();
2013-07-06 14:23:32 -07:00
});
self.isFlyout = true;
var startIndex = 0;
2015-01-19 22:19:13 -07:00
var limit = 5;
var elem = $('.notificationsFlyoutlist');
2013-07-06 14:23:32 -07:00
2014-10-04 11:05:24 -07:00
refreshNotifications(startIndex, limit, elem, null, false).done(function() {
self.markNotificationsRead([]);
});
};
2014-05-30 12:23:56 -07:00
self.markNotificationsRead = function (ids, callback) {
2014-05-30 12:23:56 -07:00
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();
2014-10-04 11:05:24 -07:00
if (callback) {
callback();
}
});
};
self.showNotificationsList = function (startIndex, limit, elem, btn) {
refreshNotifications(startIndex, limit, elem, btn, true);
};
}
function refreshNotifications(startIndex, limit, elem, btn, showPaging) {
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
var apiClient = ConnectionManager.currentApiClient();
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
if (apiClient) {
return apiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).done(function (result) {
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
listUnreadNotifications(result.Notifications, result.TotalRecordCount, startIndex, limit, elem, btn, showPaging);
});
}
2013-07-06 14:23:32 -07:00
}
function listUnreadNotifications(list, totalRecordCount, startIndex, limit, elem, btn, 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
if (btn) {
btn.hide();
}
2013-07-06 14:23:32 -07:00
return;
}
Notifications.total = totalRecordCount;
2014-10-04 11:05:24 -07:00
if (btn) {
if (list.filter(function (n) {
2013-07-06 14:23:32 -07:00
2014-10-04 11:05:24 -07:00
return !n.IsRead;
2014-10-04 11:05:24 -07:00
}).length) {
btn.show();
} else {
btn.hide();
}
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
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">';
2015-01-19 22:19:13 -07:00
html += '<p style="font-size:16px;margin: .5em 0 .5em;" class="notificationName">';
if (notification.Url) {
html += '<a href="' + notification.Url + '" target="_blank" style="text-decoration:none;">' + notification.Name + '</a>';
} else {
html += notification.Name;
}
html += '</p>';
2013-07-06 14:23:32 -07:00
2015-01-19 22:19:13 -07:00
html += '<p class="notificationTime" style="margin: .5em 0;">' + humane_date(notification.Date) + '</p>';
2013-07-06 14:23:32 -07:00
if (notification.Description) {
2015-01-19 22:19:13 -07:00
html += '<p style="margin: .5em 0;max-height:100px;overflow:hidden;text-overflow:ellipsis;">' + notification.Description + '</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();
2014-06-21 22:52:31 -07:00
$(document).on('headercreated', function (e) {
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
if (ConnectionManager.currentApiClient()) {
2014-12-30 23:24:49 -07:00
$('<a class="headerButton headerButtonRight btnNotifications" href="#" title="Notifications"><div class="btnNotificationsInner">0</div></a>').insertAfter($('.headerSearchButton')).on('click', Notifications.showNotificationsFlyout);
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
Notifications.updateNotificationCount();
}
2013-07-06 14:23:32 -07:00
});
2014-10-25 11:32:58 -07:00
function initializeApiClient(apiClient) {
$(apiClient).on("websocketmessage", function (e, msg) {
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
if (msg.MessageType === "NotificationUpdated" || msg.MessageType === "NotificationAdded" || msg.MessageType === "NotificationsMarkedRead") {
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
Notifications.getNotificationsSummaryPromise = null;
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
Notifications.updateNotificationCount();
}
2013-07-06 14:23:32 -07:00
2014-10-25 11:32:58 -07:00
});
}
2014-10-30 21:57:24 -07:00
$(ConnectionManager).on('apiclientcreated', function (e, apiClient) {
2013-07-06 14:23:32 -07:00
2014-10-30 21:57:24 -07:00
initializeApiClient(apiClient);
2014-10-25 11:32:58 -07:00
});
2013-07-06 14:23:32 -07:00
})(jQuery, document, Dashboard, LibraryBrowser);