Skip to content

Commit

Permalink
UX - Display an error message if the layer is not found in the CFG fi…
Browse files Browse the repository at this point in the history
…le, if admin
  • Loading branch information
Gustry committed Feb 6, 2025
1 parent b7d05cb commit 9526fdc
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 31 deletions.
17 changes: 16 additions & 1 deletion assets/src/modules/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class Config {
this._options = null;
this._layers = null;
this._layerTree = null;
this._invalid_layers = null;
this._baselayers = null;
this._layersOrder = null;
this._hasMetadata = true;
Expand Down Expand Up @@ -182,11 +183,25 @@ export class Config {
*/
get layerTree() {
if (this._layerTree == null) {
this._layerTree = buildLayerTreeConfig(this._theWmsCapabilities.Capability.Layer, this.layers);
const [root, invalid] = buildLayerTreeConfig(this._theWmsCapabilities.Capability.Layer, this.layers);
this._layerTree = root;
this._invalid_layers = invalid;
}
return this._layerTree;
}

/**
* List of invalid layers, not found in the Lizmap configuration file, but found in the WMS GetCapabilities.
* Note,
* @type {LayerTreeGroupConfig}
*/
get invalidLayersNotFoundInCfg() {
if (this._layerTree == null) {
return [];
}
return this._invalid_layers;
}

/**
* Config base layers
* @type {BaseLayersConfig}
Expand Down
33 changes: 19 additions & 14 deletions assets/src/modules/config/LayerTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AttributionConfig } from './Attribution.js';
import { LayerConfig, LayersConfig } from './Layer.js';

/**
* Class representing a wMS layer Geographic Bounding Box
* Class representing a WMS layer Geographic Bounding Box
* @class
* @augments Extent
*/
Expand Down Expand Up @@ -403,45 +403,50 @@ export class LayerTreeGroupConfig extends LayerTreeItemConfig {
/**
* Function to build layer tree items config based on WMS capabilities
* @function
* @param {object} wmsCapaLayerGroup - the wms layer capabilities
* @param {object} wmsCapaLayerGroup - the WMS layer capabilities
* @param {LayersConfig} layersCfg - the lizmap layers config instance
* @param {number} level - the wms layer level
* @returns {LayerTreeItemConfig[]} the layer tree items of the wms layer
* @param {number} level - the WMS layer level
* @returns {Array[LayerTreeItemConfig[], String[]]} The layer tree items of the WMS layer, and the list of invalid layer names
*/
function buildLayerTreeGroupConfigItems(wmsCapaLayerGroup, layersCfg, level) {
let items = [];
let invalid = [];

if (!wmsCapaLayerGroup.hasOwnProperty('Layer')) {
return items;
return [items, invalid];
}

for(const wmsCapaLayer of wmsCapaLayerGroup.Layer) {
const wmsName = wmsCapaLayer.Name;
const cfg = layersCfg.getLayerConfigByWmsName(wmsName);
if (cfg == null) {
console.log('The WMS layer name `'+ wmsName +'` is unknown!');
invalid.push(wmsName);
continue;
}
if (wmsCapaLayer.hasOwnProperty('Layer') && wmsCapaLayer.Layer.length != 0) {
const groupItems = buildLayerTreeGroupConfigItems(wmsCapaLayer, layersCfg, level+1);

if (wmsCapaLayer.hasOwnProperty('Layer') && wmsCapaLayer.Layer.length !== 0) {
const [groupItems, invalid_layers] = buildLayerTreeGroupConfigItems(wmsCapaLayer, layersCfg, level+1);
items.push(new LayerTreeGroupConfig(cfg.name, level+1, groupItems, wmsCapaLayer, cfg));
invalid.concat(invalid_layers);
} else {
// avoid to add the baseLayers group to the map if it doesn't contain any layer.
if(wmsName.toLowerCase() != 'baselayers') {
if(wmsName.toLowerCase() !== 'baselayers') {
items.push(new LayerTreeLayerConfig(cfg.name, level+1, wmsCapaLayer, cfg));
}

}
}
return items;
return [items, invalid];
}

/**
* Function to build the root layer tree config based on WMS capabilities
* @function
* @param {object} wmsCapaLayerRoot - the wms root layer capabilities
* @param {object} wmsCapaLayerRoot - the WMS root layer capabilities
* @param {LayersConfig} layersCfg - the lizmap layers config instance
* @returns {LayerTreeGroupConfig} The root layer tree config based on WMS capabilities
* @returns {LayerTreeGroupConfig} The root layer tree config based on WMS capabilities, and the list of invalid layer names.
*/
export function buildLayerTreeConfig(wmsCapaLayerRoot, layersCfg) {
let items = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0);
return new LayerTreeGroupConfig('root', 0, items, wmsCapaLayerRoot);
const [items, invalid] = buildLayerTreeGroupConfigItems(wmsCapaLayerRoot, layersCfg, 0);
return [new LayerTreeGroupConfig('root', 0, items, wmsCapaLayerRoot), invalid];
}
12 changes: 8 additions & 4 deletions tests/js-units/node/config/baselayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,9 @@ describe('BaseLayersConfig', function () {
config.layers[blName] = blGroupCfg;

const layers = new LayersConfig(config.layers);
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [root, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);

expect(invalid).to.have.length(0)
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -665,8 +666,9 @@ describe('BaseLayersConfig', function () {
expect(config).to.not.be.undefined

const layers = new LayersConfig(config.layers);
const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [root, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);

expect(invalid).to.have.length(0)
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -870,8 +872,9 @@ describe('BaseLayersConfig', function () {
});
}

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [root, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);

expect(invalid).to.have.length(0)
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -1016,8 +1019,9 @@ describe('BaseLayersConfig', function () {
});
}

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [root, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);

expect(invalid).to.have.length(0)
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down
6 changes: 4 additions & 2 deletions tests/js-units/node/config/layerTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ describe('buildLayerTreeConfig', function () {

const layers = new LayersConfig(config.layers);

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [root, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(invalid).to.have.length(0)
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down Expand Up @@ -301,7 +302,8 @@ describe('buildLayerTreeConfig', function () {

const layers = new LayersConfig(config.layers);

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [root, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(invalid).to.have.length(0)
expect(root).to.be.instanceOf(LayerTreeGroupConfig)
expect(root.name).to.be.eq('root')
expect(root.type).to.be.eq('group')
Expand Down
3 changes: 2 additions & 1 deletion tests/js-units/node/config/layersOrder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ describe('buildLayersOrder', function () {

const layers = new LayersConfig(config.layers);

const root = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [root, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);

expect(invalid).to.have.length(0)
const layersOrder = buildLayersOrder(config, root);
expect(layersOrder).to.have.ordered.members([
"points_of_interest",
Expand Down
6 changes: 4 additions & 2 deletions tests/js-units/node/state/baselayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ function getBaseLayersState(name) {
expect(config).to.not.be.undefined

const layers = new LayersConfig(config.layers);
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(invalid).to.have.length(0)

let baseLayerTreeItem = null;
for (const layerTreeItem of rootCfg.getChildren()) {
Expand Down Expand Up @@ -158,7 +159,8 @@ describe('BaseLayersState', function () {
config.layers[blName] = blGroupCfg;

const layers = new LayersConfig(config.layers);
const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(invalid).to.have.length(0)

const blGroup = rootCfg.children[6];
expect(blGroup).to.be.instanceOf(LayerTreeGroupConfig)
Expand Down
3 changes: 2 additions & 1 deletion tests/js-units/node/state/externallayertree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ function getRootLayerTreeGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
expect(invalid).to.have.length(0)

const layersOrder = buildLayersOrder(config, rootCfg);

Expand Down
3 changes: 2 additions & 1 deletion tests/js-units/node/state/externalmaplayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ function getRootMapGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
expect(invalid).to.have.length(0)

const layersOrder = buildLayersOrder(config, rootCfg);

Expand Down
9 changes: 6 additions & 3 deletions tests/js-units/node/state/layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ function getRootLayerGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
expect(invalid).to.have.length(0)

const layersOrder = buildLayersOrder(config, rootCfg);

Expand Down Expand Up @@ -63,8 +64,9 @@ function getLayersAndGroupsCollection(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
expect(invalid).to.have.length(0)

const layersOrder = buildLayersOrder(config, rootCfg);

Expand Down Expand Up @@ -1670,8 +1672,9 @@ describe('LayersAndGroupsCollection', function () {
"group-without-children"
)

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
expect(invalid).to.have.length(0)

// `group-without-children` has a layerTree config and it is a layer not a group
expect(rootCfg.childrenCount).to.be.eq(4)
Expand Down
3 changes: 2 additions & 1 deletion tests/js-units/node/state/layerTree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ function getRootLayerTreeGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
expect(invalid).to.have.length(0)

const layersOrder = buildLayersOrder(config, rootCfg);

Expand Down
3 changes: 2 additions & 1 deletion tests/js-units/node/state/maplayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ function getRootMapGroupState(name) {

const layers = new LayersConfig(config.layers);

const rootCfg = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
const [rootCfg, invalid] = buildLayerTreeConfig(capabilities.Capability.Layer, layers);
expect(rootCfg).to.be.instanceOf(LayerTreeGroupConfig)
expect(invalid).to.have.length(0)

const layersOrder = buildLayersOrder(config, rootCfg);

Expand Down

0 comments on commit 9526fdc

Please sign in to comment.