-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
92 lines (78 loc) · 2.84 KB
/
controller.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
class Controller {
constructor() {
}
setCallbacks() {
this.setForwardCallbacks();
this.setBackwardCallbacks();
this.setStartCallbacks();
}
setForwardCallbacks() {
document.querySelectorAll('[data-app-controls-forward]').forEach(element =>
element.addEventListener("click", { handleEvent: this.goForwardEventHandler, controller: this }, false)
);
}
setBackwardCallbacks() {
document.querySelectorAll('[data-app-controls-back]').forEach(element =>
element.addEventListener("click", { handleEvent: this.goBackwardEventHandler, controller: this }, false)
);
}
setStartCallbacks() {
document.querySelectorAll('[data-app-controls-start]').forEach(element =>
element.addEventListener("click", { handleEvent: this.goStartEventHandler, controller: this }, false)
);
}
goForwardEventHandler(e) {
this.controller.currentPage += 1;
this.controller.showCurrentPage();
}
goBackwardEventHandler(e) {
this.controller.currentPage -= 1;
this.controller.showCurrentPage();
}
goStartEventHandler(e) {
window.history.pushState({}, document.title, "/");
this.controller.currentPage = 0;
this.controller.showCurrentPage();
}
goForward() {
this.currentPage += 1;
this.showCurrentPage();
}
goBackward() {
this.currentPage -= 1;
this.showCurrentPage();
}
goStart() {
window.history.pushState({}, document.title, "/");
this.currentPage = 0;
this.showCurrentPage();
}
showCurrentPage() {
document.querySelectorAll('[data-app-page]').forEach(element => {
if (this.currentPage == element.getAttribute("data-app-page")) {
element.classList.remove("app-page-hidden");
let onShowCallback = element.getAttribute("data-on-show");
if (onShowCallback) {
window[onShowCallback]();
}
}
else if (!element.classList.contains("app-page-hidden")) {
element.classList.add("app-page-hidden");
let onHideCallback = element.getAttribute("data-on-hide");
if (onHideCallback) {
window[onHideCallback]();
}
}
});
}
startApp() {
this.currentPage = 0;
this.setCallbacks();
let appElement = document.getElementById('app');
let onLoadCallback = appElement.getAttribute("data-on-load");
if (onLoadCallback) {
window[onLoadCallback]();
}
this.showCurrentPage();
}
}