accountant-ui/src/scheduler/schedule.controller.ts

132 lines
3.5 KiB
TypeScript
Raw Normal View History

var scheduleFormTmpl = require('./schedule.form.tmpl.html'),
scheduleDeleteTmpl = require('./schedule.delete.tmpl.html');
export class ScheduleController{
2017-07-21 23:11:04 +02:00
$inject=['$stateParams', 'Notification', 'ScheduledOperation', '$modal']
operations = [];
constructor(
private $stateParams,
private Notification,
private ScheduledOperation,
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.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
}
});
};
};