diff --git a/accountant-ui/js/accounts.js b/accountant-ui/js/accounts.js index 85d0f92..98921b1 100644 --- a/accountant-ui/js/accounts.js +++ b/accountant-ui/js/accounts.js @@ -24,12 +24,12 @@ angular.module('accountant.accounts', [ 'ngBootbox' ]) -.config(['$resourceProvider', function($resourceProvider) { +.config(function($resourceProvider) { // Keep trailing slashes to avoid redirect by flask.. $resourceProvider.defaults.stripTrailingSlashes = false; -}]) +}) -.factory('Account', ['$resource', function($resource) { +.factory('Account', function($resource) { var Account = $resource( '/api/account/:id', { id: '@id' @@ -54,114 +54,113 @@ angular.module('accountant.accounts', [ }; return Account; -}]) +}) -.controller('AccountController', [ - '$scope', '$ngBootbox', 'Account', 'Notification', - function($scope, $ngBootbox, Account, Notification) { - /* - * Return the class for an account current value compared to authorized - * overdraft. - */ - $scope.rowClass = function(account) { +.controller('AccountController', function($scope, $ngBootbox, Account, Notification) { + var vm=this; + + /* + * Return the class for an account current value compared to authorized + * overdraft. + */ + vm.rowClass = function(account) { + // eslint-disable-next-line camelcase + if (!account || !account.authorized_overdraft || !account.current) { + return; + } + + // eslint-disable-next-line camelcase + if (account.current < account.authorized_overdraft) { + return 'danger'; + } else if (account.current < 0) { + return 'warning'; + } + }; + + /* + * Return the class for a value compared to account authorized overdraft. + */ + vm.valueClass = function(account, value) { + if (!account || !value) { + return; + } + + // eslint-disable-next-line camelcase + if (value < account.authorized_overdraft) { + return 'text-danger'; + } else if (value < 0) { + return 'text-warning'; + } + }; + + /* + * Add an empty account. + */ + vm.add = function() { + var account = new Account({ // eslint-disable-next-line camelcase - if (!account || !account.authorized_overdraft || !account.current) { - return; - } + authorized_overdraft: 0 + }); - // eslint-disable-next-line camelcase - if (account.current < account.authorized_overdraft) { - return 'danger'; - } else if (account.current < 0) { - return 'warning'; - } - }; + // Insert account at the begining of the array. + $scope.accounts.splice(0, 0, account); + }; - /* - * Return the class for a value compared to account authorized overdraft. - */ - $scope.valueClass = function(account, value) { - if (!account || !value) { - return; - } + /* + * Cancel account edition. Remove it from array if a new one. + */ + vm.cancelEdit = function(rowform, account, $index) { + if (account.id) { + rowform.$cancel(); + } else { + // Account not saved, just remove it from array. + $scope.accounts.splice($index, 1); + } + }; - // eslint-disable-next-line camelcase - if (value < account.authorized_overdraft) { - return 'text-danger'; - } else if (value < 0) { - return 'text-warning'; - } - }; + /* + * Save account. + */ + vm.save = function(account) { + // var account = $scope.accounts[$index]; - /* - * Add an empty account. - */ - $scope.add = function() { - var account = new Account({ - // eslint-disable-next-line camelcase - authorized_overdraft: 0 - }); + // account = angular.merge(account, $data); - // Insert account at the begining of the array. - $scope.accounts.splice(0, 0, account); - }; + return account.$save().then(function(data) { + Notification.success('Account #' + data.id + ' saved.'); - /* - * Cancel account edition. Remove it from array if a new one. - */ - $scope.cancelEdit = function(rowform, account, $index) { - if (account.id) { - rowform.$cancel(); - } else { - // Account not saved, just remove it from array. - $scope.accounts.splice($index, 1); - } - }; + // TODO Alexis Lahouze 2016-03-08 Update solds - /* - * Save account. - */ - $scope.save = function(account) { - // var account = $scope.accounts[$index]; + return data; + }); + }; - // account = angular.merge(account, $data); + /* + * Delete an account. + */ + vm.delete = function(account, $index) { + var id = account.id; - return account.$save().then(function(data) { - Notification.success('Account #' + data.id + ' saved.'); + $ngBootbox.confirm( + 'Voulez-vous supprimer le compte \'' + account.name + '\' ?', + function(result) { + if (result) { + account.$delete().then(function() { + Notification.success('Account #' + id + ' deleted.'); - // TODO Alexis Lahouze 2016-03-08 Update solds - - return data; - }); - }; - - /* - * Delete an account. - */ - $scope.delete = function(account, $index) { - var id = account.id; - - $ngBootbox.confirm( - 'Voulez-vous supprimer le compte \'' + account.name + '\' ?', - function(result) { - if (result) { - account.$delete().then(function() { - Notification.success('Account #' + id + ' deleted.'); - - // Remove account from array. - $scope.accounts.splice($index, 1); - }); - } + // Remove account from array. + $scope.accounts.splice($index, 1); + }); } - ); - }; + } + ); + }; - // Load accounts. - $scope.accounts = Account.query(); - }] -) + // Load accounts. + vm.accounts = Account.query(); +}) -.directive('accountFormDialog', function(Account, $ngBootbox, Notification) { +.directive('accountFormDialog', function(Account, $ngBootbox, Notification, $log) { return { restrict: 'A', scope: { @@ -189,7 +188,7 @@ angular.module('accountant.accounts', [ angular.copy(scope.data, scope.account); // Save account - console.log(scope.account); + $log.log(scope.account); return scope.account.$save().then( function(data) { Notification.success('Account #' + data.id + ' saved.'); @@ -201,7 +200,7 @@ angular.module('accountant.accounts', [ function(data) { Notification.error('Error while saving account #' + data.id); scope.account.getSolds(); - console.log(data); + $log.log(data); return false; } );