Skip to content

Commit c02dc2c

Browse files
authored
Merge pull request #84 from felix-mu/81-persist-layer-selection-as-panel-configuration
feat: add toggle to make a data layer visible or invisible by default closes #81
2 parents 8531fcb + 07ec997 commit c02dc2c

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

src/GeomapPanel.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ export class GeomapPanel extends Component<Props, State> {
533533
const handler = await item.create(this.map!, overlay, config.theme2);
534534
// If it is a basemap create group with title to make it visible in the legend: https://github.com/walkermatt/ol-layerswitcher/blob/main/examples/layerswitcher.js#L10
535535
const layer = item.isBaseMap ? new Group({layers: [handler.init()], title: overlay.name, combine: true,} as GroupLayerOptions) : handler.init();
536+
537+
// Set visible to false if toggle is not set to make layer not selected in switch layer control
538+
if (overlay.visible !== undefined && overlay.visible === false) {
539+
layer.setVisible(false)
540+
}
541+
536542
(layer as any).___handler = handler;
537543
this.map!.addLayer(layer);
538544
this.layers.push({

src/editor/BaseLayerEditor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ export const BaseLayerEditor: FC<StandardEditorProps<ExtendMapLayerOptions, any,
2424
return <div>The base layer is configured by the server admin.</div>;
2525
}
2626

27-
return <LayerEditor options={value} data={context.data} onChange={onChange} filter={baseMapFilter} showNameField={false} />;
27+
return <LayerEditor options={value} data={context.data} onChange={onChange} filter={baseMapFilter} showNameField={false} isBaselayerEditor={true} />;
2828
};

src/editor/LayerEditor.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ export interface LayerEditorProps<TConfig = any> {
2323
onChange: (options: ExtendMapLayerOptions<TConfig>) => void;
2424
filter: (item: ExtendMapLayerRegistryItem) => boolean;
2525
showNameField?: boolean; // Show the name field in the options
26+
isBaselayerEditor?: boolean;
2627
}
2728

28-
export const LayerEditor: FC<LayerEditorProps> = ({ options, onChange, data, filter, showNameField }) => {
29+
export const LayerEditor: FC<LayerEditorProps> = ({ options, onChange, data, filter, showNameField, isBaselayerEditor = false }) => {
2930
// all basemaps
3031
const layerTypes = useMemo(() => {
3132
return geomapLayerRegistry.selectOptions(
@@ -210,23 +211,31 @@ export const LayerEditor: FC<LayerEditorProps> = ({ options, onChange, data, fil
210211
builder.addSliderInput({
211212
path: 'opacity',
212213
name: 'Opacity',
213-
defaultValue: 1,
214214
settings: {
215215
min: 0,
216216
max: 1,
217217
step: 0.1,
218218
},
219+
defaultValue: 1,
220+
});
221+
}
222+
223+
if (!isBaselayerEditor) {
224+
builder.addBooleanSwitch({
225+
path: 'visible',
226+
name: 'Layer is selected by default in the layer switch control',
227+
description: 'If toggled the layer is selected by default in the layer switch control. Uncheck to have the layer invisible by default.',
228+
settings: {},
229+
defaultValue: true
219230
});
220231
}
221232

222233
if (layer.registerOptionsUI) {
223234
layer.registerOptionsUI(builder);
224235
}
225236

226-
227-
228237
return builder;
229-
}, [options?.type, showNameField]);
238+
}, [options?.type, showNameField, isBaselayerEditor]);
230239

231240
// The react components
232241
const layerOptions = useMemo(() => {
@@ -243,11 +252,13 @@ export const LayerEditor: FC<LayerEditorProps> = ({ options, onChange, data, fil
243252
const context: StandardEditorContext<any> = {
244253
data,
245254
// options: options,
246-
options: {...options, opacity: options?.opacity === undefined && layer.showOpacity ? 1.0 : options?.opacity}
255+
options: {
256+
...options,
257+
opacity: options?.opacity === undefined && layer.showOpacity ? 1.0 : options?.opacity,
258+
visible: options?.visible === undefined ? true : options?.visible
259+
}
247260
};
248261

249-
250-
251262
const currentOptions = { ...options, type: layer.id, config: { ...layer.defaultOptions, ...options?.config } };
252263

253264
// Update the panel options if not set

src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface ExtendMapLayerOptions<TConfig = any> {
5050
displayProperties?: string[];
5151
titleField?: string;
5252
timeField?: string;
53+
visible?: boolean
5354
}
5455

5556
export interface ExtendMapLayerRegistryItem<TConfig = ExtendMapLayerOptions> extends RegistryItemWithOptions {

0 commit comments

Comments
 (0)