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.
This commit is contained in:
Éric Lemoine
2013-07-05 22:37:31 +02:00
parent a071c3521c
commit ccf6aa10ac
2 changed files with 14 additions and 5 deletions
+13 -5
View File
@@ -62,6 +62,13 @@ ol.control.ZoomSlider = function(opt_options) {
*/ */
this.direction_ = ol.control.ZoomSlider.direction.VERTICAL; this.direction_ = ol.control.ZoomSlider.direction.VERTICAL;
/**
* Whether the slider is initialized.
* @type {boolean}
* @private
*/
this.sliderInitialized_ = false;
/** /**
* @private * @private
* @type {Array.<?number>} * @type {Array.<?number>}
@@ -107,11 +114,8 @@ ol.control.ZoomSlider.direction = {
*/ */
ol.control.ZoomSlider.prototype.setMap = function(map) { ol.control.ZoomSlider.prototype.setMap = function(map) {
goog.base(this, 'setMap', map); goog.base(this, 'setMap', map);
this.initSlider_(); if (!goog.isNull(map)) {
var resolution = map.getView().getView2D().getResolution(); map.render();
if (goog.isDef(resolution)) {
this.currentResolution_ = resolution;
this.positionThumbForResolution_(resolution);
} }
}; };
@@ -147,6 +151,7 @@ ol.control.ZoomSlider.prototype.initSlider_ = function() {
limits = new goog.math.Rect(0, 0, 0, h); limits = new goog.math.Rect(0, 0, 0, h);
} }
this.dragger_.setLimits(limits); this.dragger_.setLimits(limits);
this.sliderInitialized_ = true;
}; };
@@ -154,6 +159,9 @@ ol.control.ZoomSlider.prototype.initSlider_ = function() {
* @inheritDoc * @inheritDoc
*/ */
ol.control.ZoomSlider.prototype.handleMapPostrender = function(mapEvent) { ol.control.ZoomSlider.prototype.handleMapPostrender = function(mapEvent) {
if (!this.sliderInitialized_) {
this.initSlider_();
}
var res = mapEvent.frameState.view2DState.resolution; var res = mapEvent.frameState.view2DState.resolution;
if (res !== this.currentResolution_) { if (res !== this.currentResolution_) {
this.currentResolution_ = res; this.currentResolution_ = res;
@@ -68,6 +68,7 @@ describe('ol.control.ZoomSlider', function() {
control.element.style.width = '1000px'; control.element.style.width = '1000px';
control.element.style.height = '10px'; control.element.style.height = '10px';
control.setMap(map); control.setMap(map);
control.initSlider_();
var horizontal = ol.control.ZoomSlider.direction.HORIZONTAL; var horizontal = ol.control.ZoomSlider.direction.HORIZONTAL;
expect(control.direction_).to.be(horizontal); expect(control.direction_).to.be(horizontal);