-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMMM-CurrencyExchange.js
240 lines (199 loc) · 7.14 KB
/
MMM-CurrencyExchange.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
/* global Module */
/* Magic Mirror
* Module: MMM-CurrencyExchange
*
* By Stefan Krause http://yawns.de
* MIT Licensed.
*/
Module.register('MMM-CurrencyExchange',{
defaults: {
base: "",
symbols: null,
units: config.units,
animationSpeed: 1000,
updateInterval: 1000 * 3600, //update every hour
timeFormat: config.timeFormat,
lang: config.language,
showCustomHeader: false,
showFlag: true,
showText: true,
layoutStyle: 'table',
initialLoadDelay: 0, // 0 seconds delay
apiBase: "https://api.exchangeratesapi.io/latest",
},
// ##################################################################################
// Define required scripts.
// ##################################################################################
getScripts: function() {
return ["moment.js"];
},
// ##################################################################################
// import css files
// ##################################################################################
getStyles: function() {
return ['MMM-CurrencyExchange.css'];
},
// ##################################################################################
// setup the module
// ##################################################################################
start: function() {
Log.info('Starting module: ' + this.name);
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
if (this.config.base == "") {
this.config.base = "EUR";
}
var self = this;
setInterval(function() {
self.updateDom();
}, this.config.animationSpeed);
},
// ##################################################################################
// handle all the output stuff
// ##################################################################################
getDom: function() {
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = this.translate('LOADING');
wrapper.className = "dimmed light small";
return wrapper;
}
// no data was retrieved, maybe the source is down or the request was invalid
if (!this.rates.length) {
wrapper.innerHTML = "No data";
wrapper.className = "dimmed light small";
return wrapper;
}
// display a custom header, just in case
if (this.config.showCustomHeader) {
var customHeader = document.createElement('div');
customHeader.innerHTML = "Base: " + this.config.base + " (" + this.rateUpdate + ")";
customHeader.className = "light small UNDERLINE";
wrapper.appendChild(customHeader);
}
// get ready to process data and print it to the screen
var outputWrapper = document.createElement('div');
var dataLimit = ((this.rates.length > 8) ? 3 : 2);
if (this.config.layoutStyle == 'table') {
var table = document.createElement('table');
table.className = "small align-left";
}
for (i in this.rates) {
if (this.config.layoutStyle == 'table') {
// we want more columns to save some space on screen, so we iterate and only create a new row with every second dataset
if ( i % dataLimit == 0) {
var row = document.createElement('tr');
}
var cell = document.createElement('td');
}
var rateContainer = document.createElement('span');
rateContainer.className = "light small";
// determine if user wants to see the currency flag
if (this.config.showFlag) {
var flagSpan = document.createElement('span');
flagSpan.innerHTML = " ";
flagSpan.className = this.rates[i].symbol + " NOREPEAT";
rateContainer.appendChild(flagSpan);
}
// determine if user wants to see the abbreviated currency text (EUR, USD, ..)
if (this.config.showText) {
var currencySpan = document.createElement('span');
currencySpan.innerHTML = this.rates[i].symbol + ": ";
rateContainer.appendChild(currencySpan);
}
var rateSpan = document.createElement('span');
rateSpan.innerHTML = ((this.config.layoutStyle == 'ticker' && i < (this.rates.length - 1)) ? this.rates[i].rate + ' • ' : this.rates[i].rate);
rateContainer.appendChild(rateSpan);
// if the user wants a table, we add the dataset to a cell. If this is the last dataset for a row or the final dataset we add the row to the table
if (this.config.layoutStyle == 'table') {
cell.appendChild(rateContainer);
row.appendChild(cell);
if ( i % dataLimit != 0 || i == (this.rates.length-1) ) { table.appendChild(row) }
} else {
outputWrapper.appendChild(rateContainer);
}
}
if (this.config.layoutStyle == 'table') {
outputWrapper.appendChild(table);
wrapper.appendChild(outputWrapper);
} else if (this.config.layoutStyle == 'ticker') {
var marqueeTicker = document.createElement("marquee");
marqueeTicker.innerHTML = outputWrapper.innerHTML;
marqueeTicker.className = "small thin light";
marqueeTicker.width = document.getElementsByClassName("MMM-CurrencyExchange")[0].clientWidth;
marqueeTicker.scrollDelay = 100;
wrapper.appendChild(marqueeTicker);
}
return wrapper;
},
// ##################################################################################
// fetch new data
// ##################################################################################
updateCurrencies: function() {
var url = this.config.apiBase + this.getParams();
var self = this;
var retry = true;
var Request = new XMLHttpRequest();
Request.open("GET", url, true);
Request.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.processCurrencies(JSON.parse(this.response));
} else {
Log.error(self.name + ": Could not load data.");
}
if (retry) {
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
Request.send();
},
// ##################################################################################
// prepare request url
// ##################################################################################
getParams: function() {
var params = '';
if (this.config.base != "") {
params += "?base=" + this.config.base;
}
if (this.config.symbols) {
params += "&symbols=" + this.config.symbols.join();
}
return params;
},
// ##################################################################################
// validate data after request, prepare for output
// ##################################################################################
processCurrencies: function(data) {
if (!data.rates) {
// Did not receive usable new data.
// Maybe this needs a better check?
return;
}
this.rateUpdate = data.date;
this.rates = [];
for (key in data.rates) {
this.rates.push({
symbol: key,
rate: data.rates[key],
});
}
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},
// ##################################################################################
// schedule updates based on define interval
// ##################################################################################
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
setTimeout(function() {
self.updateCurrencies();
}, nextLoad);
},
});