Defer ZoomSlider initialization until its size is available

This commit is contained in:
Maximilian Krög
2020-05-26 09:08:39 +02:00
parent 4d014be3c1
commit c4c790a2be
+12 -10
View File
@@ -179,14 +179,16 @@ class ZoomSlider extends Control {
* direction_ and also constrain the dragging of the thumb to always be within * direction_ and also constrain the dragging of the thumb to always be within
* the bounds of the container. * the bounds of the container.
* *
* @return {boolean} Initialization successful
* @private * @private
*/ */
initSlider_() { initSlider_() {
const container = this.element; const container = this.element;
const containerSize = { const containerWidth = container.offsetWidth;
width: container.offsetWidth, const containerHeight = container.offsetHeight;
height: container.offsetHeight, if (containerWidth === 0 && containerHeight === 0) {
}; return (this.sliderInitialized_ = false);
}
const thumb = /** @type {HTMLElement} */ (container.firstElementChild); const thumb = /** @type {HTMLElement} */ (container.firstElementChild);
const computedStyle = getComputedStyle(thumb); const computedStyle = getComputedStyle(thumb);
@@ -200,14 +202,14 @@ class ZoomSlider extends Control {
parseFloat(computedStyle['marginBottom']); parseFloat(computedStyle['marginBottom']);
this.thumbSize_ = [thumbWidth, thumbHeight]; this.thumbSize_ = [thumbWidth, thumbHeight];
if (containerSize.width > containerSize.height) { if (containerWidth > containerHeight) {
this.direction_ = Direction.HORIZONTAL; this.direction_ = Direction.HORIZONTAL;
this.widthLimit_ = containerSize.width - thumbWidth; this.widthLimit_ = containerWidth - thumbWidth;
} else { } else {
this.direction_ = Direction.VERTICAL; this.direction_ = Direction.VERTICAL;
this.heightLimit_ = containerSize.height - thumbHeight; this.heightLimit_ = containerHeight - thumbHeight;
} }
this.sliderInitialized_ = true; return (this.sliderInitialized_ = true);
} }
/** /**
@@ -366,8 +368,8 @@ class ZoomSlider extends Control {
if (!mapEvent.frameState) { if (!mapEvent.frameState) {
return; return;
} }
if (!this.sliderInitialized_) { if (!this.sliderInitialized_ && !this.initSlider_()) {
this.initSlider_(); return;
} }
const res = mapEvent.frameState.viewState.resolution; const res = mapEvent.frameState.viewState.resolution;
this.currentResolution_ = res; this.currentResolution_ = res;