aboutsummaryrefslogtreecommitdiff
path: root/beamer/viewer/stage/actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'beamer/viewer/stage/actions.js')
-rw-r--r--beamer/viewer/stage/actions.js121
1 files changed, 121 insertions, 0 deletions
diff --git a/beamer/viewer/stage/actions.js b/beamer/viewer/stage/actions.js
new file mode 100644
index 0000000..601a441
--- /dev/null
+++ b/beamer/viewer/stage/actions.js
@@ -0,0 +1,121 @@
1/*
2 * Beamer Viewer, a web-based PDF presentation viewer
3 * Copyright (C) 2018 Pacien TRAN-GIRARD
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19"use strict";
20
21class ActionEventHandler {
22 constructor(onNext, onPrevious) {
23 this.onNext = onNext;
24 this.onPrevious = onPrevious;
25 }
26}
27
28class KeyboardEventHandler extends ActionEventHandler {
29 register(window) {
30 const self = this;
31 window.addEventListener("keydown", function(event) {
32 self._onCommand(event);
33 })
34 }
35
36 _onCommand(keyboardEvent) {
37 switch (keyboardEvent.key) {
38 case "Enter":
39 case " ":
40 case "ArrowRight":
41 case "n":
42 return this.onNext();
43
44 case "ArrowLeft":
45 case "p":
46 return this.onPrevious();
47 }
48 }
49}
50
51class MouseClickEventHandler extends ActionEventHandler {
52 register(window) {
53 const self = this;
54 window.addEventListener("click", function(event) {
55 self._onCommand(event);
56 })
57 }
58
59 _onCommand(mouseEvent) {
60 this.onNext();
61 }
62}
63
64class TouchSwipeEventHandler extends ActionEventHandler {
65 constructor(onNext, onPrevious) {
66 super(onNext, onPrevious);
67 this.touchStartEvent = null;
68 this.touchMoveEvent = null;
69 }
70
71 register(window) {
72 const self = this;
73
74 window.addEventListener("touchstart", function(event) {
75 event.preventDefault();
76 self._onTouchStart(event);
77 });
78
79 window.addEventListener("touchmove", function(event) {
80 event.preventDefault();
81 self._onTouchMove(event);
82 });
83
84 window.addEventListener("touchend", function(event) {
85 event.preventDefault();
86 self._onTouchEnd();
87 });
88
89 window.addEventListener("touchcancel", function(event) {
90 event.preventDefault();
91 });
92 }
93
94 _onTouchStart(touchEvent) {
95 this.touchStartEvent = touchEvent;
96 }
97
98 _onTouchMove(touchEvent) {
99 this.touchMoveEvent = touchEvent;
100 }
101
102 _onTouchEnd() {
103 if (this.touchStartEvent == null || this.touchMoveEvent == null) return;
104
105 const touchDown = this._xCoordinate(this.touchStartEvent);
106 const touchUp = this._xCoordinate(this.touchMoveEvent);
107 const xDelta = touchDown - touchUp;
108
109 if (xDelta > 0)
110 this.onNext();
111 else if (xDelta < 0)
112 this.onPrevious();
113
114 this.touchStartEvent = null;
115 this.touchMoveEvent = null;
116 }
117
118 _xCoordinate(touchEvent) {
119 return touchEvent.touches[0].clientX; // first finger
120 }
121}