keva-stratum/www/script.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-12-06 14:25:07 +00:00
HandlebarsIntl.registerWith(Handlebars);
$(function() {
2017-03-26 22:54:04 +00:00
switch (location.hash) {
case '#blocks':
window.homeTab = false;
break;
default:
window.homeTab = true;
}
2016-12-06 14:25:07 +00:00
var userLang = (navigator.language || navigator.userLanguage) || 'en-US';
window.intlData = { locales: userLang };
2017-03-26 22:54:04 +00:00
var statsSource = $("#stats-template").html();
var statsTemplate = Handlebars.compile(statsSource);
var blocksSource = $("#blocks-template").html();
var blocksTemplate = Handlebars.compile(blocksSource);
refreshStats(statsTemplate, blocksTemplate);
2016-12-06 14:25:07 +00:00
2017-03-26 22:54:04 +00:00
$('#homeTab').on('click', function() {
window.homeTab = true;
refreshStats(statsTemplate, blocksTemplate);
});
$('#blocksTab').on('click', function() {
window.homeTab = false;
refreshStats(statsTemplate, blocksTemplate);
});
2016-12-06 14:25:07 +00:00
setInterval(function() {
2017-03-26 22:54:04 +00:00
refreshStats(statsTemplate, blocksTemplate);
2016-12-06 14:25:07 +00:00
}, 5000)
});
2017-03-26 22:54:04 +00:00
function refreshStats(statsTemplate, blocksTemplate) {
2016-12-06 14:25:07 +00:00
$.getJSON("/stats", function(stats) {
$("#alert").addClass('hide');
// Sort miners by ID
if (stats.miners) {
2017-03-26 22:54:04 +00:00
stats.miners = stats.miners.sort(compareMiners);
}
// Reverse sort blocks by height
if (stats.blocks) {
stats.blocks = stats.blocks.sort(compareBlocks);
}
var html = null;
$('.nav-pills > li').removeClass('active');
if (window.homeTab) {
html = statsTemplate(stats, { data: { intl: window.intlData } });
$('.nav-pills > li > #homeTab').parent().addClass('active');
} else {
html = blocksTemplate(stats, { data: { intl: window.intlData } });
$('.nav-pills > li > #blocksTab').parent().addClass('active');
2016-12-06 14:25:07 +00:00
}
$('#stats').html(html);
}).fail(function() {
$("#alert").removeClass('hide');
});
}
2017-03-26 22:54:04 +00:00
function compareMiners(a, b) {
2016-12-06 14:25:07 +00:00
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
}
2017-03-26 22:54:04 +00:00
function compareBlocks(a, b) {
if (a.height > b.height)
return -1;
if (a.height < b.height)
return 1;
return 0;
}