Simplified api

This commit is contained in:
simonseyock
2016-12-07 11:14:59 +01:00
parent 8edc2be103
commit 55ec0af072
6 changed files with 18 additions and 33 deletions
+5 -5
View File
@@ -15,17 +15,17 @@ Old syntax:
New syntax: New syntax:
``` ```
map.forEachFeatureAtPixel(pixel, callback, callbackThis, { map.forEachFeatureAtPixel(pixel, callback, {
layerFilter: layerFilterFn, layerFilter: layerFilterFn
layerFilterThis: layerFilterThis
}); });
map.hasFeatureAtPixel(pixel, { map.hasFeatureAtPixel(pixel, {
layerFilter: layerFilterFn, layerFilter: layerFilterFn
layerFilterThis: layerFilterThis
}); });
``` ```
To bind a function to a this, please use the bind method of the function (See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind).
This change is due to the introduction of the `hitTolerance` parameter which can be passed in via this `ol.AtPixelOptions` object, too. This change is due to the introduction of the `hitTolerance` parameter which can be passed in via this `ol.AtPixelOptions` object, too.
#### Use `view.animate()` instead of `map.beforeRender()` and `ol.animation` functions #### Use `view.animate()` instead of `map.beforeRender()` and `ol.animation` functions
+1 -10
View File
@@ -306,7 +306,6 @@ olx.MapOptions.prototype.view;
* Object literal with options for the {@link ol.Map#forEachFeatureAtPixel} and * Object literal with options for the {@link ol.Map#forEachFeatureAtPixel} and
* {@link ol.Map#hasFeatureAtPixel} methods. * {@link ol.Map#hasFeatureAtPixel} methods.
* @typedef {{layerFilter: ((function(ol.layer.Layer): boolean)|undefined), * @typedef {{layerFilter: ((function(ol.layer.Layer): boolean)|undefined),
* layerFilterThis: (Object|undefined),
* hitTolerance: (number|undefined)}} * hitTolerance: (number|undefined)}}
*/ */
olx.AtPixelOptions; olx.AtPixelOptions;
@@ -323,14 +322,6 @@ olx.AtPixelOptions;
olx.AtPixelOptions.prototype.layerFilter; olx.AtPixelOptions.prototype.layerFilter;
/**
* Value to use as `this` when executing `layerFilter`.
* @type {Object|undefined}
* @api stable
*/
olx.AtPixelOptions.prototype.layerFilterThis;
/** /**
* Hit-detection tolerance. Pixels inside the radius around the given position * Hit-detection tolerance. Pixels inside the radius around the given position
* will be checked for features. This only works for the canvas renderer and * will be checked for features. This only works for the canvas renderer and
@@ -7684,7 +7675,7 @@ olx.view.FitOptions.prototype.maxZoom;
/** /**
* The duration of the animation in milliseconds. By default, there is no * The duration of the animation in milliseconds. By default, there is no
* animations. * animations.
* @type {number|undefined} * @type {number|undefined}
* @api * @api
+4 -4
View File
@@ -217,7 +217,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
// the pixel. // the pixel.
ol.obj.clear(this.featureLayerAssociation_); ol.obj.clear(this.featureLayerAssociation_);
map.forEachFeatureAtPixel(mapBrowserEvent.pixel, map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
/** (/**
* @param {ol.Feature|ol.render.Feature} feature Feature. * @param {ol.Feature|ol.render.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer. * @param {ol.layer.Layer} layer Layer.
* @return {boolean|undefined} Continue to iterate over the features. * @return {boolean|undefined} Continue to iterate over the features.
@@ -228,7 +228,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
this.addFeatureLayerAssociation_(feature, layer); this.addFeatureLayerAssociation_(feature, layer);
return !this.multi_; return !this.multi_;
} }
}, this, { }).bind(this), {
layerFilter: this.layerFilter_, layerFilter: this.layerFilter_,
hitTolerance: this.hitTolerance_ hitTolerance: this.hitTolerance_
}); });
@@ -250,7 +250,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
} else { } else {
// Modify the currently selected feature(s). // Modify the currently selected feature(s).
map.forEachFeatureAtPixel(mapBrowserEvent.pixel, map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
/** (/**
* @param {ol.Feature|ol.render.Feature} feature Feature. * @param {ol.Feature|ol.render.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer. * @param {ol.layer.Layer} layer Layer.
* @return {boolean|undefined} Continue to iterate over the features. * @return {boolean|undefined} Continue to iterate over the features.
@@ -268,7 +268,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
} }
return !this.multi_; return !this.multi_;
} }
}, this, { }).bind(this), {
layerFilter: this.layerFilter_, layerFilter: this.layerFilter_,
hitTolerance: this.hitTolerance_ hitTolerance: this.hitTolerance_
}); });
+1 -1
View File
@@ -203,7 +203,7 @@ ol.interaction.Translate.prototype.featuresAtPixel_ = function(pixel, map) {
ol.array.includes(this.features_.getArray(), feature)) { ol.array.includes(this.features_.getArray(), feature)) {
return feature; return feature;
} }
}, this, { }.bind(this), {
layerFilter: this.layerFilter_, layerFilter: this.layerFilter_,
hitTolerance: this.hitTolerance_ hitTolerance: this.hitTolerance_
}); });
+4 -10
View File
@@ -567,14 +567,13 @@ ol.Map.prototype.disposeInternal = function() {
* the {@link ol.layer.Layer layer} of the feature and will be null for * the {@link ol.layer.Layer layer} of the feature and will be null for
* unmanaged layers. To stop detection, callback functions can return a * unmanaged layers. To stop detection, callback functions can return a
* truthy value. * truthy value.
* @param {S=} opt_this Value to use as this when executing callback.
* @param {olx.AtPixelOptions=} opt_options Optional options. * @param {olx.AtPixelOptions=} opt_options Optional options.
* @return {T|undefined} Callback result, i.e. the return value of last * @return {T|undefined} Callback result, i.e. the return value of last
* callback execution, or the first truthy callback return value. * callback execution, or the first truthy callback return value.
* @template S,T * @template S,T
* @api stable * @api stable
*/ */
ol.Map.prototype.forEachFeatureAtPixel = function(pixel, callback, opt_this, opt_options) { ol.Map.prototype.forEachFeatureAtPixel = function(pixel, callback, opt_options) {
if (!this.frameState_) { if (!this.frameState_) {
return; return;
} }
@@ -582,14 +581,11 @@ ol.Map.prototype.forEachFeatureAtPixel = function(pixel, callback, opt_this, opt
opt_options = opt_options !== undefined ? opt_options : {}; opt_options = opt_options !== undefined ? opt_options : {};
var hitTolerance = opt_options.hitTolerance !== undefined ? var hitTolerance = opt_options.hitTolerance !== undefined ?
opt_options.hitTolerance * this.frameState_.pixelRatio : 0; opt_options.hitTolerance * this.frameState_.pixelRatio : 0;
var thisArg = opt_this !== undefined ? opt_this : null;
var layerFilter = opt_options.layerFilter !== undefined ? var layerFilter = opt_options.layerFilter !== undefined ?
opt_options.layerFilter : ol.functions.TRUE; opt_options.layerFilter : ol.functions.TRUE;
var thisArg2 = opt_options.layerFilterThis !== undefined ?
opt_options.layerFilterThis : null;
return this.renderer_.forEachFeatureAtCoordinate( return this.renderer_.forEachFeatureAtCoordinate(
coordinate, this.frameState_, hitTolerance, callback, thisArg, coordinate, this.frameState_, hitTolerance, callback, null,
layerFilter, thisArg2); layerFilter, null);
}; };
@@ -648,12 +644,10 @@ ol.Map.prototype.hasFeatureAtPixel = function(pixel, opt_options) {
opt_options = opt_options !== undefined ? opt_options : {}; opt_options = opt_options !== undefined ? opt_options : {};
var layerFilter = opt_options.layerFilter !== undefined ? var layerFilter = opt_options.layerFilter !== undefined ?
opt_options.layerFilter : ol.functions.TRUE; opt_options.layerFilter : ol.functions.TRUE;
var thisArg = opt_options.layerFilterThis !== undefined ?
opt_options.layerFilterThis : null;
var hitTolerance = opt_options.hitTolerance !== undefined ? var hitTolerance = opt_options.hitTolerance !== undefined ?
opt_options.hitTolerance * this.frameState_.pixelRatio : 0; opt_options.hitTolerance * this.frameState_.pixelRatio : 0;
return this.renderer_.hasFeatureAtCoordinate( return this.renderer_.hasFeatureAtCoordinate(
coordinate, this.frameState_, hitTolerance, layerFilter, thisArg); coordinate, this.frameState_, hitTolerance, layerFilter, null);
}; };
+3 -3
View File
@@ -111,7 +111,7 @@ describe('ol.renderer.canvas.Map', function() {
map.addLayer(layer); map.addLayer(layer);
map.renderSync(); map.renderSync();
var cb = sinon.spy(); var cb = sinon.spy();
map.forEachFeatureAtPixel(map.getPixelFromCoordinate([0, 0]), cb, null, { map.forEachFeatureAtPixel(map.getPixelFromCoordinate([0, 0]), cb, {
layerFilter: function() { layerFilter: function() {
return false; return false;
} }
@@ -151,13 +151,13 @@ describe('ol.renderer.canvas.Map', function() {
]; ];
for (var i = 0; i < 4; i++) { for (var i = 0; i < 4; i++) {
map.forEachFeatureAtPixel(pixelsInside[i], cb1, null, {hitTolerance:10}); map.forEachFeatureAtPixel(pixelsInside[i], cb1, {hitTolerance:10});
} }
expect(cb1.callCount).to.be(4); expect(cb1.callCount).to.be(4);
expect(cb1.firstCall.args[1]).to.be(layer); expect(cb1.firstCall.args[1]).to.be(layer);
for (var j = 0; j < 4; j++) { for (var j = 0; j < 4; j++) {
map.forEachFeatureAtPixel(pixelsOutside[j], cb2, null, {hitTolerance:10}); map.forEachFeatureAtPixel(pixelsOutside[j], cb2, {hitTolerance:10});
} }
expect(cb2).not.to.be.called(); expect(cb2).not.to.be.called();
}); });