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 LatLng from '../containers/LatLng.js'; |
26 | import PrintView from './PrintView.js'; |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | class PrintViewsFactory { |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | #printViews; |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | #route; |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | #maxViewSize; |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | #isItineraryHorOrVer ( currentView, firstItineraryPoint, lastItineraryPoint ) { |
67 | if ( firstItineraryPoint.lng === lastItineraryPoint.lng ) { |
68 | |
69 | |
70 | return new LatLng ( |
71 | firstItineraryPoint.lat > lastItineraryPoint.lat ? currentView.bottomLeft.lat : currentView.upperRight.lat, |
72 | firstItineraryPoint.lng |
73 | ); |
74 | } |
75 | if ( firstItineraryPoint.lat === lastItineraryPoint.lat ) { |
76 | |
77 | |
78 | return new LatLng ( |
79 | firstItineraryPoint.lat, |
80 | firstItineraryPoint.lng < lastItineraryPoint.lng ? currentView.upperRight.lng : currentView.bottomLeft.lng |
81 | ); |
82 | } |
83 | return null; |
84 | } |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | #isPointOnViewFrame ( currentView, itineraryPoint ) { |
94 | |
95 | const LAT_LNG_TOLERANCE = 0.000001; |
96 | |
97 | if ( |
98 | itineraryPoint.lat - currentView.bottomLeft.lat < LAT_LNG_TOLERANCE |
99 | || |
100 | currentView.upperRight.lat - itineraryPoint.lat < LAT_LNG_TOLERANCE |
101 | || |
102 | itineraryPoint.lng - currentView.bottomLeft.lng < LAT_LNG_TOLERANCE |
103 | || |
104 | currentView.upperRight.lng - itineraryPoint.lng < LAT_LNG_TOLERANCE |
105 | ) { |
106 | |
107 | |
108 | return LatLng.clone ( itineraryPoint ); |
109 | } |
110 | return null; |
111 | } |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | #haveViewOnlyOnePoint ( currentView, firstItineraryPoint, lastItineraryPoint ) { |
123 | if ( |
124 | currentView.bottomLeft.lat === currentView.upperRight.lat |
125 | && |
126 | currentView.bottomLeft.lng === currentView.upperRight.lng |
127 | ) { |
128 | const coef = Math.min ( |
129 | Math.abs ( this.#maxViewSize.height / ( lastItineraryPoint.lat - firstItineraryPoint.lat ) ), |
130 | Math.abs ( this.#maxViewSize.width / ( lastItineraryPoint.lng - firstItineraryPoint.lng ) ) |
131 | ); |
132 | return new LatLng ( |
133 | firstItineraryPoint.lat + ( coef * ( lastItineraryPoint.lat - firstItineraryPoint.lat ) ), |
134 | firstItineraryPoint.lng + ( coef * ( lastItineraryPoint.lng - firstItineraryPoint.lng ) ) |
135 | ); |
136 | } |
137 | return null; |
138 | } |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | #computeIntermediatePoint ( currentView, firstItineraryPoint, lastItineraryPoint ) { |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | |
164 | |
165 | |
166 | |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | let intermediatePoint = this.#haveViewOnlyOnePoint ( currentView, firstItineraryPoint, lastItineraryPoint ); |
184 | if ( intermediatePoint ) { |
185 | return intermediatePoint; |
186 | } |
187 | |
188 | intermediatePoint = this.#isPointOnViewFrame ( currentView, firstItineraryPoint ); |
189 | if ( intermediatePoint ) { |
190 | return intermediatePoint; |
191 | } |
192 | |
193 | intermediatePoint = this.#isItineraryHorOrVer ( currentView, firstItineraryPoint, lastItineraryPoint ); |
194 | if ( intermediatePoint ) { |
195 | return intermediatePoint; |
196 | } |
197 | |
198 | const coefA = |
199 | ( firstItineraryPoint.lat - lastItineraryPoint.lat ) |
200 | / |
201 | ( firstItineraryPoint.lng - lastItineraryPoint.lng ); |
202 | const coefB = firstItineraryPoint.lat - ( coefA * firstItineraryPoint.lng ); |
203 | |
204 | |
205 | intermediatePoint = new LatLng ( ( coefA * currentView.upperRight.lng ) + coefB, currentView.upperRight.lng ); |
206 | |
207 | if ( |
208 | intermediatePoint.lat <= currentView.upperRight.lat |
209 | && |
210 | intermediatePoint.lat >= currentView.bottomLeft.lat |
211 | && |
212 | intermediatePoint.lng < lastItineraryPoint.lng |
213 | ) { |
214 | return intermediatePoint; |
215 | } |
216 | |
217 | |
218 | intermediatePoint = new LatLng ( currentView.upperRight.lat, ( currentView.upperRight.lat - coefB ) / coefA ); |
219 | |
220 | if ( |
221 | intermediatePoint.lng >= currentView.bottomLeft.lng |
222 | && |
223 | intermediatePoint.lng <= currentView.upperRight.lng |
224 | && |
225 | intermediatePoint.lat < lastItineraryPoint.lat |
226 | ) { |
227 | return intermediatePoint; |
228 | } |
229 | |
230 | |
231 | intermediatePoint = new LatLng ( ( coefA * currentView.bottomLeft.lng ) + coefB, currentView.bottomLeft.lng ); |
232 | |
233 | if ( |
234 | intermediatePoint.lat <= currentView.upperRight.lat |
235 | && |
236 | intermediatePoint.lat >= currentView.bottomLeft.lat |
237 | && |
238 | intermediatePoint.lng > lastItineraryPoint.lng |
239 | ) { |
240 | return intermediatePoint; |
241 | } |
242 | |
243 | |
244 | intermediatePoint = new LatLng ( currentView.bottomLeft.lat, ( currentView.bottomLeft.lat - coefB ) / coefA ); |
245 | |
246 | if ( |
247 | intermediatePoint.lng >= currentView.bottomLeft.lng |
248 | && |
249 | intermediatePoint.lng <= currentView.upperRight.lng |
250 | && |
251 | intermediatePoint.lat > lastItineraryPoint.lat |
252 | ) { |
253 | return intermediatePoint; |
254 | } |
255 | throw new Error ( 'intermediate point not found' ); |
256 | } |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | #computePrintViews ( ) { |
263 | |
264 | this.#printViews = []; |
265 | |
266 | |
267 | const itineraryPointsIterator = this.#route.itinerary.itineraryPoints.iterator; |
268 | let done = itineraryPointsIterator.done; |
269 | |
270 | |
271 | |
272 | |
273 | let currentView = new PrintView ( ); |
274 | currentView.bottomLeft = LatLng.clone ( itineraryPointsIterator.value ); |
275 | currentView.upperRight = LatLng.clone ( itineraryPointsIterator.value ); |
276 | currentView.entryPoint = LatLng.clone ( itineraryPointsIterator.value ); |
277 | |
278 | let previousItineraryPoint = itineraryPointsIterator.value; |
279 | |
280 | let intermediatePoint = LatLng.clone ( itineraryPointsIterator.value ); |
281 | |
282 | |
283 | done = itineraryPointsIterator.done; |
284 | let currentItineraryPoint = itineraryPointsIterator.value; |
285 | while ( ! done ) { |
286 | |
287 | |
288 | const tmpView = new PrintView ( ); |
289 | tmpView.bottomLeft = new LatLng ( |
290 | Math.min ( currentView.bottomLeft.lat, currentItineraryPoint.lat ), |
291 | Math.min ( currentView.bottomLeft.lng, currentItineraryPoint.lng ) |
292 | ); |
293 | tmpView.upperRight = new LatLng ( |
294 | Math.max ( currentView.upperRight.lat, currentItineraryPoint.lat ), |
295 | Math.max ( currentView.upperRight.lng, currentItineraryPoint.lng ) |
296 | ); |
297 | tmpView.entryPoint = LatLng.clone ( currentView.entryPoint ); |
298 | |
299 | |
300 | if ( |
301 | this.#maxViewSize.height > tmpView.upperRight.lat - tmpView.bottomLeft.lat |
302 | && |
303 | this.#maxViewSize.width > tmpView.upperRight.lng - tmpView.bottomLeft.lng |
304 | ) { |
305 | |
306 | |
307 | |
308 | currentView = tmpView; |
309 | previousItineraryPoint = itineraryPointsIterator.value; |
310 | intermediatePoint = LatLng.clone ( itineraryPointsIterator.value ); |
311 | done = itineraryPointsIterator.done; |
312 | currentItineraryPoint = itineraryPointsIterator.value; |
313 | if ( done ) { |
314 | currentView.exitPoint = intermediatePoint; |
315 | this.#printViews.push ( Object.freeze ( currentView ) ); |
316 | } |
317 | } |
318 | else { |
319 | |
320 | |
321 | |
322 | intermediatePoint = this.#computeIntermediatePoint ( |
323 | currentView, |
324 | previousItineraryPoint, |
325 | currentItineraryPoint |
326 | ); |
327 | |
328 | |
329 | currentView.bottomLeft = new LatLng ( |
330 | Math.min ( currentView.bottomLeft.lat, intermediatePoint.lat ), |
331 | Math.min ( currentView.bottomLeft.lng, intermediatePoint.lng ) |
332 | ); |
333 | currentView.upperRight = new LatLng ( |
334 | Math.max ( currentView.upperRight.lat, intermediatePoint.lat ), |
335 | Math.max ( currentView.upperRight.lng, intermediatePoint.lng ) |
336 | ); |
337 | |
338 | |
339 | currentView.exitPoint = intermediatePoint; |
340 | |
341 | |
342 | this.#printViews.push ( Object.freeze ( currentView ) ); |
343 | |
344 | |
345 | currentView = new PrintView ( ); |
346 | currentView.bottomLeft = LatLng.clone ( intermediatePoint ); |
347 | currentView.upperRight = LatLng.clone ( intermediatePoint ); |
348 | currentView.entryPoint = LatLng.clone ( intermediatePoint ); |
349 | } |
350 | } |
351 | } |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | |
359 | constructor ( route, maxViewSize ) { |
360 | Object.freeze ( this ); |
361 | this.#route = route; |
362 | this.#maxViewSize = maxViewSize; |
363 | this.#computePrintViews ( ); |
364 | } |
365 | |
366 | |
367 | |
368 | |
369 | |
370 | |
371 | get printViews ( ) { return this.#printViews; } |
372 | |
373 | } |
374 | |
375 | export default PrintViewsFactory; |
376 | |
377 | |
378 | |