2017-07-08 12:20:17 +02:00
|
|
|
var scheduleFormTmpl = require('./schedule.form.tmpl.html'),
|
|
|
|
scheduleDeleteTmpl = require('./schedule.delete.tmpl.html');
|
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
export class ScheduleController{
|
2017-07-21 23:11:04 +02:00
|
|
|
$inject=['$stateParams', 'Notification', 'ScheduledOperation', '$modal']
|
2017-07-21 23:06:59 +02:00
|
|
|
|
|
|
|
operations = [];
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private $stateParams,
|
|
|
|
private Notification,
|
|
|
|
private ScheduledOperation,
|
|
|
|
private $modal
|
|
|
|
) {
|
|
|
|
// Load operations on controller initialization.
|
|
|
|
this.operations = this.load();
|
|
|
|
}
|
2017-07-08 12:20:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new operation at the beginning of th array.
|
|
|
|
*/
|
2017-07-21 23:06:59 +02:00
|
|
|
add = function() {
|
|
|
|
var operation = new this.ScheduledOperation({
|
2017-07-08 12:20:17 +02:00
|
|
|
// eslint-disable-next-line camelcase
|
2017-07-21 23:06:59 +02:00
|
|
|
account_id: this.$stateParams.accountId
|
2017-07-08 12:20:17 +02:00
|
|
|
});
|
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
return this.modify(operation);
|
2017-07-08 12:20:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load operations.
|
|
|
|
*/
|
2017-07-21 23:06:59 +02:00
|
|
|
load = function() {
|
|
|
|
return this.ScheduledOperation.query({
|
2017-07-08 12:20:17 +02:00
|
|
|
// eslint-disable-next-line camelcase
|
2017-07-21 23:06:59 +02:00
|
|
|
account_id: this.$stateParams.accountId
|
2017-07-08 12:20:17 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save operation.
|
|
|
|
*/
|
2017-07-21 23:06:59 +02:00
|
|
|
save = function(operation) {
|
2017-07-08 12:20:17 +02:00
|
|
|
return operation.$save().then(function(operation) {
|
2017-07-21 23:06:59 +02:00
|
|
|
this.Notification.success('Scheduled operation #' + operation.id + ' saved.');
|
2017-07-08 12:20:17 +02:00
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
this.operations = this.load();
|
2017-07-08 12:20:17 +02:00
|
|
|
|
|
|
|
return operation;
|
|
|
|
}, function(result){
|
2017-07-21 23:06:59 +02:00
|
|
|
this.Notification.error(
|
2017-07-08 12:20:17 +02:00
|
|
|
'Error while saving scheduled operation: ' + result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete an operation and return a promise.
|
|
|
|
*/
|
2017-07-21 23:06:59 +02:00
|
|
|
confirmDelete = function(operation) {
|
2017-07-08 12:20:17 +02:00
|
|
|
var title = "Delete operation #" + operation.id;
|
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
this.$modal({
|
2017-07-08 12:20:17 +02:00
|
|
|
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,
|
2017-07-21 23:06:59 +02:00
|
|
|
$delete: this.delete
|
2017-07-08 12:20:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete operation.
|
|
|
|
*/
|
2017-07-21 23:06:59 +02:00
|
|
|
delete = function(operation) {
|
2017-07-08 12:20:17 +02:00
|
|
|
var id = operation.id;
|
|
|
|
|
|
|
|
return operation.$delete().then(function() {
|
2017-07-21 23:06:59 +02:00
|
|
|
this.Notification.success('Scheduled operation #' + id + ' deleted.');
|
2017-07-08 12:20:17 +02:00
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
this.operations = this.load();
|
2017-07-08 12:20:17 +02:00
|
|
|
|
|
|
|
return operation;
|
|
|
|
}, function(result) {
|
2017-07-21 23:06:59 +02:00
|
|
|
this.Notification.error(
|
2017-07-08 12:20:17 +02:00
|
|
|
'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.
|
|
|
|
*/
|
2017-07-21 23:06:59 +02:00
|
|
|
modify = function(operation) {
|
2017-07-08 12:20:17 +02:00
|
|
|
// FIXME Alexis Lahouze 2017-06-15 i18n
|
|
|
|
var title = "Operation";
|
|
|
|
|
|
|
|
if (operation.id) {
|
|
|
|
title = title + " #" + operation.id;
|
|
|
|
}
|
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
this.$modal({
|
2017-07-08 12:20:17 +02:00
|
|
|
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,
|
2017-07-21 23:06:59 +02:00
|
|
|
$save: this.save
|
2017-07-08 12:20:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|