|
130 | 130 | <script src="//unpkg.com/mithril@^1.1.6/mithril.js"></script>
|
131 | 131 | <script>
|
132 | 132 | // setup
|
133 |
| - var Direction = {LEFT: 0, RIGHT: 1}; |
134 | 133 | var Theme = {BLACK: 0, WHITE: 1, GRADIENT: 2, SHOWROOM: 3};
|
135 |
| - var Time = {TAP: 250, HOLD: 500}; |
| 134 | + var TouchDirection = {LEFT: 0, CENTER: 1, RIGHT: 2}; |
| 135 | + var TouchType = {TAP: 0, PRESS: 1, HOLD: 2}; |
136 | 136 |
|
137 | 137 | var Fullscreen = {
|
138 | 138 | isEnabled: function() {
|
|
171 | 171 | index: 0,
|
172 | 172 | pointer: true,
|
173 | 173 | theme: Theme.GRADIENT,
|
174 |
| - touch: {direction: null, time: null}, |
| 174 | + touch: {direction: null, start: null}, |
175 | 175 |
|
176 | 176 | setIndex: function(value) {
|
177 | 177 | if (value >= 0 && value < state.slides.length) {
|
|
189 | 189 | }
|
190 | 190 | },
|
191 | 191 | setTouch: function(value) {
|
192 |
| - if (value in Object.values(Direction)) { |
| 192 | + if (value in Object.values(TouchDirection)) { |
193 | 193 | state.touch.direction = value;
|
194 |
| - state.touch.time = Date.now(); |
| 194 | + state.touch.start = Date.now(); |
195 | 195 | }
|
196 | 196 | },
|
197 | 197 |
|
|
200 | 200 | },
|
201 | 201 | clearTouch: function() {
|
202 | 202 | state.touch.direction = null;
|
203 |
| - state.touch.time = null; |
| 203 | + state.touch.start = null; |
204 | 204 | },
|
205 | 205 | currentSlide: function() {
|
206 | 206 | return state.slides.length > 0 ? state.slides[state.index] : {};
|
|
230 | 230 | nextTheme: function() {
|
231 | 231 | state.setTheme((state.theme + 1) % Object.keys(Theme).length);
|
232 | 232 | },
|
233 |
| - touchTime: function() { |
234 |
| - var startTime = state.touch.time; |
235 |
| - return startTime ? Date.now() - startTime : 0; |
| 233 | + touchType: function() { |
| 234 | + if (!state.touch.start) { |
| 235 | + return null; |
| 236 | + } |
| 237 | + var elapsedTime = Date.now() - state.touch.start; |
| 238 | + if (elapsedTime > 500) { |
| 239 | + return TouchType.HOLD; |
| 240 | + } else if (elapsedTime > 250) { |
| 241 | + return TouchType.PRESS; |
| 242 | + } else { |
| 243 | + return TouchType.TAP; |
| 244 | + } |
236 | 245 | },
|
237 | 246 | togglePointer: function() {
|
238 | 247 | state.pointer = !state.pointer;
|
|
280 | 289 | var touch = event.touches[0];
|
281 | 290 | var pageCenter = document.body.clientWidth / 2;
|
282 | 291 | state.setTouch(
|
283 |
| - touch.pageX < pageCenter ? Direction.LEFT : Direction.RIGHT |
| 292 | + touch.pageX < pageCenter ? TouchDirection.LEFT : TouchDirection.RIGHT |
284 | 293 | );
|
285 | 294 | },
|
286 | 295 | handleTouchmove: function(event) {
|
|
292 | 301 | if (event.touches.length > 0) {
|
293 | 302 | return;
|
294 | 303 | }
|
295 |
| - var time = state.touchTime(); |
296 |
| - if (time > Time.HOLD) { |
| 304 | + var type = state.touchType(); |
| 305 | + var direction = state.touch.direction; |
| 306 | + if (type === TouchType.HOLD) { |
297 | 307 | state.nextTheme();
|
298 |
| - } else if (time > Time.TAP) { |
299 |
| - } else if (state.touch.direction === Direction.LEFT) { |
| 308 | + } else if (type === TouchType.TAP && direction === TouchDirection.LEFT) { |
300 | 309 | this.play(state.backward);
|
301 | 310 | m.redraw();
|
302 |
| - } else if (state.touch.direction === Direction.RIGHT) { |
| 311 | + } else if (type === TouchType.TAP && direction === TouchDirection.RIGHT) { |
303 | 312 | this.play(state.forward);
|
304 | 313 | }
|
305 | 314 | state.clearTouch();
|
|
0 commit comments