Skip to content

Commit

Permalink
Merge pull request #6285 from ahocevar/calculate-extent
Browse files Browse the repository at this point in the history
Make size argument of ol.View#calculateExtent() optional
  • Loading branch information
ahocevar committed Jan 13, 2017
2 parents 2c0d876 + 3f4f38b commit 472ab00
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/ol/view.js
Expand Up @@ -398,6 +398,23 @@ ol.View.prototype.calculateCenterZoom = function(resolution, anchor) {
};


/**
* @private
* @return {ol.Size} Viewport size or `[100, 100]` when no viewport is found.
*/
ol.View.prototype.getSizeFromViewport_ = function() {
var size = [100, 100];
var selector = '.ol-viewport[data-view="' + ol.getUid(this) + '"]';
var element = document.querySelector(selector);
if (element) {
var metrics = getComputedStyle(element);
size[0] = parseInt(metrics.width, 10);
size[1] = parseInt(metrics.height, 10);
}
return size;
};


/**
* Get the constrained center of this view.
* @param {ol.Coordinate|undefined} center Center.
Expand Down Expand Up @@ -470,11 +487,13 @@ ol.View.prototype.getHints = function(opt_hints) {
* The size is the pixel dimensions of the box into which the calculated extent
* should fit. In most cases you want to get the extent of the entire map,
* that is `map.getSize()`.
* @param {ol.Size} size Box pixel size.
* @param {ol.Size=} opt_size Box pixel size. If not provided, the size of the
* first map that uses this view will be used.
* @return {ol.Extent} Extent.
* @api stable
*/
ol.View.prototype.calculateExtent = function(size) {
ol.View.prototype.calculateExtent = function(opt_size) {
var size = opt_size || this.getSizeFromViewport_();
var center = /** @type {!ol.Coordinate} */ (this.getCenter());
ol.asserts.assert(center, 1); // The view center is not defined
var resolution = /** @type {!number} */ (this.getResolution());
Expand Down Expand Up @@ -673,14 +692,7 @@ ol.View.prototype.fit = function(geometryOrExtent, opt_options) {
var options = opt_options || {};
var size = options.size;
if (!size) {
size = [100, 100];
var selector = '.ol-viewport[data-view="' + ol.getUid(this) + '"]';
var element = document.querySelector(selector);
if (element) {
var metrics = getComputedStyle(element);
size[0] = parseInt(metrics.width, 10);
size[1] = parseInt(metrics.height, 10);
}
size = this.getSizeFromViewport_();
}
/** @type {ol.geom.SimpleGeometry} */
var geometry;
Expand Down
21 changes: 21 additions & 0 deletions test/spec/ol/view.test.js
Expand Up @@ -883,6 +883,27 @@ describe('ol.View', function() {
});
});

describe('#getSizeFromViewport_()', function() {
var map, target;
beforeEach(function() {
target = document.createElement('div');
target.style.width = '200px';
target.style.height = '150px';
map = new ol.Map({
target: target
});
document.body.appendChild(target);
});
afterEach(function() {
map.setTarget(null);
document.body.removeChild(target);
});
it('calculates the size correctly', function() {
var size = map.getView().getSizeFromViewport_();
expect(size).to.eql([200, 150]);
});
});

describe('fit', function() {

var originalRequestAnimationFrame = window.requestAnimationFrame;
Expand Down

0 comments on commit 472ab00

Please sign in to comment.