2014-05-26 21:29:35 -07:00
|
|
|
|
(function ($, document, Dashboard, LibraryBrowser) {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
function notifications() {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
var self = this;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
self.getNotificationsSummaryPromise = null;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -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
|
|
|
|
|
2014-05-26 21:29:35 -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
|
|
|
|
|
2014-05-26 21:29:35 -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) {
|
2014-05-26 21:29:35 -07:00
|
|
|
|
|
2014-05-30 12:23:56 -07:00
|
|
|
|
ApiClient.markNotificationsRead(Dashboard.getCurrentUserId(), ids, true).done(function () {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
self.getNotificationsSummaryPromise = null;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
self.updateNotificationCount();
|
|
|
|
|
|
2014-10-04 11:05:24 -07:00
|
|
|
|
if (callback) {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
2014-05-26 21:29:35 -07:00
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2015-05-31 14:07:44 -07:00
|
|
|
|
self.showNotificationsList = function (startIndex, limit, elem) {
|
2014-05-26 21:29:35 -07:00
|
|
|
|
|
2015-05-31 14:07:44 -07:00
|
|
|
|
refreshNotifications(startIndex, limit, elem, true);
|
2014-05-26 21:29:35 -07:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
return apiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).done(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;
|
|
|
|
|
}
|
2013-07-17 08:19:00 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
Notifications.total = totalRecordCount;
|
|
|
|
|
|
2013-07-06 14:23:32 -07:00
|
|
|
|
var html = '';
|
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
if (totalRecordCount > limit && showPaging === true) {
|
2014-05-26 21:29:35 -07:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
});
|
2014-05-26 21:29:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-17 08:19:00 -07:00
|
|
|
|
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">';
|
2013-07-17 08:19:00 -07:00
|
|
|
|
|
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-05-31 14:07:44 -07:00
|
|
|
|
html += '<p style="margin: .5em 0;max-height:150px;overflow:hidden;text-overflow:ellipsis;">' + notification.Description + '</p>';
|
2013-07-17 08:19:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
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-17 08:19:00 -07:00
|
|
|
|
|
2013-07-06 14:23:32 -07:00
|
|
|
|
function getImageHtml(notification) {
|
2013-07-17 08:19:00 -07:00
|
|
|
|
|
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-17 08:19:00 -07:00
|
|
|
|
|
2013-07-06 14:23:32 -07:00
|
|
|
|
return '<div class="imgNotification imgNotificationNormal"><div class="imgNotificationInner imgNotificationIcon"></div></div>';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
window.Notifications = new notifications();
|
|
|
|
|
|
2015-05-31 14:07:44 -07:00
|
|
|
|
$(document).on('libraryMenuCreated', function (e) {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2015-05-18 15:23:03 -07:00
|
|
|
|
if (window.ApiClient) {
|
2015-05-31 14:07:44 -07:00
|
|
|
|
Notifications.updateNotificationCount();
|
2014-10-25 11:32:58 -07:00
|
|
|
|
}
|
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) {
|
|
|
|
|
$(apiClient).off("websocketmessage", onWebSocketMessage).on("websocketmessage", onWebSocketMessage);
|
2014-10-25 11:32:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
Dashboard.ready(function () {
|
|
|
|
|
|
|
|
|
|
if (window.ApiClient) {
|
|
|
|
|
initializeApiClient(window.ApiClient);
|
|
|
|
|
}
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2015-05-19 12:15:40 -07:00
|
|
|
|
$(ConnectionManager).on('apiclientcreated', function (e, apiClient) {
|
|
|
|
|
initializeApiClient(apiClient);
|
|
|
|
|
});
|
2014-10-25 11:32:58 -07:00
|
|
|
|
});
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
})(jQuery, document, Dashboard, LibraryBrowser);
|