Cleanup, improve addition.

This commit is contained in:
Alexis Lahouze 2015-08-19 17:39:03 +02:00
parent 4bd8bb3819
commit 2884ce78aa
2 changed files with 54 additions and 39 deletions

View File

@ -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,19 +91,10 @@ 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.id && b.id) {
if(a.name < b.name) {
return -1;
} else if(a.name > b.name) {
@ -101,6 +102,15 @@ accountantApp
} else {
return a.id - b.id;
}
} else if (!a.id && !b.id) {
return 0;
} else if (!a.id) {
return -1;
} else if (!b.id) {
return 1;
}
return 0;
});
notificationService.success("Account #" + data.id + " saved.");

View File

@ -70,35 +70,40 @@
</td>
<td>
<form editable-form name="rowform" onbeforesave="saveAccount($data, $index)" shown="inserted == account">
<form editable-form name="rowform"
onbeforesave="saveAccount($data, $index)" shown="!account.id">
<div class="btn-group">
<!-- Save account. -->
<button type="submit" class="btn btn-xs btn-success"
ng-if="rowform.$visible">
<span class="fa fa-floppy-o"></span>
</button>
<button class="btn btn-xs btn-default"
ng-if="rowform.$visible && inserted == account"
ng-click="accounts.splice(0,1)">
<!-- Cancel account edition. -->
<button type="button" class="btn btn-xs btn-default"
ng-if="rowform.$visible"
ng-click="cancelEdit(rowform, account, $index)">
<span class="fa fa-times"></span>
</button>
<button class="btn btn-xs btn-default"
ng-if="rowform.$visible && inserted != account" ng-click="rowform.$cancel()">
<span class="fa fa-times"></span>
</button>
<button type="button" ng-show="!rowform.$visible"
class="btn btn-xs btn-success" ng-click="rowform.$show()">
<!-- Edit account. -->
<button type="button" class="btn btn-xs btn-success"
ng-show="!rowform.$visible"
ng-click="rowform.$show()">
<span class="fa fa-pencil-square-o"></span>
</button>
<!-- Delete account, with confirm. -->
<button type="button" class="btn btn-xs btn-default"
ng-if="account.id"
ng-click="deleteAccount(account, $index)">
<span class="fa fa-trash-o"></span>
</button>
<a ng-if="!isNew(account)" class="btn btn-xs btn-default" href="account/{{ account.id }}/scheduler">
<!-- Open account scheduler. -->
<a class="btn btn-xs btn-default"
ng-if="!isNew(account)"
href="account/{{ account.id }}/scheduler">
<span class="fa fa-clock-o"></span>
</a>
</div>