-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
406 lines (347 loc) · 14 KB
/
index.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
class Logger {
static warning(...args) {
console.log(`%c ${args.join(' ')}`, 'background: #FFFFD4; color: #808000;');
}
static error(...args) {
console.log(`%c ${args.join(' ')}`, 'background: #FFDADA; color: #800000;');
}
static success(...args) {
console.log(`%c ${args.join(' ')}`, 'background: #DEF3DE; color: #008000;');
}
static info(...args) {
console.log(`%c ${args.join(' ')}`, 'background: #E9E9FF; color: #000080;');
}
}
class Time {
constructor(hour, minute) {
this.hour = hour;
this.minute = minute;
}
getHour() {
return this.hour;
}
getMinute() {
return this.minute;
}
getSeconds() {
return this.hour * 3600 + this.minute * 60;
}
static fromSeconds(seconds) {
const hour = Math.floor(seconds / 3600);
const minute = Math.floor((seconds - hour * 3600) / 60);
return new Time(hour, minute);
}
static isInBetween(start, end, time) {
const startSeconds = start.getSeconds();
const endSeconds = end.getSeconds();
const timeSeconds = time.getSeconds();
return timeSeconds >= startSeconds && timeSeconds <= endSeconds;
}
}
class SlideShow {
constructor() {
this.hourElement = document.querySelector("#hour");
this.minuteElement = document.querySelector("#minute");
this.dateElement = document.querySelector("#date_element");
this.lessonTitleElement = document.querySelector("#lesson_title");
this.slideElement = document.querySelector(".frame-child");
this.dataJson = {};
this.timeOnSlides = 30000; // default time in seconds
this.currentSlide = 0;
this.timeOnCurrentSlide = 0;
this.slides = [];
this.gotten = { slides: false, data: false };
this.animating = false;
this.hideBackground = false;
this.hideSlideshow = false;
this.timeonly = false;
this.init();
}
async init() {
await this.fetchData();
await this.fetchSlides();
this.run();
if (!this.hideSlideshow) {
this.animateSlideChange(this.slides[this.currentSlide]);
}
if (this.hideBackground) {
document.querySelector(".slide-169-1").style.backgroundColor = "black";
}
setInterval(() => this.run(), 500);
}
async fetchData() {
try {
const response = await fetch("data.json");
this.dataJson = await response.json();
Logger.success("Successfully fetched data from data.json");
this.timeOnSlides = this.dataJson.timeOnSlides;
this.gotten.data = true;
} catch (error) {
Logger.error("Failed to fetch data from data.json");
}
}
async fetchSlides() {
let index = 1;
while (true) {
const slide = await this.fetchSlide(index, "png");
if (slide) {
this.slides.push(slide);
this.cacheSlide(slide);
index++;
continue;
}
const slideJPG = await this.fetchSlide(index, "jpg");
if (slideJPG) {
this.slides.push(slideJPG);
this.cacheSlide(slideJPG);
index++;
continue;
}
break;
}
this.gotten.slides = true;
Logger.info("Slides fetched successfully");
}
async fetchSlide(index, extension) {
const url = `slides/slide${index}.${extension}`;
const response = await fetch(url);
return response.ok ? url : null;
}
cacheSlide(slideUrl) {
const image = document.createElement("img");
image.src = slideUrl;
image.loading = "eager";
image.style.display = "none";
document.querySelector(".cache_images").appendChild(image);
}
run() {
if (!this.gotten.slides || !this.gotten.data) {
Logger.info("Waiting for data and slides");
return;
}
if (this.getLesson()) {
if (this.getLesson().timeOnly === true && !this.timeonly) {
this.enableTimeOnly();
return;
} else {
if (this.getLesson().timeOnly === false && this.timeonly) {
Logger.info("Disabling time only");
this.disableTimeOnly();
}
}
}
Logger.info("Running Slideshow: ", this.timeOnCurrentSlide, this.timeOnSlides);
this.updateTime();
this.updateDate();
this.updateLessonTitle();
this.manageSlides();
}
updateTime() {
if (this.timeonly) {
this.updateTimeOnly();
return;
};
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
this.hourElement.innerHTML = hours < 10 ? "0" + hours : hours;
this.minuteElement.innerHTML = minutes < 10 ? "0" + minutes : minutes;
}
updateDate() {
const date = new Date();
const year = date.getFullYear().toString().substr(2);
const month = date.getMonth() + 1;
const day = date.getDate();
if (this.timeonly) {
document.querySelector(".dateonly").innerHTML = `${day < 10 ? "0" + day : day}.${month < 10 ? "0" + month : month}.${year}`;
return;
};
this.dateElement.innerHTML = `${day < 10 ? "0" + day : day}.${month < 10 ? "0" + month : month}.${year}`;
}
updateLessonTitle() {
if (this.timeonly) {
this.lessonTitleElement.innerHTML = "";
return;
};
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const currentTime = new Time(hours, minutes);
const currentLesson = this.dataJson.lessons.find((lesson) => {
const start = lesson.start.split(":");
const end = lesson.end.split(":");
const startTime = new Time(parseInt(start[0]), parseInt(start[1]));
const endTime = new Time(parseInt(end[0]), parseInt(end[1]));
return Time.isInBetween(startTime, endTime, currentTime);
});
this.hideSlideshow = currentLesson ? currentLesson.showSlides === false : false;
this.hideBackground = currentLesson ? currentLesson.showBackground === false : false;
this.lessonTitleElement.innerHTML = currentLesson ? currentLesson.title.toUpperCase() : "";
}
getLesson() {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const currentTime = new Time(hours, minutes);
return this.dataJson.lessons.find((lesson) => {
const start = lesson.start.split(":");
const end = lesson.end.split(":");
const startTime = new Time(parseInt(start[0]), parseInt(start[1]));
const endTime = new Time(parseInt(end[0]), parseInt(end[1]));
return Time.isInBetween(startTime, endTime, currentTime);
});
}
manageSlides() {
if (this.hideSlideshow || this.timeonly) {
this.slideElement.style.backgroundImage = "none";
return;
}
if (this.slides.length === 0) {
this.slideElement.innerHTML = "No slides found";
this.slideElement.style.display = "flex";
this.slideElement.style.justifyContent = "center";
this.slideElement.style.alignItems = "center";
return;
}
if (this.slideElement.innerHTML === "No slides found") {
this.slideElement.innerHTML = "";
this.slideElement.style.display = "block";
}
if (this.timeOnCurrentSlide >= this.timeOnSlides) {
this.currentSlide = (this.currentSlide + 1) % this.slides.length;
this.timeOnCurrentSlide = 0;
this.animateSlideChange(this.slides[this.currentSlide]);
Logger.info(`Switched to slide ${this.currentSlide + 1}`);
}
this.timeOnCurrentSlide += 500;
}
animateSlideChange(slide) {
if (this.animating) return;
this.animating = true;
const newSlide = document.createElement("div");
newSlide.style.backgroundImage = `url(${slide})`;
newSlide.style.position = "absolute";
newSlide.style.top = "0";
newSlide.style.left = "0";
newSlide.style.bottom = "0";
newSlide.style.right = "0";
newSlide.style.backgroundSize = "cover";
newSlide.style.opacity = "0";
newSlide.style.zIndex = "1";
newSlide.style.borderRadius = "5px";
newSlide.setAttribute("data-animation_slide", "true");
newSlide.style.animation = "fadeIn 1s forwards";
this.slideElement.appendChild(newSlide);
setTimeout(() => {
this.slideElement.style.backgroundImage = `url(${slide})`;
this.slideElement.removeChild(this.slideElement.firstChild);
this.animating = false;
}, 1000);
}
enableTimeOnly() {
if (this.getLesson().timeonly === false && this.timeonly) {
Logger.info("Attempted to enable time only while already enabled");
return
};
if (document.querySelector("#blackDiv")) return;
var blackDiv = document.createElement("div");
blackDiv.style.backgroundColor = "black";
blackDiv.style.position = "absolute";
blackDiv.id = "blackDiv";
blackDiv.style.top = "0";
blackDiv.style.left = "0";
blackDiv.style.bottom = "0";
blackDiv.style.right = "0";
blackDiv.style.zIndex = "100";
blackDiv.style.animation = "fadeIn 2s forwards";
document.querySelector(".slide-169-1").appendChild(blackDiv);
setTimeout(() => {
document.querySelector("#blackDiv").remove();
this.timeonly = true;
this.hideBackground = true;
document.querySelector(".slide-169-1").style.backgroundColor = "black";
this.hideSlideshow = true;
document.querySelector(".frame-child").style.display = "none";
this.lessonTitleElement.innerHTML = "";
this.dateElement.innerHTML = "";
this.hourElement.parentElement.style.display = "none";
this.hideSlideshow = true;
var icon = document.querySelector(".group-icon")
var iconClone = icon.cloneNode(true);
icon.style.display = "none";
console.log(icon, iconClone)
iconClone.querySelectorAll("path").forEach((path) => {
path.style.fill = "currentColor";
});
iconClone.id = "newIconClone";
iconClone.style.mixBlendMode = "normal";
iconClone.style.fill = "currentColor";
iconClone.style.color = "#696969"
iconClone.style.animation = "fadeIn 2s forwards";
document.querySelector(".slide-169-1").appendChild(iconClone);
var newTimeElement = document.createElement("div");
newTimeElement.classList.add("timeonly");
newTimeElement.innerHTML = `<span id="timeonly_hour"></span>:<span id="timeonly_minute"></span>`;
document.querySelector(".slide-169-1").appendChild(newTimeElement);
this.updateTimeOnly();
var newDateElement = document.createElement("div");
newDateElement.classList.add("dateonly");
newDateElement.innerHTML = `<span id="timeonly_date"></span>`;
document.querySelector(".slide-169-1").appendChild(newDateElement);
this.updateDate();
}, 2000);
}
disableTimeOnly() {
if (document.querySelector("#transitionDiv")) {
Logger.info("Attempted to disable time only while transitioning");
return;
};
var blackDiv = document.createElement("div");
blackDiv.style.backgroundColor = "#102e5a";
blackDiv.style.position = "absolute";
blackDiv.id = "transitionDiv";
blackDiv.style.top = "0";
blackDiv.style.left = "0";
blackDiv.style.bottom = "0";
blackDiv.style.right = "0";
blackDiv.style.zIndex = "100";
blackDiv.style.animation = "fadeIn 2s forwards";
document.querySelector(".slide-169-1").appendChild(blackDiv);
setTimeout(() => {
// document.querySelector("#transitionDiv").remove();
this.timeonly = false;
this.hideBackground = false;
document.querySelector(".slide-169-1").style.backgroundColor = "#102e5a";
this.hideSlideshow = false;
this.slideElement.style.display = "block";
document.querySelector(".frame-child").style.display = "block";
this.lessonTitleElement.innerHTML = "";
this.dateElement.innerHTML = "";
this.hourElement.parentElement.style.display = "block";
this.hideSlideshow = false;
var icon = document.querySelector(".group-icon")
icon.style.display = "block";
document.querySelector(".slide-169-1").removeChild(document.querySelector("#newIconClone"));
document.querySelector(".timeonly").remove();
document.querySelector(".dateonly").remove();
this.animateSlideChange(this.slides[this.currentSlide]);
this.updateLessonTitle();
this.updateDate();
this.updateTime();
const transitionDiv = document.querySelector("#transitionDiv");
// fade out transition div
transitionDiv.style.animation = "fadeOut 1s forwards";
setTimeout(() => {
transitionDiv.remove();
}, 1000);
}, 2000)
}
updateTimeOnly() {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
document.querySelector("#timeonly_hour").innerHTML = hours < 10 ? "0" + hours : hours;
document.querySelector("#timeonly_minute").innerHTML = minutes < 10 ? "0" + minutes : minutes;
}
}
new SlideShow();