-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
114 lines (104 loc) · 5.27 KB
/
index.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
let gController = null; // gController useful just for easy debug
(function () {
'use strict';
document.addEventListener('DOMContentLoaded', () => {
const ClassController = function (theNow) {
const self = this; // eslint-disable-line unicorn/no-this-assignment
self.util = {
createElement(arg) {
const ret = document.createElement(arg[0]);
if ((typeof undefined !== typeof arg[1]) && (arg[1] !== null)) {
Object.keys(arg[1]).forEach(theProperty => {
ret[theProperty] = arg[1][theProperty];
});
}
if ((typeof undefined !== typeof arg[2]) && (arg[2] !== null)) {
arg[2].forEach(theClass => ret.classList.add(theClass));
}
return ret;
},
};
self.model = gModel; // eslint-disable-line no-undef
self.jsonToMatrix = json => json.map(element => Object.keys(element).map(key => element[key]));
self.matrixToTable = (header, matrix, applyOnElement) => {
const domTable = self.util.createElement(['table']);
const domHeader = self.util.createElement(['tr']);
header.forEach(value => domHeader.append(self.util.createElement(['th', {textContent: value}])));
domTable.append(domHeader);
matrix.forEach(row => {
const domRow = self.util.createElement(['tr']);
row.forEach((value, i) => domRow.append(applyOnElement(row, self.util.createElement(['td', {textContent: value}, ['col_' + i]]), i)));
domTable.append(domRow);
if (typeof (row[5]) !== 'undefined') {
domRow.classList.add('penalita');
}
});
return domTable;
};
self.convert = value => self.model.constants.satoshiPerBitcoin / value;
self.showFloat = value => Number.parseFloat(value).toFixed(2);
self.getOrDefault = (params, field, defaultValue) => {
let ret = params.get(field);
if (ret === null) {
ret = defaultValue;
}
return ret;
};
// with Number.isNaN instead of isNaN we have different wrong result.
self.infinityIfIsNaN = value => [value, Number.POSITIVE_INFINITY][Number(Number.isNaN(Number(value)) === true)];
self.show = () => {
const url = new URL(location);
const path = url.origin + url.pathname;
console.log(path);
const params = url.searchParams;
self.model.current.annoGenesi = self.getOrDefault(params, 'picco', self.model.constants.defaultAnnoGenesi);
const favicon = document.querySelector('#favicon');
const navigators = Object.keys(self.model.picco).map(key => ['a', {href: path + '?picco=' + key, textContent: '#picco' + key}, ['navigator']]);
try{
favicon.setAttribute('href', 'favicons/' + self.model.picco[self.model.current.annoGenesi].favicon);
const domHeader = self.util.createElement(['div', null, ['header']]);
[['h1', {id: 'title', textContent: self.model.picco[self.model.current.annoGenesi].title}],
['h2', {id: 'sub-title', textContent: self.model.picco[self.model.current.annoGenesi].subTitle}]].forEach(dom => domHeader.append(self.util.createElement(dom)));
// document.title = self.model.picco[self.model.current.annoGenesi].title;
self.model.picco[self.model.current.annoGenesi].data.sort((a, b) => self.infinityIfIsNaN(b[self.model.constants.fieldSatoshiEuro]) - self.infinityIfIsNaN(a[self.model.constants.fieldSatoshiEuro]));
const domMatrix = self.matrixToTable(
['indice', 'nome', self.model.constants.fieldSatoshiEuro, 'telegram-id', '€/₿', 'penalità'],
self.jsonToMatrix(self.model.picco[self.model.current.annoGenesi].data).map((row, i) => [(i + 1), row[0], self.showFloat(row[1]), row[2], self.showFloat(self.convert(row[1])), row[3]]),
(row, domElement, i) => {
if (self.infinityIfIsNaN(row[self.model.constants.columnSatoshiPerBitcoinIndex]) > self.model.picco[self.model.current.annoGenesi].minValue) {
const classes = ['lost', 'lost-element'];
domElement.classList.add(classes[Number(i === self.model.constants.columnSatoshiPerBitcoinIndex)]);
}
return domElement;
},
);
const domFooter = self.util.createElement(['div', null, ['footer']]);
[
['div', {textContent: 'anno genesi ' + self.model.current.annoGenesi}],
['div', {textContent: 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks'}, ['genesis']],
['div', {textContent: theNow}],
['a', {textContent: 'GitHub: picco-bitcoin', href: self.model.constants.repository}],
['div', {textContent: self.model.constants.p2pkh}],
].concat(navigators)
.concat([['h1', {textContent: '#p' + self.model.current.annoGenesi}]])
.forEach(dom => domFooter.append(self.util.createElement(dom)));
const domContainer = self.util.createElement(['div', null, ['container']]);
// domContainer.append ([domHeader, domMatrix, domFooter]);
[domHeader, domMatrix, domFooter].forEach(dom => domContainer.append(dom));
document.body.append(domContainer);
}
catch (error){
// alert (error);
const $error = self.util.createElement(['div', {textContent: error}])
const $h1 = self.util.createElement(['h1', {textContent: '#p' + self.model.current.annoGenesi}])
const domContainer = self.util.createElement(['div', null, ['container']]);
domContainer.append ($h1);
domContainer.append ($error);
document.body.append(domContainer);
}
};
};
gController = new ClassController(new Date());
gController.show();
});
})();