Skip to content

Commit

Permalink
Satisfying linter, jsdoc & compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock committed Dec 8, 2016
1 parent 2493eb2 commit 80e392e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 53 deletions.
43 changes: 43 additions & 0 deletions externs/olx.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,49 @@ olx.MapOptions.prototype.target;
*/
olx.MapOptions.prototype.view;

/**
* The filter function will receive one argument, the
* {@link ol.layer.Layer layer-candidate} and it should return a boolean
* value.
* @typedef {(function(ol.layer.Layer): boolean)}
*/
olx.LayerFilterFunction;

/**
* Object literal with options for the forEachFeatureAtCoordinate methods.
* @typedef {{layerFilter: (olx.LayerFilterFunction|undefined),
* layerFilterThis: (Object|undefined),
* hitTolerance: (number|undefined)}}
*/
olx.ForEachFeatureOptions;


/**
* Layer filter function. 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.
* @type {olx.LayerFilterFunction|undefined}
* @api stable
*/
olx.ForEachFeatureOptions.prototype.layerFilter;


/**
* Value to use as `this` when executing `layerFilter`.
* @type {Object}
* @api stable
*/
olx.ForEachFeatureOptions.prototype.layerFilterThis;


/**
* Value of a radius in whichs area around the given coordinate features are
* called.
* @type {number}
* @api stable
*/
olx.ForEachFeatureOptions.prototype.hitTolerance;


