-
Notifications
You must be signed in to change notification settings - Fork 498
/
ui.ts
48 lines (40 loc) · 1.18 KB
/
ui.ts
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
const filtersButtons = Array.prototype.slice.call(
document.querySelectorAll('[data-action="open-overlay"]')
);
const closeOverlayButtons = Array.prototype.slice.call(
document.querySelectorAll('[data-action="close-overlay"]')
);
const header = document.querySelector('#header');
const resultsContainer = document.querySelector('.container-results');
function openFilters() {
document.body.classList.add('filtering');
window.scrollTo(0, 0);
window.addEventListener('keyup', onKeyUp);
window.addEventListener('click', onClick);
}
function closeFilters() {
document.body.classList.remove('filtering');
resultsContainer!.scrollIntoView();
window.removeEventListener('keyup', onKeyUp);
window.removeEventListener('click', onClick);
}
function onKeyUp(event: KeyboardEvent) {
if (event.key !== 'Escape') {
return;
}
closeFilters();
}
function onClick(event: MouseEvent) {
if (event.target !== header) {
return;
}
closeFilters();
}
export function attachEventListeners() {
filtersButtons.forEach(button => {
button.addEventListener('click', openFilters);
});
closeOverlayButtons.forEach(button => {
button.addEventListener('click', closeFilters);
});
}