diff --git a/src/lib/catalog/catalog-layers-list/catalog-layers-list-binding.directive.ts b/src/lib/catalog/catalog-layers-list/catalog-layers-list-binding.directive.ts index c5c70c741c..a239427101 100644 --- a/src/lib/catalog/catalog-layers-list/catalog-layers-list-binding.directive.ts +++ b/src/lib/catalog/catalog-layers-list/catalog-layers-list-binding.directive.ts @@ -1,5 +1,7 @@ -import { Directive, Self, OnInit, OnDestroy, - HostListener } from '@angular/core'; +import { + Directive, Self, OnInit, OnDestroy, + HostListener +} from '@angular/core'; import { Subscription } from 'rxjs/Subscription'; import { MapService } from '../../map'; @@ -35,18 +37,18 @@ export class CatalogLayersListBindingDirective implements OnInit, OnDestroy { this.dataSourceService .createAsyncDataSource(dataSourceContext as AnyDataSourceContext) - .subscribe(dataSource => { + .subscribe(dataSource => { const layerInstance = this.layerService.createLayer(dataSource, layerContext); map.addLayer(layerInstance); }); } constructor(@Self() component: CatalogLayersListComponent, - private catalogService: CatalogService, - private mapService: MapService, - private dataSourceService: DataSourceService, - private layerService: LayerService, - private capabilitiesService: CapabilitiesService) { + private catalogService: CatalogService, + private mapService: MapService, + private dataSourceService: DataSourceService, + private layerService: LayerService, + private capabilitiesService: CapabilitiesService) { this.component = component; } @@ -59,6 +61,66 @@ export class CatalogLayersListBindingDirective implements OnInit, OnDestroy { this.selectedCatalog$$.unsubscribe(); } + /** + * Dig in the layerList for each layer definition + @param catalog: object of config.json parameter + @param layerList: object of current level of layers + @param groupsLayers: object of group of layers to show in the app + */ + includeRecursiveLayer(catalog, layerList, groupsLayers) { + let currentRegFilter; + let boolRegFilter = true; + let objGroupLayers; + // Dig all levels until last level (layer object are not defined on last level) + for (const group of layerList.Layer) { + if (group.queryable === false && typeof group.Layer !== 'undefined') { + // recursive, check next level + this.includeRecursiveLayer(catalog, group, groupsLayers); + } else { + // Define object of group layer + objGroupLayers = { + title: layerList.Title, + // Add only layers with regFilter condition respected + layers: layerList.Layer.reduce((arrLayer, layer) => { + boolRegFilter = true; + // Check for regex validation on layer's name + if (typeof catalog.regFilters !== 'undefined') { + // Test layer.Name for each regex define in config.json + for (const regFilter of catalog.regFilters) { + boolRegFilter = false; + currentRegFilter = new RegExp(regFilter); + boolRegFilter = currentRegFilter.test(layer.Name); + // If regex is respected, stop the for loop + if (boolRegFilter === true) { + break; + } + } + } + // If layer regex is okay (or not define), add the layer to the group + if (boolRegFilter === true) { + arrLayer.push({ + title: layer.Title, + type: 'wms', + url: catalog.url, + params: { + layers: layer.Name + } + }); + } + return arrLayer; + }, []) + }; + /* If object contain layers (when regFilters is define, the condition + in Layer.map can define group with no layer) */ + if (objGroupLayers.layers.length !== 0) { + groupsLayers.push(objGroupLayers); + } + // Break the group (don't add a group of layer for each of their layer!) + break; + } + } + } + handleCatalogChanged(catalog: Catalog) { if (!catalog || !catalog.url) { return; @@ -78,24 +140,8 @@ export class CatalogLayersListBindingDirective implements OnInit, OnDestroy { const groupsLayers: GroupLayers[] = []; this.capabilitiesService.getCapabilities('wms', catalog.url) .subscribe((capabilities) => { - for (const group of capabilities.Capability.Layer.Layer) { - groupsLayers.push({ - title: group.Title, - layers: group.Layer.map((layer) => { - return { - title: layer.Title, - type: 'wms', - url: catalog.url, - params: { - layers: layer.Name - } - }; - }) - }); - } - + this.includeRecursiveLayer(catalog, capabilities.Capability.Layer, groupsLayers); this.component.groupsLayers = groupsLayers; }); } - } diff --git a/src/lib/catalog/shared/catalog.interface.ts b/src/lib/catalog/shared/catalog.interface.ts index 6d3db36579..9719db824a 100644 --- a/src/lib/catalog/shared/catalog.interface.ts +++ b/src/lib/catalog/shared/catalog.interface.ts @@ -3,6 +3,7 @@ export interface Catalog { title?: string; url?: string; type?: string; + regFilters?: Array; } export interface CatalogServiceOptions {