From 79312c2083fbd556e96ae519b3e0dd93bd33e4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 13 Jan 2015 20:51:08 +0100 Subject: [PATCH] Add ol.control.Control#setTarget This commit adds a setTarget method to ol.control.Control. This function can be used in cases where the target cannot be set at control construction time. For example, with Angular, it makes sense to create the control instance in an Angular controller, and have a "control" directive insert the control into the DOM. --- src/ol/control/control.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ol/control/control.js b/src/ol/control/control.js index 73ce0e112e..ead9a74b8e 100644 --- a/src/ol/control/control.js +++ b/src/ol/control/control.js @@ -51,8 +51,7 @@ ol.control.Control = function(options) { * @private * @type {Element} */ - this.target_ = goog.isDef(options.target) ? - goog.dom.getElement(options.target) : null; + this.target_ = null; /** * @private @@ -71,6 +70,10 @@ ol.control.Control = function(options) { */ this.render = goog.isDef(options.render) ? options.render : goog.nullFunction; + if (goog.isDef(options.target)) { + this.setTarget(options.target); + } + }; goog.inherits(ol.control.Control, ol.Object); @@ -121,3 +124,17 @@ ol.control.Control.prototype.setMap = function(map) { map.render(); } }; + + +/** + * This function is used to set a target element for the control. It has no + * effect if it is called after the control has been added to the map (i.e. + * after `setMap` is called on the control). If no `target` is set in the + * options passed to the control constructor and if `setTarget` is not called + * then the control is added to the map's overlay container. + * @param {Element|string} target Target. + * @api + */ +ol.control.Control.prototype.setTarget = function(target) { + this.target_ = goog.dom.getElement(target); +};