From 5e3093d0d3b55566cdddfc7d513de83ddd94f83d Mon Sep 17 00:00:00 2001 From: jendib Date: Sat, 28 Mar 2015 18:02:21 +0100 Subject: [PATCH] Attach orphan files to a new document --- .../app/docs/controller/DocumentDefault.js | 14 +++++ .../src/app/docs/controller/DocumentEdit.js | 58 ++++++++++++------- .../src/partial/docs/document.default.html | 4 ++ .../src/partial/docs/document.edit.html | 5 +- 4 files changed, 58 insertions(+), 23 deletions(-) diff --git a/docs-web/src/main/webapp/src/app/docs/controller/DocumentDefault.js b/docs-web/src/main/webapp/src/app/docs/controller/DocumentDefault.js index 00d6358c..9fefe384 100644 --- a/docs-web/src/main/webapp/src/app/docs/controller/DocumentDefault.js +++ b/docs-web/src/main/webapp/src/app/docs/controller/DocumentDefault.js @@ -79,4 +79,18 @@ angular.module('docs').controller('DocumentDefault', function($scope, $state, Re }); return false; }; + + /** + * Returns checked files. + */ + $scope.checkedFiles = function() { + return _.where($scope.files, { checked: true }); + }; + + /** + * Add a document with checked files. + */ + $scope.addDocument = function() { + $state.transitionTo('document.add', { files: _.pluck($scope.checkedFiles(), 'id') }); + }; }); \ No newline at end of file diff --git a/docs-web/src/main/webapp/src/app/docs/controller/DocumentEdit.js b/docs-web/src/main/webapp/src/app/docs/controller/DocumentEdit.js index 7aff1ac2..c0d7d4b3 100644 --- a/docs-web/src/main/webapp/src/app/docs/controller/DocumentEdit.js +++ b/docs-web/src/main/webapp/src/app/docs/controller/DocumentEdit.js @@ -6,7 +6,10 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $q, $http, $state, $stateParams, Restangular) { // Alerts $scope.alerts = []; - + + // Orphan files to add + $scope.orphanFiles = $stateParams.files ? $stateParams.files.split(',') : []; + /** * Close an alert. */ @@ -51,6 +54,8 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $ /** * Edit a document. + * Workflow: + * Edit/add the file -> upload local files -> attach orphan files -> redirect to edited document or stay if adding */ $scope.edit = function() { var promise = null; @@ -65,35 +70,44 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $ document.tags = _.pluck(document.tags, 'id'); if ($scope.isEdit()) { - promise = Restangular - .one('document', $stateParams.id) - .post('', document); + promise = Restangular.one('document', $stateParams.id).post('', document); } else { - promise = Restangular - .one('document') - .put(document); + promise = Restangular.one('document').put(document); } + + // Attach orphan files after edition + var attachOrphanFiles = function(data) { + var promises = []; + _.each($scope.orphanFiles, function(fileId) { + promises.push(Restangular.one('file/' + fileId).post('', { id: data.id })); + }); + $scope.orphanFiles = []; + return $q.all(promises); + }; // Upload files after edition promise.then(function(data) { $scope.fileProgress = 0; - // When all files upload are over, move on + // When all files upload are over, attach orphan files and move on var navigateNext = function() { - if ($scope.isEdit()) { - // Go back to the edited document - $scope.pageDocuments(); - $state.transitionTo('document.view', { id: $stateParams.id }); - } else { - // Reset the scope and stay here - var fileUploadCount = _.size($scope.newFiles); - $scope.alerts.unshift({ - type: 'success', - msg: 'Document successfully added (with ' + fileUploadCount + ' file' + (fileUploadCount > 1 ? 's' : '') + ')' - }); - $scope.resetForm(); - $scope.loadDocuments(); - } + attachOrphanFiles(data).then(function(resolve) { + if ($scope.isEdit()) { + // Go back to the edited document + $scope.pageDocuments(); + $state.transitionTo('document.view', { id: $stateParams.id }); + } else { + // Reset the scope and stay here + var fileUploadCount = _.size($scope.newFiles) + resolve.length; + $scope.alerts.unshift({ + type: 'success', + msg: 'Document successfully added (with ' + fileUploadCount + ' file' + (fileUploadCount > 1 ? 's' : '') + ')' + }); + + $scope.resetForm(); + $scope.loadDocuments(); + } + }); }; if (_.size($scope.newFiles) == 0) { diff --git a/docs-web/src/main/webapp/src/partial/docs/document.default.html b/docs-web/src/main/webapp/src/partial/docs/document.default.html index e5b906f1..1d0bbd2b 100644 --- a/docs-web/src/main/webapp/src/partial/docs/document.default.html +++ b/docs-web/src/main/webapp/src/partial/docs/document.default.html @@ -39,6 +39,10 @@

+
+ +
+
diff --git a/docs-web/src/main/webapp/src/partial/docs/document.edit.html b/docs-web/src/main/webapp/src/partial/docs/document.edit.html index 51b3d951..717fc3a5 100644 --- a/docs-web/src/main/webapp/src/partial/docs/document.edit.html +++ b/docs-web/src/main/webapp/src/partial/docs/document.edit.html @@ -37,10 +37,13 @@
-
+
+
+ + {{ orphanFiles.length }} file{{ orphanFiles.length > 1 ? 's' : '' }} +