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

154 lines
3.9 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-08-01 22:50:08 +02:00
// Alerts
$scope.alerts = [];
/**
* Close an alert.
*/
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
2013-08-02 23:59:00 +02:00
/**
* Returns a promise for typeahead title.
*/
$scope.getTitleTypeahead = function($viewValue) {
var deferred = $q.defer();
Restangular.one('document')
.getList('list', {
limit: 5,
sort_column: 1,
asc: true,
search: $viewValue
}).then(function(data) {
deferred.resolve(_.uniq(_.pluck(data.documents, 'title'), true));
2013-08-02 23:59:00 +02:00
});
return deferred.promise;
};
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 {
2013-08-02 00:08:05 +02:00
$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);
2013-08-02 23:59:00 +02:00
// Transform date to timestamp
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-08-01 23:32:55 +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
$scope.fileProgress = 0;
// When all files upload are over, move on
var navigateNext = function() {
if ($scope.isEdit()) {
2013-08-01 00:35:27 +02:00
$scope.pageDocuments();
2013-07-28 18:29:03 +02:00
$state.transitionTo('document.view', { id: $stateParams.id });
} else {
var fileUploadCount = _.size($scope.newFiles);
$scope.alerts.unshift({
type: 'success',
msg: 'Document successfully added (with ' + fileUploadCount + ' file' + (fileUploadCount > 1 ? 's' : '') + ')'
});
2013-08-02 00:08:05 +02:00
$scope.document = { tags: [] };
2013-08-01 00:06:21 +02:00
$scope.newFiles = [];
2013-07-28 18:29:03 +02:00
$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;
2013-08-01 00:06:21 +02:00
$scope.fileProgress = 0;
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
};
});