From 95533e24252620595d7f2fb395738f8a8c609e9e Mon Sep 17 00:00:00 2001 From: ahocevar Date: Wed, 15 Aug 2018 10:58:33 +0200 Subject: [PATCH 1/3] Use configured zIndex for unmanaged layers --- src/ol/layer/Base.js | 9 ++++---- src/ol/layer/Layer.js | 4 +++- test/spec/ol/layer/layer.test.js | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/ol/layer/Base.js b/src/ol/layer/Base.js index 2cc30365c5..a8ce09d3fe 100644 --- a/src/ol/layer/Base.js +++ b/src/ol/layer/Base.js @@ -13,8 +13,8 @@ import {assign} from '../obj.js'; * @property {boolean} [visible=true] Visibility. * @property {module:ol/extent~Extent} [extent] The bounding extent for layer rendering. The layer will not be * rendered outside of this extent. - * @property {number} [zIndex=0] The z-index for layer rendering. At rendering time, the layers - * will be ordered, first by Z-index and then by position. + * @property {number} [zIndex] The z-index for layer rendering. At rendering time, the layers + * will be ordered, first by Z-index and then by position. When undefined, a zIndex of 0 is assumed. * @property {number} [minResolution] The minimum resolution (inclusive) at which this layer will be * visible. * @property {number} [maxResolution] The maximum resolution (exclusive) below which this layer will @@ -48,8 +48,7 @@ class BaseLayer extends BaseObject { options.opacity !== undefined ? options.opacity : 1; properties[LayerProperty.VISIBLE] = options.visible !== undefined ? options.visible : true; - properties[LayerProperty.Z_INDEX] = - options.zIndex !== undefined ? options.zIndex : 0; + properties[LayerProperty.Z_INDEX] = options.zIndex; properties[LayerProperty.MAX_RESOLUTION] = options.maxResolution !== undefined ? options.maxResolution : Infinity; properties[LayerProperty.MIN_RESOLUTION] = @@ -91,7 +90,7 @@ class BaseLayer extends BaseObject { this.state_.sourceState = this.getSourceState(); this.state_.visible = this.getVisible(); this.state_.extent = this.getExtent(); - this.state_.zIndex = this.getZIndex(); + this.state_.zIndex = this.getZIndex() || 0; this.state_.maxResolution = this.getMaxResolution(); this.state_.minResolution = Math.max(this.getMinResolution(), 0); diff --git a/src/ol/layer/Layer.js b/src/ol/layer/Layer.js index 9d361818f1..ceb8c186b3 100644 --- a/src/ol/layer/Layer.js +++ b/src/ol/layer/Layer.js @@ -191,7 +191,9 @@ class Layer extends BaseLayer { this.mapPrecomposeKey_ = listen(map, RenderEventType.PRECOMPOSE, function(evt) { const layerState = this.getLayerState(); layerState.managed = false; - layerState.zIndex = Infinity; + if (this.getZIndex() === undefined) { + layerState.zIndex = Infinity; + } evt.frameState.layerStatesArray.push(layerState); evt.frameState.layerStates[getUid(this)] = layerState; }, this); diff --git a/test/spec/ol/layer/layer.test.js b/test/spec/ol/layer/layer.test.js index 807531e8e2..ff3aeb97a5 100644 --- a/test/spec/ol/layer/layer.test.js +++ b/test/spec/ol/layer/layer.test.js @@ -434,6 +434,41 @@ describe('ol.layer.Layer', function() { }); + describe('zIndex for unmanaged layers', function() { + + let frameState, layer; + + beforeEach(function() { + layer = new Layer({ + map: map + }); + frameState = { + layerStatesArray: [], + layerStates: {} + }; + }); + + afterEach(function() { + layer.setMap(null); + }); + + it('has Infinity as zIndex when not configured otherwise', function() { + map.dispatchEvent(new RenderEvent('precompose', null, + frameState, null, null)); + const layerState = frameState.layerStatesArray[0]; + expect(layerState.zIndex).to.be(Infinity); + }); + + it('respects the configured zIndex', function() { + layer.setZIndex(42); + map.dispatchEvent(new RenderEvent('precompose', null, + frameState, null, null)); + const layerState = frameState.layerStatesArray[0]; + expect(layerState.zIndex).to.be(42); + }); + + }); + }); }); From 8ab1589f9afd26945ecbb4d02ff8be4543c90e74 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Wed, 15 Aug 2018 10:59:38 +0200 Subject: [PATCH 2/3] Add getOverlay method to access sketch and selection layers --- src/ol/interaction/Draw.js | 9 +++++++++ src/ol/interaction/Modify.js | 9 +++++++++ src/ol/interaction/Select.js | 21 ++++++++++++++++----- test/spec/ol/interaction/draw.test.js | 7 +++++++ test/spec/ol/interaction/modify.test.js | 9 +++++++++ test/spec/ol/interaction/select.test.js | 7 +++++++ 6 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/ol/interaction/Draw.js b/src/ol/interaction/Draw.js index b9fb7419f1..63713f8369 100644 --- a/src/ol/interaction/Draw.js +++ b/src/ol/interaction/Draw.js @@ -441,6 +441,15 @@ class Draw extends PointerInteraction { this.updateState_(); } + /** + * Get the overlay layer that this interaction renders sketch features to. + * @return {module:ol/layer/Vector} Overlay layer. + * @api + */ + getOverlay() { + return this.overlay_; + } + /** * Handle move events. * @param {module:ol/MapBrowserEvent} event A move event. diff --git a/src/ol/interaction/Modify.js b/src/ol/interaction/Modify.js index 37879f6298..c91a7c9370 100644 --- a/src/ol/interaction/Modify.js +++ b/src/ol/interaction/Modify.js @@ -422,6 +422,15 @@ class Modify extends PointerInteraction { super.setMap(map); } + /** + * Get the overlay layer that this interaction renders sketch features to. + * @return {module:ol/layer/Vector} Overlay layer. + * @api + */ + getOverlay() { + return this.overlay_; + } + /** * @param {module:ol/source/Vector~VectorSourceEvent} event Event. * @private diff --git a/src/ol/interaction/Select.js b/src/ol/interaction/Select.js index 4c123fa154..aaaba63537 100644 --- a/src/ol/interaction/Select.js +++ b/src/ol/interaction/Select.js @@ -255,11 +255,13 @@ class Select extends Interaction { */ this.featureLayerAssociation_ = {}; - const features = this.featureOverlay_.getSource().getFeaturesCollection(); - listen(features, CollectionEventType.ADD, - this.addFeature_, this); - listen(features, CollectionEventType.REMOVE, - this.removeFeature_, this); + if (this.featureOverlay_) { + const features = this.featureOverlay_.getSource().getFeaturesCollection(); + listen(features, CollectionEventType.ADD, + this.addFeature_, this); + listen(features, CollectionEventType.REMOVE, + this.removeFeature_, this); + } } @@ -307,6 +309,15 @@ class Select extends Interaction { ); } + /** + * Get the overlay layer that this interaction renders selected features to. + * @return {module:ol/layer/Vector} Overlay layer. + * @api + */ + getOverlay() { + return this.featureOverlay_; + } + /** * Hit-detection tolerance. Pixels inside the radius around the given position * will be checked for features. This only works for the canvas renderer and diff --git a/test/spec/ol/interaction/draw.test.js b/test/spec/ol/interaction/draw.test.js index 7f70e0e30d..cfc5f45a89 100644 --- a/test/spec/ol/interaction/draw.test.js +++ b/test/spec/ol/interaction/draw.test.js @@ -1060,6 +1060,13 @@ describe('ol.interaction.Draw', function() { }); }); + describe('#getOverlay', function() { + it('returns the feature overlay layer', function() { + const draw = new Draw({}); + expect (draw.getOverlay()).to.eql(draw.overlay_); + }); + }); + describe('createRegularPolygon', function() { it('creates a regular polygon in Circle mode', function() { const draw = new Draw({ diff --git a/test/spec/ol/interaction/modify.test.js b/test/spec/ol/interaction/modify.test.js index 2ac9ebb07d..aa6ca05749 100644 --- a/test/spec/ol/interaction/modify.test.js +++ b/test/spec/ol/interaction/modify.test.js @@ -712,4 +712,13 @@ describe('ol.interaction.Modify', function() { }); }); + describe('#getOverlay', function() { + it('returns the feature overlay layer', function() { + const modify = new Modify({ + features: new Collection() + }); + expect (modify.getOverlay()).to.eql(modify.overlay_); + }); + }); + }); diff --git a/test/spec/ol/interaction/select.test.js b/test/spec/ol/interaction/select.test.js index 79379cabc1..0445fecf53 100644 --- a/test/spec/ol/interaction/select.test.js +++ b/test/spec/ol/interaction/select.test.js @@ -442,4 +442,11 @@ describe('ol.interaction.Select', function() { }); }); }); + + describe('#getOverlay', function() { + it('returns the feature overlay layer', function() { + const select = new Select(); + expect (select.getOverlay()).to.eql(select.featureOverlay_); + }); + }); }); From 161c5b0105a459066812f279b5ad7d846c41450b Mon Sep 17 00:00:00 2001 From: ahocevar Date: Wed, 15 Aug 2018 11:24:29 +0200 Subject: [PATCH 3/3] Better documentation of the zIndex default --- src/ol/layer/Base.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ol/layer/Base.js b/src/ol/layer/Base.js index a8ce09d3fe..379da22901 100644 --- a/src/ol/layer/Base.js +++ b/src/ol/layer/Base.js @@ -14,7 +14,9 @@ import {assign} from '../obj.js'; * @property {module:ol/extent~Extent} [extent] The bounding extent for layer rendering. The layer will not be * rendered outside of this extent. * @property {number} [zIndex] The z-index for layer rendering. At rendering time, the layers - * will be ordered, first by Z-index and then by position. When undefined, a zIndex of 0 is assumed. + * will be ordered, first by Z-index and then by position. When `undefined`, a `zIndex` of 0 is assumed + * for layers that are added to the map's `layers` collection, or `Infinity` when the layer's `setMap()` + * method was used. * @property {number} [minResolution] The minimum resolution (inclusive) at which this layer will be * visible. * @property {number} [maxResolution] The maximum resolution (exclusive) below which this layer will