2017-07-08 12:20:17 +02:00
|
|
|
var scheduleFormTmpl = require('./schedule.form.tmpl.html'),
|
|
|
|
scheduleDeleteTmpl = require('./schedule.delete.tmpl.html');
|
|
|
|
|
2017-07-16 13:56:49 +02:00
|
|
|
module.exports= function($rootScope, $stateParams, Notification, ScheduledOperation, $log, $modal) {
|
2017-07-08 12:20:17 +02:00
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
// Operation store.
|
|
|
|
vm.operations = [];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new operation at the beginning of th array.
|
|
|
|
*/
|
|
|
|
vm.add = function() {
|
|
|
|
var operation = new ScheduledOperation({
|
|
|
|
// eslint-disable-next-line camelcase
|
2017-07-16 13:56:49 +02:00
|
|
|
account_id: $stateParams.accountId
|
2017-07-08 12:20:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return vm.modify(operation);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load operations.
|
|
|
|
*/
|
|
|
|
vm.load = function() {
|
|
|
|
return ScheduledOperation.query({
|
|
|
|
// eslint-disable-next-line camelcase
|
2017-07-16 13:56:49 +02:00
|
|
|
account_id: $stateParams.accountId
|
2017-07-08 12:20:17 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save operation.
|
|
|
|
*/
|
|
|
|
vm.save = function(operation) {
|
|
|
|
return operation.$save().then(function(operation) {
|
|
|
|
Notification.success('Scheduled operation #' + operation.id + ' saved.');
|
|
|
|
|
|
|
|
vm.operations = vm.load();
|
|
|
|
|
|
|
|
return operation;
|
|
|
|
}, function(result){
|
|
|
|
$log.error('Error while saving scheduled operation', operation, result);
|
|
|
|
|
|
|
|
Notification.error(
|
|
|
|
'Error while saving scheduled operation: ' + result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete an operation and return a promise.
|
|
|
|
*/
|
|
|
|
vm.confirmDelete = function(operation) {
|
|
|
|
var title = "Delete operation #" + operation.id;
|
|
|
|
|
|
|
|
$modal({
|
|
|
|
templateUrl: scheduleDeleteTmpl,
|
|
|
|
controller: function($scope, title, operation, $delete) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = operation;
|
|
|
|
$scope.$delete = function() {
|
|
|
|
$scope.$hide();
|
|
|
|
$delete($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
operation: operation,
|
|
|
|
$delete: vm.delete
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete operation.
|
|
|
|
*/
|
|
|
|
vm.delete = function(operation) {
|
|
|
|
var id = operation.id;
|
|
|
|
|
|
|
|
return operation.$delete().then(function() {
|
|
|
|
Notification.success('Scheduled operation #' + id + ' deleted.');
|
|
|
|
|
|
|
|
vm.operations = vm.load();
|
|
|
|
|
|
|
|
return operation;
|
|
|
|
}, function(result) {
|
|
|
|
Notification.error(
|
|
|
|
'An error occurred while trying to delete scheduled operation #' +
|
|
|
|
id + ':<br />' + result
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the popup to modify the operation, save it on confirm.
|
|
|
|
* @returns a promise.
|
|
|
|
*/
|
|
|
|
vm.modify = function(operation) {
|
|
|
|
// FIXME Alexis Lahouze 2017-06-15 i18n
|
|
|
|
var title = "Operation";
|
|
|
|
|
|
|
|
if (operation.id) {
|
|
|
|
title = title + " #" + operation.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$modal({
|
|
|
|
templateUrl: scheduleFormTmpl,
|
|
|
|
controller: function($scope, title, operation, $save) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = operation;
|
|
|
|
$scope.$save = function() {
|
|
|
|
$scope.$hide();
|
|
|
|
$save($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
operation: operation,
|
|
|
|
$save: vm.save
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load operations on controller initialization.
|
|
|
|
vm.operations = vm.load();
|
|
|
|
};
|