-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
137 lines (114 loc) · 3.66 KB
/
main.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
// CopyrightⒸ PanDamax
// email: karol.lichon@zsz.bobowa.pl
let th, tbody, informatorParent, p, spanLesson, spanTime, isBreak
let settings = {
colorBreak: 'red',
colorLesson: 'aqua',
colorEnd: 'green'
}
const main = () => {
prepareDOMElements()
prepareDOMEvents()
}
const prepareDOMElements = () => {
informatorParent = document.querySelector('.tytul')
tbody = document.querySelector('.tabela tbody')
th = document.querySelectorAll('.g')
createInformator()
}
const prepareDOMEvents = () => {
const time = getCurrentTime()
if (isSchoolTime(time)) {
const i = findLesson(time)
setColor(i, time)
timer(i, time)
} else {
p.style.backgroundColor = settings.colorEnd
p.textContent = 'Brak aktualnie lekcji'
}
}
const createInformator = () => {
p = document.createElement('p')
p.textContent = 'Do końca '
spanLesson = document.createElement('span')
spanLesson.classList.add('subject')
p.appendChild(spanLesson)
const span = document.createElement('span')
span.textContent = ' ⮞'
p.appendChild(span)
spanTime = document.createElement('span')
spanTime.classList.add('time')
p.appendChild(spanTime)
informatorParent.appendChild(p)
}
const getCurrentTime = () => {
const now = new Date()
return {
day: now.getDay(),
hour: now.getHours(),
minute: now.getMinutes(),
}
}
const findLesson = ({ minute, hour }) => {
for (let i = 0; i < th.length; i++) {
const time = th[i].textContent.match(/(\d+:\d+)\s*-\s*(\d+:\d+)/)
const [startHour, startMinute] = time[1].split(':').map(Number)
const [endHour, endMinute] = time[2].split(':').map(Number)
if (
(hour > startHour || (hour === startHour && minute >= startMinute)) &&
(hour < endHour || (hour === endHour && minute < endMinute))
) {
return {i , isBreak: false}
} else if (
hour < startHour || (hour === startHour && minute < startMinute)
) {
return {i , isBreak: true}
}
}
}
const setColor = ({i, isBreak}, { day }) => {
if (isBreak) {
p.style.backgroundColor = settings.colorBreak
spanLesson.textContent = `przerwy`
tbody.rows[i].cells[day + 1].style.backgroundColor = settings.colorBreak
} else {
p.style.backgroundColor = settings.colorLesson
spanLesson.textContent = `lekcji ${i}`
tbody.rows[i + 1].cells[day + 1].style.backgroundColor = settings.colorLesson
tbody.rows[i].cells[day + 1].style.backgroundColor = ''
}
}
const timer = ({ i, isBreak }, { minute, hour }) => {
let remainingMinutes
const currentTime = hour * 60 + minute
const time = th[i].textContent.match(/(\d+:\d+)\s*-\s*(\d+:\d+)/)
if (isBreak) {
const [startHour, startMinute] = time[1].split(':').map(Number)
const startTime = startHour * 60 + startMinute
remainingMinutes = startTime - currentTime
} else {
const [endHour, endMinute] = time[2].split(':').map(Number)
const endTime = endHour * 60 + endMinute
remainingMinutes = endTime - currentTime
}
spanTime.textContent = ` ${remainingMinutes}min`
}
function isSchoolTime({ day, hour, minute }) {
if (day >= 1 && day <= 5) {
const firstTh = th[0]
const firstTime = firstTh.textContent.match(/(\d+:\d+)\s*-\s*(\d+:\d+)/)
const [startHour, startMinute] = firstTime[1].split(':').map(Number)
const lastTh = th[th.length - 1]
const lastTime = lastTh.textContent.match(/(\d+:\d+)\s*-\s*(\d+:\d+)/)
const [endHour, endMinute] = lastTime[2].split(':').map(Number)
if (
(hour > startHour || (hour === startHour && minute >= startMinute)) &&
(hour < endHour || (hour === endHour && minute < endMinute))
) {
return true
}
}
return false
}
document.addEventListener('DOMContentLoaded', main)
setInterval(prepareDOMEvents, 60_000)