EnVisageOnline/Main/Source/EnVisage/Scripts/Angular/Controllers/notificationController.js

85 lines
2.4 KiB
JavaScript

'use strict';
app.controller('notificationCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.ids = [];
$scope.dataUrls = {
updateNotificationUrl: '/Notification/Edit',
LoadActiveNotificationUrl: '/Notification/ActiveNotifications',
};
$scope.$on('notification-received-data-event', function (env, data) {
var found = false;
for (var i = 0; i < $scope.ids.length; i++) {
var task = $scope.ids[i];
if (task.Id == data.Id) {
$scope.ids[i] = data;
found = true;
break;
}
}
if (!found)
$scope.ids.push(data);
});
$.ajax({
type: "get",
url: siteRoot + $scope.dataUrls.LoadActiveNotificationUrl,
async: true,
error: function (response) {
ShowPopover(buttonId, serverErrorText);
},
success: function (data) {
$scope.ids = data;
}
});
$scope.dismissNotification = function(id){
for (var i = 0; i < $scope.ids.length; i++) {
var task = $scope.ids[i];
if (task.Id == id) {
UpdateNotification(task);
// $scope.ids.splice(i, 1);
break;
}
}
}
function UpdateNotification(data) {
$.ajax({
type: "get",
url: siteRoot +$scope.dataUrls.updateNotificationUrl,
data:data,
async: false,
error: function (response) {
ShowPopover("", serverErrorText);
},
success: function (data) {
$scope.ids = data;
}
});
}
$scope.init = function () {
var signalR = $.connection.clientnotificationsHub;
signalR.client.TaskStarted = function (taskID, task) {
$scope.$broadcast('notification-received-data-event', task);
$scope.$apply();
};
signalR.client.TaskFinished = function (taskID, task) {
$scope.$broadcast('notification-received-data-event', task);
$scope.$apply();
};
signalR.client.UserMembershipSet = function (message) {
};
$.connection.hub.logging = false;
$.connection.hub.start().done(function (data) {
var transportName = $.connection.hub.transport.name;
console.debug("SignalR protocol: " + transportName);
signalR.server.registerWithHub();
}).fail(function (data) {
console.log(data);
});
}
}]);