summaryrefslogtreecommitdiff
path: root/client/js/util/touch.js
blob: 9903c711877d2df3c2718dbf17b37b78175e9b70 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"use strict";

const direction = {
    NONE: null,
    LEFT: "left",
    RIGHT: "right",
    DOWN: "down",
    UP: "up",
};

function getSwipeThresholdInPx() {
    // how big is the movement in pixels to be registered as swipe
    return Math.min(screen.width, screen.height) * 0.15;
}

function handleTouchStart(handler, evt) {
    const touchEvent = evt.touches[0];
    if (window.visualViewport.scale === 1) {
        handler._xStart = touchEvent.clientX;
        handler._yStart = touchEvent.clientY;
        handler._startScrollY = window.scrollY;
    }
}

function handleTouchMove(handler, evt) {
    if (!handler._xStart || !handler._yStart) {
        return;
    }
    if (window.visualViewport.scale > 1) {
        handler._xStart = null;
        handler._yStart = null;
        return;
    }

    const xDirection = handler._xStart - evt.touches[0].clientX;
    const yDirection = handler._yStart - evt.touches[0].clientY;
    let threshold = getSwipeThresholdInPx();

    if (Math.abs(xDirection) > Math.abs(yDirection)) {
        if (Math.abs(xDirection) < threshold) {
            return;
        } else if (xDirection > 0) {
            handler._direction = direction.LEFT;
        } else {
            handler._direction = direction.RIGHT;
        }
    } else {
        if (Math.abs(yDirection) < threshold) {
            return;
        } else if (yDirection > 0) {
            handler._direction = direction.UP;
        } else {
            handler._direction = direction.DOWN;
        }
    }
}

function handleTouchEnd(handler, evt) {
    evt.startScrollY = handler._startScrollY;
    switch (handler._direction) {
        case direction.NONE:
            return;
        case direction.LEFT:
            handler._swipeLeftTask(evt);
            break;
        case direction.RIGHT:
            handler._swipeRightTask(evt);
            break;
        case direction.DOWN:
            handler._swipeDownTask(evt);
            break;
        case direction.UP:
            handler._swipeUpTask(evt);
        // no default
    }

    handler._xStart = null;
    handler._yStart = null;
}

class Touch {
    constructor(
        target,
        swipeLeft = () => {},
        swipeRight = () => {},
        swipeUp = () => {},
        swipeDown = () => {}
    ) {
        this._target = target;

        this._swipeLeftTask = swipeLeft;
        this._swipeRightTask = swipeRight;
        this._swipeUpTask = swipeUp;
        this._swipeDownTask = swipeDown;

        this._xStart = null;
        this._yStart = null;
        this._direction = direction.NONE;

        this._target.addEventListener("touchstart", (evt) => {
            handleTouchStart(this, evt);
        });
        this._target.addEventListener("touchmove", (evt) => {
            handleTouchMove(this, evt);
        });
        this._target.addEventListener("touchend", (evt) => {
            handleTouchEnd(this, evt);
        });
    }
}

module.exports = Touch;