From 80188b204463671775b37fdb50a950e12d152def Mon Sep 17 00:00:00 2001 From: simonseyock Date: Wed, 19 Oct 2016 13:24:15 +0200 Subject: [PATCH] Change signature of api methods --- src/ol/interaction/select.js | 12 +++++--- src/ol/interaction/translate.js | 4 ++- src/ol/map.js | 38 ++++++++++++++++-------- test/spec/ol/renderer/canvas/map.test.js | 7 +++-- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/ol/interaction/select.js b/src/ol/interaction/select.js index 7817067c3d..2820df269f 100644 --- a/src/ol/interaction/select.js +++ b/src/ol/interaction/select.js @@ -200,7 +200,9 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { // pixel, or clear the selected feature(s) if there is no feature at // the pixel. ol.obj.clear(this.featureLayerAssociation_); - map.forEachFeatureAtPixel(mapBrowserEvent.pixel, + map.forEachFeatureAtPixel(mapBrowserEvent.pixel, { + layerFilter: this.layerFilter_ + }, /** * @param {ol.Feature|ol.render.Feature} feature Feature. * @param {ol.layer.Layer} layer Layer. @@ -212,7 +214,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { this.addFeatureLayerAssociation_(feature, layer); return !this.multi_; } - }, this, this.layerFilter_); + }, this); var i; for (i = features.getLength() - 1; i >= 0; --i) { var feature = features.item(i); @@ -230,7 +232,9 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { } } else { // Modify the currently selected feature(s). - map.forEachFeatureAtPixel(mapBrowserEvent.pixel, + map.forEachFeatureAtPixel(mapBrowserEvent.pixel, { + layerFilter: this.layerFilter_ + }, /** * @param {ol.Feature|ol.render.Feature} feature Feature. * @param {ol.layer.Layer} layer Layer. @@ -249,7 +253,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) { } return !this.multi_; } - }, this, this.layerFilter_); + }, this); var j; for (j = deselected.length - 1; j >= 0; --j) { features.remove(deselected[j]); diff --git a/src/ol/interaction/translate.js b/src/ol/interaction/translate.js index 3b61e333dc..b1aade1db1 100644 --- a/src/ol/interaction/translate.js +++ b/src/ol/interaction/translate.js @@ -197,7 +197,9 @@ ol.interaction.Translate.prototype.featuresAtPixel_ = function(pixel, map) { ol.array.includes(this.features_.getArray(), feature)) { return feature; } - }, this, this.layerFilter_); + }, this, { + layerFilter: this.layerFilter_ + }); }; diff --git a/src/ol/map.js b/src/ol/map.js index 8acbe9a76a..8cea0b20e3 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -554,11 +554,26 @@ ol.Map.prototype.disposeInternal = function() { }; +/** + * @typedef {object} ol.MapForEachFeatureOptions + * @property {(function(this: U, ol.layer.Layer): boolean)=} layerFilter Layer + * filter function. The filter function will receive one argument, the + * {@link ol.layer.Layer layer-candidate} and it should return a boolean + * value. Only layers which are visible and for which this function returns + * `true` will be tested for features. By default, all visible layers will + * be tested. + * @property {U=} layerFilterThis Value to use as `this` when executing `layerFilter`. + * @property {number=} hitTolerance the hitTolerance in pixels in which features + * get hit. + */ + + /** * Detect features that intersect a pixel on the viewport, and execute a * callback with each intersecting feature. Layers included in the detection can * be configured through `opt_layerFilter`. * @param {ol.Pixel} pixel Pixel. + * @param {ol.MapForEachFeatureOptions=} opt_options * @param {function(this: S, (ol.Feature|ol.render.Feature), * ol.layer.Layer): T} callback Feature callback. The callback will be * called with two arguments. The first argument is one @@ -567,30 +582,27 @@ ol.Map.prototype.disposeInternal = function() { * the {@link ol.layer.Layer layer} of the feature and will be null for * unmanaged layers. To stop detection, callback functions can return a * truthy value. - * @param {S=} opt_this Value to use as `this` when executing `callback`. - * @param {(function(this: U, ol.layer.Layer): boolean)=} opt_layerFilter Layer - * filter function. The filter function will receive one argument, the - * {@link ol.layer.Layer layer-candidate} and it should return a boolean - * value. Only layers which are visible and for which this function returns - * `true` will be tested for features. By default, all visible layers will - * be tested. - * @param {U=} opt_this2 Value to use as `this` when executing `layerFilter`. + * @param {S=} opt_this * @return {T|undefined} Callback result, i.e. the return value of last * callback execution, or the first truthy callback return value. * @template S,T,U * @api stable */ -ol.Map.prototype.forEachFeatureAtPixel = function(pixel, callback, opt_this, opt_layerFilter, opt_this2) { +ol.Map.prototype.forEachFeatureAtPixel = function(pixel, opt_options, callback, opt_this) { + if (typeof opt_options !== 'object') { + opt_this = callback; + callback = opt_options; + opt_options = {}; + } if (!this.frameState_) { return; } var coordinate = this.getCoordinateFromPixel(pixel); var thisArg = opt_this !== undefined ? opt_this : null; - var layerFilter = opt_layerFilter !== undefined ? - opt_layerFilter : ol.functions.TRUE; - var thisArg2 = opt_this2 !== undefined ? opt_this2 : null; + var layerFilter = opt_options.layerFilter || ol.functions.TRUE; + var thisArg2 = opt_options.layerFilterThis || null; return this.renderer_.forEachFeatureAtCoordinate( - coordinate, this.frameState_, callback, thisArg, + coordinate, this.frameState_, opt_options.hitTolerance || 0, callback, thisArg, layerFilter, thisArg2); }; diff --git a/test/spec/ol/renderer/canvas/map.test.js b/test/spec/ol/renderer/canvas/map.test.js index a651cb7854..a9ae31d819 100644 --- a/test/spec/ol/renderer/canvas/map.test.js +++ b/test/spec/ol/renderer/canvas/map.test.js @@ -111,10 +111,11 @@ describe('ol.renderer.canvas.Map', function() { map.addLayer(layer); map.renderSync(); var cb = sinon.spy(); - map.forEachFeatureAtPixel(map.getPixelFromCoordinate([0, 0]), cb, null, - function() { + map.forEachFeatureAtPixel(map.getPixelFromCoordinate([0, 0]), { + layerFilter: function() { return false; - }); + } + }, cb); expect(cb).to.not.be.called(); });