File : data/TravelUpdater.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 { theDataVersion } from '../data/Version.js';
26
27
import { ZERO, ROUTE_EDITION_STATUS, ELEV, DISTANCE } from '../main/Constants.js';
28
29
/* ------------------------------------------------------------------------------------------------------------------------- */
30
/**
31
This class update a Travel contained in a json object and created with a previous version to the
32
current version
33
*/
34
/* ------------------------------------------------------------------------------------------------------------------------- */
35
36
class TravelUpdater {
37
38
    /* eslint-disable camelcase */
39
40
    /**
41
    The travel to update
42
    @type {JsonObject}
43
    */
44
45
    #jsonTravel;
46
47
    /**
48
    Update the routes when version is v1.0.0
49
    */
50
51
    #updateRoutesV_1_0_0 ( ) {
52
        this.#jsonTravel.routes.forEach (
53
            route => {
54
                route.dashArray = ZERO;
55
                route.hidden = false;
56
            }
57
        );
58
    }
59
60
    /**
61
    Update the routes when version is v1.4.0
62
    */
63
64
    #updateRoutesV_1_4_0 ( ) {
65
        this.#jsonTravel.routes.forEach (
66
            route => {
67
                route.edited = ROUTE_EDITION_STATUS.notEdited;
68
            }
69
        );
70
    }
71
72
    /**
73
    Update the travel when version is v1.4.0
74
    */
75
76
    #updateTravelV_1_4_0 ( ) {
77
        this.#updateRoutesV_1_4_0 ( );
78
        /* eslint-disable no-magic-numbers */
79
        this.#jsonTravel.editedRoute = {
80
            name : '',
81
            wayPoints : [
82
                { name : '', lat : 0, lng : 0, objId : 2, objType : { name : 'WayPoint', version : '1.4.0' } },
83
                { name : '', lat : 0, lng : 0, objId : 3, objType : { name : 'WayPoint', version : '1.4.0' } }
84
            ],
85
            notes : [],
86
            itinerary : {
87
                itineraryPoints : [],
88
                maneuvers : [],
89
                provider : '',
90
                transitMode : '',
91
                objId : 1,
92
                objType : { name : 'Itinerary', version : '1.4.0' }
93
            },
94
            width : 3,
95
            color : '#ff0000',
96
            dashArray : 0,
97
            chain : true,
98
            distance : 0,
99
            duration : 0,
100
            edited : ROUTE_EDITION_STATUS.notEdited,
101
            hidden : false,
102
            chainedDistance : 0,
103
            objId : 4,
104
            objType : { name : 'Route', version : '1.4.0' }
105
        };
106
        /* eslint-enable no-magic-numbers */
107
    }
108
109
    /**
110
    Update the travel when version is v1.5.0
111
    */
112
113
    #updateTravelV_1_5_0 ( ) {
114
        if ( this.#jsonTravel.userData.layerId ) {
115
116
            // old layersId from maps are converted to TravelNotes layerName
117
            const layerConvert =
118
                [
119
                    { layerId : '0', layerName : 'OSM - Color' },
120
                    { layerId : '1', layerName : 'OSM - Black and White' },
121
                    { layerId : '2', layerName : 'Thunderforest - Transport' },
122
                    { layerId : '3', layerName : 'Thunderforest - OpenCycleMap' },
123
                    { layerId : '4', layerName : 'Thunderforest - Outdoors' },
124
                    { layerId : '5', layerName : 'Esri - Aerial view' },
125
                    { layerId : '6', layerName : 'Kartverket - Norway' },
126
                    { layerId : '7', layerName : 'IGN-NGI - Belgium now' },
127
                    { layerId : '12', layerName : 'Thunderforest - Landscape' },
128
                    { layerId : '24', layerName : 'Lantmäteriet - Sweden' },
129
                    { layerId : '25', layerName : 'Maanmittauslaitos - Finland' }
130
                ].find ( layerConversion => layerConversion.layerId === this.#jsonTravel.userData.layerId );
131
            if ( layerConvert ) {
132
                this.#jsonTravel.layerName = layerConvert.layerName;
133
            }
134
            else {
135
                this.#jsonTravel.layerName = 'OSM - Color';
136
            }
137
        }
138
        else {
139
            this.#jsonTravel.layerName = 'OSM - Color';
140
        }
141
    }
