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
|
/* global ga FLAGS */
require('flexibility')
const fuzzy = require('fuzzy')
// const getUsers = require('./getUsers')
const getURLOfUser = require('./getURLOfUser')
const removeDiacritics = require('diacritics').remove
const getWeek = require('./getWeek')
const easterEggs = require('./easterEggs')
const searchNode = document.querySelector('#search')
const inputNode = searchNode.querySelector('input[type="text"]')
const autocompleteNode = document.querySelector('.autocomplete')
const scheduleIframe = document.querySelector('#schedule')
const prevButton = document.querySelectorAll('input[type="button"]')[0]
const nextButton = document.querySelectorAll('input[type="button"]')[1]
const currentWeekNode = document.querySelector('.current')
const favNode = document.querySelector('.fav')
if (FLAGS.indexOf('NO_FEATURE_DETECT') === -1) {
if (document.querySelector('#schedule').getClientRects()[0].bottom !==
document.body.getClientRects()[0].bottom) {
window.location = 'http://www.meetingpointmco.nl/Roosters-AL/doc/'
} else {
window.onerror = function () {
window.location = 'http://www.meetingpointmco.nl/Roosters-AL/doc/'
}
}
} else {
console.log('feature detection is OFF')
}
let selectedResult = -1
let selectedUser
let results = []
let offset = 0
function getUsers () {
const nodes = document.querySelector('#data')
.querySelectorAll('.data-user')
const elements = Array.prototype.slice.call(nodes)
const users = elements.map(userNode => {
const type = userNode.querySelector('.data-type').textContent
const value = userNode.querySelector('.data-value').textContent
const index = Number(userNode.querySelector('.data-index').textContent)
return { type, value, index }
})
document.querySelector('#data').outerHTML = ''
return users
}
const users = getUsers()
function getCurrentFav () {
if (!window.localStorage.getItem('fav')) return
const favCode = window.localStorage.getItem('fav').split(':')
const fav = users.filter(user => user.type === favCode[0] && user.index === Number(favCode[1]))
return fav[0]
}
function changeFav (isFav) {
if (!selectedUser) return
if (isFav) {
window.localStorage.setItem('fav', selectedUser.type + ':' + selectedUser.index)
} else {
window.localStorage.removeItem('fav')
}
updateFavNode()
}
function usersEqual (user1, user2) {
if (user1 == null || user2 == null) return false
return user1.type === user2.type && user1.index === user2.index
}
function updateFavNode () {
if (usersEqual(getCurrentFav(), selectedUser)) {
favNode.innerHTML = ''
} else {
favNode.innerHTML = ''
}
}
function updateWeekText () {
if (offset === 0) currentWeekNode.innerHTML = `Week ${getWeek() + offset}`
else currentWeekNode.innerHTML = `<strong>Week ${getWeek() + offset}</strong>`
}
updateWeekText()
searchNode.addEventListener('keydown', function (e) {
if ((results.length !== 0) && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
e.preventDefault()
if (document.querySelector('.selected')) document.querySelector('.selected').classList.remove('selected')
const change = e.key === 'ArrowDown' ? 1 : -1
selectedResult += change
if (selectedResult < -1) selectedResult = results.length - 1
else if (selectedResult > results.length - 1) selectedResult = -1
if (selectedResult !== -1) autocompleteNode.children[selectedResult].classList.add('selected')
}
})
searchNode.addEventListener('input', function (e) {
searchNode.className = ''
autocompleteNode.innerHTML = ''
if (inputNode.value.trim() === '') return
selectedResult = -1
results = fuzzy.filter(removeDiacritics(inputNode.value), users, {
extract: function (el) { return removeDiacritics(el.value) }
}).slice(0, 7)
results.forEach(function (result) {
const resultNode = document.createElement('li')
resultNode.innerHTML = `${result.original.value}`
autocompleteNode.appendChild(resultNode)
})
})
searchNode.addEventListener('submit', submitForm)
function submitForm (e) {
if (e) e.preventDefault()
if (results.length !== 0) {
const indexInResult = selectedResult === -1 ? 0 : selectedResult
selectedUser = users[results[indexInResult].index]
}
if (selectedUser == null) return
updateFavNode()
inputNode.value = selectedUser.value
autocompleteNode.innerHTML = ''
inputNode.blur()
scheduleIframe.src = getURLOfUser(offset, selectedUser.type, selectedUser.index + 1)
let eventAction
switch (selectedUser.type) {
case 'c':
eventAction = 'Class'
break
case 't':
eventAction = 'Teacher'
break
case 'r':
eventAction = 'Room'
break
case 's':
eventAction = 'Student'
break
}
const eventLabel = selectedUser.value
ga(function () {
ga('send', { hitType: 'event', eventCategory: 'search', eventAction, eventLabel })
})
}
autocompleteNode.addEventListener('click', function (e) {
if (autocompleteNode.contains(e.target)) {
selectedResult = Array.prototype.indexOf.call(e.target.parentElement.childNodes, e.target)
submitForm()
}
})
prevButton.addEventListener('click', function () {
offset--
updateWeekText()
submitForm()
})
nextButton.addEventListener('click', function () {
offset++
updateWeekText()
submitForm()
})
inputNode.addEventListener('click', function () {
inputNode.select()
})
inputNode.addEventListener('blur', function () {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
if (!isSafari) {
inputNode.selectionStart = inputNode.selectionEnd = -1
}
})
searchNode.addEventListener('blur', function (e) {
autocompleteNode.innerHTML = ''
})
favNode.addEventListener('click', function () {
if (usersEqual(getCurrentFav(), selectedUser)) {
changeFav(false)
} else {
changeFav(true)
}
})
const currentFav = getCurrentFav()
if (currentFav) {
selectedUser = currentFav
inputNode.value = selectedUser.value
scheduleIframe.src = getURLOfUser(offset, selectedUser.type, selectedUser.index + 1)
updateFavNode()
let eventAction
switch (selectedUser.type) {
case 'c':
eventAction = 'Class'
break
case 't':
eventAction = 'Teacher'
break
case 'r':
eventAction = 'Room'
break
case 's':
eventAction = 'Student'
break
}
const eventLabel = selectedUser.value
ga(function () {
ga('send', { hitType: 'event', eventCategory: 'search fav', eventAction, eventLabel })
})
} else if (inputNode.value === '') {
searchNode.className = 'no-input'
inputNode.focus()
}
document.body.style = ''
window.easterEggs = easterEggs
|