Skip to content

Commit

Permalink
feat: request application layers in bundle
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Methods `parseNodes` and `parseFolder` are expecting a mandatory array of layer entities as second argument
  • Loading branch information
annarieger committed Jun 28, 2022
1 parent 8c02fe2 commit 715caad
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
9 changes: 9 additions & 0 deletions src/graphqlqueries/Layers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const allLayersByIds = `query($ids: [Int]) {
allLayersByIds(ids: $ids) {
id
clientConfig
name
sourceConfig
type
}
}`;
84 changes: 57 additions & 27 deletions src/parser/SHOGunApplicationUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ import SHOGunAPIClient from '../service/SHOGunAPIClient';

import { getBearerTokenHeader } from '../security/getBearerTokenHeader';


import {
allLayersByIds
} from '../graphqlqueries/Layers';
export interface SHOGunApplicationUtilOpts {
client?: SHOGunAPIClient;
}
Expand Down Expand Up @@ -95,49 +99,75 @@ class SHOGunApplicationUtil<T extends Application, S extends Layer> {
return;
}

const nodes = await this.parseNodes(layerTree.children, projection);
if (!this.client) {
Logger.warn('Cannot parse the layers in layertree because no ' +
'SHOGunClient has been provided.');
return;
}

const tree = new OlLayerGroup({
layers: nodes.reverse(),
visible: layerTree.checked
});
let applicationLayerIds: number[] = this.getLayerIds(layerTree.children);

return tree;
if (applicationLayerIds.length > 0) {
try {
const { allLayersByIds: layers } = await this.client.graphql().sendQuery<Layer>({
query: allLayersByIds,
variables: {
ids: applicationLayerIds
}
});
if (layerTree.children) {
const nodes = await this.parseNodes(layerTree.children, layers, projection);

const tree = new OlLayerGroup({
layers: nodes.reverse(),
visible: layerTree.checked
});
return tree;
}
} catch (e) {
Logger.warn('Could not parse the layer tree: ' + e);
return new OlLayerGroup();
}
}
return new OlLayerGroup();
}

async parseNodes(nodes: DefaultLayerTree[], projection?: OlProjectionLike) {
const collection: OlLayerBase[] = [];
getLayerIds(nodes: DefaultLayerTree[], ids?: number[]) {
let layerIds: number[] = ids ?? [];

for (const node of nodes) {
if (node.children) {
collection.push(await this.parseFolder(node, projection));
if (node.children?.length > 0) {
this.getLayerIds(node.children, layerIds);
} else {
if (!this.client) {
Logger.warn(`Couldn\'t parse the layer with ID ${node.layerId} because no ` +
'SHOGunClient has been provided.');

continue;
}

// TODO Fetch via graphlQL (multiple at once)
const layer = await this.client.layer<S>().findOne(node.layerId);

const olLayer = await this.parseLayer(layer, projection);
layerIds.push(node.layerId);
}
}
return layerIds;
}

olLayer.setVisible(node.checked);
async parseNodes(nodes: DefaultLayerTree[], layers: Layer[], projection?: OlProjectionLike) {
const collection: OlLayerBase[] = [];

collection.push(olLayer);
for (const node of nodes) {
if (node.children?.length > 0) {
collection.push(await this.parseFolder(node, layers, projection));
} else {
const layerNode = layers.find(l => l.id === node.layerId);
if (layerNode) {
const olLayer = await this.parseLayer(layerNode as S, projection);
olLayer.setVisible(node.checked);
collection.push(olLayer);
}
}
}

return collection;
}

async parseFolder(el: DefaultLayerTree, projection?: OlProjectionLike) {
const layers = await this.parseNodes(el.children, projection);
async parseFolder(el: DefaultLayerTree, layers: Layer[], projection?: OlProjectionLike) {
const layersInFolder = await this.parseNodes(el.children, layers, projection);

const folder = new OlLayerGroup({
layers: layers.reverse(),
layers: layersInFolder.reverse(),
visible: el.checked
});

Expand Down

0 comments on commit 715caad

Please # to comment.