-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrols.js
More file actions
147 lines (110 loc) · 4.25 KB
/
Copy pathcontrols.js
File metadata and controls
147 lines (110 loc) · 4.25 KB
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
// *** GLOBAL VARIABLE DECLARATION *** //
var wsURI = "wss://ws.blockchain.info/inv";
var firstRun = true;
var counterTX = 0;
var counterBlocks = 0;
var zero, time, timeHr, timeMin, timeSec, cleanTime;
var qInfoHidden = true;
var donateQRHidden = true;
var webSocketOpen = false;
var continentsData = { NA: 1, SA: 2, EU: 3, AF: 4, AS: 2, OC: 1 };
// *** WEBSOCKET FUNCTIONS *** //
function initWebSocket() {
websocket = new WebSocket(wsURI);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt) {
// Needed for updateClock func
zero = Date.now() / 1000 |0;
webSocketOpen = true;
document.getElementById('pageStatus').innerHTML = "Connected to Bitcoin network!";
websocket.send('{"op":"unconfirmed_sub"}');
websocket.send('{"op":"blocks_sub"}');
if (firstRun) initLogo();
firstRun = false;
}
function onClose(evt) {
initWebSocket(); // Reinitialize WebSocket
}
function onMessage(evt) {
// Parse JSON string into JSON object
var wsData = JSON.parse(evt.data);
// Call tx or block function based on response
if (wsData.op == "utx") handleOP(wsData);
if (wsData.op == "block") handleBlock(wsData);
}
function onError(evt) {
// Do nothing
}
// *** TIER II FUNCTIONS *** //
function handleOP(wsData) {
// Parse IP address into integer representation and query postgreSQL CDDB for location info
var tempIPtoStrArray = wsData.x.relayed_by.split(".");
var tempIPtoIntArray = tempIPtoStrArray.map(function(x) { return parseInt(x, 10); });
var tempIntIpAdd = (16777216*tempIPtoIntArray[0])+(65536*tempIPtoIntArray[1])+(256*tempIPtoIntArray[2])+tempIPtoIntArray[3];
jspgSetOption("output_type","json");
jspgQuery(tempIntIpAdd, wsData);
// Let other functions know if errors occur
// if (!jsonLoc.hasOwnProperty("locid_del")) doWhat?
// manageNewTX() will be called from jspgsql.js
}
function handleBlock(wsData) {
// When new block is found, start animation and update user display
animateNewBlock();
counterBlocks++;
document.getElementById('pageBlocks').innerHTML = counterBlocks;
}
function manageNewTX(ajaxResp, wsData) {
// Instantiate new TX and plot on map after checking for errors
var newTX = new TX(ajaxResp, wsData);
if (newTX.hasError) return;
// Map TX on mainCanvas
newTX.mapNode();
newTX.ripple();
newTX.heatNet();
newTX.sideLine();
updateExternalDisplays(newTX);
}
function toggleDivDisplay(el, elHidden) {
// Both el and elHidden must be passed as strings
if (window[elHidden]) {
document.getElementById(el).style.visibility = 'visible';
window[elHidden] = false;
} else {
document.getElementById(el).style.visibility = 'hidden';
window[elHidden] = true;
}
}
function updateExternalDisplays(tx) {
// Update TX-specific info in respective divs for user display
counterTX++;
document.getElementById('pageNumofTX').innerHTML = counterTX;
document.title = "Bitcoin-RT [" + counterTX + " tx]";
var txText = "TX Hash: <b>" + tx.hash.substring(0,15) + "</b><br />Relay IP: <b>" + tx.ipAddress + "</b><br />Location: <b>"
+ cleanStringLocation(tx) + "</b><br />Lat, Long: <b>" + tx.latitude + ", " + tx.longitude + "</b>";
document.getElementById('pageStatus').innerHTML = txText;
}
function cleanStringLocation(tx) {
// Prettify tx location strings and concatenate
var cleanString;
cleanString = (tx.city !== "") ? tx.city + ", " : "Unidentified city in ";
cleanString = ((tx.country == "USA") && (tx.region !== "")) ? cleanString + tx.region + ", " + tx.country : cleanString + tx.country;
cleanString = ((tx.ipName == "") || (tx.ipName == "null")) ? cleanString : cleanString + " (" + tx.ipName + ")";
return cleanString;
}
function updateClock() {
// Update page clock
if (!webSocketOpen) return;
// There has to be an easier way to do this - why the fuck don't we have metric time yet?
time = (Date.now() / 1000 |0) - zero;
timeHr = time / 3600 |0;
timeMin = (time % 3600) / 60 |0;
timeSec = (time % 3600) % 60;
timeMin = (timeMin < 10) ? "0" + timeMin : timeMin;
timeSec = (timeSec < 10) ? "0" + timeSec : timeSec;
cleanTime = timeHr + ":" + timeMin + ":" + timeSec;
document.getElementById('pageClock').innerHTML = cleanTime;
}