-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocation-map.js
More file actions
478 lines (398 loc) · 13.3 KB
/
location-map.js
File metadata and controls
478 lines (398 loc) · 13.3 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import {
EVENTS,
centerMap,
createMap,
defaultConfig,
eastingNorthingToLatLong,
latLongToEastingNorthing,
latLongToOsGridRef,
osGridRefToLatLong
} from '~/src/client/javascripts/map.js'
const LOCATION_FIELD_SELECTOR = 'input.govuk-input'
/**
* Gets initial map config for a location field
* @param {HTMLDivElement} locationField - the location field element
*/
function getInitMapConfig(locationField) {
const locationType = locationField.dataset.locationtype
switch (locationType) {
case 'latlongfield':
return getInitLatLongMapConfig(locationField)
case 'eastingnorthingfield':
return getInitEastingNorthingMapConfig(locationField)
case 'osgridreffield':
return getInitOsGridRefMapConfig(locationField)
default:
throw new Error('Not implemented')
}
}
/**
* Validates lat and long is numeric and within UK bounds
* @param {string} strLat - the latitude string
* @param {string} strLong - the longitude string
* @returns {{ valid: false } | { valid: true, value: { lat: number, long: number } }}
*/
function validateLatLong(strLat, strLong) {
const lat = strLat.trim() && Number(strLat.trim())
const long = strLong.trim() && Number(strLong.trim())
if (!lat || !long) {
return { valid: false }
}
const latMin = 49.85
const latMax = 60.859
const longMin = -13.687
const longMax = 1.767
const latInBounds = lat >= latMin && lat <= latMax
const longInBounds = long >= longMin && long <= longMax
if (!latInBounds || !longInBounds) {
return { valid: false }
}
return { valid: true, value: { lat, long } }
}
/**
* Validates easting and northing is numeric and within UK bounds
* @param {string} strEasting - the easting string
* @param {string} strNorthing - the northing string
* @returns {{ valid: false } | { valid: true, value: { easting: number, northing: number } }}
*/
function validateEastingNorthing(strEasting, strNorthing) {
const easting = strEasting.trim() && Number(strEasting.trim())
const northing = strNorthing.trim() && Number(strNorthing.trim())
if (!easting || !northing) {
return { valid: false }
}
const eastingMin = 0
const eastingMax = 700000
const northingMin = 0
const northingMax = 1300000
const latInBounds = easting >= eastingMin && easting <= eastingMax
const longInBounds = northing >= northingMin && northing <= northingMax
if (!latInBounds || !longInBounds) {
return { valid: false }
}
return { valid: true, value: { easting, northing } }
}
/**
* Validates OS grid reference is correct
* @param {string} osGridRef - the OsGridRef
* @returns {{ valid: false } | { valid: true, value: string }}
*/
function validateOsGridRef(osGridRef) {
if (!osGridRef) {
return { valid: false }
}
const pattern =
/^((([sS]|[nN])[a-hA-Hj-zJ-Z])|(([tT]|[oO])[abfglmqrvwABFGLMQRVW])|([hH][l-zL-Z])|([jJ][lmqrvwLMQRVW]))\s?(([0-9]{3})\s?([0-9]{3})|([0-9]{4})\s?([0-9]{4})|([0-9]{5})\s?([0-9]{5}))$/
const match = pattern.exec(osGridRef)
if (match === null) {
return { valid: false }
}
return { valid: true, value: match[0] }
}
/**
* Gets the inputs for a latlong location field
* @param {HTMLDivElement} locationField - the latlong location field element
*/
function getLatLongInputs(locationField) {
const inputs = locationField.querySelectorAll(LOCATION_FIELD_SELECTOR)
if (inputs.length !== 2) {
throw new Error('Expected 2 inputs for lat and long')
}
const latInput = /** @type {HTMLInputElement} */ (inputs[0])
const longInput = /** @type {HTMLInputElement} */ (inputs[1])
return { latInput, longInput }
}
/**
* Gets the inputs for a easting/northing location field
* @param {HTMLDivElement} locationField - the eastingnorthing location field element
*/
function getEastingNorthingInputs(locationField) {
const inputs = locationField.querySelectorAll(LOCATION_FIELD_SELECTOR)
if (inputs.length !== 2) {
throw new Error('Expected 2 inputs for easting and northing')
}
const eastingInput = /** @type {HTMLInputElement} */ (inputs[0])
const northingInput = /** @type {HTMLInputElement} */ (inputs[1])
return { eastingInput, northingInput }
}
/**
* Gets the input for a OS grid reference location field
* @param {HTMLDivElement} locationField - the osgridref location field element
*/
function getOsGridRefInput(locationField) {
const input = locationField.querySelector(LOCATION_FIELD_SELECTOR)
if (input === null) {
throw new Error('Expected 1 input for osgridref')
}
return /** @type {HTMLInputElement} */ (input)
}
/**
* Get the initial map config for a center point
* @param {MapCenter} center - the point
*/
function getInitMapCenterConfig(center) {
return {
zoom: '16',
center,
markers: [
{
id: 'location',
coords: center
}
]
}
}
/**
* Gets initial map config for a latlong location field
* @param {HTMLDivElement} locationField - the latlong location field element
* @returns {InteractiveMapInitConfig | undefined}
*/
function getInitLatLongMapConfig(locationField) {
const { latInput, longInput } = getLatLongInputs(locationField)
const result = validateLatLong(latInput.value, longInput.value)
if (!result.valid) {
return undefined
}
/** @type {MapCenter} */
const center = [result.value.long, result.value.lat]
return getInitMapCenterConfig(center)
}
/**
* Gets initial map config for a easting/northing location field
* @param {HTMLDivElement} locationField - the eastingnorthing location field element
* @returns {InteractiveMapInitConfig | undefined}
*/
function getInitEastingNorthingMapConfig(locationField) {
const { eastingInput, northingInput } =
getEastingNorthingInputs(locationField)
const result = validateEastingNorthing(
eastingInput.value,
northingInput.value
)
if (!result.valid) {
return undefined
}
const latlong = eastingNorthingToLatLong(result.value)
/** @type {MapCenter} */
const center = [latlong.long, latlong.lat]
return getInitMapCenterConfig(center)
}
/**
* Gets initial map config for an OS grid reference location field
* @param {HTMLDivElement} locationField - the osgridref location field element
* @returns {InteractiveMapInitConfig | undefined}
*/
function getInitOsGridRefMapConfig(locationField) {
const osGridRefInput = getOsGridRefInput(locationField)
const result = validateOsGridRef(osGridRefInput.value)
if (!result.valid) {
return undefined
}
const latlong = osGridRefToLatLong(result.value)
/** @type {MapCenter} */
const center = [latlong.long, latlong.lat]
return getInitMapCenterConfig(center)
}
/**
* Bind a latlong field to the map
* @param {HTMLDivElement} locationField - the latlong location field
* @param {InteractiveMap} map - the map component instance (of InteractiveMap)
* @param {MapLibreMap} mapProvider - the map provider instance (of MapLibreMap)
*/
function bindLatLongField(locationField, map, mapProvider) {
const { latInput, longInput } = getLatLongInputs(locationField)
map.on(
EVENTS.interactMarkerChange,
/**
* Callback function which fires when the map marker changes
* @param {object} e - the event
* @param {[number, number]} e.coords - the map marker coordinates
*/
function onInteractMarkerChange(e) {
const maxPrecision = 7
latInput.value = e.coords[1].toFixed(maxPrecision)
longInput.value = e.coords[0].toFixed(maxPrecision)
}
)
/**
* Lat & long input change event listener
* Update the map view location when the inputs are changed
*/
function onUpdateInputs() {
const result = validateLatLong(latInput.value, longInput.value)
if (result.valid) {
/** @type {MapCenter} */
const center = [result.value.long, result.value.lat]
centerMap(map, mapProvider, center)
}
}
latInput.addEventListener('change', onUpdateInputs, false)
longInput.addEventListener('change', onUpdateInputs, false)
}
/**
* Bind an eastingnorthing field to the map
* @param {HTMLDivElement} locationField - the eastingnorthing location field
* @param {InteractiveMap} map - the map component instance (of InteractiveMap)
* @param {MapLibreMap} mapProvider - the map provider instance (of MapLibreMap)
*/
function bindEastingNorthingField(locationField, map, mapProvider) {
const { eastingInput, northingInput } =
getEastingNorthingInputs(locationField)
map.on(
EVENTS.interactMarkerChange,
/**
* Callback function which fires when the map marker changes
* @param {object} e - the event
* @param {[number, number]} e.coords - the map marker coordinates
*/
function onInteractMarkerChange(e) {
const maxPrecision = 0
const point = latLongToEastingNorthing({
lat: e.coords[1],
long: e.coords[0]
})
eastingInput.value = point.easting.toFixed(maxPrecision)
northingInput.value = point.northing.toFixed(maxPrecision)
}
)
/**
* Easting & northing input change event listener
* Update the map view location when the inputs are changed
*/
function onUpdateInputs() {
const result = validateEastingNorthing(
eastingInput.value,
northingInput.value
)
if (result.valid) {
const latlong = eastingNorthingToLatLong(result.value)
/** @type {MapCenter} */
const center = [latlong.long, latlong.lat]
centerMap(map, mapProvider, center)
}
}
eastingInput.addEventListener('change', onUpdateInputs, false)
northingInput.addEventListener('change', onUpdateInputs, false)
}
/**
* Bind an OS grid reference field to the map
* @param {HTMLDivElement} locationField - the osgridref location field
* @param {InteractiveMap} map - the map component instance (of InteractiveMap)
* @param {MapLibreMap} mapProvider - the map provider instance (of MapLibreMap)
*/
function bindOsGridRefField(locationField, map, mapProvider) {
const osGridRefInput = getOsGridRefInput(locationField)
map.on(
EVENTS.interactMarkerChange,
/**
* Callback function which fires when the map marker changes
* @param {object} e - the event
* @param {[number, number]} e.coords - the map marker coordinates
*/
function onInteractMarkerChange(e) {
const point = latLongToOsGridRef({
lat: e.coords[1],
long: e.coords[0]
})
osGridRefInput.value = point
}
)
/**
* OS grid reference input change event listener
* Update the map view location when the input is changed
*/
function onUpdateInput() {
const result = validateOsGridRef(osGridRefInput.value)
if (result.valid) {
const latlong = osGridRefToLatLong(result.value)
/** @type {MapCenter} */
const center = [latlong.long, latlong.lat]
centerMap(map, mapProvider, center)
}
}
osGridRefInput.addEventListener('change', onUpdateInput, false)
}
/**
* Processes a location field to add map capability
* @param {MapsEnvironmentConfig} config - the location field element
* @param {Element} location - the location field element
* @param {number} index - the 0-based index
*/
export function processLocation(config, location, index) {
if (!(location instanceof HTMLDivElement)) {
return
}
const locationInputs = location.querySelector('.app-location-field-inputs')
if (!(locationInputs instanceof HTMLDivElement)) {
return
}
const locationType = location.dataset.locationtype
// Check for support
const supportedLocations = [
'latlongfield',
'eastingnorthingfield',
'osgridreffield'
]
if (!locationType || !supportedLocations.includes(locationType)) {
return
}
const mapContainer = document.createElement('div')
const mapId = `map_${index}`
mapContainer.setAttribute('id', mapId)
mapContainer.setAttribute('class', 'map-container')
const initConfig = getInitMapConfig(location) ?? defaultConfig
locationInputs.after(mapContainer)
const { map, interactPlugin } = createMap(mapId, initConfig, config)
map.on(
EVENTS.mapReady,
/**
* Callback function which fires when the map is ready
* @param {object} e - the event
* @param {MapLibreMap} e.map - the map provider instance
*/
function onMapReady(e) {
switch (locationType) {
case 'latlongfield':
bindLatLongField(location, map, e.map)
break
case 'eastingnorthingfield':
bindEastingNorthingField(location, map, e.map)
break
case 'osgridreffield':
bindOsGridRefField(location, map, e.map)
break
default:
throw new Error('Not implemented')
}
// Add info panel
map.addPanel('info', {
showLabel: true,
label: 'How to use the map',
mobile: {
slot: 'drawer',
open: true,
dismissible: true,
modal: false
},
tablet: {
slot: 'drawer',
open: true,
dismissible: true,
modal: false
},
desktop: {
slot: 'drawer',
open: true,
dismissible: true,
modal: false
},
html: 'If using a map click on a point to update the location.<br><br>If using a keyboard, navigate to the point, centering the crosshair at the location and press enter.'
})
// Enable the interact plugin
interactPlugin.enable()
}
)
}
/**
* @import { InteractiveMap, InteractiveMapInitConfig, MapCenter, MapLibreMap, MapsEnvironmentConfig } from '~/src/client/javascripts/map.js'
*/