142
143
    /**
144
    Update the itinerary points when version is v1.6.0
145
    @param {Array.<Object>} itineraryPoints A list with itinerary points to update
146
    */
147
148
    #updateItineraryPointsV_1_6_0 ( itineraryPoints ) {
149
        itineraryPoints.forEach (
150
            itineraryPoint => {
151
                itineraryPoint.elev = ELEV.defaultValue;
152
            }
153
        );
154
    }
155
156
    /**
157
    Update the itinerary when version is v1.6.0
158
    */
159
160
    #updateItineraryV_1_6_0 ( ) {
161
        this.#jsonTravel.routes.forEach (
162
            route => {
163
                route.itinerary.hasProfile = false;
164
                route.itinerary.ascent = ZERO;
165
                route.itinerary.descent = ZERO;
166
                this.#updateItineraryPointsV_1_6_0 ( route.itinerary.itineraryPoints );
167
            }
168
        );
169
        this.#jsonTravel.editedRoute.itinerary.hasProfile = false;
170
        this.#jsonTravel.editedRoute.itinerary.ascent = ZERO;
171
        this.#jsonTravel.editedRoute.itinerary.descent = ZERO;
172
        this.#updateItineraryPointsV_1_6_0 ( this.#jsonTravel.editedRoute.itinerary.itineraryPoints );
173
    }
174
175
    /**
176
    Update the maneuvers when version is v1.11.0
177
    @param {JsonObject} route The route containing the maneuvers
178
    */
179
180
    #updateManeuversV_1_11_0 ( route ) {
181
        route.itinerary.maneuvers.forEach (
182
            maneuver => {
183
                if ( 'kArriveDefault' === maneuver.iconName ) {
184
                    maneuver.distance = DISTANCE.defaultValue;
185
                }
186
            }
187
        );
188
    }
189
190
    /**
191
    Update the way points when version is v1.11.0
192
    @param {JsonObject} route The route containing the way points
193
    */
194
195
    #updateWayPointsV_1_11_0 ( route ) {
196
        route.wayPoints.forEach (
197
            wayPoint => {
198
                wayPoint.address = wayPoint.name;
199
                wayPoint.name = '';
200
            }
201
        );
202
    }
203
204
    /**
205
    Update the routes when version is v1.11.0
206
    */
207
208
    #updateRoutesV_1_11_0 ( ) {
209
        this.#jsonTravel.routes.forEach (
210
            route => {
211
                route.editionStatus = route.edited;
212
                this.#updateManeuversV_1_11_0 ( route );
213
                this.#updateWayPointsV_1_11_0 ( route );
214
            }
215
        );
216
        this.#jsonTravel.editedRoute.editionStatus = this.#jsonTravel.editedRoute.edited;
217
        this.#updateManeuversV_1_11_0 ( this.#jsonTravel.editedRoute );
218
        this.#updateWayPointsV_1_11_0 ( this.#jsonTravel.editedRoute );
219
    }
220
221
    /**
222
    Update the routes when version is v2.3.0
223
    */
224
225
    #updateRouteV_2_3_0 ( ) {
226
        this.#jsonTravel.routes.forEach (
227
            route => {
228
                route.dashIndex = route.dashArray;
229
            }
230
        );
231
        this.#jsonTravel.editedRoute.dashIndex = this.#jsonTravel.editedRoute.dashArray;
232
    }
233
234
    /**
235
    Update a note style when version is v1.13.0
236
    @param {String} somethingText A text containing HTML with style attributes
237
    */
238
239
    #UpdateStyleNoteV1_13_0 ( somethingText ) {
240
        const returnValue = somethingText
241
            .replaceAll ( /style='color:white;background-color:red'/g, 'class=\'TravelNotes-Note-WhiteRed\'' )
242
            .replaceAll ( /style='color:white;background-color:green'/g, 'class=\'TravelNotes-Note-WhiteGreen\'' )
243
            .replaceAll ( /style='color:white;background-color:blue'/g, 'class=\'TravelNotes-Note-WhiteBlue\'' )
244
            .replaceAll ( /style='color:white;background-color:brown'/g, 'class=\'TravelNotes-Note-WhiteBrown\'' )
245
            .replaceAll ( /style='color:white;background-color:black'/g, 'class=\'TravelNotes-Note-WhiteBlack\'' )
246
            .replaceAll ( /style='border:solid 0.1em'/g, 'class=\'TravelNotes-Note-BlackWhite\'' )
