From ccf6aa10acc6eb5a09c13822f35b5a99f12a4f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Fri, 5 Jul 2013 22:37:31 +0200 Subject: [PATCH 1/2] Zoomslider cannot be added to a map with no target The zoom slider control's initSlider_ function requires that the control's element is inserted in the document. So if initSlider_ is called before the map have a target then the slider isn't correctly initialized. This commit fixes that by defering the slider initialization until the first handleMapPostrender call. --- src/ol/control/zoomslidercontrol.js | 18 +++++++++++++----- test/spec/ol/control/zoomslidercontrol.test.js | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ol/control/zoomslidercontrol.js b/src/ol/control/zoomslidercontrol.js index e324eab6a6..a9248c41c0 100644 --- a/src/ol/control/zoomslidercontrol.js +++ b/src/ol/control/zoomslidercontrol.js @@ -62,6 +62,13 @@ ol.control.ZoomSlider = function(opt_options) { */ this.direction_ = ol.control.ZoomSlider.direction.VERTICAL; + /** + * Whether the slider is initialized. + * @type {boolean} + * @private + */ + this.sliderInitialized_ = false; + /** * @private * @type {Array.} @@ -107,11 +114,8 @@ ol.control.ZoomSlider.direction = { */ ol.control.ZoomSlider.prototype.setMap = function(map) { goog.base(this, 'setMap', map); - this.initSlider_(); - var resolution = map.getView().getView2D().getResolution(); - if (goog.isDef(resolution)) { - this.currentResolution_ = resolution; - this.positionThumbForResolution_(resolution); + if (!goog.isNull(map)) { + map.render(); } }; @@ -147,6 +151,7 @@ ol.control.ZoomSlider.prototype.initSlider_ = function() { limits = new goog.math.Rect(0, 0, 0, h); } this.dragger_.setLimits(limits); + this.sliderInitialized_ = true; }; @@ -154,6 +159,9 @@ ol.control.ZoomSlider.prototype.initSlider_ = function() { * @inheritDoc */ ol.control.ZoomSlider.prototype.handleMapPostrender = function(mapEvent) { + if (!this.sliderInitialized_) { + this.initSlider_(); + } var res = mapEvent.frameState.view2DState.resolution; if (res !== this.currentResolution_) { this.currentResolution_ = res; diff --git a/test/spec/ol/control/zoomslidercontrol.test.js b/test/spec/ol/control/zoomslidercontrol.test.js index 893d735482..a7265d1759 100644 --- a/test/spec/ol/control/zoomslidercontrol.test.js +++ b/test/spec/ol/control/zoomslidercontrol.test.js @@ -68,6 +68,7 @@ describe('ol.control.ZoomSlider', function() { control.element.style.width = '1000px'; control.element.style.height = '10px'; control.setMap(map); + control.initSlider_(); var horizontal = ol.control.ZoomSlider.direction.HORIZONTAL; expect(control.direction_).to.be(horizontal); From 44a9c181aa4a756989198cb01c7f81a11134acc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 9 Jul 2013 10:06:01 +0200 Subject: [PATCH 2/2] handleMapPostrender may get a null frameState --- src/ol/control/zoomslidercontrol.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ol/control/zoomslidercontrol.js b/src/ol/control/zoomslidercontrol.js index a9248c41c0..f8d5c55bbf 100644 --- a/src/ol/control/zoomslidercontrol.js +++ b/src/ol/control/zoomslidercontrol.js @@ -159,6 +159,11 @@ ol.control.ZoomSlider.prototype.initSlider_ = function() { * @inheritDoc */ ol.control.ZoomSlider.prototype.handleMapPostrender = function(mapEvent) { + if (goog.isNull(mapEvent.frameState)) { + return; + } + goog.asserts.assert( + goog.isDefAndNotNull(mapEvent.frameState.view2DState)); if (!this.sliderInitialized_) { this.initSlider_(); }