From f6cb4c296de1743b1f226f6f1c815b782f1f9ca0 Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 16 Dec 2021 00:11:15 +0100 Subject: [PATCH 1/2] Populate view's object properties --- src/ol/View.js | 27 +++++++++++---------------- test/browser/spec/ol/View.test.js | 17 +++++++++++++++-- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/ol/View.js b/src/ol/View.js index 0faf434d73..72f099edd0 100644 --- a/src/ol/View.js +++ b/src/ol/View.js @@ -416,10 +416,11 @@ class View extends BaseObject { * @param {ViewOptions} options View options. */ applyOptions_(options) { - /** - * @type {Object} - */ - const properties = {}; + const properties = assign({}, options); + for (const key in ViewProperty) { + delete properties[key]; + } + this.setProperties(properties, true); const resolutionConstraintInfo = createResolutionConstraint(options); @@ -479,17 +480,10 @@ class View extends BaseObject { ); if (options.resolution !== undefined) { this.setResolution(options.resolution); - } else if (options.zoom !== undefined) { + } + if (options.zoom !== undefined) { this.setZoom(options.zoom); } - - this.setProperties(properties); - - /** - * @private - * @type {ViewOptions} - */ - this.options_ = options; } /** @@ -531,12 +525,13 @@ class View extends BaseObject { * @return {ViewOptions} New options updated with the current view state. */ getUpdatedOptions_(newOptions) { - const options = assign({}, this.options_); + const options = this.getProperties(); // preserve resolution (or zoom) if (options.resolution !== undefined) { options.resolution = this.getResolution(); - } else { + } + if (options.zoom !== undefined) { options.zoom = this.getZoom(); } @@ -973,7 +968,7 @@ class View extends BaseObject { * @return {boolean} Resolution constraint is set */ getConstrainResolution() { - return this.options_.constrainResolution; + return this.get('constrainResolution'); } /** diff --git a/test/browser/spec/ol/View.test.js b/test/browser/spec/ol/View.test.js index 7246d8b85d..489217bf1b 100644 --- a/test/browser/spec/ol/View.test.js +++ b/test/browser/spec/ol/View.test.js @@ -617,6 +617,19 @@ describe('ol/View', function () { expect(options.zoom).to.eql(10); }); + it('returns the current properties with getProperties()', function () { + const view = new View({ + center: [0, 0], + minZoom: 2, + zoom: 10, + }); + const options = view.getProperties(); + + expect(options.center).to.eql([0, 0]); + expect(options.minZoom).to.eql(2); + expect(options.zoom).to.eql(10); + }); + it('applies the current zoom', function () { const view = new View({ center: [0, 0], @@ -1874,8 +1887,8 @@ describe('ol/View', function () { expect(view.getCenter()[1]).to.be(1500); }); it('fits correctly to the extent when a view extent is configured', function () { - view.options_.extent = [1500, 0, 2500, 10000]; - view.applyOptions_(view.options_); + view.set('extent', [1500, 0, 2500, 10000]); + view.applyOptions_(view.getProperties()); view.fit([1000, 1000, 2000, 2000]); expect(view.calculateExtent()).eql([1500, 1000, 2500, 2000]); }); From 628e5c1c90924009cebfbde46d8eae6e5819eb0c Mon Sep 17 00:00:00 2001 From: Andreas Hocevar Date: Thu, 16 Dec 2021 09:54:06 +0100 Subject: [PATCH 2/2] Also update zoom property --- src/ol/View.js | 7 +++---- test/browser/spec/ol/View.test.js | 10 +++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ol/View.js b/src/ol/View.js index 72f099edd0..c09bf01cd3 100644 --- a/src/ol/View.js +++ b/src/ol/View.js @@ -480,8 +480,7 @@ class View extends BaseObject { ); if (options.resolution !== undefined) { this.setResolution(options.resolution); - } - if (options.zoom !== undefined) { + } else if (options.zoom !== undefined) { this.setZoom(options.zoom); } } @@ -530,8 +529,7 @@ class View extends BaseObject { // preserve resolution (or zoom) if (options.resolution !== undefined) { options.resolution = this.getResolution(); - } - if (options.zoom !== undefined) { + } else { options.zoom = this.getZoom(); } @@ -1731,6 +1729,7 @@ class View extends BaseObject { } if (this.get(ViewProperty.RESOLUTION) !== newResolution) { this.set(ViewProperty.RESOLUTION, newResolution); + this.set('zoom', this.getZoom(), true); } if ( !newCenter || diff --git a/test/browser/spec/ol/View.test.js b/test/browser/spec/ol/View.test.js index 489217bf1b..1bb95219ca 100644 --- a/test/browser/spec/ol/View.test.js +++ b/test/browser/spec/ol/View.test.js @@ -623,11 +623,15 @@ describe('ol/View', function () { minZoom: 2, zoom: 10, }); - const options = view.getProperties(); + view.setZoom(8); + view.setCenter([1, 2]); + view.setRotation(1); - expect(options.center).to.eql([0, 0]); + const options = view.getProperties(); expect(options.minZoom).to.eql(2); - expect(options.zoom).to.eql(10); + expect(options.zoom).to.eql(8); + expect(options.center).to.eql([1, 2]); + expect(options.rotation).to.eql(1); }); it('applies the current zoom', function () {