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

View File

@@ -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.<?number>}
@@ -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;