2019-01-09 15:18:10 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2020-06-03 23:17:06 +02:00
|
|
|
/* global async */
|
|
|
|
|
/* global angular */
|
|
|
|
|
/* global $ */
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2020-03-05 21:08:27 -08:00
|
|
|
angular.module('Application').controller('NotificationsController', ['$scope', '$timeout', 'Client', function ($scope, $timeout, Client) {
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2019-01-09 17:36:33 +01:00
|
|
|
$scope.clearAllBusy = false;
|
|
|
|
|
|
2020-03-05 21:08:27 -08:00
|
|
|
$scope.reboot = {
|
|
|
|
|
busy: false,
|
|
|
|
|
|
|
|
|
|
show: function () {
|
|
|
|
|
$scope.reboot.busy = false;
|
|
|
|
|
$('#rebootModal').modal('show');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
submit: function () {
|
|
|
|
|
$scope.reboot.busy = true;
|
|
|
|
|
|
|
|
|
|
Client.reboot(function (error) {
|
|
|
|
|
if (error) return Client.error(error);
|
|
|
|
|
|
|
|
|
|
$('#rebootModal').modal('hide');
|
|
|
|
|
|
2020-03-06 02:38:17 -08:00
|
|
|
// trigger refetch to show offline banner
|
|
|
|
|
$timeout(function () { Client.getStatus(function () {}); }, 5000);
|
2020-03-05 21:08:27 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-09 15:18:10 +01:00
|
|
|
$scope.notifications = {
|
|
|
|
|
notifications: [],
|
|
|
|
|
activeNotification: null,
|
|
|
|
|
busy: true,
|
|
|
|
|
|
|
|
|
|
refresh: function () {
|
2020-03-06 02:38:17 -08:00
|
|
|
Client.getNotifications(false, 1, 100, function (error, result) {
|
2019-01-09 15:18:10 +01:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
2019-01-23 16:44:57 +01:00
|
|
|
// collapse by default
|
|
|
|
|
result.forEach(function (r) { r.isCollapsed = true; });
|
2019-01-09 17:16:41 +01:00
|
|
|
|
2019-02-08 14:05:15 +01:00
|
|
|
// 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.notifications = result;
|
|
|
|
|
|
|
|
|
|
$scope.notifications.busy = false;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clicked: function (notification) {
|
|
|
|
|
if ($scope.notifications.activeNotification === notification) return $scope.notifications.activeNotification = null;
|
|
|
|
|
$scope.notifications.activeNotification = notification;
|
|
|
|
|
},
|
|
|
|
|
|
2020-10-08 17:50:28 -07:00
|
|
|
ackOne: function (id, callback) {
|
|
|
|
|
Client.ackNotification(id, function (error) {
|
2019-01-09 15:18:10 +01:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2020-10-08 17:50:28 -07:00
|
|
|
$scope.$parent.notificationAcknowledged(id);
|
2019-01-09 15:18:10 +01:00
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2020-10-08 17:50:28 -07:00
|
|
|
ack: function (notification, event) {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
|
|
$scope.notifications.ackOne(notification.id, function (error) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
|
|
|
|
|
$scope.notifications.refresh();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2019-01-09 15:18:10 +01:00
|
|
|
action: function (notification) {
|
|
|
|
|
if (notification.action) window.location = notification.action;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
clearAll: function () {
|
2019-01-09 17:36:33 +01:00
|
|
|
$scope.clearAllBusy = true;
|
2019-01-09 15:18:10 +01:00
|
|
|
|
2020-10-08 17:50:28 -07:00
|
|
|
async.eachLimit($scope.notifications.notifications, 20, function (notification, callback) {
|
2019-01-09 17:36:33 +01:00
|
|
|
if (notification.acknowledged) return callback();
|
2020-10-08 17:50:28 -07:00
|
|
|
$scope.notifications.ackOne(notification, callback);
|
2019-01-09 15:18:10 +01:00
|
|
|
}, function (error) {
|
|
|
|
|
if (error) console.error(error);
|
|
|
|
|
|
2020-10-08 17:50:28 -07:00
|
|
|
$scope.notifications.refresh();
|
2019-01-09 17:36:33 +01:00
|
|
|
$scope.clearAllBusy = false;
|
2019-01-09 15:18:10 +01:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-23 16:44:57 +01:00
|
|
|
$scope.notificationExpanding = function (notification) {
|
2019-02-06 16:33:57 +01:00
|
|
|
if (!notification.eventId) return;
|
|
|
|
|
|
2019-01-23 16:44:57 +01:00
|
|
|
notification.busyLoadEvent = true;
|
|
|
|
|
|
|
|
|
|
Client.getEvent(notification.eventId, function (error, result) {
|
|
|
|
|
notification.busyLoadEvent = false;
|
|
|
|
|
|
|
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
|
|
|
|
notification.event = result;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-01-09 15:18:10 +01:00
|
|
|
Client.onReady(function () {
|
|
|
|
|
$scope.notifications.refresh();
|
|
|
|
|
});
|
2020-03-06 02:38:17 -08:00
|
|
|
|
|
|
|
|
Client.onReconnect(function () {
|
|
|
|
|
$scope.notifications.refresh();
|
2020-03-17 22:09:34 -07:00
|
|
|
});
|
2019-01-09 15:18:10 +01:00
|
|
|
}]);
|