forked from matteodanelli/MMM-cryptocurrency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-cryptocurrency.js
281 lines (246 loc) · 9.08 KB
/
MMM-cryptocurrency.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
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
Module.register("MMM-cryptocurrency", {
result: {},
defaults: {
currency: ['bitcoin'],
conversion: 'USD',
displayLongNames: false,
headers: [],
displayTpe: 'detail',
showGraphs: false,
logoHeaderText: 'Crypto currency',
significantDigits: 2,
coloredLogos: false
},
sparklineIds: {
bitcoin: 1,
ethereum: 1027,
ripple: 52,
litecoin: 2,
'ethereum-classic': 1321,
nem: 873,
stratis: 1343,
dash: 131,
decent: 1478,
foldincoin: 606,
neo: 1376
},
start: function() {
this.getTicker();
this.scheduleUpdate();
},
getStyles: function() {
return ["MMM-cryptocurrency.css"];
},
getTicker: function() {
var conversion = this.config.conversion;
var url = 'https://api.coinmarketcap.com/v1/ticker/?convert=' + conversion + '&limit=300';
this.sendSocketNotification('get_ticker', url);
},
scheduleUpdate: function() {
var self = this;
// Refresh time should not be less than 5 minutes
var delay = 300000;
setInterval(function() {
self.getTicker();
}, delay);
},
getDom: function() {
if (this.config.displayType == 'logo') {
this.folder = (this.config.coloredLogos ? 'colored/' : 'black-white/');
return this.buildIconView(this.result);
}
var data = this.result;
var wrapper = document.createElement("table");
wrapper.className = 'small mmm-cryptocurrency';
var tableHeader = document.createElement("tr");
tableHeader.className = 'header-row';
var tableHeaderValues = [
this.translate("CURRENCY"),
this.translate('PRICE')
];
if (this.config.headers.indexOf('change1h') > -1) {
tableHeaderValues.push(this.translate('CHANGE') + ' (1h)');
}
if (this.config.headers.indexOf('change24h') > -1) {
tableHeaderValues.push(this.translate('CHANGE') + ' (24h)');
}
if (this.config.headers.indexOf('change7d') > -1) {
tableHeaderValues.push(this.translate('CHANGE') + ' (7d)');
}
for (var i = 0; i < tableHeaderValues.length; i++) {
var tableHeadSetup = document.createElement("th");
tableHeadSetup.innerHTML = tableHeaderValues[i];
tableHeader.appendChild(tableHeadSetup);
}
wrapper.appendChild(tableHeader);
for (i = 0; i < data.length; i++) {
var currentCurrency = data[i];
var trWrapper = document.createElement("tr");
trWrapper.className = 'currency';
var name;
if (this.config.displayLongNames) {
name = currentCurrency.name;
} else {
name = currentCurrency.symbol;
}
var tdValues = [
name,
currentCurrency.price,
];
if (this.config.headers.indexOf('change1h') > -1) {
tdValues.push(currentCurrency.percent_change_1h + '%');
}
if (this.config.headers.indexOf('change24h') > -1) {
tdValues.push(currentCurrency.percent_change_24h + '%');
}
if (this.config.headers.indexOf('change7d') > -1) {
tdValues.push(currentCurrency.percent_change_7d + '%');
}
for (var j = 0; j < tdValues.length; j++) {
var tdWrapper = document.createElement("td");
tdWrapper.innerHTML = tdValues[j];
trWrapper.appendChild(tdWrapper);
}
wrapper.appendChild(trWrapper);
}
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "got_result") {
this.result = this.getWantedCurrencies(this.config.currency, payload);
this.updateDom();
}
},
/**
* Returns configured currencies
*
* @param chosenCurrencies
* @param apiResult
* @returns {Array}
*/
getWantedCurrencies: function(chosenCurrencies, apiResult) {
var filteredCurrencies = [];
for (var i = 0; i < chosenCurrencies.length; i++) {
for (var j = 0; j < apiResult.length; j++) {
var userCurrency = chosenCurrencies[i];
var remoteCurrency = apiResult[j];
if (userCurrency == remoteCurrency.id) {
remoteCurrency = this.formatPrice(remoteCurrency);
filteredCurrencies.push(remoteCurrency);
}
}
}
return filteredCurrencies;
},
/**
* Formats the price of the API result and adds it to the object with simply .price as key
* instead of price_eur
*
* @param apiResult
* @returns {*}
*/
formatPrice: function(apiResult) {
var rightCurrencyFormat = this.config.conversion.toLowerCase();
// rounding the price
var unroundedPrice = apiResult['price_' + rightCurrencyFormat];
var digitsBeforeDecimalPoint = Math.floor(unroundedPrice).toString().length;
var requiredDigitsAfterDecimalPoint = Math.max(this.config.significantDigits - digitsBeforeDecimalPoint, 2);
var price = this.roundNumber(unroundedPrice, requiredDigitsAfterDecimalPoint);
// add the currency string
apiResult['price'] = price + ' ' + this.config.conversion;
return apiResult;
},
/**
* Rounds a number to a given number of digits after the decimal point
*
* @param number
* @param precision
* @returns {number}
*/
roundNumber: function(number, precision) {
var factor = Math.pow(10, precision);
var tempNumber = number * factor;
var roundedTempNumber = Math.round(tempNumber);
return roundedTempNumber / factor;
},
/**
* Creates the icon view type
*
* @param apiResult
* @returns {Element}
*/
buildIconView: function(apiResult) {
var wrapper = document.createElement('div');
var header = document.createElement('header');
header.className = 'module-header';
header.innerHTML = this.config.logoHeaderText;
wrapper.appendChild(header);
var table = document.createElement('table');
table.className = 'medium mmm-cryptocurrency-icon';
for (var j = 0; j < apiResult.length; j++) {
var tr = document.createElement('tr');
tr.className = 'icon-row';
var logoWrapper = document.createElement('td');
logoWrapper.className = 'icon-field';
if (this.imageExists(apiResult[j].id)) {
var logo = new Image();
logo.src = '/MMM-cryptocurrency/' + this.folder + apiResult[j].id + '.png';
logo.setAttribute('width', '50px');
logo.setAttribute('height', '50px');
logoWrapper.appendChild(logo);
} else {
this.sendNotification('SHOW_ALERT', {
timer: 5000,
title: 'MMM-cryptocurrency',
message: '' +
this.translate("IMAGE") + ' ' + apiResult[j].id + '.png ' + this.translate("NOTFOUND") + ' /MMM-cryptocurrency/public/' + this.folder
});
}
var priceWrapper = document.createElement('td');
priceWrapper.className = 'price';
var price = document.createElement('price');
price.innerHTML = apiResult[j].price;
priceWrapper.appendChild(price);
tr.appendChild(logoWrapper);
tr.appendChild(priceWrapper);
if (this.config.showGraphs) {
var graphWrapper = document.createElement('td');
graphWrapper.className = 'graph';
if (this.sparklineIds[apiResult[j].id]) {
var graph = document.createElement('img');
graph.src = 'https://files.coinmarketcap.com/generated/sparklines/' + this.sparklineIds[apiResult[j].id] + '.png?cachePrevention=' + Math.random();
console.log(graph.src);
graphWrapper.appendChild(graph);
}
tr.appendChild(graphWrapper);
}
table.appendChild(tr);
}
wrapper.appendChild(table);
return wrapper;
},
/**
* Checks if an image with the passed name exists
*
* @param currencyName
* @returns {boolean}
*/
imageExists: function(currencyName) {
var imgPath = '/MMM-cryptocurrency/' + this.folder + currencyName + '.png';
var http = new XMLHttpRequest();
http.open('HEAD', imgPath, false);
http.send();
return http.status != 404;
},
/**
* Load translations files
* @returns {{en: string, de: string, it: string}}
*/
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json",
it: "translations/it.json"
};
},
});