Repository navigation
Expand file tree
/
Copy pathMMM-BusyWeekends.js
More file actions
223 lines (186 loc) · 6.76 KB
/
Copy pathMMM-BusyWeekends.js
File metadata and controls
223 lines (186 loc) · 6.76 KB
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
Module.register("MMM-BusyWeekends", {
defaults: {
weeksToShow: 6,
weekendDays: [6, 0], // Saturday=6, Sunday=0
calendarColorMap: {}, // { "Work": "#ff4081", "Personal": "#00e676" }
showWeather: true,
weatherModuleName: "weather",
weatherIconSize: 32,
timeBuckets: [
{ name: "morning", start: 5, end: 11, icon: "sunrise" },
{ name: "afternoon", start: 12, end: 17, icon: "sun" },
{ name: "evening", start: 18, end: 23, icon: "sunset" }
],
fontSize: "14px"
},
// Helper to reliably parse dates from strings or numbers
parseDate(date) {
if (!date) return new Date(0);
if (typeof date === "string" && /^\d+$/.test(date)) {
return new Date(Number(date));
}
return new Date(date);
},
start() {
this.events = [];
this.weather = {};
this.loaded = false;
this.sendNotification("CALENDAR_EVENTS_REQUEST");
this.sendNotification("WEATHER_REQUEST", { location: "any" });
},
notificationReceived(notification, payload, sender) {
if (notification === "CALENDAR_EVENTS") {
const now = new Date();
this.events = payload.filter(ev => {
const end = ev.endDate || ev.startDate;
const isFuture = this.parseDate(end) >= now;
return isFuture;
});
this.loaded = true;
this.updateDom();
return;
}
if (notification === "WEATHER_UPDATED") {
if (sender?.name === this.config.weatherModuleName) {
// Map stock weather 'forecastArray' to the module's expected 'forecast' structure
this.weather = {
forecast: payload.forecastArray || []
};
this.updateDom();
}
return;
}
},
getDom() {
const wrapper = document.createElement("div");
wrapper.className = "bw-calendar";
wrapper.style.fontSize = this.config.fontSize;
if (!this.loaded) {
wrapper.innerHTML = "Loading weekend calendar…";
return wrapper;
}
const today = new Date();
today.setHours(0, 0, 0, 0);
// Calculate start of "weekend week" (last Monday)
const start = new Date(today);
const day = start.getDay();
const diff = (day === 0 ? 6 : day - 1); // Get Monday (1)
start.setDate(start.getDate() - diff);
for (let w = 0; w < this.config.weeksToShow; w++) {
const weekRow = document.createElement("div");
weekRow.className = "bw-week-row";
weekRow.style.gridTemplateColumns = `repeat(${this.config.weekendDays.length}, 1fr)`;
this.config.weekendDays.forEach(dayIndex => {
const date = new Date(start);
// Correctly handle Sunday (0) as the end of the weekend, not the start
let offset = dayIndex - 1;
if (dayIndex === 0) offset = 6; // Sunday is day 7 of our "Monday-start" week
date.setDate(start.getDate() + w * 7 + offset);
const cell = document.createElement("div");
cell.className = "bw-day-cell";
if (date < today) {
cell.classList.add("bw-past-day");
weekRow.appendChild(cell);
return;
}
const label = document.createElement("div");
label.className = "bw-day-label";
label.innerHTML = date.toLocaleDateString(undefined, {
weekday: "short",
month: "short",
day: "numeric"
});
const indicators = document.createElement("div");
indicators.className = "bw-indicator-row";
this.config.timeBuckets.forEach(bucket => {
const bucketIcon = document.createElement("div");
bucketIcon.className = "bw-bucket-icon";
const hasEvent = this.bucketHasEvent(date, bucket);
if (hasEvent) {
const color = this.getEventColor(hasEvent);
bucketIcon.style.backgroundColor = color;
} else {
bucketIcon.style.opacity = 0.15;
}
indicators.appendChild(bucketIcon);
});
if (this.config.showWeather) {
const weatherIcon = document.createElement("div");
weatherIcon.className = "bw-weather-icon";
const icon = this.getWeatherIcon(date);
if (icon) {
weatherIcon.innerHTML = `<img src="${icon}" width="${this.config.weatherIconSize}">`;
}
cell.appendChild(weatherIcon);
}
cell.appendChild(label);
cell.appendChild(indicators);
weekRow.appendChild(cell);
});
wrapper.appendChild(weekRow);
}
return wrapper;
},
bucketHasEvent(date, bucket) {
const start = new Date(date);
start.setHours(bucket.start, 0, 0, 0);
const end = new Date(date);
end.setHours(bucket.end, 59, 59, 999);
return this.events.find(ev => {
const evStart = this.parseDate(ev.startDate);
const evEnd = this.parseDate(ev.endDate);
return evEnd >= start && evStart <= end;
});
},
getEventColor(event) {
let chosenColor = null;
let reason = "none";
const isWhiteOrBlack = (c) => {
if (!c) return true;
const normalized = c.toLowerCase().trim();
return ["#fff", "#ffffff", "white", "#000", "#000000", "black"].includes(normalized);
};
// 1. Prioritize Configured colour map
// Try different possible properties for the calendar name
const calNames = [event.calendarName, event.calendarTitle, "default"];
for (const name of calNames) {
if (!name) continue;
// Check case-insensitive match in color map
const match = Object.keys(this.config.calendarColorMap).find(k => k.toLowerCase() === name.toLowerCase());
if (match) {
chosenColor = this.config.calendarColorMap[match];
reason = "calendarColorMap [" + match + "]";
break;
}
}
// 2. Event-specific colour (only if it's not white/black)
if (!chosenColor && event.color && !isWhiteOrBlack(event.color)) {
chosenColor = event.color;
reason = "event.color";
}
// 3. Calendar-level colour (only if it's not white/black)
if (!chosenColor && event.backgroundColor && !isWhiteOrBlack(event.backgroundColor)) {
chosenColor = event.backgroundColor;
reason = "event.backgroundColor";
}
// 4. Final Fallback (back to your GREEN!)
if (!chosenColor) {
chosenColor = "#00e676";
reason = "hard-fallback (GREEN)";
}
// console.log("MMM-BusyWeekends: Event '" + event.title + "' color: " + chosenColor + " (Reason: " + reason + ")");
return chosenColor;
},
getWeatherIcon(date) {
if (!this.weather?.forecast) return null;
const target = date.toDateString();
const match = this.weather.forecast.find(f => new Date(f.date).toDateString() === target);
return match ? match.icon : null;
},
getStyles() {
return ["MMM-BusyWeekends.css"];
},
getHeader() {
return this.data.header;
}
});