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

155 lines
4.3 KiB
JavaScript

var app = angular.module('vocabulariesApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/list', { templateUrl: 'vocs/list.html', controller: 'vocListController' })
.when('/edit', { templateUrl: 'vocs/editor.html', controller: 'vocEditorController' })
.otherwise({ redirectTo: '/list' });
}
]);
app.controller('vocListController', function($scope, $http, $location) {
$scope.vocabularies = [];
$scope.tmpVoc = {};
$scope.mode = '';
$http.get('./ajax/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('./ajax/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('./ajax/vocs/' + encodeURIComponent(id) + '?' + $.now()).then(function successCallback(res) {
$scope.vocabularies = res.data;
alert("Vocabulary deleted");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}
};
});
app.controller('vocEditorController', function($scope, $http, $location, $routeParams) {
$scope.terms = [];
$scope.vocId = $routeParams.id;
$scope.vocInfo = {};
$scope.editTermCode = '';
$scope.tmpTerm = {};
$scope.mode = '';
$scope.currTerm = [];
$scope.baseUrl = './ajax/vocs/' + encodeURIComponent($scope.vocId);
$http.get($scope.baseUrl + '?' + $.now()).then(function successCallback(res) {
$scope.vocInfo = res.data;
$http.get($scope.baseUrl + '/terms?' + $.now()).then(function successCallback(res) {
$scope.terms = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
$scope.setCurrTerm = function(term) {
$scope.currTerm = angular.copy(term);
}
$scope.prepareNewTerm = function() {
$scope.mode = 'new';
$scope.editTermCode = '';
$scope.tmpTerm = {
'code' : '',
'name' : '',
'encoding' : 'OPENAIRE',
'synonyms' : []
};
}
$scope.prepareEditTerm = function(term) {
$scope.mode = 'edit';
$scope.editTermCode = term.code;
$scope.tmpTerm = angular.copy(term);
}
$scope.saveTerm = function(term) {
var url = $scope.baseUrl + '/terms?' + $.now();
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post(url, term).then(function successCallback(res) {
if ($scope.editTermCode != '' && $scope.editTermCode != $scope.tmpTerm.code) {
var deleteUrl = $scope.baseUrl + '/terms/' + encodeURIComponent($scope.editTermCode) + '?' + $.now();
$http.delete(deleteUrl).then(function successCallback(res) {
$scope.terms = res.data;
alert("Term replaced");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
} else {
$scope.terms = res.data;
alert("Term saved");
}
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.deleteTerm = function(code) {
if (confirm("Are you sure ?")) {
var url = $scope.baseUrl + '/terms/' + encodeURIComponent(code) + '?' + $.now();
$http.delete(url).then(function successCallback(res) {
$scope.terms = res.data;
alert("Term deleted");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}
};
});