File : contextMenus/baseContextMenu/ContextMenuTouchEL.js

1
/*
2
Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/
3
4
This  program is free software;
5
you can redistribute it and/or modify it under the terms of the
6
GNU General Public License as published by the Free Software Foundation;
7
either version 3 of the License, or any later version.
8
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU General Public License for more details.
13
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
*/
18
/*
19
Changes:
20
    - v4.0.0:
21
        - created from v3.6.0
22
Doc reviewed 202208
23
 */
24
25
import { ZERO, ONE } from '../../main/Constants.js';
26
27
/* ------------------------------------------------------------------------------------------------------------------------- */
28
/**
29
Touch event listener for the context menus
30
*/
31
/* ------------------------------------------------------------------------------------------------------------------------- */
32
33
class ContextMenuTouchEL {
34
35
    /**
36
    The X position of the pointer of the previous event
37
    @type {?Number}
38
    */
39
40
    #clientX = null;
41
42
    /**
43
    The Y position of the pointer of the previous event
44
    @type {?Number}
45
    */
46
47
    #clientY = null;
48
49
    /**
50
    The minimal difference on the Y position between two events
51
    @type {Number}
52
    */
53
54
    // eslint-disable-next-line no-magic-numbers
55
    static get #MIN_DELTA_CLIENT_Y ( ) { return 100; }
56
57
    /**
58
    The maximal difference on the X position between two events
59
    @type {Number}
60
    */
61
    // eslint-disable-next-line no-magic-numbers
62
    static get #MAX_DELTA_CLIENT_X ( ) { return 30; }
63
64
    /**
65
    A reference to the menuOperator Object
66
    @type {BaseContextMenuOperator}
67
    */
68
69
    #menuOperator = null;
70
71
    /**
72
    The constructor
73
    @param {BaseContextMenuOperator} menuOperator A reference to the menuOperator Object
74
    */
75
76
    constructor ( menuOperator ) {
77
        Object.freeze ( this );
78
        this.#menuOperator = menuOperator;
79
    }
80
81
    /**
82
    Event listener method
83
    @param {Event} touchEvent The event to handle
84
    */
85
86
    handleEvent ( touchEvent ) {
87
        if ( ONE === touchEvent.changedTouches.length ) {
88
            const touch = touchEvent.changedTouches.item ( ZERO );
89
            switch ( touchEvent.type ) {
90
            case 'touchstart' :
91
                this.#clientX = touch.clientX;
92
                this.#clientY = touch.clientY;
93
                break;
94
            case 'touchend' :
95
                if ( this.#clientX && this.#clientY ) {
96
                    if (
97
                        ContextMenuTouchEL.#MIN_DELTA_CLIENT_Y < this.#clientY - touch.clientY
98
                            &&
99
                            ContextMenuTouchEL.#MAX_DELTA_CLIENT_X > Math.abs ( this.#clientX - touch.clientX )
100
                    ) {
101
                        this.#menuOperator.onCancelMenu ( );
102
                    }
103
                }
104
                this.#clientX = null;
105
                this.#clientY = null;
106
                break;
107
            case 'touchcancel' :
108
                this.#clientX = null;
109
                this.#clientY = null;
110
                break;
111
            default :
112
                break;
113
            }
114
        }
115
    }
116
}
117
118
export default ContextMenuTouchEL;
119
120
/* --- End of file --------------------------------------------------------------------------------------------------------- */
121