File : core/AllManeuverNotesBuilder.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 WaitUI from '../uis/waitUI/WaitUI.js';
26
import MapIconFromOsmFactory from './mapIcon/MapIconFromOsmFactory.js';
27
import Note from '../data/Note.js';
28
import theGeometry from './lib/Geometry.js';
29
import theEventDispatcher from './lib/EventDispatcher.js';
30
import theErrorsUI from '../uis/errorsUI/ErrorsUI.js';
31
import theConfig from '../data/Config.js';
32
import theTranslator from './uiLib/Translator.js';
33
import TwoButtonsDialog from '../dialogs/twoButtonsDialog/TwoButtonsDialog.js';
34
import theDataSearchEngine from '../data/DataSearchEngine.js';
35
36
import { ZERO, ONE, INVALID_OBJ_ID } from '../main/Constants.js';
37
38
/* ------------------------------------------------------------------------------------------------------------------------- */
39
/**
40
This class add all maneuvers notes to a route
41
*/
42
/* ------------------------------------------------------------------------------------------------------------------------- */
43
44
class AllManeuverNotesBuilder {
45
46
    /** The route for witch the maneuvers note are created
47
    @type {Route}
48
    */
49
50
    #route = null;
51
52
    /**
53
    the number of maneuvers
54
    @type {Number}
55
    */
56
57
    #maneuversLength = ZERO;
58
59
    /**
60
    This method creates a new route note with data from osm
61
    @param {Object} noteData The data needed for building the note
62
    */
63
64
    #newNoteFromOsmData ( noteData ) {
65
        const note = new Note ( );
66
        for ( const property in noteData ) {
67
            note [ property ] = noteData [ property ];
68
        }
69
70
        note.iconLatLng = note.latLng;
71
        note.distance = theGeometry.getClosestLatLngDistance ( this.#route, note.latLng ).distance;
72
        note.chainedDistance = this.#route.chainedDistance;
73
        this.#route.notes.add ( note );
74
        theEventDispatcher.dispatch (
75
            'noteupdated',
76
            {
77
                removedNoteObjId : INVALID_OBJ_ID,
78
                addedNoteObjId : note.objId
79
            }
80
        );
81
    }
82
83
    /**
84
    This method add a note with data from osm for each maneuver of a route.
85
    */
86
87
    async #addAllManeuverNotes ( ) {
88
        const waitUI = new WaitUI ( );
89
        waitUI.createUI ( );
90
        const mapIconFromOsmFactory = new MapIconFromOsmFactory ( );
91
        const maneuverIterator = this.#route.itinerary.maneuvers.iterator;
92
        while ( ! maneuverIterator.done ) {
93
            waitUI.showInfo (
94
                theTranslator.getText (
95
                    'AllManeuverNotesBuilder - Creating note',
96
                    { noteNumber : maneuverIterator.index + ONE, notesLength : this.#maneuversLength }
97
                )
98
            );
99
100
            const latLng = this.#route.itinerary.itineraryPoints.getAt ( maneuverIterator.value.itineraryPointObjId ).latLng;
101
            const noteData = await mapIconFromOsmFactory.getIconAndAdressAsync ( latLng, this.#route );
102
            if ( noteData ) {
103
                this.#newNoteFromOsmData ( noteData );
104
            }
105
            else {
106
                console.error ( 'An error occurs when creating the svg icon ' + maneuverIterator.index );
107
            }
108
        }
109
        this.#route.notes.sort ( ( first, second ) => first.distance - second.distance );
110
        theEventDispatcher.dispatch ( 'updateroadbook' );
111
        waitUI.close ( );
112
    }
113
114
    /**
115
    The constructor
116
    */
117
118
    constructor ( ) {
119
        Object.freeze ( this );
120
    }
121
122
    /**
123
    This method add a note with data from osm for each maneuver of a route
124
    A confirmation message is showed before starting.
125
    @param {Number} routeObjId The Route objId
126
    */
127
128
    addAllManeuverNotes ( routeObjId ) {
129
130
        this.#route = theDataSearchEngine.getRoute ( routeObjId );
131
        this.#maneuversLength = this.#route.itinerary.maneuvers.length;
132
133
        if ( theConfig.note.maxManeuversNotes < this.#maneuversLength ) {
134
            theErrorsUI.showError (
135
                theTranslator.getText (
136
                    'AllManeuverNotesBuilder - max maneuvers notes reached {maneuversLength}{maxManeuversNotes}',
137
                    { maneuversLength : this.#maneuversLength, maxManeuversNotes : theConfig.note.maxManeuversNotes } )
138
            );
139
            return;
140
        }
141
142
        new TwoButtonsDialog (
143
            {
144
                title : theTranslator.getText ( 'AllManeuverNotesBuilder - Add a note for each maneuver' ),
145
                text : theTranslator.getText (
146
                    'AllManeuverNotesBuilder - Add a note for each maneuver. Are you sure?',
147
                    { noteLength : this.#maneuversLength }
148
                ),
149
                secondButtonText : '❌'
150
            }
151
        )
152
            .show ( )
153
            .then ( ( ) => this.#addAllManeuverNotes ( ) )
154
            .catch (
155
                err => {
156
                    if ( err instanceof Error ) {
157
                        console.error ( err );
158
                    }
159
                }
160
            );
161
    }
162
}
163
164
export default AllManeuverNotesBuilder;
165
166
/* --- End of file --------------------------------------------------------------------------------------------------------- */
167