-
Notifications
You must be signed in to change notification settings - Fork 8
/
MMM-Webuntis.js
224 lines (184 loc) · 5.84 KB
/
MMM-Webuntis.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
Module.register("MMM-Webuntis", {
defaults: {
students: [
{
title: "SET CONFIG!",
school: "",
username: "",
password: "",
server: "",
class: "",
useClassTimetable: false,
},
],
days: 7,
fetchInterval: 5 * 60 * 1000,
showStartTime: false,
showRegularLessons: false,
showTeacher: true,
shortSubject: false,
showSubstText: false,
debug: false
},
getStyles: function () {
return ["MMM-Webuntis.css"];
},
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
start: function (){
this.lessonsByStudent = [];
this.sendSocketNotification("FETCH_DATA", this.config);
},
getDom: function() {
var wrapper = document.createElement("div");
var table = document.createElement("table");
table.className = "bright small light";
// no student
if (this.lessonsByStudent === undefined) {
return table;
}
var addedRows = 0;
// iterate through students
// TODO: for..in does not guarantee specific order
for (let studentTitle in this.lessonsByStudent) {
//for (const [studentTitle, lessons] of this.lessonsByStudent.entries()) {
// student name
//Only display title cell if there are more than one student
if (this.config.students.length > 1) {
var studentRow = document.createElement("tr");
table.appendChild(studentRow);
var studentCell = document.createElement("td");
studentCell.colSpan = 2;
studentCell.innerHTML = studentTitle;
studentCell.className = "student align-left bold";
studentRow.appendChild(studentCell);
}
var lessons = this.lessonsByStudent[studentTitle];
// sort lessons by start time
lessons.sort((a,b) => a.sortString - b.sortString);
// iterate through lessons of current student
for (let i = 0; i < lessons.length; i++) {
var lesson = lessons[i];
var time = new Date(lesson.year,lesson.month-1,lesson.day,lesson.hour,lesson.minutes);
if (!this.config.showRegularLessons) {
// skip if nothing special
if (lesson.code == "") {continue;}
}
// skip past lessons
if (time < new Date() && lesson.code != "error") {continue;}
addedRows++;
var row = document.createElement("tr");
table.appendChild(row);
// date and time
var dateTimeCell = document.createElement("td");
dateTimeCell.innerHTML = time.toLocaleDateString("de-DE",{weekday:"short"}).toUpperCase() + " ";
if (this.config.showStartTime || lesson.lessonNumber === undefined) {
dateTimeCell.innerHTML += time.toLocaleTimeString("de-DE", {hour:"2-digit",minute:"2-digit"});
}
else {
dateTimeCell.innerHTML += lesson.lessonNumber + ".";
}
dateTimeCell.className = "align-right alignTop";
row.appendChild(dateTimeCell);
// subject cell
var subjectCell = document.createElement("td");
subjectCell.innerHTML = "";
// Subject
if (this.config.shortSubject) {
subjectCell.innerHTML += this.capitalize(lesson.subjectShort);
}
else {
subjectCell.innerHTML += this.capitalize(lesson.subject);
}
//Teachers name
if (this.config.showTeacher) {
if (this.config.showTeacher == "initial") {
if (lesson.teacherInitial !== "") {
subjectCell.innerHTML += " " + "(";
subjectCell.innerHTML += this.capitalize(lesson.teacherInitial);
subjectCell.innerHTML += ")";
}
}
else {
if (lesson.teacher !== "") {
subjectCell.innerHTML += " " + "(";
subjectCell.innerHTML += this.capitalize(lesson.teacher);
subjectCell.innerHTML += ")";
}
}
}
// lesson substitute text
if (this.config.showSubstText && lesson.substText !== "") {
subjectCell.innerHTML += "<br/>"
var subText = document.createElement("span");
subText.className = "xsmall dimmed";
subText.innerHTML = lesson.substText;
subjectCell.appendChild(subText);
}
/*
if (lesson.text !== "") {
subjectCell.innerHTML += "<br/>"
var lessonText = document.createElement("span");
lessonText.className = "xsmall dimmed";
lessonText.innerHTML = lesson.text;
subjectCell.appendChild(lessonText);
}
*/
subjectCell.className = "leftSpace align-left alignTop";
if (lesson.code == "cancelled") {
subjectCell.className += " cancelled";
}
else if (lesson.code == "error") {
subjectCell.className += " error";
}
else if (lesson.code == "info") {
subjectCell.className += " info";
}
row.appendChild(subjectCell);
} // end for lessons
} // end for students
// add message row if table is empty
if (addedRows == 0) {
var nothingRow = document.createElement("tr");
table.appendChild(nothingRow);
var nothingCell = document.createElement("td");
nothingCell.innerHTML = this.translate("nothing");
nothingRow.appendChild(nothingCell);
}
wrapper.appendChild(table);
return wrapper;
},
capitalize: function(str) {
return str;
//Changed, because the strings does not look nice for me after capitalize function, is this necessary?
str = str.toLowerCase().split(" ");
for (let i = 0, x = str.length; i < x; i++) {
if (str[i]) {
if (str[i] === "ii" || str[i] === "iii") {str[i] = str[i].toUpperCase();}
else {str[i] = str[i][0].toUpperCase() + str[i].substr(1);}
}
}
return str.join(" ");
},
notificationReceived: function(notification, payload) {
switch(notification) {
case "DOM_OBJECTS_CREATED":
var timer = setInterval(() => {
this.sendSocketNotification("FETCH_DATA", this.config);
}, this.config.fetchInterval);
break;
}
},
socketNotificationReceived: function(notification, payload) {
if (notification === "GOT_DATA") {
if (payload.lessons) {
this.lessonsByStudent[payload.title] = payload.lessons;
this.updateDom();
}
}
},
});