File : main/travelNotes/TravelNotes.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 theConfig from '../../data/Config.js';
26
import theTravelNotesData from '../../data/TravelNotesData.js';
27
import theRouteEditor from '../../core/RouteEditor.js';
28
import theApiKeysManager from '../../core/ApiKeysManager.js';
29
import Travel from '../../data/Travel.js';
30
import ViewerFileLoader from '../../core/ViewerFileLoader.js';
31
import { theAppVersion } from '../../data/Version.js';
32
import theEventDispatcher from '../../core/lib/EventDispatcher.js';
33
import theMapLayersManager from '../../core/MapLayersManager.js';
34
import theMapLayersToolbar from '../../toolbars/mapLayersToolbar/MapLayersToolbar.js';
35
import theMouseUI from '../../uis/mouseUI/MouseUI.js';
36
import theAttributionsUI from '../../uis/attributionsUI/AttributionsUI.js';
37
import theTravelNotesToolbar from '../../toolbars/travelNotesToolbar/TravelNotesToolbar.js';
38
import theErrorsUI from '../../uis/errorsUI/ErrorsUI.js';
39
import theTranslator from '../../core/uiLib/Translator.js';
40
import theFullScreenUI from '../../uis/fullScreenUI/FullScreenUI.js';
41
import theProvidersToolbar from '../../toolbars/providersToolbar/ProvidersToolbar.js';
42
import MapMouseELs from '../../core/mapEditor/mapMouseELs/MapMouseELs.js';
43
import { LAT_LNG, TWO, SAVE_STATUS, HTTP_STATUS_OK } from '../Constants.js';
44
import theNoteEditor from '../../core/NoteEditor.js';
45
46
/* ------------------------------------------------------------------------------------------------------------------------- */
47
/**
48
This class is the entry point of the application.
49
50
See theTravelNotes for the one and only one instance of this class
51
*/
52
/* ------------------------------------------------------------------------------------------------------------------------- */
53
54
class TravelNotes {
55
56
    /**
57
    Guard to avoid a second upload
58
    @type {Boolean}
59
    */
60
61
    #travelNotesLoaded = false;
62
63
    /**
64
    The constructor
65
    */
66
67
    constructor ( ) {
68
        Object.freeze ( this );
69
    }
70
71
    /**
72
    This method load TravelNotes and open a read only map passed trought the url.
73
    This method can only be executed once. Others call will be ignored.
74
    @param {String} travelUrl The url of the TaN file to open
75
    */
76
77
    async addReadOnlyTravel ( travelUrl ) {
78
79
        if ( this.#travelNotesLoaded ) {
80
            return;
81
        }
82
83
        this.#travelNotesLoaded = true;
84
85
        theAttributionsUI.createUI ( );
86
        theMapLayersManager.setMapLayer ( 'OSM - Color' );
87
        const travelResponse = await fetch ( travelUrl );
88
        if ( HTTP_STATUS_OK === travelResponse.status && travelResponse.ok ) {
89
            new ViewerFileLoader ( ).openDistantFile ( await travelResponse.json ( ) );
90
        }
91
        else {
92
            theTravelNotesData.map.setView ( [ LAT_LNG.defaultValue, LAT_LNG.defaultValue ], TWO );
93
            document.title = 'Travel & Notes';
94
        }
95
    }
96
97
    /**
98
    This method load TravelNotes and open an empty read and write map.
99
    This method can only be executed once. Others call will be ignored.
100
    @param {?Number} latUrl the lat found in the url parameters
101
    @param {?Number} lonUrl the lon found in the url parameters
102
    */
103
104
    addToolbarsMenusUIs ( latUrl, lonUrl ) {
105
106
        if ( this.#travelNotesLoaded ) {
107
            return;
108
        }
109
110
        this.#travelNotesLoaded = true;
111
112
        // Loading the user interfaces...
113
        document.title = 'Travel & Notes';
114
        theTravelNotesData.map.on ( 'contextmenu', MapMouseELs.handleContextMenuEvent );
115
        theTravelNotesData.map.on ( 'click', MapMouseELs.handleClickEvent );
116
        theTravelNotesData.map.setView ( [ theConfig.map.center.lat, theConfig.map.center.lng ], theConfig.map.zoom );
117
118
        // ... the attributions UI...
119
        theAttributionsUI.createUI ( );
120
121
        // ... the mouse UI
122
        theMouseUI.createUI ( );
123
        theMouseUI.saveStatus = SAVE_STATUS.saved;
124
125
        // ...help UI
126
        theErrorsUI.showHelp (
127
            '<p>' + theTranslator.getText ( 'Help - Continue with interface1' ) + '</p>' +
128
            '<p>' + theTranslator.getText ( 'Help - Continue with interface2' ) + '</p>'
129
        );
130
131
        // ... the map layers toolbar ...
132
        theMapLayersToolbar.createUI ( );
133
        theMapLayersManager.setMapLayer ( 'OSM - Color' );
134
135
        // ... the Travel & Notes toolbar
136
        theTravelNotesToolbar.createUI ( );
137
138
        // ...the providers toolbar
139
        theProvidersToolbar.createUI ( );
140
141
        // ... loading the Api keys
142
        theApiKeysManager.setKeysFromServerFile ( );
143
144
        // /// loading a new empty travel
145
        theTravelNotesData.travel.jsonObject = new Travel ( ).jsonObject;
146
147
        // ... start edition of the route
148
        if ( theConfig.travelNotes.startupRouteEdition ) {
149
            theRouteEditor.editRoute ( theTravelNotesData.travel.routes.first.objId );
150
        }
151
152
        // ...updating the route list and roadbook
153
        theEventDispatcher.dispatch ( 'updatetravelproperties' );
154
        theEventDispatcher.dispatch ( 'updateroadbook' );
155
156
        // ... full screen UI
157
        theFullScreenUI.show ( );
158
159
        if ( latUrl && lonUrl ) {
160
            theNoteEditor.newUrlNote ( [ latUrl, lonUrl ] );
161
        }
162
    }
163
164
    /**
165
    This method add a provider. Used by plugins.
166
    @param {class} providerClass The provider to add
167
    */
168
169
    addProvider ( providerClass ) {
170
        theApiKeysManager.addProvider ( providerClass );
171
    }
172
173
    /**
174
    Show an info, using theErrorsUI. Used by plugins.
175
    @param {String} info The info to show
176
    */
177
178
    showInfo ( info ) {
179
        theErrorsUI.showInfo ( info );
180
    }
181
182
    /**
183
    The overpassApi url to use by plugins
184
    @type {String}
185
    */
186
187
    get overpassApiUrl ( ) { return theConfig.overpassApi.url; }
188
189
    /**
190
    The Leaflet map object
191
    @type {LeafletObject}
192
    */
193
194
    get map ( ) { return theTravelNotesData.map; }
195
196
    /**
197
    theTravelNotes version
198
    @type {String}
199
    */
200
201
    get version ( ) { return theAppVersion; }
202
}
203
204
/* ------------------------------------------------------------------------------------------------------------------------- */
205
/**
206
The one and only one instance of TravelNotes class
207
@type {TravelNotes}
208
209
/* ------------------------------------------------------------------------------------------------------------------------- */
210
211
const theTravelNotes = new TravelNotes ( );
212
213
export default theTravelNotes;
214
215
/* --- End of file --------------------------------------------------------------------------------------------------------- */
216