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-26 21:29:35 -07:00
|
|
|
|
self.getNotificationsSummary = function() {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
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-26 21:29:35 -07:00
|
|
|
|
self.updateNotificationCount = function() {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
self.getNotificationsSummary().done(function(summary) {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
var item = $('.btnNotifications').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-26 21:29:35 -07:00
|
|
|
|
self.showNotificationsFlyout = function() {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
var context = this;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
var html = '<div data-role="popup" class="notificationsFlyout" style="min-width:250px;margin-top:30px;margin-right:20px;" data-theme="a">';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
html += '<a href="#" data-rel="back" data-role="button" data-theme="b" data-icon="delete" data-iconpos="notext" class="ui-btn-right">Close</a>';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
html += '<div class="ui-bar-a" style="text-align:center;">';
|
|
|
|
|
html += '<h3 style="margin: .5em 0;">Notifications</h3>';
|
|
|
|
|
html += '</div>';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
html += '<div data-role="content" style="padding: 0;">';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
html += '<p class="notificationsFlyoutlist">Loading...';
|
|
|
|
|
|
|
|
|
|
html += '</p>';
|
|
|
|
|
|
2014-05-27 14:17:48 -07:00
|
|
|
|
html += '<div style="display:none;" class="btnMarkReadContainer"><button class="btnMarkRead" type="button" data-icon="check" data-mini="true" data-theme="b">Mark these read</button></div>';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-27 14:17:48 -07:00
|
|
|
|
html += '<div class="btnNotificationListContainer"><button class="btnNotificationList" type="button" data-icon="action" data-mini="true">View Notifications</button></div>';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
html += '</div>';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
html += '</div>';
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
$(document.body).append(html);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
$('.notificationsFlyout').popup({ positionTo: context }).trigger('create').popup("open").on("popupafterclose", function() {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
$(this).off("popupafterclose").remove();
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
}).on('click', '.btnMarkRead', function() {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
var ids = $('.unreadFlyoutNotification').map(function() {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
return this.getAttribute('data-notificationid');
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
}).get();
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
self.markNotificationsRead(ids, function() {
|
|
|
|
|
|
|
|
|
|
$('.notificationsFlyout').popup("close");
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}).on("click", ".btnNotificationList", function(e) {
|
|
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
Dashboard.navigate("notificationlist.html");
|
2014-03-30 09:49:40 -07:00
|
|
|
|
|
2013-07-06 14:23:32 -07:00
|
|
|
|
});
|
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
self.isFlyout = true;
|
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
var startIndex = 0;
|
2014-05-27 07:30:21 -07:00
|
|
|
|
var limit = 4;
|
2014-05-26 21:29:35 -07:00
|
|
|
|
var elem = $('.notificationsFlyoutlist');
|
|
|
|
|
var markReadButton = $('.btnMarkReadContainer');
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
refreshNotifications(startIndex, limit, elem, markReadButton, false);
|
2014-05-26 21:29:35 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.markNotificationsRead = function(ids, callback) {
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
self.showNotificationsList = function (startIndex, limit, elem, btn) {
|
2014-05-26 21:29:35 -07:00
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
refreshNotifications(startIndex, limit, elem, btn, true);
|
2014-05-26 21:29:35 -07:00
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
function refreshNotifications(startIndex, limit, elem, btn, showPaging) {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
|
|
|
|
ApiClient.getNotifications(Dashboard.getCurrentUserId(), { StartIndex: startIndex, Limit: limit }).done(function (result) {
|
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
listUnreadNotifications(result.Notifications, result.TotalRecordCount, startIndex, limit, elem, btn, showPaging);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 17:23:00 -07:00
|
|
|
|
function listUnreadNotifications(list, totalRecordCount, startIndex, limit, elem, btn, showPaging) {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
|
|
|
|
if (!totalRecordCount) {
|
2013-07-06 19:01:14 -07:00
|
|
|
|
elem.html('<p style="padding:.5em 1em;">No unread notifications.</p>');
|
2014-05-26 21:29:35 -07:00
|
|
|
|
btn.hide();
|
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;
|
|
|
|
|
|
|
|
|
|
if (list.filter(function (n) {
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
|
|
|
|
return !n.IsRead;
|
2013-07-17 08:19:00 -07:00
|
|
|
|
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}).length) {
|
2014-05-26 21:29:35 -07:00
|
|
|
|
btn.show();
|
2013-07-06 14:23:32 -07:00
|
|
|
|
} else {
|
2014-05-26 21:29:35 -07:00
|
|
|
|
btn.hide();
|
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
|
|
|
|
|
2014-05-26 21:29:35 -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);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
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-02-28 21:12:56 -07:00
|
|
|
|
html += '<p 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
|
|
|
|
}
|
|
|
|
|
|
2013-07-17 08:19:00 -07:00
|
|
|
|
if (notification.Url) {
|
2014-02-28 21:12:56 -07:00
|
|
|
|
html += '<p style="margin: .25em 0;"><a href="' + notification.Url + '" target="_blank">More information</a></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();
|
|
|
|
|
|
2013-07-06 14:23:32 -07:00
|
|
|
|
$(Dashboard).on('interiorheaderrendered', function (e, header, user) {
|
|
|
|
|
|
|
|
|
|
if (!user || $('.notificationsButton', header).length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
$('<a class="imageLink btnNotifications" href="#" title="Notifications">0</a>').insertAfter($('.btnCurrentUser', header)).on('click', Notifications.showNotificationsFlyout);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -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") {
|
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
Notifications.getNotificationsSummaryPromise = null;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
Notifications.updateNotificationCount();
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2014-05-26 21:29:35 -07:00
|
|
|
|
})(jQuery, document, Dashboard, LibraryBrowser);
|