-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
211 lines (176 loc) · 5.51 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var blockchainExplorer = "?hash={id}#blockchain_block";
var transactionExplorer = "?hash={id}#blockchain_transaction";
var paymentIdExplorer = "?hash={id}#blockchain_payment_id";
var style_cookie_name = "style";
var style_cookie_duration = 365;
var style_domain = window.location.hostname;
var renderDate = function(d) {
return d.getFullYear() + '-' +
('0' + (d.getMonth() + 1)).slice(-2) + '-' +
('0' + d.getDate()).slice(-2) + ' ' +
('0' + d.getHours()).slice(-2) + ':' +
('0' + d.getMinutes()).slice(-2);
};
$(document).ready(function() {
if (donationAddress !== undefined && donationAddress !== "") {
$("#donations").show();
$("#donationAddress").html(donationAddress);
}
});
function getTransactionUrl(id) {
return transactionExplorer.replace('{symbol}', symbol.toLowerCase()).replace('{id}', id);
}
$.fn.update = function(txt){
var el = this[0];
if (el.textContent !== txt)
el.textContent = txt;
return this;
};
function updateTextClasses(className, text){
var els = document.getElementsByClassName(className);
for (var i = 0; i < els.length; i++){
var el = els[i];
if (el.textContent !== text)
el.textContent = text;
}
}
function updateText(elementId, text){
var el = document.getElementById(elementId);
if (el.textContent !== text){
el.textContent = text;
}
return el;
}
function updateTextLinkable(elementId, text){
var el = document.getElementById(elementId);
if (el.innerHTML !== text){
el.innerHTML = text;
}
return el;
}
var currentPage;
var lastStats;
var numberFormatter = new Intl.NumberFormat('en-US'); // US formatting, force commas.
function localizeNumber(number) {
return numberFormatter.format(number);
}
function getReadableHashRateString(hashrate){
var i = 0;
var byteUnits = [' H', ' kH', ' MH', ' GH', ' TH', ' PH', ' EH', ' ZH', ' YH' ];
while (hashrate > 1000){
hashrate = hashrate / 1000;
i++;
}
return localizeNumber(hashrate.toFixed(2)) + byteUnits[i];
}
function getReadableDifficultyString(difficulty, precision){
if (isNaN(parseFloat(difficulty)) || !isFinite(difficulty)) return 0;
if (typeof precision === 'undefined') precision = 0;
var units = ['', 'k', 'M', 'G', 'T', 'P'],
number = Math.floor(Math.log(difficulty) / Math.log(1000));
if (units[number] === undefined || units[number] === null) {
return 0
}
return localizeNumber((difficulty / Math.pow(1000, Math.floor(number))).toFixed(precision)) + ' ' + units[number];
}
function formatBlockLink(hash){
return '<a href="' + getBlockchainUrl(hash) + '">' + hash + '</a>';
}
function getReadableCoins(coins, digits, withoutSymbol){
var amount = (parseInt(coins || 0) / coinUnits).toFixed(digits || coinUnits.toString().length - 1);
return localizeNumber(amount) + (withoutSymbol ? '' : (' ' + symbol));
}
function formatDate(time){
if (!time) return '';
return renderDate(new Date(parseInt(time) * 1000));
}
function formatPaymentLink(hash){
return '<a href="' + getTransactionUrl(hash) + '">' + hash + '</a>';
}
function pulseLiveUpdate(){
var stats_update = document.getElementById('stats_updated');
stats_update.style.transition = 'opacity 100ms ease-out';
stats_update.style.opacity = 1;
setTimeout(function(){
stats_update.style.transition = 'opacity 7000ms linear';
stats_update.style.opacity = 0;
}, 500);
}
window.onhashchange = function(){
routePage();
};
function fetchLiveStats() {
$.ajax({
url: api + '/getinfo',
dataType: 'json',
type: 'GET',
cache: 'false'
}).done(function(data){
pulseLiveUpdate();
lastStats = data;
currentPage.update();
}).always(function () {
setTimeout(function() {
fetchLiveStats();
}, refreshDelay);
});
}
function floatToString(float) {
return float.toFixed(6).replace(/[0\.]+$/, '');
}
var xhrPageLoading;
function routePage(loadedCallback) {
if (currentPage) currentPage.destroy();
$('#page').html('');
$('#loading').show();
if (xhrPageLoading)
xhrPageLoading.abort();
$('.hot_link').parent().removeClass('active');
var $link = $('a.hot_link[href="' + (window.location.hash || '#') + '"]');
$link.parent().addClass('active');
var page = $link.data('page');
xhrPageLoading = $.ajax({
url: 'pages/' + page,
cache: false,
success: function (data) {
$('#loading').hide();
$('#page').show().html(data);
currentPage.init();
currentPage.update();
if (loadedCallback) loadedCallback();
}
});
}
function getBlockchainUrl(id) {
return blockchainExplorer.replace('{id}', id);
}
$(function(){
$.get(api + '/getinfo', function(data){
try {
lastStats = JSON.parse(data);
} catch(e) {
lastStats = data;
}
routePage(fetchLiveStats);
});
});
// Blockexplorer functions
urlParam = function(name){
var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (results==null){
return null;
}
else{
return results[1] || 0;
}
}
$(function() {
$('[data-toggle="tooltip"]').tooltip();
});
function hex2a(hexx) {
var hex = hexx.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}