Skip to content

Commit

Permalink
Merge 5b8357d into 6aa9884
Browse files Browse the repository at this point in the history
  • Loading branch information
ahennr committed Jan 16, 2019
2 parents 6aa9884 + 5b8357d commit da26fd8
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/LayerTree/LayerTree.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import OlMap from 'ol/Map';
import OlLayerBase from 'ol/layer/Base';
import OlLayerGroup from 'ol/layer/Group';
import OlCollection from 'ol/Collection';
import OlMapEvent from 'ol/MapEvent';
import { unByKey } from 'ol/Observable';

import isBoolean from 'lodash/isBoolean';
Expand All @@ -30,7 +31,6 @@ import { CSS_PREFIX } from '../constants';
*/
class LayerTree extends React.Component {


/**
* The className added to this component.
* @type {String}
Expand Down Expand Up @@ -110,8 +110,6 @@ class LayerTree extends React.Component {
};
}
}

return null;
}

/**
Expand All @@ -126,7 +124,8 @@ class LayerTree extends React.Component {
layerGroup: null,
layerGroupRevision: null,
treeNodes: [],
checkedKeys: []
checkedKeys: [],
mapResolution: -1
};
}

Expand Down Expand Up @@ -225,10 +224,8 @@ class LayerTree extends React.Component {
* nodes whenever the view's resolution changes.
*/
registerResolutionChangeHandler() {
const mapView = this.props.map.getView();
const evtKey = mapView.on('change:resolution', () => {
this.rebuildTreeNodes();
});
const { map } = this.props;
const evtKey = map.on('moveend', this.rebuildTreeNodes.bind(this));
this.olListenerKeys.push(evtKey); // TODO when and how to we unbind?
}

Expand Down Expand Up @@ -309,12 +306,24 @@ class LayerTree extends React.Component {

/**
* Rebuilds the treeNodes and its checked states.
* @param {ol.MapEvent} evt The OpenLayers MapEvent (passed by moveend)
*
*/
rebuildTreeNodes = () => {
rebuildTreeNodes = evt => {
const { mapResolution } = this.state;

if (evt && evt instanceof OlMapEvent && evt.target && evt.target.getView) {
if (mapResolution === evt.target.getView().getResolution()) {
// If map resolution didn't change => no redraw of tree nodes needed.
return;
}
}

this.treeNodesFromLayerGroup(this.state.layerGroup);
const checkedKeys = this.getVisibleOlUids();
this.setState({
checkedKeys
checkedKeys,
mapResolution: evt ? evt.target.getView().getResolution() : -1
});
}

Expand Down

0 comments on commit da26fd8

Please sign in to comment.