beautified js

This commit is contained in:
Gwenhael Le Moine 2014-10-01 15:25:20 +02:00
parent 3b87e98514
commit 4444fb442a
5 changed files with 137 additions and 105 deletions

View file

@ -2,4 +2,5 @@ var app = angular.module( 'app', [ 'ui.router',
'nvd3ChartDirectives',
'angularMoment',
'chieffancypants.loadingBar',
'ngTable' ] );
'ngTable'
] );

View file

@ -1,3 +1,5 @@
app.controller('AppCtrl', function($scope) {
app.controller( 'AppCtrl',
[ '$scope',
function ( $scope ) {
});
} ] );

View file

@ -1,35 +1,27 @@
app.controller( 'BalanceCtrl',
[ '$scope', '$http', '$filter', 'ngTableParams',
function( $scope, $http, $filter, ngTableParams ) {
$scope.xFunction = function() {
return function( d ) {
function ( $scope, $http, $filter, ngTableParams ) {
$scope.xFunction = function () {
return function ( d ) {
return d.account;
};
};
$scope.yFunction = function() {
return function( d ) {
$scope.yFunction = function () {
return function ( d ) {
return d.amount;
};
};
$scope.toolTipContentFunction = function() {
return function( key, x, y, e, graph ) {
$scope.toolTipContentFunction = function () {
return function ( key, x, y, e, graph ) {
var details = $scope.balance.details[ key ];
return '<material-content><h3>' + details.key + '</h3>'
+ '<table>'
+ _(details.values).map( function( transaction ) {
return '<tr><td>'
+ transaction.date + '</td><td>'
+ transaction.payee + '</td><td style="text-align: right">'
+ $filter( 'number' )( transaction.amount, 2 ) + ' '
+ transaction.currency + '</td></tr>';
}).join( '' )
+ '<tr><th></th><th>Total :</th><th>' + x + ' €</th></tr>'
+ '</table></material-content>';
return '<material-content><h3>' + details.key + '</h3>' + '<table>' + _( details.values ).map( function ( transaction ) {
return '<tr><td>' + transaction.date + '</td><td>' + transaction.payee + '</td><td style="text-align: right">' + $filter( 'number' )( transaction.amount, 2 ) + ' ' + transaction.currency + '</td></tr>';
} ).join( '' ) + '<tr><th></th><th>Total :</th><th>' + x + ' €</th></tr>' + '</table></material-content>';
};
};
// compute an account's score: from 1 (good) to 10 (bad), 0 is neutral/undecided
$scope.score_account = function( account ) {
$scope.score_account = function ( account ) {
if ( account.match( /^Income:(salaire|Sécu|Mutuelle)$/ ) ) {
return 1;
} else if ( account.match( /^Income:(Gift|Remboursement)$/ ) ) {
@ -53,106 +45,141 @@ app.controller( 'BalanceCtrl',
}
};
$scope.coloring_score = function( score ) {
$scope.coloring_score = function ( score ) {
var color_scale = [ '#99f', '#0f0', '#3f0', '#6f0', '#9f0', '#cf0', '#fc0', '#f90', '#f60', '#f30', '#f00' ];
return color_scale[ score ];
};
$scope.color = function() {
return function( d, i ) {
$scope.color = function () {
return function ( d, i ) {
return $scope.coloring_score( $scope.score_account( d.data.account ) );
};
};
$scope.tableParams = new ngTableParams( { page: 1, // show first page
count: 999 // count per page
},
{ counts: [], // hide page counts control
total: 1//, // value less than count hide pagination
// getData: function($defer, params) {
// $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
// }
} );
$scope.tableParams = new ngTableParams( {
page: 1, // show first page
count: 999 // count per page
}, {
counts: [], // hide page counts control
total: 1 //, // value less than count hide pagination
// getData: function($defer, params) {
// $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
// }
} );
var retrieve_data = function() {
var retrieve_data = function () {
$scope.from_date = new Date( $scope.dates_salaries[ $scope.period_offset ] );
$scope.to_date = ( $scope.period_offset < $scope.dates_salaries.length - 1 ) ? new Date( $scope.dates_salaries[ $scope.period_offset + 1 ] ) : null;
var from = moment( $scope.from_date );
var period = 'from ' + from.year() + '-' + ( from.month() + 1 ) + '-' + from.date();
if ( !_($scope.to_date).isNull() ) {
if ( !_( $scope.to_date ).isNull() ) {
var to = moment( $scope.to_date );
period = period + ' to ' + to.year() + '-' + ( to.month() + 1 ) + '-' + to.date();
}
$scope.balance = { buckets: [ { name: 'Expenses',
data: [],
total: 0 },
{ name: 'Income',
data: [],
total: 0 } ],
details: {} };
$scope.balance = {
buckets: [ {
name: 'Expenses',
data: [],
total: 0
}, {
name: 'Income',
data: [],
total: 0
} ],
details: {}
};
$http.get( '/api/ledger/balance',
{ params: { period: period,
categories: 'Expenses' } } )
.then( function( response ) {
$scope.balance.buckets[ 0 ].data = _(response.data).sortBy( function( account ) {
$http.get( '/api/ledger/balance', {
params: {
period: period,
categories: 'Expenses'
}
} )
.then( function ( response ) {
$scope.balance.buckets[ 0 ].data = _( response.data ).sortBy( function ( account ) {
return 1 / account.amount;
} );
_($scope.balance.buckets[ 0 ].data).each(
function( account ) {
$http.get( '/api/ledger/register',
{ params: { period: period,
category: account.account } } )
.then( function( response ) {
_( $scope.balance.buckets[ 0 ].data ).each(
function ( account ) {
$http.get( '/api/ledger/register', {
params: {
period: period,
category: account.account
}
} )
.then( function ( response ) {
$scope.balance.details[ account.account ] = response.data;
} );
} );
$scope.balance.buckets[ 0 ].total = _(response.data).reduce( function( memo, account ){ return memo + account.amount; }, 0 );
$scope.balance.buckets[ 0 ].total = _( response.data ).reduce( function ( memo, account ) {
return memo + account.amount;
}, 0 );
} );
$http.get( '/api/ledger/balance',
{ params: { period: period,
categories: 'Income' } } )
$http.get( '/api/ledger/balance', {
params: {
period: period,
categories: 'Income'
}
} )
.then( function( response ) {
$scope.balance.buckets[ 1 ].data = _(response.data)
.map( function( account ) {
.then( function ( response ) {
$scope.balance.buckets[ 1 ].data = _( response.data )
.map( function ( account ) {
account.amount = account.amount * -1;
return account;
} );
$scope.balance.buckets[ 1 ].data = _($scope.balance.buckets[ 1 ].data)
.sortBy( function( account ) {
$scope.balance.buckets[ 1 ].data = _( $scope.balance.buckets[ 1 ].data )
.sortBy( function ( account ) {
return account.amount;
} );
_($scope.balance.buckets[ 1 ].data)
.each( function( account ) {
$http.get( '/api/ledger/register',
{ params: { period: period,
category: account.account } } )
.then( function( response ) {
_( $scope.balance.buckets[ 1 ].data )
.each( function ( account ) {
$http.get( '/api/ledger/register', {
params: {
period: period,
category: account.account
}
} )
.then( function ( response ) {
$scope.balance.details[ account.account ] = response.data;
} );
} );
$scope.balance.buckets[ 1 ].total = _(response.data)
.reduce( function( memo, account ){ return memo + account.amount; }, 0 );
$scope.balance.buckets[ 1 ].total = _( response.data )
.reduce( function ( memo, account ) {
return memo + account.amount;
}, 0 );
} );
};
$scope.dates_salaries = [];
$scope.period_offset = 0;
$scope.after = function() { if ( $scope.period_offset < $scope.dates_salaries.length - 1 ) { $scope.period_offset++; } };
$scope.before = function() { if ( $scope.period_offset > 0 ) { $scope.period_offset--; } };
$scope.reset_offset = function() { $scope.period_offset = $scope.dates_salaries.length - 1; };
$scope.after = function () {
if ( $scope.period_offset < $scope.dates_salaries.length - 1 ) {
$scope.period_offset++;
}
};
$scope.before = function () {
if ( $scope.period_offset > 0 ) {
$scope.period_offset--;
}
};
$scope.reset_offset = function () {
$scope.period_offset = $scope.dates_salaries.length - 1;
};
$http.get( '/api/ledger/dates_salaries' )
.then( function( response ) {
.then( function ( response ) {
$scope.dates_salaries = response.data;
$scope.reset_offset();
// retrieve_data() when the value of week_offset changes
// n.b.: triggered when week_offset is initialized above
$scope.$watch( 'period_offset', function() { retrieve_data(); } );
$scope.$watch( 'period_offset', function () {
retrieve_data();
} );
} );
} ] );
}
] );

View file

@ -1,4 +1,5 @@
app.controller( 'NavbarCtrl',
function( $scope ) {
$scope.items = [ 'Balance' ];
} );
[ '$scope',
function( $scope ) {
$scope.items = [ 'Balance' ];
} ] );

View file

@ -3,29 +3,30 @@ app.config( [ '$stateProvider', '$urlRouterProvider',
function ( $stateProvider, $urlRouterProvider ) {
$urlRouterProvider.when( '', '/balance' );
$stateProvider
.state('app.balance', {
url: '/balance',
templateUrl: 'js/templates/balance.tpl.html',
controller: 'BalanceCtrl'
})
.state('app', {
url: '',
controller: 'AppCtrl',
views: {
'navbar': {
templateUrl: 'js/templates/navbar.tpl.html',
controller: 'NavbarCtrl'
},
'main': {
templateUrl: 'js/templates/main.tpl.html'
}
}
})
.state('404', {
url: '/404',
templateUrl: 'js/templates/404.tpl.html',
controller: 'AppCtrl'
});
$stateProvider
.state( 'app.balance', {
url: '/balance',
templateUrl: 'js/templates/balance.tpl.html',
controller: 'BalanceCtrl'
} )
.state( 'app', {
url: '',
controller: 'AppCtrl',
views: {
'navbar': {
templateUrl: 'js/templates/navbar.tpl.html',
controller: 'NavbarCtrl'
},
'main': {
templateUrl: 'js/templates/main.tpl.html'
}
}
} )
.state( '404', {
url: '/404',
templateUrl: 'js/templates/404.tpl.html',
controller: 'AppCtrl'
} );
}]);
}
] );