diff --git a/index.js b/index.js index 2c02cab..370ec69 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,25 @@ var config = iniparser.parseSync('./config.ini'); var whiskers = require('whiskers'); var numeral = require('numeral'); +numeral.language('id', { + delimiters: { + thousands: '.', + decimal: ',' + }, + abbreviations: { + thousand: 'k', + million: 'm', + billion: 'b', + trillion: 't' + }, + ordinal : function (number) { + return number === 1 ? 'er' : 'ème'; + }, + currency: { + symbol: 'Rp.' + } +}); +numeral.language('id'); var Sync = require('sync'); var redis = require('redis').createClient(config.globals.redis_port, config.globals.redis_host); @@ -13,9 +32,12 @@ var app = express(); var suppliers = config.globals.suppliers.split(','); app.engine('.html', whiskers.__express); +app.engine('.txt', whiskers.__express); +app.engine('.json', whiskers.__express); + app.set('views', __dirname+'/views'); -function getBalances(suppliers, callback) { +function getBalances(suppliers, thousands, callback) { var kw = []; var count = suppliers.length; @@ -29,9 +51,17 @@ function getBalances(suppliers, callback) { var balances = []; for (var i=0; i<count; i++) { + var value = res[i]; + if (!value) { + value = 0; + } + + if (thousands) { + value = numeral(res[i]).format('0,0'); + } balances.push({ title: suppliers[i], - balance: numeral(res[i]).format('0,0') + balance: value, }); } @@ -42,12 +72,27 @@ function getBalances(suppliers, callback) { app.get('/', function(req, res) { Sync(function() { + var template_file; + var thousands = true; + + res.format({ + text: function() { + template_file = 'index.txt'; + }, + json: function() { + thousands = false; + template_file = 'index.json'; + }, + 'default': function() { + template_file = 'index.html'; + }, + + }); - var balances = getBalances.sync(null, suppliers); + var balances = getBalances.sync(null, suppliers, thousands); //balances = getBalances(suppliers); - res.render('basic_template.html', { - // logo, berdasar isu #1 + res.render(template_file, { logo: 'http://reload97.com/sites/default/files/web-reload97.png', title: 'Supplier Balances', balances: balances, diff --git a/views/basic_template.html b/views/basic_template.html deleted file mode 100644 index d4c8330..0000000 --- a/views/basic_template.html +++ /dev/null @@ -1,50 +0,0 @@ -<!DOCTYPE html> -<html lang="en"> - <head> - <meta charset="utf-8"> - <meta http-equiv="X-UA-Compatible" content="IE=edge"> - <meta name="viewport" content="width=device-width, initial-scale=1"> - <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> - <title>{title}</title> - - <!-- Latest compiled and minified CSS --> - <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> - - <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> - <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> - <!--[if lt IE 9]> - <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> - <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> - <![endif]--> - </head> - <body> - <div class="pull-left"> - <img src="{logo}"> - </div> - <h1> - <br/><br/> - {title} - </h1> - - <table class="table table-hover table-striped"> - {for balance in balances} - <tr> - <td>{balance.title}</td> - <td> - <div class="pull-right"> - {balance.balance} - </div> - </td> - </tr> - {/for} - </table> - - Generated on {timestamp} - - - <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> - <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> - <!-- Latest compiled and minified JavaScript --> - <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> - </body> -</html> diff --git a/views/index.html b/views/index.html index b58b312..d4c8330 100644 --- a/views/index.html +++ b/views/index.html @@ -1,17 +1,50 @@ -<html> -<head> +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta http-equiv="X-UA-Compatible" content="IE=edge"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --> <title>{title}</title> -</head> -<body> -<h1>{title}</h1> -<table> + <!-- Latest compiled and minified CSS --> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> + + <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> + <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> + <!--[if lt IE 9]> + <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> + <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> + <![endif]--> + </head> + <body> + <div class="pull-left"> + <img src="{logo}"> + </div> + <h1> + <br/><br/> + {title} + </h1> + + <table class="table table-hover table-striped"> {for balance in balances} - <tr> - <td>{balance.title}</td> - <td class="pull-right">{balance.balance}</td> - </tr> + <tr> + <td>{balance.title}</td> + <td> + <div class="pull-right"> + {balance.balance} + </div> + </td> + </tr> {/for} -</table> -</body> + </table> + + Generated on {timestamp} + + + <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> + <!-- Latest compiled and minified JavaScript --> + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> + </body> </html> diff --git a/views/index.json b/views/index.json new file mode 100644 index 0000000..7d61dae --- /dev/null +++ b/views/index.json @@ -0,0 +1,3 @@ +{ +{for balance in balances}{balance.title}: {balance.balance}, +{/for}} diff --git a/views/index.txt b/views/index.txt new file mode 100644 index 0000000..4378977 --- /dev/null +++ b/views/index.txt @@ -0,0 +1,2 @@ +{for balance in balances}{balance.title} {balance.balance} +{/for}