diff --git a/accountant/frontend/static/js/accounts.js b/accountant/frontend/static/js/accounts.js index 28029ba..346171c 100644 --- a/accountant/frontend/static/js/accounts.js +++ b/accountant/frontend/static/js/accounts.js @@ -29,9 +29,13 @@ accountantApp .controller( "AccountController", [ - "$scope", "$rootScope", "$routeParams", "Accounts", "notificationService", - function($scope, $rootScope, $routeParams, Accounts, notificationService) { + "$scope", "Accounts", "notificationService", + function($scope, Accounts, notificationService) { + /* + * Return the class for an account current value compared to authorized + * overdraft. + */ $scope.rowClass = function(account) { if(!account || !account.authorized_overdraft || !account.current) { return; @@ -44,6 +48,9 @@ accountantApp } }; + /* + * Return the class for a value compared to account authorized overdraft. + */ $scope.valueClass = function(account, value) { if(!account || !value) { return; @@ -56,18 +63,21 @@ accountantApp } }; + /* + * Add an empty account if not already added. + */ $scope.addAccount = function() { - if(!$scope.inserted) { - $scope.inserted = new Accounts(); - $scope.inserted.authorized_overdraft = 0; - $scope.accounts.splice(0,0, $scope.inserted); - } + var account = new Accounts(); + account.authorized_overdraft = 0; + $scope.accounts.splice(0, 0, account); }; - $scope.cancelEdit = function(rowform, account) { - if(account == $scope.inserted) { - $scope.inserted=false; - $scope.accounts.splice(0,1); + /* + * Cancel account edition. Remove it from array if a new one. + */ + $scope.cancelEdit = function(rowform, account, $index) { + if(!account.id) { + $scope.accounts.splice($index, 1); } else { rowform.$cancel(); } @@ -81,26 +91,26 @@ accountantApp account = angular.merge(account, $data); - var promise = account.$save(); - - if(account == $scope.inserted) { - promise = promise.then(function(data) { - $scope.inserted = false; - - return data; - }); - } - - return promise.then(function(data) { + account.$save().then(function(data) { // Sort accounts by name. $scope.accounts.sort(function(a, b) { - if(a.name < b.name) { + if(a.id && b.id) { + if(a.name < b.name) { + return -1; + } else if(a.name > b.name) { + return 1; + } else { + return a.id - b.id; + } + } else if (!a.id && !b.id) { + return 0; + } else if (!a.id) { return -1; - } else if(a.name > b.name) { + } else if (!b.id) { return 1; - } else { - return a.id - b.id; } + + return 0; }); notificationService.success("Account #" + data.id + " saved."); diff --git a/accountant/frontend/static/templates/accounts.html b/accountant/frontend/static/templates/accounts.html index f6071f5..a56af19 100644 --- a/accountant/frontend/static/templates/accounts.html +++ b/accountant/frontend/static/templates/accounts.html @@ -70,36 +70,41 @@