67 lines
1.7 KiB
JavaScript
67 lines
1.7 KiB
JavaScript
var app = angular.module('contextsApp', []);
|
|
|
|
app.controller('contextsController', function($scope, $http) {
|
|
$scope.contexts = [];
|
|
$scope.tmpCtx = {};
|
|
$scope.mode = '';
|
|
|
|
$scope.reload = function() {
|
|
$http.get('./api/contexts/?' + $.now()).then(function successCallback(res) {
|
|
$scope.contexts = res.data;
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
};
|
|
|
|
$scope.prepareNewCtx = function() {
|
|
$scope.mode = 'new';
|
|
$scope.tmpCtx = {
|
|
'id' : '',
|
|
'label' : '',
|
|
'type' : '',
|
|
'parameters' : []
|
|
};
|
|
}
|
|
|
|
$scope.prepareEditCtx = function(ctx) {
|
|
$scope.mode = 'edit';
|
|
$scope.tmpCtx = angular.copy(ctx);
|
|
}
|
|
|
|
$scope.saveContext = function(ctx) {
|
|
if ($scope.mode == 'new') {
|
|
var found = false;
|
|
|
|
angular.forEach($scope.contexts, function(v) {
|
|
if (ctx.id == ctx.id) { found = true; };
|
|
});
|
|
|
|
if (found) {
|
|
alert("Insertion failed: context already exists !");
|
|
return;
|
|
}
|
|
}
|
|
|
|
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
|
|
$http.post('./api/contexts/?' + $.now(), ctx).then(function successCallback(res) {
|
|
$scope.contexts = res.data;
|
|
alert("Context saved");
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
};
|
|
|
|
$scope.deleteContext = function(id) {
|
|
if (confirm("Are you sure ?")) {
|
|
$http.delete('./api/contexts/' + encodeURIComponent(id) + '?' + $.now()).then(function successCallback(res) {
|
|
$scope.contexts = res.data;
|
|
alert("Context deleted");
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
}
|
|
};
|
|
|
|
$scope.reload();
|
|
});
|