accountant-ui/src/scheduler/schedule.controller.ts
2017-07-21 23:06:59 +02:00

136 lines
3.7 KiB
TypeScript

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