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

99 lines
2.6 KiB
JavaScript

var app = angular.module('resourcesApp', []);
app.controller('resourcesController', function($scope, $http) {
$scope.resources = [];
$scope.tmpRes = {};
$scope.tmpContent = "loading...";
$scope.mode = '';
$scope.type = typeId();
$scope.reload = function() {
$http.get('./api/resources/?type=' + $scope.type + '&' + $.now()).then(function successCallback(res) {
$scope.resources = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.prepareNewRes = function() {
$scope.mode = 'new';
$scope.tmpRes = {
'id' : '',
'name' : '',
'description' : ''
};
}
$scope.prepareEditMetadata = function(res) {
$scope.tmpRes = angular.copy(res);
$scope.mode = 'edit';
}
$scope.prepareEditContent = function(res) {
$scope.tmpRes = angular.copy(res);
$scope.tmpContent = "loading...";
$http.get('./api/resources/' + res.id + '/content?' + $.now()).then(function successCallback(res) {
$scope.tmpContent = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}
$scope.prepareUploadContent = function(res) {
$scope.tmpRes = angular.copy(res);
}
$scope.saveMetadata = function(md) {
if ($scope.mode == 'new') {
var found = false;
angular.forEach($scope.resources, function(r) {
if (md.id == r.id) { found = true; };
});
if (found) {
alert("Insertion failed: resource already exists !");
return;
}
}
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/resources/?' + $.now(), md).then(function successCallback(res) {
alert("Resource saved");
$scope.reload();
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.saveContent = function(id, content) {
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
$http.post('./api/resources/' + id + '/content?' + $.now(), $.param({
'content' : content
})).then(function successCallback(res) {
alert("Resource saved");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.uploadContent = function(id, content) {
// TODO
};
$scope.deleteResource = function(r) {
if (confirm("Are you sure ?")) {
$http.delete('./api/resources/' + encodeURIComponent(r.id) + '?' + $.now()).then(function successCallback(res) {
alert("Resource deleted");
$scope.reload();
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}
};
$scope.reload();
});