@@ -31,9 +31,11 @@ import LanguageDetector from 'i18next-browser-languagedetector';
3131
3232import Keycloak from 'keycloak-js' ;
3333
34+ import { Collection } from 'ol' ;
3435import {
3536 defaults as OlControlDefaults
3637} from 'ol/control' ;
38+ import BaseLayer from 'ol/layer/Base' ;
3739import OlLayerGroup from 'ol/layer/Group' ;
3840import OlLayerTile from 'ol/layer/Tile' ;
3941import OlMap from 'ol/Map' ;
@@ -88,6 +90,10 @@ import {
8890import {
8991 setAppInfo
9092} from './store/appInfo' ;
93+ import {
94+ setAllowEmptyBackground as setBackgroundLayerChooserAllowEmptyBackground ,
95+ setVisible as setBackgroundLayerChooserVisible
96+ } from './store/backgroundLayerChooser' ;
9197import {
9298 setDescription
9399} from './store/description' ;
@@ -324,6 +330,11 @@ export const setApplicationToStore = async (application?: Application) => {
324330 // eslint-disable-next-line camelcase
325331 user_menu : ( config ) => {
326332 store . dispatch ( setUserMenuVisible ( config ?. visible ?? true ) ) ;
333+ } ,
334+ // eslint-disable-next-line camelcase
335+ background_layer_chooser : ( config ) => {
336+ store . dispatch ( setBackgroundLayerChooserVisible ( config ?. visible ?? false ) ) ;
337+ store . dispatch ( setBackgroundLayerChooserAllowEmptyBackground ( config ?. allowEmptyBackground ?? false ) ) ;
327338 }
328339 } ;
329340
@@ -469,6 +480,8 @@ const setupSHOGunMap = async (application: Application) => {
469480
470481 const interactions = await parser . parseMapInteractions ( application ) ;
471482
483+ layers = await addBackgroundLayers ( application , layers ) ;
484+
472485 return new OlMap ( {
473486 view,
474487 layers,
@@ -479,6 +492,67 @@ const setupSHOGunMap = async (application: Application) => {
479492 } ) ;
480493} ;
481494
495+ const addBackgroundLayers = async ( application : Application , layers : OlLayerGroup | undefined ) : Promise < Collection < BaseLayer > | undefined > => {
496+ const blcLayers = application . toolConfig ?. find ( config => config . name === 'background_layer_chooser' ) ?. config . layers ;
497+ const initiallySelectedLayerId = application . toolConfig ?. find ( config => config . name === 'background_layer_chooser' ) ?. config . initiallySelectedLayer ;
498+ const backgroundMapLayers = [ ] ;
499+
500+ if ( blcLayers ) {
501+ let initiallySelectedLayerPresent = false ;
502+
503+ for ( const layer of blcLayers ) {
504+ try {
505+ if ( ! layer . layerId ) {
506+ continue ;
507+ }
508+
509+ const existingLayer = layers ?. getLayersArray ( ) . find ( item => item . get ( 'shogunId' ) === layer . layerId ) ;
510+ let olLayer ;
511+ if ( existingLayer ) {
512+ olLayer = existingLayer ;
513+ } else {
514+ const l = await client ?. layer ( ) . findOne ( layer . layerId ) ;
515+ if ( ! l ) {
516+ continue ;
517+ }
518+ olLayer = await parser . parseLayer ( l ) ;
519+ }
520+
521+ if ( ! olLayer || olLayer instanceof OlLayerGroup ) {
522+ continue ;
523+ }
524+ olLayer . set ( 'isBackgroundLayer' , true ) ;
525+ olLayer . setVisible ( false ) ;
526+
527+ if ( layer . title ) {
528+ olLayer . set ( 'name' , layer . title ) ;
529+ }
530+
531+ if ( layer . layerId === initiallySelectedLayerId ) {
532+ olLayer . setVisible ( true ) ;
533+ initiallySelectedLayerPresent = true ;
534+ }
535+ if ( ! existingLayer ) {
536+ backgroundMapLayers . push ( olLayer ) ;
537+ }
538+ }
539+ catch ( error ) {
540+ Logger . error ( `Layer ${ layer . layerId } could not be loaded successfully: ` , error ) ;
541+ }
542+ }
543+
544+ // Fallback for when no initiallySelectedLayerId is set. Choose first layer as default.
545+ if ( ! initiallySelectedLayerPresent && backgroundMapLayers . length >= 1 ) {
546+ backgroundMapLayers [ 0 ] . setVisible ( true ) ;
547+ }
548+
549+ for ( const backgroundLayerMap of backgroundMapLayers ) {
550+ layers ?. getLayers ( ) . insertAt ( 0 , backgroundLayerMap ) ;
551+ }
552+ return layers ?. getLayers ( ) ;
553+ }
554+ } ;
555+
482556// TODO Make default/fallback app configurable?
483557const setupDefaultMap = ( ) => {
484558 const osmLayer = new OlLayerTile ( {
0 commit comments