accountant-ui/src/operations/operation.controller.ts

171 lines
4.4 KiB
TypeScript
Raw Normal View History

2017-07-07 23:12:48 +02:00
var operationFormTmpl = require('./operation.form.tmpl.html'),
operationDeleteTmpl = require('./operation.delete.tmpl.html');
import { Observable } from 'rxjs/Rx';
import { ToastrService } from 'ngx-toastr';
2017-07-29 22:58:34 +02:00
import { AccountService } from '../accounts/account.service';
import { Operation } from './operation';
import { OperationService } from './operation.service';
module.exports = function(
$modal,
accountIdService,
toastrService: ToastrService,
operationService: OperationService,
2017-07-29 22:58:34 +02:00
AccountService: AccountService
){
var vm = this;
/*
* Add an empty operation.
*/
vm.add = function() {
var operation = new Operation();
operation.account_id = accountIdService.get();
return vm.modify(operation);
};
/*
* Load operations.
*/
vm.load = function(minDate, maxDate) {
vm.minDate = minDate;
vm.maxDate = maxDate;
return operationService.query(
accountIdService.get(),
minDate,
maxDate
).subscribe((operations: Operation[]) => {
vm.operations = operations;
});
};
/*
* Toggle pointed indicator for an operation.
*/
vm.togglePointed = function(operation, rowform) {
operation.pointed = !operation.pointed;
vm.save(operation);
};
/*
* Toggle cancel indicator for an operation.
*/
vm.toggleCanceled = function(operation) {
operation.canceled = !operation.canceled;
vm.save(operation);
};
/*
* Save an operation and return a promise.
*/
vm.save = function(operation) {
operation.confirmed = true;
var observable: Observable<Operation>;
if(operation.id){
observable = operationService.update(operation);
} else {
observable = operationService.create(operation);
}
return observable.subscribe((operation) => {
toastrService.success('Operation #' + operation.id + ' saved.');
vm.load();
return operation;
}, (result) => {
toastrService.error(
'Error while saving operation: ' + result.message
);
});
};
/*
* Delete an operation and return a promise.
*/
vm.confirmDelete = function(operation) {
var title = "Delete operation #" + operation.id;
$modal({
templateUrl: operationDeleteTmpl,
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
}
});
};
vm.delete = function(operation) {
var id = operation.id;
return operationService.delete(operation).subscribe(() => {
toastrService.success('Operation #' + id + ' deleted.');
vm.load();
return operation;
}, (result) => {
toastrService.error(
'An error occurred while trying to delete 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: operationFormTmpl,
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
}
});
};
vm.onUpdate = function(dateRange) {
vm.load(dateRange.minDate, dateRange.maxDate);
};
AccountService.get(accountIdService.get()).subscribe(account => {
2017-07-14 10:24:46 +02:00
vm.account = account
});
};