dnet-applications/apps/dnet-is-application/src/main/resources/static/js/vocabularies.js

66 lines
1.7 KiB
JavaScript

var app = angular.module('vocabulariesApp', []);
app.controller('vocabulariesController', function($scope, $http) {
$scope.vocabularies = [];
$scope.tmpVoc = {};
$scope.mode = '';
$scope.reload = function() {
$http.get('./api/vocs/?' + $.now()).then(function successCallback(res) {
$scope.vocabularies = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.prepareNewVoc = function() {
$scope.mode = 'new';
$scope.tmpVoc = {
'id' : '',
'name' : '',
'description' : ''
};
}
$scope.prepareEditVoc = function(voc) {
$scope.mode = 'edit';
$scope.tmpVoc = angular.copy(voc);
}
$scope.saveVocabulary = function(voc) {
if ($scope.mode == 'new') {
var found = false;
angular.forEach($scope.vocabularies, function(v) {
if (voc.id == v.id) { found = true; };
});
if (found) {
alert("Insertion failed: vocabulary already exists !");
return;
}
}
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/vocs/?' + $.now(), voc).then(function successCallback(res) {
$scope.vocabularies = res.data;
alert("Vocabulary saved");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.deleteVocabulary = function(id) {
if (confirm("Are you sure ?")) {
$http.delete('./api/vocs/' + encodeURIComponent(id)).then(function successCallback(res) {
$scope.vocabularies = res.data;
alert("Vocabulary deleted");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}
};
$scope.reload();
});