Skip to content

Commit 3c7877d

Browse files
Add event constants
1 parent 115718d commit 3c7877d

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

src/client/javascripts/location-map.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ import { bbox } from '@turf/bbox'
22
// @ts-expect-error - no types
33
import OsGridRef, { LatLon } from 'geodesy/osgridref.js'
44

5-
// @ts-expect-error - Defra namespace currently comes from UMD support files
6-
const defra = window.defra
7-
85
/**
96
* Converts lat long to easting and northing
107
* @param {object} param
@@ -70,7 +67,12 @@ const defaultConfig = {
7067
const COMPANY_SYMBOL_CODE = 169
7168
const LOCATION_FIELD_SELECTOR = 'input.govuk-input'
7269
const EVENTS = {
73-
interactMarkerChange: 'interact:markerchange'
70+
mapReady: 'map:ready',
71+
interactMarkerChange: 'interact:markerchange',
72+
drawReady: 'draw:ready',
73+
drawCreated: 'draw:created',
74+
drawEdited: 'draw:edited',
75+
drawCancelled: 'draw:cancelled'
7476
}
7577

7678
const defaultData = {
@@ -243,7 +245,7 @@ function processLocation(config, location, index) {
243245
const { map, interactPlugin } = createMap(mapId, initConfig, config)
244246

245247
map.on(
246-
'map:ready',
248+
EVENTS.mapReady,
247249
/**
248250
* Callback function which fires when the map is ready
249251
* @param {object} e - the event
@@ -302,6 +304,9 @@ function processLocation(config, location, index) {
302304
* @param {number} index - the 0-based index
303305
*/
304306
function processGeospatial(config, geospatial, index) {
307+
// @ts-expect-error - Defra namespace currently comes from UMD support files
308+
const defra = window.defra
309+
305310
if (!(geospatial instanceof HTMLDivElement)) {
306311
return
307312
}
@@ -321,18 +326,19 @@ function processGeospatial(config, geospatial, index) {
321326
const listId = `${mapId}_list`
322327
listContainer.setAttribute('id', listId)
323328

329+
const value = geospatialInput.value.trim()
330+
const hasValue = !!value
331+
324332
/** @type {FeatureCollection} */
325-
const features = geospatialInput.value
326-
? JSON.parse(geospatialInput.value)
327-
: []
333+
const features = hasValue ? JSON.parse(value) : []
328334

329335
/** @type {GeoJSON} */
330336
const geojson = {
331337
type: 'FeatureCollection',
332338
features
333339
}
334340

335-
const bounds = bbox(geojson)
341+
const bounds = hasValue ? bbox(geojson) : undefined
336342
const drawPlugin = defra.drawMLPlugin()
337343

338344
const initConfig = {
@@ -348,7 +354,7 @@ function processGeospatial(config, geospatial, index) {
348354
const { map, interactPlugin } = createMap(mapId, initConfig, config)
349355

350356
map.on(
351-
'map:ready',
357+
EVENTS.mapReady,
352358
/**
353359
* Callback function which fires when the map is ready
354360
*/
@@ -425,7 +431,7 @@ function processGeospatial(config, geospatial, index) {
425431
}
426432
)
427433

428-
map.on('draw:ready', function () {
434+
map.on(EVENTS.drawReady, function () {
429435
geojson.features.forEach((feature) => {
430436
switch (feature.geometry.type) {
431437
case 'Polygon':
@@ -463,6 +469,9 @@ function processGeospatial(config, geospatial, index) {
463469
map.toggleButtonState('btnAddLine', 'hidden', hidden)
464470
}
465471

472+
/**
473+
* Set focus to the last description input
474+
*/
466475
function focusDescriptionInput() {
467476
const inputs = listContainer.querySelectorAll('input')
468477
if (inputs.length) {
@@ -473,6 +482,7 @@ function processGeospatial(config, geospatial, index) {
473482
lastInput.select()
474483
}
475484
}
485+
476486
/**
477487
* Removes a feature from the geojson
478488
* @param {string} id - the feature id
@@ -516,7 +526,7 @@ function processGeospatial(config, geospatial, index) {
516526
toggleActionButtons(false)
517527
focusDescriptionInput()
518528
}
519-
map.on('draw:created', onDrawCreated)
529+
map.on(EVENTS.drawCreated, onDrawCreated)
520530

521531
/**
522532
* Callback when a draw feature has been edited
@@ -541,15 +551,15 @@ function processGeospatial(config, geospatial, index) {
541551

542552
resetActiveFeature()
543553
}
544-
map.on('draw:edited', onDrawEdited)
554+
map.on(EVENTS.drawEdited, onDrawEdited)
545555

546556
/**
547557
* Callback when a draw feature has been cancelled
548558
*/
549559
function onDrawCancelled() {
550560
toggleActionButtons(false)
551561
}
552-
map.on('draw:cancelled', onDrawCancelled)
562+
map.on(EVENTS.drawCancelled, onDrawCancelled)
553563

554564
/**
555565
* Callback when an interact marker has been changed
@@ -673,6 +683,9 @@ function createMap(mapId, initConfig, mapsConfig) {
673683
const { assetPath, apiPath, data = defaultData } = mapsConfig
674684
const logoAltText = 'Ordnance survey logo'
675685

686+
// @ts-expect-error - Defra namespace currently comes from UMD support files
687+
const defra = window.defra
688+
676689
const interactPlugin = defra.interactPlugin({
677690
markerColor: { outdoor: '#ff0000', dark: '#00ff00' },
678691
interactionMode: 'marker',

0 commit comments

Comments
 (0)