Files
Teedy_custom/docs-web/src/main/webapp/js/controller/DocumentEdit.js
T

121 lines
3.0 KiB
JavaScript
Raw Normal View History

2013-07-27 18:33:20 +02:00
'use strict';
/**
* Document edition controller.
*/
2013-07-30 21:00:16 +02:00
App.controller('DocumentEdit', function($scope, $q, $http, $state, $stateParams, Restangular, Tag) {
2013-07-27 18:33:20 +02:00
/**
* Returns true if in edit mode (false in add mode).
*/
$scope.isEdit = function() {
return $stateParams.id;
};
2013-07-28 01:07:04 +02:00
/**
* In edit mode, load the current document.
*/
if ($scope.isEdit()) {
Restangular.one('document', $stateParams.id).get().then(function(data) {
$scope.document = data;
});
2013-07-31 23:12:25 +02:00
} else {
$scope.document = {
tags: []
};
2013-07-28 01:07:04 +02:00
}
2013-07-27 18:33:20 +02:00
/**
* Edit a document.
*/
$scope.edit = function() {
2013-07-28 01:07:04 +02:00
var promise = null;
2013-07-30 21:00:16 +02:00
var document = angular.copy($scope.document);
if (document.create_date instanceof Date) {
document.create_date = document.create_date.getTime();
}
2013-07-30 21:00:16 +02:00
// Extract ids from tags
2013-07-31 23:12:25 +02:00
document['tags[]'] = _.pluck(document.tags, 'id');
2013-07-28 01:07:04 +02:00
2013-07-27 18:33:20 +02:00
if ($scope.isEdit()) {
2013-07-28 01:07:04 +02:00
promise = Restangular
2013-07-28 18:29:03 +02:00
.one('document', $stateParams.id)
2013-07-30 21:00:16 +02:00
.post('', document);
2013-07-27 18:33:20 +02:00
} else {
2013-07-28 01:07:04 +02:00
promise = Restangular
2013-07-28 18:29:03 +02:00
.one('document')
2013-07-30 21:00:16 +02:00
.put(document);
2013-07-27 18:33:20 +02:00
}
2013-07-28 01:07:04 +02:00
// Upload files after edition
promise.then(function(data) {
2013-07-28 18:29:03 +02:00
var promises = [];
$scope.fileProgress = 0;
// When all files upload are over, move on
var navigateNext = function() {
if ($scope.isEdit()) {
2013-07-28 18:29:03 +02:00
$scope.loadDocuments();
$state.transitionTo('document.view', { id: $stateParams.id });
} else {
2013-07-28 18:29:03 +02:00
$scope.document = {};
$scope.loadDocuments();
}
}
if (_.size($scope.newFiles) == 0) {
navigateNext();
} else {
$scope.fileIsUploading = true;
// Send a file from the input file array and return a promise
var sendFile = function(key) {
// Build the payload
var file = $scope.newFiles[key];
var formData = new FormData();
formData.append('id', data.id);
formData.append('file', file);
// 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);
});
return promiseFile;
};
// Upload files sequentially
var key = 0;
var then = function() {
key++;
if ($scope.newFiles[key]) {
sendFile(key).then(then);
} else {
$scope.fileIsUploading = false;
navigateNext();
}
};
sendFile(key).then(then);
2013-07-28 18:29:03 +02:00
}
2013-07-28 01:07:04 +02:00
});
2013-07-27 18:33:20 +02:00
};
/**
* Cancel edition.
*/
$scope.cancel = function() {
2013-07-28 01:07:04 +02:00
if ($scope.isEdit()) {
$state.transitionTo('document.view', { id: $stateParams.id });
} else {
$state.transitionTo('document.default');
}
2013-07-27 18:33:20 +02:00
};
});