94 lines
2.9 KiB
JavaScript
94 lines
2.9 KiB
JavaScript
var app = angular.module('resourcesApp', []);
|
|
|
|
app.controller('resourcesController', function($scope, $http) {
|
|
$scope.resources = [];
|
|
$scope.tmpRes = {};
|
|
$scope.tmpContent = "loading...";
|
|
$scope.type = typeId();
|
|
|
|
$scope.reload = function() {
|
|
$http.get('./ajax/resources/' + encodeURIComponent($scope.type) + '?' + $.now()).then(function successCallback(res) {
|
|
$scope.resources = res.data;
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
};
|
|
|
|
$scope.prepareNewResource = function() {
|
|
$scope.tmpRes = {
|
|
'name' : '',
|
|
'description' : '',
|
|
'content' : ''
|
|
};
|
|
}
|
|
|
|
$scope.prepareEditMetadata = function(r) {
|
|
$scope.tmpRes = angular.copy(r);
|
|
}
|
|
|
|
$scope.prepareEditContent = function(r) {
|
|
$scope.tmpRes = angular.copy(r);
|
|
$scope.tmpContent = "loading...";
|
|
$http.get('./ajax/resources/' + encodeURIComponent(r.id) + '/content?' + $.now()).then(function successCallback(res) {
|
|
if (res.data instanceof Object) {
|
|
$scope.tmpContent = JSON.stringify(res.data, null, "\t");
|
|
} else {
|
|
$scope.tmpContent = res.data;
|
|
}
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
}
|
|
|
|
$scope.createNewResource = function(r) {
|
|
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8";
|
|
$http.post('./ajax/resources/?' + $.now(), $.param({
|
|
'name' : r.name,
|
|
'type' : $scope.type,
|
|
'description' : r.description,
|
|
'content' : r.content
|
|
})).then(function successCallback(res) {
|
|
alert("Resource saved");
|
|
$('#newResourceModal').modal('hide');
|
|
$scope.reload();
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
}
|
|
|
|
$scope.saveMetadata = function(id, md) {
|
|
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
|
|
$http.post('./ajax/resources/' + encodeURIComponent(id) + '/metadata?' + $.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('./ajax/resources/' + encodeURIComponent(id) + '/content?' + $.now(), $.param({
|
|
'content' : content
|
|
})).then(function successCallback(res) {
|
|
alert("Resource saved");
|
|
$('#editContentModal').modal('hide');
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
};
|
|
|
|
$scope.deleteResource = function(r) {
|
|
if (confirm("Are you sure ?")) {
|
|
$http.delete('./ajax/resources/' + encodeURIComponent(r.id) + '?' + $.now()).then(function successCallback(res) {
|
|
alert("Resource deleted");
|
|
$scope.reload();
|
|
}, function errorCallback(res) {
|
|
alert('ERROR: ' + res.data.message);
|
|
});
|
|
}
|
|
};
|
|
|
|
$scope.reload();
|
|
});
|