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