Skip to content

Commit

Permalink
Zoomslider cannot be added to a map with no target
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
elemoine committed Jul 9, 2013
1 parent a071c35 commit ccf6aa1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/ol/control/zoomslidercontrol.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>}
Expand Down Expand Up @@ -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();
}
};

Expand Down Expand Up @@ -147,13 +151,17 @@ ol.control.ZoomSlider.prototype.initSlider_ = function() {
limits = new goog.math.Rect(0, 0, 0, h);
}
this.dragger_.setLimits(limits);
this.sliderInitialized_ = true;
};


/**
* @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;
Expand Down
1 change: 1 addition & 0 deletions test/spec/ol/control/zoomslidercontrol.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ccf6aa1

Please sign in to comment.