File : core/GeoLocator.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 theEventDispatcher from './lib/EventDispatcher.js';
26
import theConfig from '../data/Config.js';
27
import theErrorsUI from '../uis/errorsUI/ErrorsUI.js';
28
import { GEOLOCATION_STATUS, ONE, TWO } from '../main/Constants.js';
29
import theTranslator from './uiLib/Translator.js';
30
31
/* ------------------------------------------------------------------------------------------------------------------------- */
32
/**
33
This class manage the geolocation
34
35
See theGeoLocator for the one and only one instance of this class
36
*/
37
/* ------------------------------------------------------------------------------------------------------------------------- */
38
39
class GeoLocator {
40
41
    /**
42
    The current status of the geoLocator.
43
    @type {GEOLOCATION_STATUS}
44
    */
45
46
    #status = navigator.geolocation ? GEOLOCATION_STATUS.inactive : GEOLOCATION_STATUS.disabled;
47
48
    /**
49
    The id returned by the
50
    [Geolocation.watchPosition ( )](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition)
51
    method
52
    @type {Number}
53
    */
54
55
    #watchId = null;
56
57
    /**
58
    Send an event to show the current position on the map
59
    @param {GeolocationPosition} position a JS GeolocationPosition object
60
    */
61
62
    #showPosition ( position ) {
63
        theEventDispatcher.dispatch ( 'geolocationpositionchanged', { position : position } );
64
    }
65
66
    /**
67
    Stop the geolocation
68
    */
69
70
    #stop ( ) {
71
        if ( GEOLOCATION_STATUS.active === this.#status ) {
72
            this.#status = GEOLOCATION_STATUS.inactive;
73
        }
74
75
        theEventDispatcher.dispatch ( 'geolocationstatuschanged', { status : this.#status } );
76
77
        if ( this.#watchId ) {
78
            navigator.geolocation.clearWatch ( this.#watchId );
79
            this.#watchId = null;
80
        }
81
    }
82
83
    /**
84
    Stop the geolocation because the user don't accept the geolocation or the geolocation
85
    is disabled on the device
86
    @param {GeolocationPositionError} positionError See GeolocationPositionError on mdn
87
    */
88
89
    #error ( positionError ) {
90
        if ( ONE === positionError.code ) {
91
            this.#status = GEOLOCATION_STATUS.refusedByUser;
92
        }
93
        if ( TWO === positionError.code ) {
94
            theErrorsUI.showError ( theTranslator.getText ( 'GeoLocator - Geolocation disabled on the device' ) );
95
        }
96
        this.#stop ( );
97
    }
98
99
    /**
100
    Start the geolocation
101
    */
102
103
    #start ( ) {
104
        this.#status = GEOLOCATION_STATUS.active;
105
        theEventDispatcher.dispatch ( 'geolocationstatuschanged', { status : this.#status } );
106
107
        if ( theConfig.geoLocation.watch ) {
108
            this.#watchId = navigator.geolocation.watchPosition (
109
                position => this.#showPosition ( position ),
110
                positionError => this.#error ( positionError ),
111
                theConfig.geoLocation.options
112
            );
113
        }
114
        else {
115
            navigator.geolocation.getCurrentPosition (
116
                position => this.#showPosition ( position ),
117
                positionError => this.#error ( positionError ),
118
                theConfig.geoLocation.options
119
            );
120
        }
121
    }
122
123
    /**
124
    The constructor
125
    */
126
127
    constructor ( ) {
128
        Object.freeze ( this );
129
    }
130
131
    /**
132
    The status of the geolocation
133
    @type {GEOLOCATION_STATUS}
134
    */
135
136
    get status ( ) { return this.#status; }
137
138
    /**
139
    Start or stop the geolocatiion, depending of the status
140
    @return {GEOLOCATION_STATUS} the status after the switch
141
    */
142
143
    switch ( ) {
144
        switch ( this.#status ) {
145
        case GEOLOCATION_STATUS.inactive :
146
            this.#start ( );
147
            break;
148
        case GEOLOCATION_STATUS.active :
149
            this.#stop ( );
150
            break;
151
        default :
152
            break;
153
        }
154
155
        return this.#status;
156
    }
157
}
158
159
/* ------------------------------------------------------------------------------------------------------------------------- */
160
/**
161
The one and only one instance of GeoLocator class
162
@type {GeoLocator}
163
*/
164
/* ------------------------------------------------------------------------------------------------------------------------- */
165
166
const theGeoLocator = new GeoLocator ( );
167
168
export default theGeoLocator;
169
170
/* --- End of file --------------------------------------------------------------------------------------------------------- */
171