Merge pull request #185 from colkito/feature/switch-currency
Feature/switch currency
This commit is contained in:
commit
9edf1afcb9
|
@ -0,0 +1,71 @@
|
|||
'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 XMLHttpRequest !== 'undefined' && XMLHttpRequest !== null) {
|
||||
return new XMLHttpRequest();
|
||||
} else if (typeof require !== 'undefined' && require !== null) {
|
||||
var XMLhttprequest = require('xmlhttprequest').XMLHttpRequest;
|
||||
return new XMLhttprequest();
|
||||
}
|
||||
};
|
||||
|
||||
var _request = function(url, cb) {
|
||||
var request;
|
||||
request = _xhr();
|
||||
request.open('GET', url, true);
|
||||
request.onreadystatechange = function() {
|
||||
if (request.readyState === 4) {
|
||||
if (request.status === 200) {
|
||||
return cb(false, request.responseText);
|
||||
}
|
||||
|
||||
return cb(true, {
|
||||
status: request.status,
|
||||
message: 'Request error'
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return request.send(null);
|
||||
};
|
||||
|
||||
// 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
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.jsonp({
|
||||
status: 200,
|
||||
data: {
|
||||
bitstamp: bitstampRate,
|
||||
delay: delay
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
|
@ -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')
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
|
|
@ -30,6 +30,10 @@ module.exports = function(app) {
|
|||
|
||||
app.get('/api/sync', st.sync);
|
||||
|
||||
// Currency
|
||||
var currency = require('../app/controllers/currency');
|
||||
app.get('/api/currency', currency.index);
|
||||
|
||||
//Home route
|
||||
var index = require('../app/controllers/index');
|
||||
app.get('/api/version', index.version);
|
||||
|
|
|
@ -78,7 +78,8 @@
|
|||
"sinon": "~1.7.3",
|
||||
"chai": "~1.8.1",
|
||||
"bitcore": "git://github.com/bitpay/bitcore.git",
|
||||
"bufferput": "git://github.com/bitpay/node-bufferput.git"
|
||||
"bufferput": "git://github.com/bitpay/node-bufferput.git",
|
||||
"xmlhttprequest": "~1.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"grunt-contrib-watch": "latest",
|
||||
|
|
|
@ -278,6 +278,11 @@ h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6 {
|
|||
overflow: hidden;
|
||||
}
|
||||
|
||||
#footer .currency a:active {
|
||||
color: #fff;
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
#footer a.insight {
|
||||
font-size: 20px;
|
||||
text-decoration: none;
|
||||
|
|
|
@ -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', []);
|
||||
|
|
|
@ -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();
|
||||
|
||||
});
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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')
|
|||
});
|
||||
}
|
||||
};
|
||||
}]);
|
||||
});
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('insight.currency').factory('Currency',
|
||||
function($resource) {
|
||||
return $resource('/api/currency');
|
||||
});
|
|
@ -1,3 +1,9 @@
|
|||
<div class="container" data-ng-controller="FooterController">
|
||||
<div class="pull-left m10v currency">
|
||||
<small>Currency:</small>
|
||||
<a href="#" data-ng-click="setCurrency('USD')">USD</a> -
|
||||
<a href="#" data-ng-click="setCurrency('BTC')">BTC</a> -
|
||||
<a href="#" data-ng-click="setCurrency('mBTC')">mBTC</a>
|
||||
</div>
|
||||
<a class="insight m10v pull-right" href="/">Insight <small>API v{{version}}</small></a>
|
||||
</div>
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<div class="col-md-5">
|
||||
<div class="row" data-ng-show="tx.isCoinBase">
|
||||
<div class="col-md-12 transaction-vin-vout" data-ng-repeat="vin in tx.vin">
|
||||
<div class="text-muted pull-right btc-value"><small>{{vin.reward}} BTC</small></div>
|
||||
<div class="text-muted pull-right btc-value"><small>{{$root.currency.getConversion(vin.reward)}}</small></div>
|
||||
<div class="ellipsis">
|
||||
<span>No Inputs (Newly Generated Coins)</span>
|
||||
</div>
|
||||
|
@ -17,7 +17,7 @@
|
|||
<div class="row" data-ng-show="!tx.isCoinBase">
|
||||
<div data-ng-repeat="vin in tx.vinSimple" data-ng-show="!itemsExpanded">
|
||||
<div class="col-md-12 transaction-vin-vout">
|
||||
<div class="text-muted pull-right btc-value" data-ng-class="{'text-danger': $root.currentAddr == vin.addr}"><small>{{vin.value}} BTC</small></div>
|
||||
<div class="text-muted pull-right btc-value" data-ng-class="{'text-danger': $root.currentAddr == vin.addr}"><small>{{$root.currency.getConversion(vin.value)}}</small></div>
|
||||
<div class="ellipsis">
|
||||
<span data-ng-show="vin.notAddr">{{vin.addr}}</span>
|
||||
<span class="text-muted" title="Current Bitcoin Address" data-ng-show="vin.addr == $root.currentAddr">{{vin.addr}}</span>
|
||||
|
@ -27,7 +27,7 @@
|
|||
</div>
|
||||
<div data-ng-repeat="vin in tx.vin" data-ng-show="itemsExpanded">
|
||||
<div class="col-md-12 transaction-vin-vout">
|
||||
<div class="text-muted pull-right btc-value"><small>{{vin.value}} BTC</small></div>
|
||||
<div class="text-muted pull-right btc-value"><small>{{$root.currency.getConversion(vin.value)}}</small></div>
|
||||
<div class="ellipsis">
|
||||
<a class="glyphicon glyphicon-chevron-right" href="/#!/tx/{{vin.txid}}" title="Outpoint: {{vin.txid}},{{vin.vout}}"></a>
|
||||
<span data-ng-show="vin.notAddr">{{vin.addr}}</span>
|
||||
|
@ -57,7 +57,7 @@
|
|||
<div class="row">
|
||||
<div data-ng-repeat="vout in tx.voutSimple" data-ng-show="!itemsExpanded">
|
||||
<div class="col-md-12 transaction-vin-vout">
|
||||
<div class="text-muted pull-right btc-value" data-ng-class="{'text-success': $root.currentAddr == vout.addr}"><small>{{vout.value}} BTC</small></div>
|
||||
<div class="text-muted pull-right btc-value" data-ng-class="{'text-success': $root.currentAddr == vout.addr}"><small>{{$root.currency.getConversion(vout.value)}}</small></div>
|
||||
<div class="ellipsis">
|
||||
<span data-ng-show="vout.notAddr">{{vout.addr}}</span>
|
||||
<span class="text-muted" title="Current Bitcoin Address" data-ng-show="address == $root.currentAddr" data-ng-repeat="address in vout.addr.split(',')">{{vout.addr}}</span>
|
||||
|
@ -67,7 +67,7 @@
|
|||
</div>
|
||||
<div data-ng-repeat="vout in tx.vout" data-ng-show="itemsExpanded">
|
||||
<div class="col-md-12 transaction-vin-vout">
|
||||
<div class="text-muted pull-right btc-value"><small>{{vout.value}} BTC</small></div>
|
||||
<div class="text-muted pull-right btc-value"><small>{{$root.currency.getConversion(vout.value)}}</small></div>
|
||||
<div class="ellipsis">
|
||||
<a href="/address/{{address}}" data-ng-repeat="address in vout.scriptPubKey.addresses">{{address}}</a>
|
||||
</div>
|
||||
|
@ -97,6 +97,6 @@
|
|||
<div class="pull-right">
|
||||
<button data-ng-show="tx.confirmations" class="btn btn-success">{{tx.confirmations}} Confirmations</button>
|
||||
<button data-ng-show="!tx.confirmations" class="btn btn-danger">Unconfirmed Transaction!</button>
|
||||
<button class="btn btn-primary">{{tx.valueOut}} BTC</button>
|
||||
<button class="btn btn-primary">{{$root.currency.getConversion(tx.valueOut)}}</button>
|
||||
</div>
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue