-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMMM-SHL.js
173 lines (165 loc) · 5.36 KB
/
MMM-SHL.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
Module.register("MMM-SHL", {
// Default module config.
defaults: {
clientId: "",
clientSecret: "",
debug: false,
teams: null,
showUpdated: true
},
getTranslations: function() {
return {
en: "translations/en.json",
sv: "translations/sv.json"
};
},
getStyles: function() {
return [this.file("/css/mmm-shl-main.css")];
},
start: function() {
Log.info("Starting module: " + this.name);
//Send config to node_helper
Log.info("Send configs to node_helper..");
this.sendSocketNotification("CONFIG", this.config);
this.updateDom();
},
getDom: function() {
Log.info("getDom triggered");
let wrapper = document.createElement("div");
wrapper.className = "stands-board";
if (!this.loaded && !this.failure) {
wrapper.innerHTML =
"<img src='https://www.shl.se/sprites/img/shl1-shl-37e0d.png'></img>";
return wrapper;
}
if (this.failure) {
wrapper.innerHTML = "Connection to SHL failed. Please review logs";
wrapper.className = "dimmed light small";
return wrapper;
}
if (this.stands.length > 0) {
let standsTable = this.generateStandsTable();
let updated = document.createElement("span");
let html = standsTable;
if (this.config.showUpdated) {
updated.className = "xsmall";
updated.style = "opacity: 0.222222;";
updated.innerText =
this.translate("UPDATED") + this.updated.toLocaleString() + this.translate("NEXTUPDATE")+this.nextUpdate.toLocaleString()+")";
html = html + updated.outerHTML;
}
wrapper.innerHTML = html;
return wrapper;
}
},
generateStandsTable: function() {
let table = document.createElement("table");
table.className = "small stands-table";
let row = document.createElement("tr");
let th = document.createElement("th");
th.className = "align-left";
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left";
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left";
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left stands-colum-left";
th.innerText = "GP";
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left stands-colum";
th.innerText = "+/-";
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left stands-colum";
th.innerText = "P";
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left";
th.innerText = this.translate("NEXTGAME");
row.appendChild(th);
table.appendChild(row);
for (let n = 0; n < this.stands.length; n++) {
let team = this.stands[n];
row = document.createElement("tr");
th = document.createElement("th");
th.className = "align-left";
th.innerText = team.rank;
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left";
th.innerHTML = "<img src='" + team.icon + "'></img>";
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left";
th.innerText = team.name;
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left stands-colum-left";
th.innerText = team.gp;
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left stands-colum";
th.innerText = team.diff;
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left stands-colum";
th.innerText = team.points;
row.appendChild(th);
th = document.createElement("th");
th.className = "align-left";
th.innerText = this.getNextGameString(team);
row.appendChild(th);
table.appendChild(row);
}
return table.outerHTML;
},
// --------------------------------------- Handle socketnotifications
socketNotificationReceived: function(notification, payload) {
Log.info(
"socketNotificationReceived: " + notification + ", payload: " + payload
);
if (notification === "STANDS") {
this.loaded = true;
this.failure = undefined;
// Handle payload
this.stands = payload.sortedStand;
this.nextUpdate = payload.nextUpdate;
this.updated = new Date(payload.updated);
this.updateDom();
} else if (notification == "SERVICE_FAILURE") {
this.loaded = true;
this.failure = payload;
if (payload) {
Log.info(
"Service failure: " +
this.failure.resp.StatusCode +
":" +
this.failure.resp.Message
);
this.updateDom();
}
}
},
notificationReceived: function(notification, payload, sender) {
if (notification == "DOM_OBJECTS_CREATED") {
this.domObjectCreated = true;
}
},
getNextGameString: function(team) {
let date = new Date(team.nextGame.start_date_time);
let options = { weekday: "long", day: "numeric" };
let playTime =
date.toLocaleDateString("sv-SE", options) +
" " +
date.getHours() +
":" +
(date.getMinutes() <= 9 ? date.getMinutes() + "0" : date.getMinutes());
return team.team_code == team.nextGame.away_team_code
? this.translate("AWAYGAME") + ", " + playTime
: this.translate("HOMEGAME") + ", " + playTime;
}
});