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

136 lines
3.6 KiB
TypeScript
Raw Normal View History

2017-07-22 09:17:50 +02:00
import { Logger } from '@nsalaun/ng-logger';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrService } from 'ngx-toastr';
var scheduleFormTmpl = require('./schedule.form.tmpl.html'),
scheduleDeleteTmpl = require('./schedule.delete.tmpl.html');
export class ScheduleController{
operations = [];
constructor(
private $stateParams,
2017-07-22 09:17:50 +02:00
private toastrService: ToastrService,
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({
account_id: this.$stateParams.accountId
});
return this.modify(operation);
};
/*
* Load operations.
*/
load = function() {
return this.ScheduledOperation.query({
account_id: this.$stateParams.accountId
});
};
/*
* Save operation.
*/
save = function(operation) {
2017-07-22 09:17:50 +02:00
return operation.$save().then((operation) => {
this.toastrService.success('Scheduled operation #' + operation.id + ' saved.');
this.operations = this.load();
return operation;
2017-07-22 09:17:50 +02:00
}, (result) => {
this.toastrService.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;
2017-07-22 09:17:50 +02:00
$scope.$delete = () => {
$scope.$hide();
$delete($scope.operation);
};
},
locals: {
title: title,
operation: operation,
2017-07-22 09:17:50 +02:00
$delete: (operation) => {
this.delete(operation);
}
}
});
};
/*
* Delete operation.
*/
delete = function(operation) {
var id = operation.id;
2017-07-22 09:17:50 +02:00
return operation.$delete().then(() => {
this.toastrService.success('Scheduled operation #' + id + ' deleted.');
this.operations = this.load();
return operation;
2017-07-22 09:17:50 +02:00
}, (result) => {
this.toastrService.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;
2017-07-22 09:17:50 +02:00
$scope.$save = () => {
$scope.$hide();
$save($scope.operation);
};
},
locals: {
title: title,
operation: operation,
2017-07-22 09:17:50 +02:00
$save: (operation) => {
this.save(operation);
}
}
});
};
};