From 4444fb442a2e02edf83d48412005f1000c70cb4a Mon Sep 17 00:00:00 2001 From: Gwenhael Le Moine Date: Wed, 1 Oct 2014 15:25:20 +0200 Subject: [PATCH] beautified js --- public/app/js/app.js | 3 +- public/app/js/controllers/AppCtrl.js | 6 +- public/app/js/controllers/BalanceCtrl.js | 175 +++++++++++++---------- public/app/js/controllers/NavbarCtrl.js | 7 +- public/app/js/state.js | 51 +++---- 5 files changed, 137 insertions(+), 105 deletions(-) diff --git a/public/app/js/app.js b/public/app/js/app.js index a0bad586..10fb68fe 100644 --- a/public/app/js/app.js +++ b/public/app/js/app.js @@ -2,4 +2,5 @@ var app = angular.module( 'app', [ 'ui.router', 'nvd3ChartDirectives', 'angularMoment', 'chieffancypants.loadingBar', - 'ngTable' ] ); + 'ngTable' + ] ); diff --git a/public/app/js/controllers/AppCtrl.js b/public/app/js/controllers/AppCtrl.js index de8e0da4..68053505 100644 --- a/public/app/js/controllers/AppCtrl.js +++ b/public/app/js/controllers/AppCtrl.js @@ -1,3 +1,5 @@ -app.controller('AppCtrl', function($scope) { +app.controller( 'AppCtrl', + [ '$scope', + function ( $scope ) { -}); + } ] ); diff --git a/public/app/js/controllers/BalanceCtrl.js b/public/app/js/controllers/BalanceCtrl.js index 1a5bbf9e..50b282f7 100644 --- a/public/app/js/controllers/BalanceCtrl.js +++ b/public/app/js/controllers/BalanceCtrl.js @@ -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 '

' + details.key + '

' - + '' - + _(details.values).map( function( transaction ) { - return ''; - }).join( '' ) - + '' - + '
' - + transaction.date + '' - + transaction.payee + '' - + $filter( 'number' )( transaction.amount, 2 ) + ' ' - + transaction.currency + '
Total :' + x + ' €
'; + return '

' + details.key + '

' + '' + _( details.values ).map( function ( transaction ) { + return ''; + } ).join( '' ) + '' + '
' + transaction.date + '' + transaction.payee + '' + $filter( 'number' )( transaction.amount, 2 ) + ' ' + transaction.currency + '
Total :' + x + ' €
'; }; }; // 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(); + } ); } ); - } ] ); + } + ] ); diff --git a/public/app/js/controllers/NavbarCtrl.js b/public/app/js/controllers/NavbarCtrl.js index c9f81852..2f101316 100644 --- a/public/app/js/controllers/NavbarCtrl.js +++ b/public/app/js/controllers/NavbarCtrl.js @@ -1,4 +1,5 @@ app.controller( 'NavbarCtrl', - function( $scope ) { - $scope.items = [ 'Balance' ]; - } ); + [ '$scope', + function( $scope ) { + $scope.items = [ 'Balance' ]; + } ] ); diff --git a/public/app/js/state.js b/public/app/js/state.js index b4b9c25d..0c13dd62 100644 --- a/public/app/js/state.js +++ b/public/app/js/state.js @@ -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' + } ); -}]); + } + ] );