forked from pinsdorf/MMM-WeeklySchedule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-WeeklySchedule.js
155 lines (128 loc) · 3.51 KB
/
MMM-WeeklySchedule.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
/* Magic Mirror
* Module: MMM_WeeklySchedule
*
* By Ulrich Pinsdorf
* MIT Licensed.
*/
Module.register("MMM-WeeklySchedule", {
defaults: {
customCssFile: "MMM-WeeklySchedule.css",
showWeekdayinHeader: true,
updateInterval: 1 * 60 * 60 * 1000, // 1 hour
showNextDayAfter: "16:00",
fadeSpeed: 4000,
debug: true
},
requiresVersion: "2.1.0", // Required version of MagicMirror
/* start()
* Start module after all modules have been loaded
* by the MagicMirror framework
*/
start: function() {
// Schedule update timer.
var self = this;
setInterval(function() {
self.updateDom(self.config.fadeSpeed);
}, this.config.updateInterval);
this.loaded = true;
},
/* getHeader()
* Create the module header. Regards configuration showWeekdayinHeader
*/
getHeader: function() {
var header = this.data.header;
if(this.config.showWeekdayinHeader) {
header += " " + this.translate("ON_DAY") + " " + this.getDisplayDate().format("dddd");
}
return header;
},
/* getDom()
* Create the DOM to show content
*/
getDom: function() {
var date = this.getDisplayDate();
// get day of week and access respective element in lessons array
var dow = date.locale('en').format("ddd").toLowerCase();
var lessons = this.config.schedule.lessons[dow];
// no lessons today, we return default text
if(lessons == undefined)
{
return this.createTextOnlyDom(
this.translate("NO_LESSONS")
);
}
// get timeslots
var timeslots = this.config.schedule.timeslots;
// build table with timeslot definitions and lessons
wrapper = document.createElement("table");
for (let index = 0; index < lessons.length; index++) {
const lesson = lessons[index];
const time = timeslots[index];
// only create a row if the timeslot's lesson is defined and not an empty string
if(lesson)
{
var row = this.createTimetableRow(time, lesson);
wrapper.appendChild(row);
}
}
return wrapper;
},
getDisplayDate: function() {
// check if config contains a threshold 'showNextDayAfter'
if(this.config.showNextDayAfter) {
var threshold = moment().startOf("day")
.add(moment.duration(this.config.showNextDayAfter));
} else {
var threshold = moment().endOf("day");
}
// get the current time and increment by one day if threshold time has passed
var now = moment();
if(now.isAfter(threshold)) {
now = now.add(1, "day");
}
return now;
},
createTextOnlyDom: function(text) {
var wrapper = document.createElement("table");
var tr = document.createElement("tr");
var td = document.createElement("td");
var text = document.createTextNode(text);
td.className = "xsmall bright lesson";
wrapper.appendChild(tr);
tr.appendChild(td);
td.appendChild(text);
return wrapper;
},
createTimetableRow: function(time, lesson) {
var row = document.createElement("tr");
var tdtime = document.createElement("td");
tdtime.className = "xsmall dimmed lessontime";
tdtime.appendChild(
document.createTextNode(time)
);
row.appendChild(tdtime);
var tdlesson = document.createElement("td");
tdlesson.className = "xsmall bright lesson";
tdlesson.appendChild(
document.createTextNode(lesson)
);
row.appendChild(tdlesson);
return row;
},
getScripts: function() {
return ["moment.js"];
},
getStyles: function () {
return [
this.config.customCssFile
];
},
getTranslations: function() {
return {
en: "translations/en.json",
de: "translations/de.json",
sv: "translations/sv.json",
da: "translations/da.json"
}
}
});