Make size argument of ol.View#calculateExtent() optional

This commit is contained in:
Andreas Hocevar
2016-12-20 21:17:08 +01:00
parent d342748563
commit 3f4f38b835
2 changed files with 43 additions and 10 deletions

View File

@@ -396,6 +396,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. * Get the constrained center of this view.
* @param {ol.Coordinate|undefined} center Center. * @param {ol.Coordinate|undefined} center Center.
@@ -468,11 +485,13 @@ ol.View.prototype.getHints = function(opt_hints) {
* The size is the pixel dimensions of the box into which the calculated extent * 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, * should fit. In most cases you want to get the extent of the entire map,
* that is `map.getSize()`. * 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. * @return {ol.Extent} Extent.
* @api stable * @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()); var center = /** @type {!ol.Coordinate} */ (this.getCenter());
ol.asserts.assert(center, 1); // The view center is not defined ol.asserts.assert(center, 1); // The view center is not defined
var resolution = /** @type {!number} */ (this.getResolution()); var resolution = /** @type {!number} */ (this.getResolution());
@@ -679,14 +698,7 @@ ol.View.prototype.fit = function(geometryOrExtent, opt_options) {
var options = opt_options || {}; var options = opt_options || {};
var size = options.size; var size = options.size;
if (!size) { if (!size) {
size = [100, 100]; size = this.getSizeFromViewport_();
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);
}
} }
/** @type {ol.geom.SimpleGeometry} */ /** @type {ol.geom.SimpleGeometry} */
var geometry; var geometry;

View File

@@ -882,6 +882,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() { describe('fit', function() {
var originalRequestAnimationFrame = window.requestAnimationFrame; var originalRequestAnimationFrame = window.requestAnimationFrame;