File : toolbars/baseToolbar/ButtonHTMLElementTouchEL.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 toolbar buttons
30
*/
31
/* ------------------------------------------------------------------------------------------------------------------------- */
32
33
class ButtonHTMLElementTouchEL {
34
35
    /**
36
    A reference to the BaseToolbar
37
    @type {BaseToolbar}
38
    */
39
40
    #baseToolbar;
41
42
    /**
43
    A reference to the toolbarItemsContainer of the BaseToolbar class
44
    @type {toolbarItemsContainer}
45
    */
46
47
    #toolbarItemsContainer;
48
49
    /**
50
    The y position of the touchstart event
51
    @type {Number}
52
    */
53
54
    #touchButtonStartY;
55
56
    /**
57
    A constant with the maximum delta y between the touchstart and touchend events for
58
    the event will be considered as a click event
59
    @type {Number}
60
    */
61
62
    // eslint-disable-next-line no-magic-numbers
63
    static get #MAX_DELTA_Y ( ) { return 5; }
64
65
    /**
66
    The constructor
67
    @param {BaseToolbar} baseToolbar A reference to the BaseToolbar
68
    @param {toolbarItemsContainer} toolbarItemsContainer A reference to the toolbarItemsContainer object
69
     of the BaseToolbar class
70
    */
71
72
    constructor ( baseToolbar, toolbarItemsContainer ) {
73
        Object.freeze ( this );
74
        this.#baseToolbar = baseToolbar;
75
        this.#toolbarItemsContainer = toolbarItemsContainer;
76
    }
77
78
    /**
79
    Event listener method
80
    @param {Event} touchEvent The event to handle
81
    */
82
83
    handleEvent ( touchEvent ) {
84
        switch ( touchEvent.type ) {
85
        case 'touchstart' :
86
            if ( ONE === touchEvent.changedTouches.length ) {
87
                const touch = touchEvent.changedTouches.item ( ZERO );
88
                this.#touchButtonStartY = touch.screenY;
89
            }
90
            break;
91
        case 'touchend' :
92
            if ( ONE === touchEvent.changedTouches.length ) {
93
                const touch = touchEvent.changedTouches.item ( ZERO );
94
                if ( ButtonHTMLElementTouchEL.#MAX_DELTA_Y > Math.abs ( touch.screenY - this.#touchButtonStartY ) ) {
95
                    touchEvent.stopPropagation ( );
96
                    touchEvent.preventDefault ( );
97
                    this.#toolbarItemsContainer
98
                        .toolbarItemsArray [ Number.parseInt ( touchEvent.target.dataset.tanItemId ) ]
99
                        .doAction ( );
100
                    this.#baseToolbar.hide ( );
101
                }
102
            }
103
            break;
104
        default :
105
            break;
106
        }
107
    }
108
}
109
110
export default ButtonHTMLElementTouchEL;
111
112
/* --- End of file --------------------------------------------------------------------------------------------------------- */
113