247
            .replaceAll ( /style='background-color:white;'/g, 'class=\'TravelNotes-Note-Knooppunt\'' )
248
            .replaceAll ( /style='fill:green;font:bold 120px sans-serif;'/g, '' )
249
            .replaceAll ( /style='fill:none;stroke:green;stroke-width:10;'/g, '' );
250
        return returnValue;
251
    }
252
253
    /**
254
    Update a note when version is v1.13.0
255
    @param {JsonObject} note The note to update
256
    */
257
258
    #updateNote_V1_13_0 ( note ) {
259
        if ( 'string' === typeof ( note.iconHeight ) ) {
260
            note.iconHeight = Number.parseInt ( note.iconHeight );
261
        }
262
        if ( 'string' === typeof ( note.iconWidth ) ) {
263
            note.iconWidth = Number.parseInt ( note.iconWidth );
264
        }
265
        note.iconContent = this.#UpdateStyleNoteV1_13_0 ( note.iconContent );
266
        note.popupContent = this.#UpdateStyleNoteV1_13_0 ( note.popupContent );
267
        note.tooltipContent = this.#UpdateStyleNoteV1_13_0 ( note.tooltipContent );
268
        note.phone = this.#UpdateStyleNoteV1_13_0 ( note.phone );
269
        note.address = this.#UpdateStyleNoteV1_13_0 ( note.address );
270
    }
271
272
    /**
273
    Update the notes when version is v1.13.0
274
    */
275
276
    #updateNotesV_1_13_0 ( ) {
277
        this.#jsonTravel.notes.forEach (
278
            note => {
279
                this.#updateNote_V1_13_0 ( note );
280
            }
281
        );
282
        this.jsonTravel.routes.forEach (
283
            route => {
284
                route.notes.forEach (
285
                    note => {
286
                        this.#updateNote_V1_13_0 ( note );
287
                    }
288
                );
289
            }
290
        );
291
    }
292
293
    /**
294
    Update the travel
295
    */
296
297
    #updateTravel ( ) {
298
        switch ( this.#jsonTravel.objType.version ) {
299
        case '1.0.0' :
300
            this.#updateRoutesV_1_0_0 ( );
301
            // eslint-disable-next-line no-fallthrough
302
        case '1.1.0' :
303
        case '1.2.0' :
304
        case '1.3.0' :
305
        case '1.4.0' :
306
            this.#updateTravelV_1_4_0 ( );
307
            // eslint-disable-next-line no-fallthrough
308
        case '1.5.0' :
309
            this.#updateTravelV_1_5_0 ( );
310
            // eslint-disable-next-line no-fallthrough
311
        case '1.6.0' :
312
            this.#updateItineraryV_1_6_0 ( );
313
            // eslint-disable-next-line no-fallthrough
314
        case '1.7.0' :
315
        case '1.7.1' :
316
        case '1.8.0' :
317
        case '1.9.0' :
318
        case '1.10.0' :
319
        case '1.11.0' :
320
            this.#updateRoutesV_1_11_0 ( );
321
            // eslint-disable-next-line no-fallthrough
322
        case '1.12.0' :
323
        case '1.13.0' :
324
            this.#updateNotesV_1_13_0 ( );
325
            // eslint-disable-next-line no-fallthrough
326
        case '2.0.0' :
327
        case '2.1.0' :
328
        case '2.2.0' :
329
        case '2.3.0' :
330
            this.#updateRouteV_2_3_0 ( );
331
            this.#jsonTravel.objType.version = '2.4.0';
332
            break;
333
334
        default :
335
            throw new Error ( 'invalid version for travel' );
336
        }
337
    }
338
339
    /**
340
    The constructor
341
    */
342
343
    constructor ( ) {
344
        Object.freeze ( this );
345
    }
346
347
    /**
348
    Update the travel
349
    @param {JsonObject} jsonTravel The travel to update
350
    */
351
352
    update ( jsonTravel ) {
353
        if ( jsonTravel.objType.version === theDataVersion ) {
354
            return;
355
        }
356
        this.#jsonTravel = jsonTravel;
357
        this.#updateTravel ( );
358
    }
359
}
360
/* eslint-enable camelcase */
361
362
export default TravelUpdater;
363
364
/* --- End of file --------------------------------------------------------------------------------------------------------- */
365