insight-ui-zcash/public/js/controllers/index.js

75 lines
1.7 KiB
JavaScript
Raw Normal View History

2014-01-06 12:34:25 -08:00
'use strict';
2014-01-14 14:42:38 -08:00
var TRANSACTION_DISPLAYED = 5;
var BLOCKS_DISPLAYED = 5;
angular.module('insight.system').controller('IndexController',
function($scope, $rootScope, Global, getSocket, Blocks, Block, Transactions, Transaction) {
2014-01-06 13:01:23 -08:00
$scope.global = Global;
2014-01-15 07:44:17 -08:00
var _getTransaction = function(txid) {
2014-01-21 18:15:44 -08:00
Transaction.get({
txId: txid
}, function(res) {
$scope.txs.unshift(res);
});
};
var _getBlock = function(hash) {
2014-01-21 18:15:44 -08:00
Block.get({
blockHash: hash
}, function(res) {
$scope.blocks.unshift(res);
});
};
var socket = getSocket($scope);
socket.emit('subscribe', 'inv');
//show errors
$scope.flashMessage = $rootScope.flashMessage || null;
2014-01-15 07:44:17 -08:00
socket.on('tx', function(tx) {
2014-01-21 18:15:44 -08:00
var txStr = tx.txid.toString();
2014-01-15 04:38:27 -08:00
console.log('Transaction received! ' + JSON.stringify(tx));
if (parseInt($scope.txs.length, 10) > parseInt(TRANSACTION_DISPLAYED, 10) - 1) {
2014-01-14 14:42:38 -08:00
$scope.txs.pop();
}
_getTransaction(txStr);
2014-01-14 14:42:38 -08:00
});
2014-01-15 07:44:17 -08:00
socket.on('block', function(block) {
2014-01-21 18:15:44 -08:00
var blockHash = block.hash.toString();
2014-01-15 04:38:27 -08:00
console.log('Block received! ' + JSON.stringify(block));
if (parseInt($scope.blocks.length, 10) > parseInt(BLOCKS_DISPLAYED, 10) - 1) {
2014-01-21 18:15:44 -08:00
$scope.blocks.pop();
}
_getBlock(blockHash);
2014-01-13 13:13:41 -08:00
});
$scope.humanSince = function(time) {
var m = moment.unix(time);
return m.max().fromNow();
2014-01-15 11:05:26 -08:00
};
2014-01-15 07:44:17 -08:00
$scope.index = function() {
Blocks.get({
limit: BLOCKS_DISPLAYED
}, function(res) {
$scope.blocks = res.blocks;
2014-01-21 18:15:44 -08:00
$scope.blocksLength = res.lenght;
2014-01-15 07:44:17 -08:00
});
Transactions.get({
2014-01-15 07:44:17 -08:00
limit: TRANSACTION_DISPLAYED
}, function(res) {
$scope.txs = res.txs;
2014-01-15 07:44:17 -08:00
});
};
$scope.txs = [];
2014-01-14 14:42:38 -08:00
$scope.blocks = [];
2014-01-20 13:09:18 -08:00
});