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

222 lines
6.9 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
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-05-30 12:23:56 -07:00
self.getNotificationsSummary().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
2014-07-17 15:21:35 -07:00
$('.notificationsFlyout').panel({}).panel('option', 'classes.modalOpen', 'notificationsPanelModelOpen ui-panel-dismiss-open').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;
2014-10-04 11:05:24 -07:00
var limit = 4;
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-04 11:05:24 -07:00
return ApiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).done(function (result) {
2013-07-06 14:23:32 -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">';
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-07-17 15:21:35 -07:00
html += '<p class="notificationTime" 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-05-30 12:23:56 -07:00
html += '<p style="margin: .25em 0;"><a href="' + notification.Url + '" target="_blank">' + Globalize.translate('ButtonMoreInformation') + '</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();
2014-06-21 22:52:31 -07:00
$(document).on('headercreated', function (e) {
2013-07-06 14:23:32 -07:00
2014-06-23 09:05:19 -07:00
$('<a class="headerButton headerButtonRight btnNotifications" href="#" title="Notifications"><div class="btnNotificationsInner">0</div></a>').insertAfter($('.headerUserButton')).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);