-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
414 lines (365 loc) · 15.5 KB
/
index.html
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Cryptoportfolio - local</title>
<style>
/* #region CSS RELATED TO PORTFOLIO DISPLAY */
/* Change mouse cursor to hand when hovering over table headings*/
#portfolio-table th {
cursor: pointer;
}
/* Align everything in the table header except the one with id=coin-name to the right*/
#portfolio-table th:not(#coin-name) {
text-align: right;
}
/*Sort arrows*/
#portfolio-table .arrows-name {
display: inline-block
}
#portfolio-table .triangle-down {
display: inline-block;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-top: 8px solid #555;
}
#portfolio-table .triangle-up {
display: inline-block;
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 8px solid #555;
}
/* Align text in table cell with id=coin-name to the left */
#coin-name {
text-align: left;
}
/* Select all td elements except those, who are the first child of something */
#portfolio-table td:not(:first-child) {
text-align: right;
}
.cryptoUp {
color: green;
}
.cryptoDown {
color: red;
}
#portfolio-table thead,
tfoot {
background-color: #F0F8FF;
font-weight: bold;
}
#portfolio-table tr:nth-child(even) {
background-color: #F0F0F0;
}
#portfolio-table {
margin: 0 auto;
width: 100%;
}
/*Set width to 70%*/
@media (min-width: 700px) {
#portfolio-table {
width: 70%;
}
}
/* #endregion */
</style>
</head>
<body>
<div id="display-portfolio"></div>
</body>
<script>
const portfolio = {
"fiat": "EUR",
"token":
[
{
"cryptoTicker": "BTC",
"cryptoName": "Bitcoin",
"cryptoQty": "0.5",
"cryptoInvestedSum": "4000"
},
{
"cryptoTicker": "ADA",
"cryptoName": "Cardano",
"cryptoQty": "25000",
"cryptoInvestedSum": "1000"
},
{
"cryptoTicker": "ETH",
"cryptoName": "Ethereum",
"cryptoQty": "15",
"cryptoInvestedSum": "1500"
},
{
"cryptoTicker": "XMR",
"cryptoName": "Monero",
"cryptoQty": "50",
"cryptoInvestedSum": "1000"
}
]
};
const fiats = [
{ code: "AUD", name: "Australian Dollar", symbol: "$" },
{ code: "BRL", name: "Brazilian real", symbol: "R$" },
{ code: "CAD", name: "Canadian dollar", symbol: "$" },
{ code: "CHF", name: "Swiss franc", symbol: "Fr." },
{ code: "CLP", name: "Chilean peso", symbol: "$" },
{ code: "CNY", name: "Chinese yuan", symbol: "¥" },
{ code: "CZK", name: "Czech koruna", symbol: "Kč" },
{ code: "DKK", name: "Danish krone", symbol: "kr" },
{ code: "EUR", name: "Euro", symbol: "€" },
{ code: "GBP", name: "Pound sterling", symbol: "£" },
{ code: "HKD", name: "Hong Kong dollar", symbol: "$" },
{ code: "HUF", name: "Hungarian forint", symbol: "Ft" },
{ code: "IDR", name: "Indonesian rupiah", symbol: "Rp" },
{ code: "ILS", name: "Israeli new shekel", symbol: "₪" },
{ code: "INR", name: "Indian rupee", symbol: "₹" },
{ code: "JPY", name: "Japanese yen", symbol: "¥" },
{ code: "KRW", name: "South Korean won", symbol: "₩" },
{ code: "MXN", name: "Mexican peso", symbol: "$" },
{ code: "MYR", name: "Malaysian ringgit", symbol: "RM" },
{ code: "NOK", name: "Norwegian krone", symbol: "kr" },
{ code: "NZD", name: "New Zealand dollar", symbol: "$" },
{ code: "PHP", name: "Philippine peso", symbol: "₱" },
{ code: "PKR", name: "Pakistani rupee", symbol: "Rs" },
{ code: "PLN", name: "Polish złoty", symbol: "zł" },
{ code: "RUB", name: "Russian ruble", symbol: "₽" },
{ code: "SEK", name: "Swedish krona", symbol: "kr" },
{ code: "SGD", name: "Singapore dollar", symbol: "S$" },
{ code: "THB", name: "Thai baht", symbol: "฿" },
{ code: "TRY", name: "Turkish lira", symbol: "₺" },
{ code: "TWD", name: "New Taiwan dollar", symbol: "NT$" },
{ code: "USD", name: "United States dollar", symbol: "$" },
{ code: "ZAR", name: "South African rand", symbol: "R" }
];
// Download current coin prices from external API. Then call the displayPortfolio function
function downloadCoinPrices() {
//Array "allTickers" is filled with all crypto tickers.
var allTickers = portfolio.token.map(item => item.cryptoTicker);
//console.log('allTicker: ' + allTickers);
//Create API request URL
var coinAPI = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=" + allTickers + "&tsyms=" + portfolio.fiat;
//console.log('coinAPI: ' + coinAPI);
//Hide fetch and undhide 2 lines below to show portfolio with locally saved demo prices
// coinPrices = coinpricesExample;
// displayPortfolio();
///*
fetch(coinAPI)
.then(handleErrors)
.then(res => {
//console.log(res.clone());//res.json() cannot be used twice in the callback. Thus res.clone() (see https://stackoverflow.com/q/46742251/5263954)
return res.json();
})
.then(prices => {
//console.log(prices);
coinPrices = prices;
displayPortfolio();
// createChartData();
//Once tables are fully rendered run the translate function to translate the app)
// let index = languages.indexOf(userLang);
// changeAppLanguage(index);
})
.catch(error => {
console.error('There was an error while downloading coin prices:', error.message);
});
//*/
}
//Handle errors from fetch operation
function handleErrors(response) {
//console.log(response);
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
// Display portfolio on page
function displayPortfolio() {
var myContainer = document.getElementById("display-portfolio");
//GET-Anfrage an cryptocompare API
var thisHTML = "";
thisHTML += "<table id='portfolio-table'>";
thisHTML += "<thead>";
thisHTML += "<tr>";
thisHTML += "<th id='coin-name' onclick='sortTable(0)' title='Name of Cryptocurrency/Token. Click here to sort alphabetically'>Name <div class='arrows-name'><div title='Descending'></div><div class='triangle-up' title='Ascending'></div></div></th>";
thisHTML += "<th id='last-price' onclick='sortTable(1)' title='Last price. Click here to sort.'>Last <div class='arrows-name'><div class='triangle-down' title='Descending'></div><div class='triangle-up' title='Ascending'></div></div></th>";
thisHTML += "<th id='change-perc' onclick='sortTable(2)' title='Change in percent in the last 24h. Click here to sort.'>Change <div class='arrows-name'><div class='triangle-down' title='Descending'></div><div class='triangle-up' title='Ascending'></div></div></th>";
thisHTML += "<th id='gain-loss-24h' onclick='sortTable(3)' title='Gain/loss in the last 24h. Click here to sort.'>G/L Today <div class='arrows-name'><div class='triangle-down' title='Descending'></div><div class='triangle-up' title='Ascending'></div></div></th>";
thisHTML += "<th id='gain-loss' onclick='sortTable(4)' title='Overall gain/loss. Click here to sort.'>Gain / Loss <div class='arrows-name'><div class='triangle-down' title='Descending'></div><div class='triangle-up' title='Ascending'></div></div></th>";
thisHTML += "</tr>";
thisHTML += "</thead>";
var i = 0;
var overallGainLoss = 0;
var overallGainLossToday = 0;
var cryptoStyle = "class='cryptoUp'";
var fiatSymbol = fiats.find(i => i.code === portfolio.fiat).symbol;
thisHTML += "<tbody>";
thisHTML += "<tr>";
for (i = 0; i < Object.keys(coinPrices['RAW']).length; i++) {
var cryptoTicker = portfolio.token[i].cryptoTicker;
var fullName = portfolio.token[i].cryptoName;
var cryptoQty = portfolio.token[i].cryptoQty;
var cryptoInvestedSum = portfolio.token[i].cryptoInvestedSum;
var lastPrice = coinPrices['RAW'][cryptoTicker][portfolio.fiat]['PRICE'];
var changePct24H = coinPrices['RAW'][cryptoTicker][portfolio.fiat]['CHANGEPCT24HOUR'];
var change24H = coinPrices['RAW'][cryptoTicker][portfolio.fiat]['CHANGE24HOUR'];
// var fiatSymbol = coinPrices['DISPLAY'][cryptoTicker][portfolio.fiat]['TOSYMBOL'];
var cryptoGainLoss = 0;
var cryptoGainLossToday = 0;
var cryptoBuyingPrice = 0;
//Some values on CryptoCompare return "null". That would break ".toFixed" below:
if (changePct24H != null) {
changePct24H = changePct24H.toFixed(1).concat(" %"); //Example: 2.36: toFixed: Only one digit after the comma (2.4). Concat: Add percentage sign (2.4%).
}
if (cryptoQty != 0) {
cryptoBuyingPrice = cryptoInvestedSum / cryptoQty;
cryptoGainLoss = cryptoQty * (lastPrice - cryptoBuyingPrice);
cryptoGainLossToday = cryptoQty * change24H;
overallGainLoss += cryptoGainLoss;
overallGainLossToday += cryptoGainLossToday;
}
//Code below makes sure that there are not too many digits after the comma. parseFloat is necessary, because lastPrice is apparently considered a string and toFixed only works with numbers.
if (lastPrice < 0.1) {
lastPrice = parseFloat(lastPrice).toFixed(3);
} else if (lastPrice < 10) {
lastPrice = parseFloat(lastPrice).toFixed(2);
} else {
lastPrice = parseFloat(lastPrice).toFixed(0);
}
thisHTML += "<td><a href='https://coinmarketcap.com/currencies/" + fullName.toLowerCase() + "' target=_blank>" + fullName + "</a></td>";
thisHTML += "<td>" + lastPrice + " " + fiatSymbol + "</td>";
//Change color of changePct24H and cryptoGainLossToday number according to value (-, 0, + -> red, black, green)
cryptoStyle = redOrGreen(change24H);
thisHTML += "<td " + cryptoStyle + ">" + changePct24H + "</td>";
thisHTML += "<td " + cryptoStyle + ">" + cryptoGainLossToday.toFixed(0) + " " + fiatSymbol + "</td>";
thisHTML += "<td " + redOrGreen(cryptoGainLoss) + ">" + cryptoGainLoss.toFixed(0) + " " + fiatSymbol + "</td>";
thisHTML += "</tr>";
}
thisHTML += "</tbody>";
thisHTML += "<tfoot>";
thisHTML += "<tr>";
thisHTML += "<td>Total</td>";
thisHTML += "<td></td>";
thisHTML += "<td></td>";
thisHTML += "<td " + redOrGreen(overallGainLossToday) + ">" + add1000Separators(overallGainLossToday.toFixed(0)) + " " + fiatSymbol + "</td>";
thisHTML += "<td " + redOrGreen(overallGainLoss) + ">" + add1000Separators(overallGainLoss.toFixed(0)) + " " + fiatSymbol + "</td>";
thisHTML += "</tr>";
thisHTML += "</tfoot>";
thisHTML += "</table>";
myContainer.innerHTML = thisHTML;
}
// Sort DISPLAY PORTFOLIO table by clicking on the table headers
function sortTable(n) {
var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0, xGreaterThanY;
table = document.getElementById("portfolio-table");
switching = true;
var arrowDown = document.getElementsByClassName("arrows-name")[n].firstElementChild;
var arrowUp = document.getElementsByClassName("arrows-name")[n].lastElementChild;
//Set the sorting direction to ascending:
dir = "asc";
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers and the
last - table footers):*/
for (i = 1; i < (rows.length - 2); i++) {
//start by saying there should be no switching:
shouldSwitch = false;
xGreaterThanY = false;
/*Get the two elements you want to compare,
one from current row and one from the next:
n=0: Compare strings
n>0: Compare numbers*/
if (n === 0) { //Compare strings
x = rows[i].getElementsByTagName("TD")[n].innerText.toLowerCase();
y = rows[i + 1].getElementsByTagName("TD")[n].innerText.toLowerCase();
if (x > y) xGreaterThanY = true;
} else { //Compare numbers
x = rows[i].getElementsByTagName("TD")[n].innerText.replace(/[^-0-9.]/g, '');//Regex: Remove everything except numbers, "-" and ".",e.g. "-35.6 %" --> "-35.6"
y = rows[i + 1].getElementsByTagName("TD")[n].innerText.replace(/[^-0-9.]/g, '');
if (Number(x) > Number(y)) xGreaterThanY = true;
}
/*check if the two rows should switch place,
based on the direction, asc or desc:*/
if (dir == "asc") {
//xGreaterThanY = true: switch rows!
if (xGreaterThanY) {
shouldSwitch = true;
break;
}
} else if (dir == "desc") {
//if xGreaterThanY = false AND the 2 numbers are not equal: switch rows! 2nd condition important because if numbers are equal there would be an infinite loop
if (!xGreaterThanY && Number(x) !== Number(y)) {
shouldSwitch = true;
break;
}
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
//Each time a switch is done, increase this count by 1:
switchcount++;
} else {
/*If no switching has been done AND the direction is "asc",
set the direction to "desc" and run the while loop again.*/
if (switchcount == 0 && dir == "asc") {
dir = "desc";
switching = true;
}
}
}
//Set the display of arrows according to "asc" or "desc"
showAllArrows(); //Show all arrows in the header, which might have been hidden during previous sort operation
if (dir == "asc") arrowDown.classList.remove("triangle-down"); //Change display to UP arrow only.
else if (dir == "desc") arrowUp.classList.remove("triangle-up"); //Change display to DOWN arrow only.
}
//Show all sort arrows
function showAllArrows() {
var allArrows = document.getElementsByClassName("arrows-name");
for (let i = 0; i < allArrows.length; i++) {
var arrowDown = document.getElementsByClassName("arrows-name")[i].firstElementChild;
var arrowUp = document.getElementsByClassName("arrows-name")[i].lastElementChild;
arrowDown.classList.add("triangle-down");
arrowUp.classList.add("triangle-up");
}
}
// If value fell: red, value rose: green
function redOrGreen(value) {
var cryptoStyle;
if (value == 0) {
cryptoStyle = "";
} else if (value < 0) {
cryptoStyle = "class='cryptoDown'";
} else {
cryptoStyle = "class='cryptoUp'";
}
return cryptoStyle;
}
//Add a dot as thousand separator
function add1000Separators(nStr) {
//Example: nStr = 1427900000.45
nStr += ''; //Convert input to string
const x = nStr.split('.');//Split string at . into arrays [ '1427900000', '45' ]
let x1 = x[0]; // 1427900000
const x2 = x.length > 1 ? ',' + x[1] : ''; //,45
const rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + '.' + '$2'); // 1427900.000 --> 1427.900.000 --> 1.427.900.000
}
return x1 + x2; // 1.427.900.000,45
}
downloadCoinPrices();
</script>
</html>