@@ -431,27 +431,6 @@ function processGeospatial(config, geospatial, index) {
431431 }
432432 )
433433
434- map . on ( EVENTS . drawReady , function ( ) {
435- geojson . features . forEach ( ( feature ) => {
436- switch ( feature . geometry . type ) {
437- case 'Polygon' :
438- drawPlugin . addFeature ( { ...feature , ...polygonFeatureProperties } )
439- break
440- case 'LineString' :
441- drawPlugin . addFeature ( { ...feature , ...lineFeatureProperties } )
442- break
443- case 'Point' :
444- map . addMarker ( feature . id , feature . geometry . coordinates )
445- break
446- default :
447- break
448- }
449- } )
450-
451- // Update the features
452- renderFeatures ( geojson , listContainer , geospatialInput )
453- } )
454-
455434 /** @type {string | undefined } */
456435 let _activeFeature
457436
@@ -500,6 +479,35 @@ function processGeospatial(config, geospatial, index) {
500479 }
501480 }
502481
482+ /**
483+ * Callback when the draw plugin is ready
484+ */
485+ function onDrawReady ( ) {
486+ geojson . features . forEach ( ( feature ) => {
487+ switch ( feature . geometry . type ) {
488+ case 'Polygon' :
489+ drawPlugin . addFeature ( { ...feature , ...polygonFeatureProperties } )
490+ break
491+ case 'LineString' :
492+ drawPlugin . addFeature ( { ...feature , ...lineFeatureProperties } )
493+ break
494+ case 'Point' :
495+ map . addMarker ( feature . id , feature . geometry . coordinates )
496+ break
497+ default :
498+ break
499+ }
500+ } )
501+
502+ // Update the features
503+ renderFeatures (
504+ geojson ,
505+ listContainer ,
506+ /** @type {HTMLTextAreaElement } */ ( geospatialInput )
507+ )
508+ }
509+ map . on ( EVENTS . drawReady , onDrawReady )
510+
503511 /**
504512 * Callback when a draw feature has been created
505513 * @param {Feature } e
@@ -566,34 +574,53 @@ function processGeospatial(config, geospatial, index) {
566574 * @param {{ coords: Coordinates } } e
567575 */
568576 function onInteractMarkerChange ( e ) {
569- const id = generateID ( )
570- const description = 'New point'
577+ if ( _activeFeature ) {
578+ // Editing an existing point
579+ const feature = geojson . features . find ( ( f ) => f . id === _activeFeature )
580+ map . addMarker ( _activeFeature , e . coords )
571581
572- geojson . features . push ( {
573- type : 'Feature' ,
574- properties : {
575- description
576- } ,
577- geometry : {
578- type : 'Point' ,
579- coordinates : e . coords
580- } ,
581- id
582- } )
582+ if ( feature ) {
583+ feature . geometry . coordinates = e . coords
584+
585+ // Update the features
586+ renderFeaturesValue (
587+ geojson ,
588+ /** @type {HTMLTextAreaElement } */ ( geospatialInput )
589+ )
590+ }
591+
592+ resetActiveFeature ( )
593+ } else {
594+ // Adding a new point
595+ const id = generateID ( )
596+ const description = 'New point'
597+
598+ geojson . features . push ( {
599+ type : 'Feature' ,
600+ properties : {
601+ description
602+ } ,
603+ geometry : {
604+ type : 'Point' ,
605+ coordinates : e . coords
606+ } ,
607+ id
608+ } )
609+ map . addMarker ( id , e . coords )
610+
611+ // Update the features
612+ renderFeatures (
613+ geojson ,
614+ listContainer ,
615+ /** @type {HTMLTextAreaElement } */ ( geospatialInput )
616+ )
583617
584- map . addMarker ( id , e . coords )
618+ focusDescriptionInput ( )
619+ }
585620 map . removeMarker ( 'location' )
586621
587622 interactPlugin . disable ( )
588-
589- // Update the features
590- renderFeatures (
591- geojson ,
592- listContainer ,
593- /** @type {HTMLTextAreaElement } */ ( geospatialInput )
594- )
595623 toggleActionButtons ( false )
596- focusDescriptionInput ( )
597624 }
598625 map . on ( EVENTS . interactMarkerChange , onInteractMarkerChange )
599626
@@ -620,11 +647,12 @@ function processGeospatial(config, geospatial, index) {
620647 const { action, id, type } = target . dataset
621648
622649 if ( action === 'edit' ) {
650+ _activeFeature = id
623651 // "Change" feature link was clicked
624652 if ( type === 'Point' ) {
625653 interactPlugin . selectFeature ( { featureId : id } )
654+ interactPlugin . enable ( )
626655 } else {
627- _activeFeature = id
628656 drawPlugin . editFeature ( id )
629657 }
630658 toggleActionButtons ( true )
@@ -1200,7 +1228,7 @@ function createFeatureHTML(feature) {
12001228 </dt>
12011229 <dd class="govuk-summary-list__actions">
12021230 <ul class="govuk-summary-list__actions-list">
1203- ${ feature . geometry . type !== 'Point' ? changeAction ( ) : '' }
1231+ ${ changeAction ( ) }
12041232 ${ deleteAction ( ) }
12051233 </ul>
12061234 </dd>
0 commit comments