1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
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 | |
41 | |
42 | |
43 | |
44 | class AllManeuverNotesBuilder { |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | #route = null; |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | #maneuversLength = ZERO; |
58 | |
59 | |
60 | |
61 | |
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 | |
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 | |
116 | |
117 | |
118 | constructor ( ) { |
119 | Object.freeze ( this ); |
120 | } |
121 | |
122 | |
123 | |
124 | |
125 | |
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 | |
167 | |