Files
cloudron-box/src/views/notifications.js
T

90 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-01-09 15:18:10 +01:00
'use strict';
2020-06-03 23:17:06 +02:00
/* global async */
/* global angular */
2019-01-09 15:18:10 +01:00
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) {}
2019-01-09 15:18:10 +01:00
});
$scope.notifications = result;
$scope.hasUnread = !!result.find(function (n) { return !n.acknowledged; });
2019-01-09 15:18:10 +01:00
$scope.busy = false;
});
};
2020-10-08 17:50:28 -07:00
$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();
2020-10-08 17:50:28 -07:00
if (notification.acknowledged === acked) return;
2020-10-08 17:50:28 -07:00
Client.ackNotification(notification.id, acked, function (error) {
if (error) console.error(error);
2019-01-09 15:18:10 +01:00
notification.acknowledged = acked;
$scope.$parent.notificationAcknowledged(acked);
});
};
2021-05-29 12:11:17 -07:00
$scope.clearAll = function () {
$scope.clearAllBusy = true;
2021-05-29 12:11:17 -07:00
async.eachLimit($scope.notifications, 20, function (notification, callback) {
if (notification.acknowledged) return callback();
2019-01-09 15:18:10 +01:00
Client.ackNotification(notification.id, true, function (error) {
if (error) {
console.error(error);
} else {
notification.acknowledged = true;
$scope.$parent.notificationAcknowledged(true);
}
callback();
2019-01-09 15:18:10 +01:00
});
}, function (error) {
if (error) console.error(error);
$scope.hasUnread = false;
$scope.clearAllBusy = false;
});
2019-01-09 15:18:10 +01:00
};
Client.onReady(function () {
$scope.refresh();
2019-01-09 15:18:10 +01:00
});
}]);