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

View File

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