From 3eba873eb5f078b1b0b07e021b3bd8dbfad73800 Mon Sep 17 00:00:00 2001 From: Alexis Lahouze Date: Sun, 30 Jul 2017 09:21:50 +0200 Subject: [PATCH] Fix callbacks. --- src/operations/index.ts | 2 +- src/operations/operation.controller.ts | 109 ++++++++++++++----------- 2 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/operations/index.ts b/src/operations/index.ts index f370cfd..b34c81e 100644 --- a/src/operations/index.ts +++ b/src/operations/index.ts @@ -36,7 +36,7 @@ import { BalanceChartComponent } from './balanceChart.component'; import { CategoryChartComponent } from './categoryChart.component'; import { OperationService } from './operation.service'; -var OperationController = require('./operation.controller'); +import { OperationController } from './operation.controller'; export default angular.module('accountant.operations', [ ngResource, diff --git a/src/operations/operation.controller.ts b/src/operations/operation.controller.ts index a81e8e5..9077cf2 100644 --- a/src/operations/operation.controller.ts +++ b/src/operations/operation.controller.ts @@ -1,89 +1,100 @@ -var operationFormTmpl = require('./operation.form.tmpl.html'), - operationDeleteTmpl = require('./operation.delete.tmpl.html'); - +import { Component, Inject, OnInit } from '@angular/core'; import { Observable } from 'rxjs/Rx'; import { ToastrService } from 'ngx-toastr'; +var operationFormTmpl = require('./operation.form.tmpl.html'), + operationDeleteTmpl = require('./operation.delete.tmpl.html'); + +import { Account } from '../accounts/account'; import { AccountService } from '../accounts/account.service'; import { Operation } from './operation'; import { OperationService } from './operation.service'; -module.exports = function( - $modal, - accountIdService, - toastrService: ToastrService, - operationService: OperationService, - AccountService: AccountService -){ - var vm = this; +export class OperationController { + private account: Account; + private minDate: Date; + private maxDate: Date; + private operations: Operation[]; + + constructor( + private $modal, + private accountIdService, + private toastrService: ToastrService, + private operationService: OperationService, + private AccountService: AccountService + ){ + AccountService.get(this.accountIdService.get()).subscribe(account => { + this.account = account + }); + } /* * Add an empty operation. */ - vm.add = function() { + add() { var operation = new Operation(); - operation.account_id = accountIdService.get(); + operation.account_id = this.accountIdService.get(); - return vm.modify(operation); + return this.modify(operation); }; /* * Load operations. */ - vm.load = function(minDate, maxDate) { - vm.minDate = minDate; - vm.maxDate = maxDate; + load(minDate, maxDate) { + this.minDate = minDate; + this.maxDate = maxDate; - return operationService.query( - accountIdService.get(), + return this.operationService.query( + this.accountIdService.get(), minDate, maxDate ).subscribe((operations: Operation[]) => { - vm.operations = operations; + this.operations = operations; }); }; /* * Toggle pointed indicator for an operation. */ - vm.togglePointed = function(operation, rowform) { + togglePointed(operation, rowform) { operation.pointed = !operation.pointed; - vm.save(operation); + this.save(operation); }; /* * Toggle cancel indicator for an operation. */ - vm.toggleCanceled = function(operation) { + toggleCanceled(operation) { operation.canceled = !operation.canceled; - vm.save(operation); + this.save(operation); }; /* * Save an operation and return a promise. */ - vm.save = function(operation) { + save(operation) { operation.confirmed = true; var observable: Observable; if(operation.id){ - observable = operationService.update(operation); + observable = this.operationService.update(operation); } else { - observable = operationService.create(operation); + observable = this.operationService.create(operation); } return observable.subscribe((operation) => { - toastrService.success('Operation #' + operation.id + ' saved.'); + this.toastrService.success('Operation #' + operation.id + ' saved.'); - vm.load(); + this.load(this.minDate, this.maxDate); return operation; }, (result) => { - toastrService.error( + this.toastrService.error( 'Error while saving operation: ' + result.message ); }); @@ -92,15 +103,15 @@ module.exports = function( /* * Delete an operation and return a promise. */ - vm.confirmDelete = function(operation) { + confirmDelete(operation) { var title = "Delete operation #" + operation.id; - $modal({ + this.$modal({ templateUrl: operationDeleteTmpl, controller: function($scope, title, operation, $delete) { $scope.title = title; $scope.operation = operation; - $scope.$delete = function() { + $scope.$delete = () => { $scope.$hide(); $delete($scope.operation); }; @@ -108,22 +119,24 @@ module.exports = function( locals: { title: title, operation: operation, - $delete: vm.delete + $delete: (operation: Operation) => { + this.delete(operation); + } } }); }; - vm.delete = function(operation) { + delete(operation) { var id = operation.id; - return operationService.delete(operation).subscribe(() => { - toastrService.success('Operation #' + id + ' deleted.'); + return this.operationService.delete(operation).subscribe(() => { + this.toastrService.success('Operation #' + id + ' deleted.'); - vm.load(); + this.load(this.minDate, this.maxDate); return operation; }, (result) => { - toastrService.error( + this.toastrService.error( 'An error occurred while trying to delete operation #' + id + ':
' + result ); @@ -134,7 +147,7 @@ module.exports = function( * Open the popup to modify the operation, save it on confirm. * @returns a promise. */ - vm.modify = function(operation) { + modify(operation) { // FIXME Alexis Lahouze 2017-06-15 i18n var title = "Operation"; @@ -142,12 +155,12 @@ module.exports = function( title = title + " #" + operation.id; } - $modal({ + this.$modal({ templateUrl: operationFormTmpl, controller: function($scope, title, operation, $save) { $scope.title = title; $scope.operation = operation; - $scope.$save = function() { + $scope.$save = () => { $scope.$hide(); $save($scope.operation); }; @@ -155,16 +168,14 @@ module.exports = function( locals: { title: title, operation: operation, - $save: vm.save + $save: (operation: Operation) => { + this.save(operation); + } } }); }; - vm.onUpdate = function(dateRange) { - vm.load(dateRange.minDate, dateRange.maxDate); + onUpdate(dateRange) { + this.load(dateRange.minDate, dateRange.maxDate); }; - - AccountService.get(accountIdService.get()).subscribe(account => { - vm.account = account - }); };