Attach orphan files to a new document

This commit is contained in:
jendib 2015-03-28 18:02:21 +01:00
parent 0d4643cc93
commit 5e3093d0d3
4 changed files with 58 additions and 23 deletions

View File

@ -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') });
};
});

View File

@ -7,6 +7,9 @@ angular.module('docs').controller('DocumentEdit', function($rootScope, $scope, $
// 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() {
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);
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) {

View File

@ -39,6 +39,10 @@
</p>
</div>
<div class="btn-group" ng-show="checkedFiles().length > 0">
<button class="btn btn-primary" ng-click="addDocument()"><span class="glyphicon glyphicon-plus"></span> Add to new document</button>
</div>
<div ui-view="file"></div>
<div class="text-muted text-right">

View File

@ -37,10 +37,13 @@
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="inputFiles">New files</label>
<div class="col-sm-10">
<div class="col-sm-6">
<file class="form-control" id="inputFiles" multiple="multiple" ng-model="newFiles"
accept="image/png,image/jpg,image/jpeg,image/gif,application/pdf" ng-disabled="fileIsUploading"></file>
</div>
<div class="col-sm-4" ng-if="orphanFiles.length > 0">
+ {{ orphanFiles.length }} file{{ orphanFiles.length > 1 ? 's' : '' }}
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="inputTags">Tags</label>