PDF handling, file upload progression

This commit is contained in:
jendib
2013-07-28 18:29:03 +02:00
parent 19000d095f
commit 471933ca8c
14 changed files with 186 additions and 105 deletions
@@ -3,7 +3,7 @@
/**
* Document edition controller.
*/
App.controller('DocumentEdit', function($scope, $http, $state, $stateParams, Restangular) {
App.controller('DocumentEdit', function($scope, $q, $http, $state, $stateParams, Restangular) {
/**
* Returns true if in edit mode (false in add mode).
*/
@@ -28,37 +28,55 @@ App.controller('DocumentEdit', function($scope, $http, $state, $stateParams, Res
if ($scope.isEdit()) {
promise = Restangular
.one('document', $stateParams.id)
.post('', $scope.document);
promise.then(function(data) {
$scope.loadDocuments();
$state.transitionTo('document.view', { id: $stateParams.id });
})
.one('document', $stateParams.id)
.post('', $scope.document);
} else {
promise = Restangular
.one('document')
.put($scope.document);
promise.then(function(data) {
$scope.document = {};
$scope.loadDocuments();
});
.one('document')
.put($scope.document);
}
// Upload files after edition
// TODO Handle file upload progression and errors
promise.then(function(data) {
_.each($scope.files, function(file) {
var promises = [];
$scope.fileProgress = 0;
_.each($scope.newFiles, function(file) {
// Build the payload
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
// Send the file
var promiseFile = $http.put('api/file',
formData, {
headers: { 'Content-Type': false },
transformRequest: function(data) { return data; }
});
// TODO Handle progression when $q.notify will be released
promiseFile.then(function() {
$scope.fileProgress += 100 / _.size($scope.newFiles);
});
// Store the promise for later
promises.push(promiseFile);
});
// When all files upload are over, move on
var promiseAll = $q.all(promises);
if ($scope.isEdit()) {
promiseAll.then(function(data) {
$scope.loadDocuments();
$state.transitionTo('document.view', { id: $stateParams.id });
});
} else {
promiseAll.then(function(data) {
$scope.document = {};
$scope.loadDocuments();
});
}
});
};