Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix layer visibility on permalink when layer in folder #413

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/PermalinkUtil/PermalinkUtil.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import TileLayer from 'ol/layer/Tile';
import OlLayerGroup from 'ol/layer/Group';
import MapUtil from '../MapUtil/MapUtil';

/**
Expand Down Expand Up @@ -48,7 +49,16 @@ export class PermalinkUtil {
if (layers) {
allLayers.filter((l) => l instanceof TileLayer)
.forEach((l) => {
l.setVisible(layers.includes(l.get('name')));
const visible = layers.includes(l.get('name'));
l.setVisible(visible);
// also make all parent folders / groups visible so
// that the layer becomes visible in map
if (visible) {
PermalinkUtil.setParentsVisible(
map,
map.getLayerGroup().getLayers(),
l.ol_uid);
}
});
}

Expand All @@ -63,6 +73,25 @@ export class PermalinkUtil {
map.getView().setZoom(parseInt(zoom, 10));
}
};

/**
* Search through the given Ol-Collection for the given id and
* set all parenting groups visible.
* @param {Object} map The openlayers map
* @param {Object} coll The Openlayers Collection
* @param {string} id Ther layer ol uid to search for
*/
static setParentsVisible = (map, coll, id) => {
coll.forEach(el => {
if (el instanceof OlLayerGroup) {
const layers = MapUtil.getLayersByGroup(map, el);
if (layers.map(layer => layer.ol_uid).includes(id)) {
el.setVisible(true);
}
PermalinkUtil.setParentsVisible(map, el.getLayers(), id);
}
});
};
}

export default PermalinkUtil;
49 changes: 48 additions & 1 deletion src/PermalinkUtil/PermalinkUtil.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*eslint-env jest*/
import OlSourceTile from 'ol/source/Tile';
import OlLayerTile from 'ol/layer/Tile';
import OlLayerGroup from 'ol/layer/Group';

import {
PermalinkUtil
Expand Down Expand Up @@ -174,7 +175,8 @@ describe('PermalinkUtil', () => {
Object.defineProperty(window, 'location', {
value: {
href: link
}
},
configurable: true
});
PermalinkUtil.applyLink(map, '|');

Expand All @@ -184,6 +186,51 @@ describe('PermalinkUtil', () => {
.map(l => l.get('name'));
expect(visibles).toEqual(['peter', 'pan']);
});

it('applies visible state to parenting groups', () => {
map.getLayers().clear();
map.addLayer(new OlLayerGroup({
name: 'peter',
visible: false,
layers: [
new OlLayerTile({
name: 'paul',
visible: false,
source: new OlSourceTile({
attributions: ''
})
}),
new OlLayerTile({
name: 'pan',
visible: false,
source: new OlSourceTile({
attributions: ''
})
})
]
}));

const link = 'http://fake?zoom=3&center=10|20&layers=pan';
delete global.window.location;
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
href: link
},
configurable: true
});
PermalinkUtil.applyLink(map, '|');

const firstLevelVisibles = map.getLayers().getArray()
.filter(l => l.getVisible())
.map(l => l.get('name'));
expect(firstLevelVisibles).toEqual(['peter']);

const secondLevelVisibles = map.getLayers().getArray()[0]
.getLayers().getArray().filter(l => l.getVisible())
.map(l => l.get('name'));
expect(secondLevelVisibles).toEqual(['pan']);
});
});
});
});