File : uis/mouseUI/MouseUI.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 theHTMLElementsFactory from '../../core/uiLib/HTMLElementsFactory.js';
26
import theTravelNotesData from '../../data/TravelNotesData.js';
27
import theConfig from '../../data/Config.js';
28
import theUtilities from '../../core/uiLib/Utilities.js';
29
import { ZERO, SAVE_STATUS, ONE } from '../../main/Constants.js';
30
31
/* ------------------------------------------------------------------------------------------------------------------------- */
32
/**
33
This class show the mouse position and the zoom on the screen
34
See theMouseUI for the one and only one instance of this class
35
*/
36
/* ------------------------------------------------------------------------------------------------------------------------- */
37
38
class MouseUI {
39
40
    /**
41
    The HTMLElement with the status, mouse and zoom infos
42
    @type {HTMLElement}
43
    */
44
45
    #mouseUIElement;
46
47
    /**
48
    the zoom plus button
49
    @type {HTMLElement}
50
    */
51
52
    #zoomPlusButton;
53
54
    /**
55
    the zoom minus button
56
    @type {HTMLElement}
57
    */
58
59
    #zoomMinusButton;
60
61
    /**
62
    The save status
63
    @type {String}
64
    */
65
66
    #saveStatus;
67
68
    /**
69
    The mouse position
70
    @type {String}
71
    */
72
73
    #mousePosition;
74
75
    /**
76
    The zoom factor
77
    @type {Number}
78
    */
79
80
    #zoom;
81
82
    /**
83
    The save timer id
84
    @type {Number}
85
    */
86
87
    #saveTimer;
88
89
    /**
90
    The time in milliseconds between the first change and the moment the SAVE_STATUS.notSaved is displayed
91
    @type {Number}
92
    */
93
94
    // eslint-disable-next-line no-magic-numbers
95
    static get #SAVE_TIME ( ) { return 300000; }
96
97
    /**
98
    Event listener for the zoom plus ans zoom minus buttons
99
    @param {Number} zoomIncrement The value to add to the zoom (normally -1 or 1)
100
    */
101
102
    #changeZoom ( zoomIncrement ) {
103
        theTravelNotesData.map.setZoom ( theTravelNotesData.map.getZoom ( ) + zoomIncrement );
104
    }
105
106
    /**
107
    Update the UI with the changed saveStatus, mouse position or zoom
108
    */
109
110
    #updateUI ( ) {
111
112
        // update text
113
        if ( this.#mouseUIElement ) {
114
            this.#mouseUIElement.textContent = '\u00a0' +
115
                this.#saveStatus + '\u00a0' +
116
                this.#mousePosition + '\u00a0-\u00a0Zoom\u00a0:\u00a0' +
117
                String ( this.#zoom ) + '\u00a0';
118
        }
119
120
        // update zoom buttons
121
        const map = theTravelNotesData.map;
122
        if ( map.getMaxZoom ( ) === this.#zoom ) {
123
            this.#zoomPlusButton.classList.add ( 'TravelNotes-MouseUI-Disabled' );
124
        }
125
        else {
126
            this.#zoomPlusButton.classList.remove ( 'TravelNotes-MouseUI-Disabled' );
127
        }
128
        if ( map.getMinZoom ( ) === this.#zoom ) {
129
            this.#zoomMinusButton.classList.add ( 'TravelNotes-MouseUI-Disabled' );
130
        }
131
        else {
132
            this.#zoomMinusButton.classList.remove ( 'TravelNotes-MouseUI-Disabled' );
133
        }
134
    }
135
136
    /**
137
    The constructor
138
    */
139
140
    constructor ( ) {
141
        Object.freeze ( this );
142
        this.#saveStatus = SAVE_STATUS.saved;
143
        this.#mousePosition = '';
144
        this.#zoom = ZERO;
145
        this.#saveTimer = null;
146
    }
147
148
    /**
149
    change the save status on the UI
150
    @param {String} saveStatus the new saveStatus
151
    */
152
153
    set saveStatus ( saveStatus ) {
154
155
        // Status unchanged... return directly
156
        if ( SAVE_STATUS.modified === saveStatus && SAVE_STATUS.notSaved === this.#saveStatus ) {
157
            return;
158
        }
159
        this.#saveStatus = saveStatus;
160
        if ( SAVE_STATUS.modified === saveStatus && ! this.#saveTimer ) {
161
162
            // Starting the timer
163
            this.#saveTimer = setTimeout (
164
                ( ) => this.#saveStatus = SAVE_STATUS.notSaved,
165
                MouseUI.#SAVE_TIME
166
            );
167
        }
168
        else if ( SAVE_STATUS.saved === saveStatus && this.#saveTimer ) {
169
170
            // clear the timer
171
            clearTimeout ( this.#saveTimer );
172
            this.#saveTimer = null;
173
        }
174
        this.#updateUI ( );
175
    }
176
177
    /**
178
    creates the user interface
179
    */
180
181
    createUI ( ) {
182
183
        // HTML creation
184
        const mouseUImainElement =
185
            theHTMLElementsFactory.create ( 'div', { id : 'TravelNotes-MouseUI' }, document.body );
186
        this.#zoomMinusButton = theHTMLElementsFactory.create (
187
            'span',
188
            {
189
                id : 'TravelNotes-MouseUI-ZoomMinus',
190
                textContent : '▼'
191
            },
192
            mouseUImainElement
193
        );
194
        this.#zoomMinusButton.addEventListener ( 'click', ( ) => this.#changeZoom ( -ONE ) );
195
        this.#mouseUIElement = theHTMLElementsFactory.create ( 'span', null, mouseUImainElement );
196
        this.#zoomPlusButton = theHTMLElementsFactory.create (
197
            'span',
198
            {
199
                id : 'TravelNotes-MouseUI-ZoomPlus',
200
                textContent : '▲'
201
            },
202
            mouseUImainElement
203
        );
204
        this.#zoomPlusButton.addEventListener ( 'click', ( ) => this.#changeZoom ( ONE ) );
205
206
        // init vars for mouse and zoom
207
        this.#zoom = theTravelNotesData.map.getZoom ( );
208
        this.#mousePosition =
209
            theUtilities.formatLat ( theConfig.map.center.lat ) +
210
            '\u00a0-\u00a0' +
211
            theUtilities.formatLng ( theConfig.map.center.lng );
212
213
        // Event listeners for mouse
214
        theTravelNotesData.map.on (
215
            'mousemove',
216
            mouseMoveEvent => {
217
                this.#mousePosition =
218
                    theUtilities.formatLatLng ( [ mouseMoveEvent.latlng.lat, mouseMoveEvent.latlng.lng ] );
219
                this.#updateUI ( );
220
            }
221
        );
222
223
        // Event listeners for zoom
224
        theTravelNotesData.map.on (
225
            'zoomend',
226
            ( ) => {
227
                this.#zoom = theTravelNotesData.map.getZoom ( );
228
                this.#updateUI ( );
229
            }
230
        );
231
    }
232
}
233
234
/* ------------------------------------------------------------------------------------------------------------------------- */
235
/**
236
The one and only one instance of MouseUI class
237
@type {MouseUI}
238
*/
239
/* ------------------------------------------------------------------------------------------------------------------------- */
240
241
const theMouseUI = new MouseUI ( );
242
243
export default theMouseUI;
244
245
/* --- End of file --------------------------------------------------------------------------------------------------------- */
246