File : core/FileLoader.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 theTranslator from './uiLib/Translator.js';
26
import theTravelNotesData from '../data/TravelNotesData.js';
27
import GpxParser from './lib/GpxParser.js';
28
import theErrorsUI from '../uis/errorsUI/ErrorsUI.js';
29
import theMouseUI from '../uis/mouseUI/MouseUI.js';
30
import theMapLayersManager from './MapLayersManager.js';
31
import theRouteEditor from './RouteEditor.js';
32
import FileCompactor from './lib/FileCompactor.js';
33
import theEventDispatcher from './lib/EventDispatcher.js';
34
import theProfileDialogsManager from './ProfileDialogsManager.js';
35
import Zoomer from './Zoomer.js';
36
import Travel from '../data/Travel.js';
37
38
import { INVALID_OBJ_ID, ROUTE_EDITION_STATUS, SAVE_STATUS } from '../main/Constants.js';
39
40
/* ------------------------------------------------------------------------------------------------------------------------- */
41
/**
42
This class load a file from the computer disk and display the travel
43
*/
44
/* ------------------------------------------------------------------------------------------------------------------------- */
45
46
class FileLoader {
47
48
    /**
49
    Display the travel and fires event for updating the map and the UI
50
    */
51
52
    #display ( ) {
53
54
        // the map is cleaned
55
        theEventDispatcher.dispatch ( 'removeallobjects' );
56
57
        // document title
58
        document.title =
59
            'Travel & Notes' +
60
            ( '' === theTravelNotesData.travel.name ? '' : ' - ' + theTravelNotesData.travel.name );
61
62
        // displaying all not edited routes
63
        const routesIterator = theTravelNotesData.travel.routes.iterator;
64
        while ( ! routesIterator.done ) {
65
            if ( ROUTE_EDITION_STATUS.notEdited === routesIterator.value.editionStatus ) {
66
                theEventDispatcher.dispatch (
67
                    'routeupdated',
68
                    {
69
                        removedRouteObjId : INVALID_OBJ_ID,
70
                        addedRouteObjId : routesIterator.value.objId
71
                    }
72
                );
73
            }
74
        }
75
76
        // displaying the edited route if any
77
        if ( INVALID_OBJ_ID !== theTravelNotesData.editedRouteObjId ) {
78
            theEventDispatcher.dispatch (
79
                'routeupdated',
80
                {
81
                    removedRouteObjId : INVALID_OBJ_ID,
82
                    addedRouteObjId : theTravelNotesData.travel.editedRoute.objId
83
                }
84
            );
85
        }
86
87
        // displaying travel notes
88
        const notesIterator = theTravelNotesData.travel.notes.iterator;
89
        while ( ! notesIterator.done ) {
90
            theEventDispatcher.dispatch (
91
                'noteupdated',
92
                {
93
                    removedNoteObjId : INVALID_OBJ_ID,
94
                    addedNoteObjId : notesIterator.value.objId
95
                }
96
            );
97
        }
98
        theEventDispatcher.dispatch ( 'updatetravelnotes' );
99
100
        // zoom on travel
101
        new Zoomer ( ).zoomToTravel ( );
102
103
        // Setting the correct map
104
        theMapLayersManager.setMapLayer ( theTravelNotesData.travel.layerName );
105
106
        // Changing provider and transit mode if an edited route is found and if possible
107
        if ( INVALID_OBJ_ID !== theTravelNotesData.editedRouteObjId ) {
108
            const providerName = theTravelNotesData.travel.editedRoute.itinerary.provider;
109
            if (
110
                ( '' !== providerName )
111
                &&
112
                ! theTravelNotesData.providers.get ( providerName.toLowerCase ( ) )
113
            ) {
114
                theErrorsUI.showError (
115
                    theTranslator.getText (
116
                        'FileLoader - Not possible to select as provider',
117
                        { provider : providerName }
118
                    )
119
                );
120
            }
121
            else {
122
123
                // Provider and transit mode are changed in the itinerary editor
124
                theEventDispatcher.dispatch ( 'setprovider', { provider : providerName } );
125
126
                const transitMode = theTravelNotesData.travel.editedRoute.itinerary.transitMode;
127
                if ( '' !== transitMode ) {
128
                    theEventDispatcher.dispatch ( 'settransitmode', { transitMode : transitMode } );
129
                }
130
            }
131
        }
132
133
        theRouteEditor.chainRoutes ( );
134
135
        // Editors and HTML pages are filled
136
        theEventDispatcher.dispatch ( 'updatetravelproperties' );
137
        theEventDispatcher.dispatch ( 'updateroadbook' );
138
    }