/**
* Object literal with config options for the overlay.
Expand Down
12 changes: 4 additions & 8 deletions src/ol/interaction/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ 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, {
layerFilter: this.layerFilter_
},
map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
/**
* @param {ol.Feature|ol.render.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer.
Expand All @@ -214,7 +212,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
this.addFeatureLayerAssociation_(feature, layer);
return !this.multi_;
}
}, this);
}, this, {layerFilter: this.layerFilter_});
var i;
for (i = features.getLength() - 1; i >= 0; --i) {
var feature = features.item(i);
Expand All @@ -232,9 +230,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
}
} else {
// Modify the currently selected feature(s).
map.forEachFeatureAtPixel(mapBrowserEvent.pixel, {
layerFilter: this.layerFilter_
},
map.forEachFeatureAtPixel(mapBrowserEvent.pixel,
/**
* @param {ol.Feature|ol.render.Feature} feature Feature.
* @param {ol.layer.Layer} layer Layer.
Expand All @@ -253,7 +249,7 @@ ol.interaction.Select.handleEvent = function(mapBrowserEvent) {
}
return !this.multi_;
}
}, this);
}, this, {layerFilter: this.layerFilter_});
var j;
for (j = deselected.length - 1; j >= 0; --j) {
features.remove(deselected[j]);
Expand Down
31 changes: 7 additions & 24 deletions src/ol/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,26 +554,11 @@ 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
Expand All @@ -582,27 +567,25 @@ 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
* @param {S=} opt_this Value to use as this when executing callback.
* @param {olx.ForEachFeatureOptions=} opt_options Optional options.
* @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
* @template S,T
* @api stable
*/
ol.Map.prototype.forEachFeatureAtPixel = function(pixel, opt_options, callback, opt_this) {
if (typeof opt_options !== 'object') {
opt_this = callback;
callback = opt_options;
opt_options = {};
}
ol.Map.prototype.forEachFeatureAtPixel = function(pixel, callback, opt_this, opt_options) {
opt_options = opt_options !== undefined ? opt_options : {};
if (!this.frameState_) {
return;
}
var coordinate = this.getCoordinateFromPixel(pixel);
var hitTolerance = opt_options.hitTolerance || 0;
var thisArg = opt_this !== undefined ? opt_this : null;
var layerFilter = opt_options.layerFilter || ol.functions.TRUE;
var thisArg2 = opt_options.layerFilterThis || null;
return this.renderer_.forEachFeatureAtCoordinate(
coordinate, this.frameState_, opt_options.hitTolerance || 0, callback, thisArg,
coordinate, this.frameState_, hitTolerance, callback, thisArg,
layerFilter, thisArg2);
};

Expand Down
29 changes: 18 additions & 11 deletions src/ol/render/canvas/replaygroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,32 @@ ol.render.canvas.ReplayGroup = function(
ol.inherits(ol.render.canvas.ReplayGroup, ol.render.ReplayGroup);

/**
* This methods creates a circle inside an exactly fitting array. Points inside the circle are marked by true, points
* on the outside are marked with false.
* This method uses the midpoint circle algorithm.
* @param {Number} radius
* @returns {Boolean[][]}
* This methods creates a circle inside a fitting array. Points inside the
* circle are marked by true, points on the outside are marked with false.
* It uses the midpoint circle algorithm.
* @param {number} radius Radius.
* @returns {Array.<Array.<Boolean>>} an array with marked circel points.
* @private
*/
ol.render.canvas.ReplayGroup.createPixelCircle_ = function(radius) {
var arraySize = radius * 2 + 1;
var arr = new Array(arraySize);
for (var i = 0; i < arraySize; i++) {
arr[i] = (new Array(arraySize)).fill(false);
arr[i] = new Array(arraySize);
for (var j = 0; j < arraySize; j++) {
arr[i][j] = false;
}
}

function fillRowToMiddle(x, y) {
var i;
if (x >= radius) {
for (i = radius; i < x; i++) {
arr[i][y] = true
arr[i][y] = true;
}
} else if (x < radius) {
for (i = x + 1; i < radius; i++) {
arr[i][y] = true
arr[i][y] = true;
}
}
}
Expand All @@ -112,8 +115,7 @@ ol.render.canvas.ReplayGroup.createPixelCircle_ = function(radius) {

y++;
error += 1 + 2 * y;
if (2 * (error - x) + 1 > 0)
{
if (2 * (error - x) + 1 > 0) {
x -= 1;
error += 1 - 2 * x;
}
Expand Down Expand Up @@ -175,7 +177,12 @@ ol.render.canvas.ReplayGroup.prototype.forEachFeatureAtCoordinate = function(
ol.extent.buffer(hitExtent, resolution * this.renderBuffer_, hitExtent);
}

var mask = ol.render.canvas.ReplayGroup.createPixelCircle_(hitTolerance);
var mask;
if (hitTolerance === 0) {
mask = [[true]];
} else {
mask = ol.render.canvas.ReplayGroup.createPixelCircle_(hitTolerance);
}

return this.replayHitDetection_(context, transform, rotation,
skippedFeaturesHash,
Expand Down
2 changes: 1 addition & 1 deletion src/ol/renderer/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ ol.renderer.Map.prototype.forEachLayerAtPixel = function(pixel, frameState, call
*/
ol.renderer.Map.prototype.hasFeatureAtCoordinate = function(coordinate, frameState, layerFilter, thisArg) {
var hasFeature = this.forEachFeatureAtCoordinate(
coordinate, frameState, ol.functions.TRUE, this, layerFilter, thisArg);
coordinate, frameState, 0, ol.functions.TRUE, this, layerFilter, thisArg);

return hasFeature !== undefined;
};
Expand Down
4 changes: 2 additions & 2 deletions src/ol/renderer/webgl/imagelayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ ol.renderer.webgl.ImageLayer.prototype.updateProjectionMatrix_ = function(canvas
*/
ol.renderer.webgl.ImageLayer.prototype.hasFeatureAtCoordinate = function(coordinate, frameState) {
var hasFeature = this.forEachFeatureAtCoordinate(
coordinate, frameState, ol.functions.TRUE, this);
coordinate, frameState, 0, ol.functions.TRUE, this);
return hasFeature !== undefined;
};

Expand All @@ -232,7 +232,7 @@ ol.renderer.webgl.ImageLayer.prototype.forEachLayerAtPixel = function(pixel, fra
var coordinate = ol.transform.apply(
frameState.pixelToCoordinateTransform, pixel.slice());
var hasFeature = this.forEachFeatureAtCoordinate(
coordinate, frameState, ol.functions.TRUE, this);
coordinate, frameState, 0, ol.functions.TRUE, this);

if (hasFeature) {
return callback.call(thisArg, this.getLayer(), null);
Expand Down
14 changes: 7 additions & 7 deletions test/spec/ol/renderer/canvas/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ describe('ol.renderer.canvas.Map', function() {
map.addLayer(layer);
map.renderSync();
var cb = sinon.spy();
map.forEachFeatureAtPixel(map.getPixelFromCoordinate([0, 0]), {
layerFilter: function() {
return false;
}
}, cb);
map.forEachFeatureAtPixel(map.getPixelFromCoordinate([0, 0]), cb, null, {
layerFilter: function() {
return false;
}
});
expect(cb).to.not.be.called();
});

Expand Down Expand Up @@ -151,13 +151,13 @@ describe('ol.renderer.canvas.Map', function() {
];

for (var i = 0; i < 4; i++) {
map.forEachFeatureAtPixel(pixelsInside[i], {hitTolerance:10}, cb1);
map.forEachFeatureAtPixel(pixelsInside[i], cb1, null, {hitTolerance:10});
}
expect(cb1.callCount).to.be(4);
expect(cb1.firstCall.args[1]).to.be(layer);

for (var j = 0; j < 4; j++) {
map.forEachFeatureAtPixel(pixelsOutside[j], {hitTolerance:10}, cb2);
map.forEachFeatureAtPixel(pixelsOutside[j], cb2, null, {hitTolerance:10});
}
expect(cb2).not.to.be.called();
});
Expand Down

0 comments on commit 80e392e

Please sign in to comment.