From 53223be376868d0a09c1e1b3c8126e5618b90dfa Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 3 Sep 2013 15:18:09 +0200 Subject: [PATCH 1/3] Perform ol.Map#setTarget string-to-Element conversion in event handler With this change, ol.Map#setTarget no longer changes the type of the value passed to it. --- src/ol/map.js | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/src/ol/map.js b/src/ol/map.js index 6cfa055824..2fe04f6f62 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -220,6 +220,12 @@ ol.Map = function(options) { this.viewport_.className = 'ol-touch'; } + /** + * @private + * @type {Element} + */ + this.target_ = null; + /** * @private * @type {Element} @@ -451,10 +457,11 @@ ol.Map.prototype.getRenderer = function() { /** * Get the element in which this map is rendered. - * @return {Element|undefined} Target. + * @return {Element|string|undefined} Target. */ ol.Map.prototype.getTarget = function() { - return /** @type {Element|undefined} */ (this.get(ol.MapProperty.TARGET)); + return /** @type {Element|string|undefined} */ ( + this.get(ol.MapProperty.TARGET)); }; goog.exportProperty( ol.Map.prototype, @@ -743,15 +750,20 @@ ol.Map.prototype.handleSizeChanged_ = function() { * @private */ ol.Map.prototype.handleTargetChanged_ = function() { - // target may be undefined, null or an Element. If it's not - // an Element we remove the viewport from the DOM. If it's - // an Element we append the viewport element to it. + // target may be undefined, null, a string or an Element. + // If it's a string we convert it to an Element before proceeding. + // If it's not now an Element we remove the viewport from the DOM. + // If it's an Element we append the viewport element to it. var target = this.getTarget(); - if (!goog.dom.isElement(target)) { + if (goog.isDef(target)) { + this.target_ = goog.dom.getElement(target); + } else { + this.target_ = null; + } + if (goog.isNull(this.target_)) { goog.dom.removeNode(this.viewport_); } else { - goog.asserts.assert(goog.isDefAndNotNull(target)); - goog.dom.appendChild(target, this.viewport_); + goog.dom.appendChild(this.target_, this.viewport_); } this.updateSize(); // updateSize calls setSize, so no need to call this.render @@ -1025,9 +1037,6 @@ goog.exportProperty( * @param {Element|string|undefined} target Target. */ ol.Map.prototype.setTarget = function(target) { - if (goog.isDef(target)) { - target = goog.dom.getElement(target); - } this.set(ol.MapProperty.TARGET, target); }; goog.exportProperty( @@ -1065,12 +1074,11 @@ ol.Map.prototype.unfreezeRendering = function() { * third-party code changes the size of the map viewport. */ ol.Map.prototype.updateSize = function() { - var target = this.getTarget(); - if (goog.isDef(target)) { - var size = goog.style.getSize(target); - this.setSize([size.width, size.height]); - } else { + if (goog.isNull(this.target_)) { this.setSize(undefined); + } else { + var size = goog.style.getSize(this.target_); + this.setSize([size.width, size.height]); } }; From 83a97242707cfae041775b392a13d1faf782d69e Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 3 Sep 2013 15:20:53 +0200 Subject: [PATCH 2/3] Update ol.control.FullScreen to handle string targets --- src/ol/control/fullscreencontrol.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ol/control/fullscreencontrol.js b/src/ol/control/fullscreencontrol.js index 60f7fee4e6..45c2f0b7c8 100644 --- a/src/ol/control/fullscreencontrol.js +++ b/src/ol/control/fullscreencontrol.js @@ -80,7 +80,9 @@ ol.control.FullScreen.prototype.handleClick_ = function(browserEvent) { if (goog.dom.fullscreen.isFullScreen()) { goog.dom.fullscreen.exitFullScreen(); } else { - var element = map.getTarget(); + var target = map.getTarget(); + goog.asserts.assert(goog.isDefAndNotNull(target)); + var element = goog.dom.getElement(target); goog.asserts.assert(goog.isDefAndNotNull(element)); if (this.keys_) { goog.dom.fullscreen.requestFullScreenWithKeys(element); From aafd7e91cc69f48bfecc7e5e840c6076615a5d76 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 3 Sep 2013 15:21:09 +0200 Subject: [PATCH 3/3] Update teleport example to use string target --- examples/teleport.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/teleport.js b/examples/teleport.js index a83e50308b..c6b942bd12 100644 --- a/examples/teleport.js +++ b/examples/teleport.js @@ -23,6 +23,6 @@ map.setTarget('map1'); var teleportButton = document.getElementById('teleport'); teleportButton.addEventListener('click', function() { - var target = map.getTarget().id === 'map1' ? 'map2' : 'map1'; + var target = map.getTarget() === 'map1' ? 'map2' : 'map1'; map.setTarget(target); }, false);