diff options
Diffstat (limited to 'pointless/viewer/stage')
-rw-r--r-- | pointless/viewer/stage/actions.js | 121 | ||||
-rw-r--r-- | pointless/viewer/stage/stage.js | 76 |
2 files changed, 197 insertions, 0 deletions
diff --git a/pointless/viewer/stage/actions.js b/pointless/viewer/stage/actions.js new file mode 100644 index 0000000..271d7b7 --- /dev/null +++ b/pointless/viewer/stage/actions.js | |||
@@ -0,0 +1,121 @@ | |||
1 | /* | ||
2 | * Pointless Viewer, a web-based Beamer 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 | |||
21 | class ActionEventHandler { | ||
22 | constructor(onNext, onPrevious) { | ||
23 | this.onNext = onNext; | ||
24 | this.onPrevious = onPrevious; | ||
25 | } | ||
26 | } | ||
27 | |||
28 | class 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 | |||
51 | class 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 | |||
64 | class 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 | } | ||
diff --git a/pointless/viewer/stage/stage.js b/pointless/viewer/stage/stage.js new file mode 100644 index 0000000..e9a2d38 --- /dev/null +++ b/pointless/viewer/stage/stage.js | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | * Pointless Viewer, a web-based Beamer 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 | |||
21 | class Stage { | ||
22 | constructor(onReady, onNext, onPrevious) { | ||
23 | this.audienceScreen = null; | ||
24 | this.presenterScreen = null; | ||
25 | |||
26 | this.projector = window.open(window.location.href, "_blank", "toolbar=0,location=0,menubar=0"); | ||
27 | if (this.projector == null) | ||
28 | alert("Please allow pop-ups, then refresh this page."); | ||
29 | |||
30 | const self = this; | ||
31 | this.projector.addEventListener("load", function() { | ||
32 | self.audienceScreen = new Screen(self.projector, false, false); | ||
33 | self.presenterScreen = new Screen(window, true, true); | ||
34 | self._watchDetach(); | ||
35 | onReady(); | ||
36 | }); | ||
37 | |||
38 | this.eventHandlers = [ | ||
39 | new KeyboardEventHandler(onNext, onPrevious), | ||
40 | new MouseClickEventHandler(onNext, onPrevious), | ||
41 | new TouchSwipeEventHandler(onNext, onPrevious) | ||
42 | ]; | ||
43 | |||
44 | this._registerEventHandler(window); | ||
45 | this._registerEventHandler(this.projector); | ||
46 | } | ||
47 | |||
48 | setPage(page) { | ||
49 | this.audienceScreen.setPage(page); | ||
50 | this.presenterScreen.setPage(page); | ||
51 | } | ||
52 | |||
53 | _registerEventHandler(window) { | ||
54 | if (window == null) return; | ||
55 | |||
56 | this.eventHandlers.forEach(function(eventHandler) { | ||
57 | eventHandler.register(window); | ||
58 | }); | ||
59 | } | ||
60 | |||
61 | _watchDetach() { | ||
62 | const self = this; | ||
63 | window.addEventListener("beforeunload", function() { | ||
64 | self._setMessage(self.projector, "Controller detached"); | ||
65 | }); | ||
66 | |||
67 | this.projector.addEventListener("beforeunload", function() { | ||
68 | self._setMessage(window, "Viewer detached"); | ||
69 | }); | ||
70 | } | ||
71 | |||
72 | _setMessage(window, message) { | ||
73 | const messageBar = window.document.getElementById("message"); | ||
74 | messageBar.textContent = message; | ||
75 | } | ||
76 | } | ||