Merge pull request #971 from twpayne/set-target

Cleaner ol.Map#setTarget handling
This commit is contained in:
Tom Payne
2013-09-04 02:32:02 -07:00
3 changed files with 28 additions and 18 deletions
+1 -1
View File
@@ -23,6 +23,6 @@ map.setTarget('map1');
var teleportButton = document.getElementById('teleport'); var teleportButton = document.getElementById('teleport');
teleportButton.addEventListener('click', function() { teleportButton.addEventListener('click', function() {
var target = map.getTarget().id === 'map1' ? 'map2' : 'map1'; var target = map.getTarget() === 'map1' ? 'map2' : 'map1';
map.setTarget(target); map.setTarget(target);
}, false); }, false);
+3 -1
View File
@@ -80,7 +80,9 @@ ol.control.FullScreen.prototype.handleClick_ = function(browserEvent) {
if (goog.dom.fullscreen.isFullScreen()) { if (goog.dom.fullscreen.isFullScreen()) {
goog.dom.fullscreen.exitFullScreen(); goog.dom.fullscreen.exitFullScreen();
} else { } 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)); goog.asserts.assert(goog.isDefAndNotNull(element));
if (this.keys_) { if (this.keys_) {
goog.dom.fullscreen.requestFullScreenWithKeys(element); goog.dom.fullscreen.requestFullScreenWithKeys(element);
+24 -16
View File
@@ -220,6 +220,12 @@ ol.Map = function(options) {
this.viewport_.className = 'ol-touch'; this.viewport_.className = 'ol-touch';
} }
/**
* @private
* @type {Element}
*/
this.target_ = null;
/** /**
* @private * @private
* @type {Element} * @type {Element}
@@ -438,10 +444,11 @@ ol.Map.prototype.getRenderer = function() {
/** /**
* Get the element in which this map is rendered. * Get the element in which this map is rendered.
* @return {Element|undefined} Target. * @return {Element|string|undefined} Target.
*/ */
ol.Map.prototype.getTarget = function() { 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( goog.exportProperty(
ol.Map.prototype, ol.Map.prototype,
@@ -730,15 +737,20 @@ ol.Map.prototype.handleSizeChanged_ = function() {
* @private * @private
*/ */
ol.Map.prototype.handleTargetChanged_ = function() { ol.Map.prototype.handleTargetChanged_ = function() {
// target may be undefined, null or an Element. If it's not // target may be undefined, null, a string or an Element.
// an Element we remove the viewport from the DOM. If it's // If it's a string we convert it to an Element before proceeding.
// an Element we append the viewport element to it. // 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(); 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_); goog.dom.removeNode(this.viewport_);
} else { } else {
goog.asserts.assert(goog.isDefAndNotNull(target)); goog.dom.appendChild(this.target_, this.viewport_);
goog.dom.appendChild(target, this.viewport_);
} }
this.updateSize(); this.updateSize();
// updateSize calls setSize, so no need to call this.render // updateSize calls setSize, so no need to call this.render
@@ -1012,9 +1024,6 @@ goog.exportProperty(
* @param {Element|string|undefined} target Target. * @param {Element|string|undefined} target Target.
*/ */
ol.Map.prototype.setTarget = function(target) { ol.Map.prototype.setTarget = function(target) {
if (goog.isDef(target)) {
target = goog.dom.getElement(target);
}
this.set(ol.MapProperty.TARGET, target); this.set(ol.MapProperty.TARGET, target);
}; };
goog.exportProperty( goog.exportProperty(
@@ -1052,12 +1061,11 @@ ol.Map.prototype.unfreezeRendering = function() {
* third-party code changes the size of the map viewport. * third-party code changes the size of the map viewport.
*/ */
ol.Map.prototype.updateSize = function() { ol.Map.prototype.updateSize = function() {
var target = this.getTarget(); if (goog.isNull(this.target_)) {
if (goog.isDef(target)) {
var size = goog.style.getSize(target);
this.setSize([size.width, size.height]);
} else {
this.setSize(undefined); this.setSize(undefined);
} else {
var size = goog.style.getSize(this.target_);
this.setSize([size.width, size.height]);
} }
}; };