-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_trailer.part
97 lines (83 loc) · 2.7 KB
/
index_trailer.part
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
</ul>
<script>
const ESCAPE = 27;
const RIGHT = 39;
const LEFT = 37;
const TARGET_CLASS = 'target';
const clickNavigationButton = (buttonClass) => {
const id = window.history.state && window.history.state.id;
if (id) {
const selector = `#${id} ${buttonClass}`;
const button = document.querySelector(selector);
button && button.click();
}
}
const openPhoto = (id, href) => {
const photo = document.getElementById(id);
const title = photo.getAttribute('title');
removeTargetClass();
photo.classList.add(TARGET_CLASS);
document.title = title;
if (href) {
window.history.pushState({id: id}, '', href);
}
}
const closePhoto = (href) => {
const title = document.querySelector('head title').getAttribute('data-title');
removeTargetClass();
document.title = title;
if (href) {
window.history.pushState({}, '', href);
}
}
const removeTargetClass = () => {
let targets = document.querySelectorAll(`.${TARGET_CLASS}`);
targets.forEach((target) => {
target.classList.remove(TARGET_CLASS);
});
}
const handleClick = (selector, event, callback) => {
if (event.target.matches(selector)) {
callback();
event.preventDefault();
}
}
const handleKey = (keyCode, event, callback) => {
if (event.keyCode === keyCode) {
callback();
event.preventDefault();
}
}
window.onpopstate = function(event) {
if (event.state && event.state.id) {
const id = event.state.id;
openPhoto(id, null);
} else {
closePhoto(null);
}
}
document.addEventListener('keydown', (event) => {
handleKey(ESCAPE, event, () => {
clickNavigationButton('.close');
});
handleKey(RIGHT, event, () => {
clickNavigationButton('.next');
});
handleKey(LEFT, event, () => {
clickNavigationButton('.previous');
});
});
document.addEventListener('click', (event) => {
handleClick('[data-target][href]', event, () => {
const id = event.target.getAttribute('data-target');
const href = event.target.getAttribute('href');
openPhoto(id, href);
});
handleClick('[href].close', event, () => {
const href = event.target.getAttribute('href');
closePhoto(href);
});
});
lazyload();
</script>
</body></html>