From 78d4c993374cf5962b08fe2aba67ebb42075b75b Mon Sep 17 00:00:00 2001 From: Mario Colque Date: Wed, 5 Feb 2014 15:59:44 -0300 Subject: [PATCH] added currency switch with cache --- app/controllers/currency.js | 56 +++++++++++++++++---------- app/views/includes/foot.jade | 1 + config/config.js | 3 ++ config/express.js | 3 ++ public/js/app.js | 4 +- public/js/controllers/footer.js | 63 +++++++++++++++++++++++++++---- public/js/controllers/header.js | 4 +- public/js/directives.js | 9 +++-- public/js/services/currency.js | 6 +++ public/views/includes/footer.html | 6 +++ public/views/transaction/tx.html | 12 +++--- 11 files changed, 128 insertions(+), 39 deletions(-) create mode 100644 public/js/services/currency.js diff --git a/app/controllers/currency.js b/app/controllers/currency.js index 3ead1442..e635a7b7 100644 --- a/app/controllers/currency.js +++ b/app/controllers/currency.js @@ -1,11 +1,16 @@ 'use strict'; +var config = require('../../config/config'); + +// Set the initial vars +var timestamp = +new Date(), + delay = config.currencyRefresh * 60000, + bitstampRate = 0; exports.index = function(req, res) { + var _xhr = function() { - if (typeof ActiveXObject !== 'undefined' && ActiveXObject !== null) { - return new ActiveXObject('Microsoft.XMLHTTP'); - } else if (typeof XMLHttpRequest !== 'undefined' && XMLHttpRequest !== null) { + if (typeof XMLHttpRequest !== 'undefined' && XMLHttpRequest !== null) { return new XMLHttpRequest(); } else if (typeof require !== 'undefined' && require !== null) { var XMLhttprequest = require('xmlhttprequest').XMLHttpRequest; @@ -21,33 +26,46 @@ exports.index = function(req, res) { if (request.readyState === 4) { if (request.status === 200) { return cb(false, request.responseText); - } else { - return cb(true, { - status: request.status, - message: 'Request error' - }); } + + return cb(true, { + status: request.status, + message: 'Request error' + }); } }; return request.send(null); }; - _request('https://www.bitstamp.net/api/ticker/', function(err, data) { - if (err) { - return res.jsonp({ - status: err.status, - message: err.message + // Init + var currentTime = +new Date(); + console.log('-----------------------------------'); + console.log(timestamp); + console.log(currentTime); + console.log(currentTime >= (timestamp + delay)); + console.log('-----------------------------------'); + if (bitstampRate === 0 || currentTime >= (timestamp + delay)) { + timestamp = currentTime; + + _request('https://www.bitstamp.net/api/ticker/', function(err, data) { + if (!err) bitstampRate = parseFloat(JSON.parse(data).last); + + res.jsonp({ + status: 200, + data: { + bitstamp: bitstampRate, + delay: delay + } }); - } - - var bitstamp = JSON.parse(data); - + }); + } else { res.jsonp({ status: 200, data: { - bitstamp: parseFloat(bitstamp.last) + bitstamp: bitstampRate, + delay: delay } }); - }); + } }; diff --git a/app/views/includes/foot.jade b/app/views/includes/foot.jade index 305c2449..323f3722 100755 --- a/app/views/includes/foot.jade +++ b/app/views/includes/foot.jade @@ -30,6 +30,7 @@ script(type='text/javascript', src='/js/services/address.js') script(type='text/javascript', src='/js/services/transactions.js') script(type='text/javascript', src='/js/services/blocks.js') script(type='text/javascript', src='/js/services/socket.js') +script(type='text/javascript', src='/js/services/currency.js') //Application Controllers script(type='text/javascript', src='/js/controllers/index.js') diff --git a/config/config.js b/config/config.js index f28964cb..39e49355 100644 --- a/config/config.js +++ b/config/config.js @@ -39,4 +39,7 @@ module.exports = { network: process.env.INSIGHT_NETWORK || 'testnet', disableP2pSync: false, disableHistoricSync: false, + + // Time to refresh the currency rate. In minutes + currencyRefresh: 10 }; diff --git a/config/express.js b/config/express.js index 2697554f..4b96d0ff 100644 --- a/config/express.js +++ b/config/express.js @@ -21,6 +21,9 @@ module.exports = function(app, historicSync) { app.set('views', config.root + '/app/views'); app.set('view engine', 'jade'); + // Compress JSON outputs + app.set('json spaces', 0); + //Enable jsonp app.enable('jsonp callback'); diff --git a/public/js/app.js b/public/js/app.js index ba71dc8e..279ea331 100755 --- a/public/js/app.js +++ b/public/js/app.js @@ -14,7 +14,8 @@ angular.module('insight',[ 'insight.address', 'insight.search', 'insight.status', - 'insight.connection' + 'insight.connection', + 'insight.currency' ]); angular.module('insight.system', []); @@ -25,3 +26,4 @@ angular.module('insight.address', []); angular.module('insight.search', []); angular.module('insight.status', []); angular.module('insight.connection', []); +angular.module('insight.currency', []); diff --git a/public/js/controllers/footer.js b/public/js/controllers/footer.js index 6aa1d875..8ad61ab5 100644 --- a/public/js/controllers/footer.js +++ b/public/js/controllers/footer.js @@ -1,15 +1,62 @@ 'use strict'; angular.module('insight.system').controller('FooterController', - function($scope, Version) { + function($rootScope, $scope, Version, Currency) { - var _getVersion = function() { - Version.get({}, - function(res) { - $scope.version = res.version; - }); - }; + var _roundFloat = function(x, n) { + if(!parseInt(n, 10)) n = 0; - $scope.version = _getVersion(); + if(!parseFloat(x)) return false; + + return Math.round(x * Math.pow(10, n)) / Math.pow(10, n); + }; + + $rootScope.currency = { + factor: 1, + symbol: 'BTC', + bitstamp: 0, + getConversion: function(value) { + if (value !== 'undefined' && value !== null) { + var response; + + if (this.symbol === 'USD') { + response = _roundFloat((value * this.factor), 2); + } else if (this.symbol === 'mBTC') { + this.factor = 1000; + response = _roundFloat((value * this.factor), 5); + } else { + this.factor = 1; + response = value; + } + + return response + ' ' + this.symbol; + } + + return null; + } + }; + + $scope.setCurrency = function(currency) { + if (currency === 'USD') { + Currency.get({}, function(res) { + $rootScope.currency.factor = res.data.bitstamp; + }); + } else if (currency === 'mBTC') { + $rootScope.currency.factor = 1000; + } else { + $rootScope.currency.factor = 1; + } + + $rootScope.currency.symbol = currency; + }; + + var _getVersion = function() { + Version.get({}, + function(res) { + $scope.version = res.version; + }); + }; + + $scope.version = _getVersion(); }); diff --git a/public/js/controllers/header.js b/public/js/controllers/header.js index 3b17ec51..673aa9a4 100755 --- a/public/js/controllers/header.js +++ b/public/js/controllers/header.js @@ -1,9 +1,11 @@ 'use strict'; angular.module('insight.system').controller('HeaderController', - function($scope, $rootScope, getSocket, Global, Block) { + function($scope, $rootScope, getSocket, Global, Block, Currency) { $scope.global = Global; + Currency.get(); + $scope.menu = [ { 'title': 'Blocks', diff --git a/public/js/directives.js b/public/js/directives.js index 3bd91db1..2d90b27d 100755 --- a/public/js/directives.js +++ b/public/js/directives.js @@ -3,7 +3,7 @@ var ZeroClipboard = window.ZeroClipboard; angular.module('insight') - .directive('whenScrolled', ['$window', function($window) { + .directive('whenScrolled', function($window) { return { restric: 'A', link: function(scope, elm, attr) { @@ -21,13 +21,14 @@ angular.module('insight') }; $window.on('scroll', handler); + scope.$on('$destroy', function() { return $window.off('scroll', handler); }); } }; - }]) - .directive('clipCopy', [function() { + }) + .directive('clipCopy', function() { ZeroClipboard.config({ moviePath: '/lib/zeroclipboard/ZeroClipboard.swf', trustedDomains: ['*'], @@ -54,4 +55,4 @@ angular.module('insight') }); } }; - }]); + }); diff --git a/public/js/services/currency.js b/public/js/services/currency.js new file mode 100644 index 00000000..1a9cc0eb --- /dev/null +++ b/public/js/services/currency.js @@ -0,0 +1,6 @@ +'use strict'; + +angular.module('insight.currency').factory('Currency', + function($resource) { + return $resource('/api/currency'); +}); diff --git a/public/views/includes/footer.html b/public/views/includes/footer.html index e87127e6..42e737c4 100644 --- a/public/views/includes/footer.html +++ b/public/views/includes/footer.html @@ -1,3 +1,9 @@
+
+ Currency: + USD - + BTC - + mBTC +
Insight API v{{version}}
diff --git a/public/views/transaction/tx.html b/public/views/transaction/tx.html index f3dc900d..0b7062e2 100644 --- a/public/views/transaction/tx.html +++ b/public/views/transaction/tx.html @@ -8,7 +8,7 @@
-
{{vin.reward}} BTC
+
{{$root.currency.getConversion(vin.reward)}}
No Inputs (Newly Generated Coins)
@@ -17,7 +17,7 @@
-
{{vin.value}} BTC
+
{{$root.currency.getConversion(vin.value)}}
{{vin.addr}} {{vin.addr}} @@ -27,7 +27,7 @@
-
{{vin.value}} BTC
+
{{$root.currency.getConversion(vin.value)}}
   {{vin.addr}} @@ -57,7 +57,7 @@
-
{{vout.value}} BTC
+
{{$root.currency.getConversion(vout.value)}}
{{vout.addr}} {{vout.addr}} @@ -67,7 +67,7 @@
-
{{vout.value}} BTC
+
{{$root.currency.getConversion(vout.value)}}
@@ -97,6 +97,6 @@
- +