139
140
    /**
141
    The constructor
142
    */
143
144
    constructor ( ) {
145
        Object.freeze ( this );
146
    }
147
148
    /**
149
    Open a local file and display the content of the file
150
    @param {String} fileContent The xml content of the selected file
151
    */
152
153
    openLocalGpxFile ( fileContent ) {
154
155
        // Closing all profiles
156
        theProfileDialogsManager.deleteAllProfiles ( );
157
158
        // Parsing the gpx
159
        theTravelNotesData.travel.jsonObject = new GpxParser ( ).parse ( fileContent ).jsonObject;
160
        theTravelNotesData.editedRouteObjId = INVALID_OBJ_ID;
161
162
        // display the travel
163
        this.#display ( );
164
165
        // Updating theMouseUI
166
        theMouseUI.saveStatus = SAVE_STATUS.saved;
167
168
    }
169
170
    /**
171
    Open a local file and display the content of the file
172
    @param {String} fileContent The json content of the selected file
173
    */
174
175
    openLocalTrvFile ( fileContent ) {
176
177
        // Closing all profiles
178
        theProfileDialogsManager.deleteAllProfiles ( );
179
180
        const travelJsonObject = JSON.parse ( fileContent );
181
182
        // Decompress the json file content and uploading the travel in theTravelNotesData object
183
        new FileCompactor ( ).decompress ( travelJsonObject );
184
        theTravelNotesData.travel.jsonObject = travelJsonObject;
185
        theTravelNotesData.editedRouteObjId = INVALID_OBJ_ID;
186
        theTravelNotesData.travel.routes.forEach (
187
            route => {
188
                if ( ROUTE_EDITION_STATUS.notEdited !== route.editionStatus ) {
189
                    theTravelNotesData.editedRouteObjId = route.objId;
190
                }
191
            }
192
        );
193
194
        // display the travel
195
        this.#display ( );
196
197
        // Updating theMouseUI
198
        theMouseUI.saveStatus = SAVE_STATUS.saved;
199
    }
200
201
    /**
202
    Merge a Travel with the curently displayed Travel
203
    @param {Travel} mergedTravel The travel to merge
204
    */
205
206
    #mergeTravel ( mergedTravel ) {
207
208
        // routes are added with their notes
209
        const routesIterator = mergedTravel.routes.iterator;
210
        while ( ! routesIterator.done ) {
211
            theTravelNotesData.travel.routes.add ( routesIterator.value );
212
        }
213
214
        // travel notes are added
215
        const notesIterator = mergedTravel.notes.iterator;
216
        while ( ! notesIterator.done ) {
217
            theTravelNotesData.travel.notes.add ( notesIterator.value );
218
        }
219
220
        // display the travel
221
        this.#display ( );
222
223
        // Updating theMouseUI
224
        theMouseUI.saveStatus = SAVE_STATUS.modified;
225
    }
226
227
    /**
228
    Merge the content of a gpx file with the curently displayed Travel
229
    @param {String} fileContent The gpx file content to merge
230
    */
231
232
    mergeLocalGpxFile ( fileContent ) {
233
        this.#mergeTravel ( new GpxParser ( ).parse ( fileContent ) );
234
    }
235
236
    /**
237
    Merge the content of a TaN file with the currently displayed Travel
238
    @param {String} fileContent The TaN file content to merge
239
    */
240
241
    mergeLocalTrvFile ( fileContent ) {
242
243
        const travelJsonObject = JSON.parse ( fileContent );
244
245
        // Decompress the json file content and uploading the travel in a new Travel object
246
        new FileCompactor ( ).decompress ( travelJsonObject );
247
        const mergedTravel = new Travel ( );
248
        mergedTravel.jsonObject = travelJsonObject;
249
250
        this.#mergeTravel ( mergedTravel );
251
    }
252
}
253
254
export default FileLoader;
255
256
/* --- End of file --------------------------------------------------------------------------------------------------------- */
257