-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMMM-OlympicGames.js
97 lines (78 loc) · 2.55 KB
/
MMM-OlympicGames.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
/* global Module Log config */
/* MagicMirror²
* Module: MMM-OlympicGames
*
* By fewieden https://github.com/fewieden/MMM-OlympicGames
* MIT Licensed.
*/
Module.register('MMM-OlympicGames', {
defaults: {
maxRows: 10,
highlight: false,
title: 'Paris Summer Games 2024',
reloadInterval: 30 * 60 * 1000, // every 30 minutes
provider: 'nbc',
countryList: false
},
getTranslations() {
return {
en: 'translations/en.json',
de: 'translations/de.json',
fr: 'translations/fr.json',
};
},
getStyles() {
return ['MMM-OlympicGames.css'];
},
getTemplate() {
return `templates/${this.name}.njk`;
},
filterCountryList() {
if (!Array.isArray(this.config.countryList)) {
return this.countries;
}
return this.countries.filter(country => this.config.countryList.includes(country.code));
},
getCountriesToDisplay() {
const filteredCountries = this.filterCountryList();
const slicedCountries = filteredCountries.slice(0, this.config.maxRows);
if (this.config.highlight) {
const highlightedIndex = this.countries.findIndex(country => country.code === this.config.highlight);
if (highlightedIndex >= this.config.maxRows) {
slicedCountries[this.config.maxRows - 1] = this.countries[highlightedIndex];
}
}
return slicedCountries;
},
getTemplateData() {
if (!Array.isArray(this.countries)) {
return { config: this.config };
}
return {
config: this.config,
countries: this.getCountriesToDisplay()
};
},
start() {
Log.info(`Starting module: ${this.name}`);
if (Array.isArray(this.config.countryList)) {
this.config.maxRows = this.config.countryList.length;
}
this.sendSocketNotification('CONFIG', this.config);
},
setCountryNames(countries) {
const regionNames = new Intl.DisplayNames(config.locale, { type: 'region' });
return countries.map(country => {
const name = country.code.length === 2
? regionNames.of(country.code)
: this.translate(country.code);
return { ...country, name };
});
},
socketNotificationReceived(notification, payload) {
if (notification === 'COUNTRIES') {
this.countries = this.setCountryNames(payload);
this.updateDom(300);
}
}
});