90 lines
2.6 KiB
JavaScript
90 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
/* global async */
|
|
/* global angular */
|
|
|
|
angular.module('Application').controller('NotificationsController', ['$scope', '$timeout', 'Client', function ($scope, $timeout, Client) {
|
|
$scope.clearAllBusy = false;
|
|
|
|
$scope.notifications = [];
|
|
$scope.activeNotification = null;
|
|
$scope.busy = true;
|
|
$scope.hasUnread = false;
|
|
$scope.currentPage = 1;
|
|
$scope.perPage = 20;
|
|
|
|
$scope.refresh = function () {
|
|
Client.getNotifications({}, $scope.currentPage, $scope.perPage, function (error, result) {
|
|
if (error) return console.error(error);
|
|
|
|
// collapse by default
|
|
result.forEach(function (r) { r.isCollapsed = true; });
|
|
|
|
// attempt to parse the message as json
|
|
result.forEach(function (r) {
|
|
try {
|
|
r.messageJson = JSON.parse(r.message);
|
|
} catch (e) {}
|
|
});
|
|
|
|
$scope.notifications = result;
|
|
$scope.hasUnread = !!result.find(function (n) { return !n.acknowledged; });
|
|
|
|
$scope.busy = false;
|
|
});
|
|
};
|
|
|
|
$scope.showNextPage = function () {
|
|
$scope.currentPage++;
|
|
$scope.refresh();
|
|
};
|
|
|
|
$scope.showPrevPage = function () {
|
|
if ($scope.currentPage > 1) $scope.currentPage--;
|
|
else $scope.currentPage = 1;
|
|
|
|
$scope.refresh();
|
|
};
|
|
|
|
$scope.ack = function (notification, acked, event) {
|
|
if (event) event.stopPropagation();
|
|
|
|
if (notification.acknowledged === acked) return;
|
|
|
|
Client.ackNotification(notification.id, acked, function (error) {
|
|
if (error) console.error(error);
|
|
|
|
notification.acknowledged = acked;
|
|
$scope.$parent.notificationAcknowledged(acked);
|
|
});
|
|
};
|
|
|
|
$scope.clearAll = function () {
|
|
$scope.clearAllBusy = true;
|
|
|
|
async.eachLimit($scope.notifications, 20, function (notification, callback) {
|
|
if (notification.acknowledged) return callback();
|
|
|
|
Client.ackNotification(notification.id, true, function (error) {
|
|
if (error) {
|
|
console.error(error);
|
|
} else {
|
|
notification.acknowledged = true;
|
|
$scope.$parent.notificationAcknowledged(true);
|
|
}
|
|
|
|
callback();
|
|
});
|
|
}, function (error) {
|
|
if (error) console.error(error);
|
|
|
|
$scope.hasUnread = false;
|
|
$scope.clearAllBusy = false;
|
|
});
|
|
};
|
|
|
|
Client.onReady(function () {
|
|
$scope.refresh();
|
|
});
|
|
}]);
|