From 8ab1589f9afd26945ecbb4d02ff8be4543c90e74 Mon Sep 17 00:00:00 2001 From: ahocevar Date: Wed, 15 Aug 2018 10:59:38 +0200 Subject: [PATCH] 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_); + }); + }); });