@@ -28,8 +28,67 @@ const helpPanelConfig = {
2828 open : true ,
2929 dismissible : true ,
3030 modal : false
31- } ,
32- html : '<p class="govuk-body-s govuk-!-margin-bottom-2">You can add points, shapes or lines to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li><li>Double‑click, or select \'Done\', when you have finished drawing a line or shape</li><li>Give the location a name</li></ul>'
31+ }
32+ }
33+
34+ /**
35+ * @param {boolean } allowLine
36+ * @param {boolean } allowShape
37+ */
38+ function getLineOrShapeText ( allowLine , allowShape ) {
39+ if ( allowLine && allowShape ) {
40+ return 'a line or shape'
41+ }
42+ if ( allowLine ) {
43+ return 'a line'
44+ }
45+ if ( allowShape ) {
46+ return 'a shape'
47+ }
48+ return ''
49+ }
50+
51+ /**
52+ * @param {boolean } allowPoint
53+ * @param {boolean } allowLine
54+ * @param {boolean } allowShape
55+ */
56+ function getAllowedTypesPhrase ( allowPoint , allowLine , allowShape ) {
57+ const items = [ ]
58+
59+ if ( allowPoint ) items . push ( 'points' )
60+ if ( allowLine ) items . push ( 'lines' )
61+ if ( allowShape ) items . push ( 'shapes' )
62+
63+ if ( items . length === 0 ) return ''
64+
65+ if ( items . length === 1 ) {
66+ return items [ 0 ]
67+ }
68+
69+ if ( items . length === 2 ) {
70+ return `${ items [ 0 ] } or ${ items [ 1 ] } `
71+ }
72+
73+ return `${ items [ 0 ] } , ${ items [ 1 ] } or ${ items [ 2 ] } `
74+ }
75+
76+ /**
77+ * @param {boolean } allowPoint
78+ * @param {boolean } allowLine
79+ * @param {boolean } allowShape
80+ */
81+ function getHelpPanelHtml ( allowPoint , allowLine , allowShape ) {
82+ const lineOrShapeText = getLineOrShapeText ( allowLine , allowShape )
83+ const doneExtra = lineOrShapeText
84+ ? `<li>Double‑click, or select 'Done', when you have finished drawing ${ lineOrShapeText } </li>`
85+ : ''
86+ const allowedTypesText = getAllowedTypesPhrase (
87+ allowPoint ,
88+ allowLine ,
89+ allowShape
90+ )
91+ return `<p class="govuk-body-s govuk-!-margin-bottom-2">You can add ${ allowedTypesText } to the map.</p><ul class="govuk-list govuk-list--number govuk-body-s"><li>Search for a county, place or postcode</li><li>Use the + and - icons to zoom in and out</li>${ doneExtra } <li>Give the location a name</li></ul>`
3392}
3493
3594const lineFeatureProperties = {
@@ -157,11 +216,18 @@ export function processGeospatial(config, geospatial, index) {
157216 const { map, interactPlugin } = createMap ( mapId , initConfig , config )
158217 const featuresManager = getFeaturesManager ( geojson )
159218 const activeFeatureManager = getActiveFeatureManager ( )
160- const geometryTypes = geospatial . dataset . geometryTypes ?? 'point,line,shape'
219+ const geometryTypes = geospatial . dataset . geometrytypes ?? 'point,line,shape'
161220 const options = {
162221 geometryTypes
163222 }
164- const uiManager = getUIManager ( geojson , map , mapId , listEl , geospatialInput , options )
223+ const uiManager = getUIManager (
224+ geojson ,
225+ map ,
226+ mapId ,
227+ listEl ,
228+ geospatialInput ,
229+ options
230+ )
165231
166232 /**
167233 * @type {Context }
@@ -593,7 +659,8 @@ function createContainers(geospatialInput, index) {
593659function onMapReadyFactory ( context ) {
594660 const { map, activeFeatureManager, uiManager, interactPlugin, drawPlugin } =
595661 context
596- const { toggleActionButtons, renderList, getAllowableGeometryTypes } = uiManager
662+ const { toggleActionButtons, renderList, getAllowableGeometryTypes } =
663+ uiManager
597664 const { resetActiveFeature } = activeFeatureManager
598665
599666 /**
@@ -602,73 +669,66 @@ function onMapReadyFactory(context) {
602669 * @param {MapLibreMap } e.map - the map provider instance
603670 */
604671 return function onMapReady ( e ) {
605- // Add info panel
606- map . addPanel ( 'info' , helpPanelConfig )
607-
608672 const types = getAllowableGeometryTypes ( )
609673 const allowPoint = types . includes ( 'point' )
610674 const allowLine = types . includes ( 'line' )
611675 const allowShape = types . includes ( 'shape' )
612676
613- // If only a single geometry type, don't show any buttons as will default to that geometry operation
614- if ( types . split ( ',' ) . length === 1 ) {
615- resetActiveFeature ( )
616- toggleActionButtons ( true )
617- renderList ( true )
618- if ( allowPoint ) interactPlugin . enable ( )
619- if ( allowLine ) drawPlugin . newLine ( generateID ( ) , lineFeatureProperties )
620- if ( allowShape ) drawPlugin . newPolygon ( generateID ( ) , polygonFeatureProperties )
621- } else {
622- if ( allowPoint ) {
623- map . addButton ( 'btnAddPoint' , {
624- variant : 'tertiary' ,
625- label : 'Add point' ,
626- iconSvgContent : POINT_SVG ,
627- onClick : ( ) => {
628- resetActiveFeature ( )
629- toggleActionButtons ( true )
630- renderList ( true )
631- interactPlugin . enable ( )
632- } ,
633- mobile : { slot : 'actions' } ,
634- tablet : { slot : 'actions' } ,
635- desktop : { slot : 'actions' }
636- } )
637- }
677+ // Add info panel
678+ map . addPanel ( 'info' , {
679+ ...helpPanelConfig ,
680+ html : getHelpPanelHtml ( allowPoint , allowLine , allowShape )
681+ } )
638682
639- if ( allowShape ) {
640- map . addButton ( 'btnAddPolygon ' , {
641- variant : 'tertiary' ,
642- label : 'Add shape ' ,
643- iconSvgContent : POLYGON_SVG ,
644- onClick : ( ) => {
645- resetActiveFeature ( )
646- toggleActionButtons ( true )
647- renderList ( true )
648- drawPlugin . newPolygon ( generateID ( ) , polygonFeatureProperties )
649- } ,
650- mobile : { slot : 'actions' } ,
651- tablet : { slot : 'actions' } ,
652- desktop : { slot : 'actions' }
653- } )
654- }
683+ if ( allowPoint ) {
684+ map . addButton ( 'btnAddPoint ' , {
685+ variant : 'tertiary' ,
686+ label : 'Add point ' ,
687+ iconSvgContent : POINT_SVG ,
688+ onClick : ( ) => {
689+ resetActiveFeature ( )
690+ toggleActionButtons ( true )
691+ renderList ( true )
692+ interactPlugin . enable ( )
693+ } ,
694+ mobile : { slot : 'actions' } ,
695+ tablet : { slot : 'actions' } ,
696+ desktop : { slot : 'actions' }
697+ } )
698+ }
655699
656- if ( allowLine ) {
657- map . addButton ( 'btnAddLine' , {
658- variant : 'tertiary' ,
659- label : 'Add line' ,
660- iconSvgContent : LINE_SVG ,
661- onClick : ( ) => {
662- resetActiveFeature ( )
663- toggleActionButtons ( true )
664- renderList ( true )
665- drawPlugin . newLine ( generateID ( ) , lineFeatureProperties )
666- } ,
667- mobile : { slot : 'actions' } ,
668- tablet : { slot : 'actions' } ,
669- desktop : { slot : 'actions' }
670- } )
671- }
700+ if ( allowShape ) {
701+ map . addButton ( 'btnAddPolygon' , {
702+ variant : 'tertiary' ,
703+ label : 'Add shape' ,
704+ iconSvgContent : POLYGON_SVG ,
705+ onClick : ( ) => {
706+ resetActiveFeature ( )
707+ toggleActionButtons ( true )
708+ renderList ( true )
709+ drawPlugin . newPolygon ( generateID ( ) , polygonFeatureProperties )
710+ } ,
711+ mobile : { slot : 'actions' } ,
712+ tablet : { slot : 'actions' } ,
713+ desktop : { slot : 'actions' }
714+ } )
715+ }
716+
717+ if ( allowLine ) {
718+ map . addButton ( 'btnAddLine' , {
719+ variant : 'tertiary' ,
720+ label : 'Add line' ,
721+ iconSvgContent : LINE_SVG ,
722+ onClick : ( ) => {
723+ resetActiveFeature ( )
724+ toggleActionButtons ( true )
725+ renderList ( true )
726+ drawPlugin . newLine ( generateID ( ) , lineFeatureProperties )
727+ } ,
728+ mobile : { slot : 'actions' } ,
729+ tablet : { slot : 'actions' } ,
730+ desktop : { slot : 'actions' }
731+ } )
672732 }
673733
674734 // Set the map provider on the context
0 commit comments