Edit document, add files, display files

This commit is contained in:
jendib
2013-07-28 01:07:04 +02:00
parent 9b74bd8194
commit 3a2ffec497
25 changed files with 1811 additions and 421 deletions
@@ -3,7 +3,7 @@
/**
* Document edition controller.
*/
App.controller('DocumentEdit', function($scope, $state, $stateParams, Restangular) {
App.controller('DocumentEdit', function($scope, $http, $state, $stateParams, Restangular) {
/**
* Returns true if in edit mode (false in add mode).
*/
@@ -11,27 +11,65 @@ App.controller('DocumentEdit', function($scope, $state, $stateParams, Restangula
return $stateParams.id;
};
/**
* In edit mode, load the current document.
*/
if ($scope.isEdit()) {
Restangular.one('document', $stateParams.id).get().then(function(data) {
$scope.document = data;
});
}
/**
* Edit a document.
*/
$scope.edit = function() {
var promise = null;
if ($scope.isEdit()) {
// TODO
promise = Restangular
.one('document', $stateParams.id)
.post('', $scope.document);
promise.then(function(data) {
$scope.loadDocuments();
$state.transitionTo('document.view', { id: $stateParams.id });
})
} else {
Restangular
promise = Restangular
.one('document')
.put($scope.document)
.then(function() {
.put($scope.document);
promise.then(function(data) {
$scope.document = {};
$scope.loadDocuments();
});
}
// Upload files after edition
// TODO Handle file upload progression and errors
promise.then(function(data) {
_.each($scope.files, function(file) {
var formData = new FormData();
formData.append('id', data.id);
formData.append('file', file);
$.ajax({
url: 'api/file',
type: 'PUT',
data: formData,
processData: false,
contentType: false
});
});
});
};
/**
* Cancel edition.
*/
$scope.cancel = function() {
$state.transitionTo('document');
if ($scope.isEdit()) {
$state.transitionTo('document.view', { id: $stateParams.id });
} else {
$state.transitionTo('document.default');
}
};
});