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
|
|
|
|
2021-06-23 23:17:42 -07:00
|
|
|
angular.module('Application').controller('NotificationsController', ['$scope', '$timeout', '$translate', '$interval', 'Client', function ($scope, $timeout, $translate, $interval, Client) {
|
2019-01-09 17:36:33 +01:00
|
|
|
$scope.clearAllBusy = false;
|
|
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.notifications = [];
|
|
|
|
|
$scope.activeNotification = null;
|
|
|
|
|
$scope.busy = true;
|
2021-06-21 14:04:25 +02:00
|
|
|
$scope.hasUnread = false;
|
2021-05-29 16:18:03 -07:00
|
|
|
$scope.currentPage = 1;
|
|
|
|
|
$scope.perPage = 20;
|
2021-05-29 16:06:41 -07:00
|
|
|
|
|
|
|
|
$scope.refresh = function () {
|
2021-05-29 16:18:03 -07:00
|
|
|
Client.getNotifications({}, $scope.currentPage, $scope.perPage, function (error, result) {
|
2021-05-29 16:06:41 -07:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
// collapse by default
|
|
|
|
|
result.forEach(function (r) { r.isCollapsed = true; });
|
|
|
|
|
|
2021-06-23 21:59:30 -07:00
|
|
|
// attempt to translate or parse the message as json
|
2021-05-29 16:06:41 -07:00
|
|
|
result.forEach(function (r) {
|
2021-06-23 23:17:42 -07:00
|
|
|
try {
|
|
|
|
|
r.messageJson = JSON.parse(r.message);
|
|
|
|
|
} catch (e) {}
|
2019-01-09 15:18:10 +01:00
|
|
|
});
|
|
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.notifications = result;
|
2021-06-21 14:04:25 +02:00
|
|
|
$scope.hasUnread = !!result.find(function (n) { return !n.acknowledged; });
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.busy = false;
|
|
|
|
|
});
|
|
|
|
|
};
|
2020-10-08 17:50:28 -07:00
|
|
|
|
2021-05-29 16:18:03 -07:00
|
|
|
$scope.showNextPage = function () {
|
|
|
|
|
$scope.currentPage++;
|
|
|
|
|
$scope.refresh();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.showPrevPage = function () {
|
|
|
|
|
if ($scope.currentPage > 1) $scope.currentPage--;
|
|
|
|
|
else $scope.currentPage = 1;
|
|
|
|
|
|
|
|
|
|
$scope.refresh();
|
|
|
|
|
};
|
|
|
|
|
|
2021-06-30 17:24:41 +02:00
|
|
|
$scope.ack = function (notification) {
|
|
|
|
|
if (notification.acknowledged) return;
|
2020-10-08 17:50:28 -07:00
|
|
|
|
2021-06-30 17:24:41 +02:00
|
|
|
Client.ackNotification(notification.id, true, function (error) {
|
2021-05-29 16:06:41 -07:00
|
|
|
if (error) console.error(error);
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2021-06-30 17:24:41 +02:00
|
|
|
notification.acknowledged = true;
|
|
|
|
|
$scope.$parent.notificationAcknowledged();
|
2021-05-29 16:06:41 -07:00
|
|
|
});
|
|
|
|
|
};
|
2021-05-29 12:11:17 -07:00
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.clearAll = function () {
|
|
|
|
|
$scope.clearAllBusy = true;
|
2021-05-29 12:11:17 -07:00
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
async.eachLimit($scope.notifications, 20, function (notification, callback) {
|
|
|
|
|
if (notification.acknowledged) return callback();
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2021-05-29 16:06:41 -07:00
|
|
|
Client.ackNotification(notification.id, true, function (error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
} else {
|
|
|
|
|
notification.acknowledged = true;
|
2021-06-30 17:24:41 +02:00
|
|
|
$scope.$parent.notificationAcknowledged();
|
2021-05-29 16:06:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback();
|
2019-01-09 15:18:10 +01:00
|
|
|
});
|
2021-05-29 16:06:41 -07:00
|
|
|
}, function (error) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
|
2021-06-21 14:04:25 +02:00
|
|
|
$scope.hasUnread = false;
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.clearAllBusy = false;
|
|
|
|
|
});
|
2019-01-09 15:18:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Client.onReady(function () {
|
2021-06-23 23:17:42 -07:00
|
|
|
var refreshTimer = $interval($scope.refresh, 60 * 1000); // keep this interval in sync with the notification count indicator in main.js
|
|
|
|
|
$scope.$on('$destroy', function () {
|
|
|
|
|
$interval.cancel(refreshTimer);
|
|
|
|
|
});
|
2021-05-29 16:06:41 -07:00
|
|
|
$scope.refresh();
|
2019-01-09 15:18:10 +01:00
|
|
|
});
|
|
|
|
|
}]);
|