From b85520f5046ebeab729c3a935801648303ff1ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 4 Jun 2013 07:08:43 +0200 Subject: [PATCH 1/3] The map now has a collection of controls --- src/objectliterals.jsdoc | 4 +-- src/ol/control/controldefaults.js | 9 +++---- src/ol/map.js | 45 ++++++++++++++++++++++--------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/objectliterals.jsdoc b/src/objectliterals.jsdoc index bc61df1fcb..e2e85cdc7f 100644 --- a/src/objectliterals.jsdoc +++ b/src/objectliterals.jsdoc @@ -43,8 +43,8 @@ /** * Object literal with config options for the map. * @typedef {Object} ol.MapOptions - * @property {Array.|undefined} controls Controls initially - * added to the map. + * @property {ol.Collection|Array.|undefined} controls + * Controls initially added to the map. * @property {ol.Collection|undefined} interactions Interactions. * @property {Array.|ol.Collection|undefined} layers Layers. * @property {ol.RendererHint|undefined} renderer Renderer. diff --git a/src/ol/control/controldefaults.js b/src/ol/control/controldefaults.js index af50c5c524..0d8c818b62 100644 --- a/src/ol/control/controldefaults.js +++ b/src/ol/control/controldefaults.js @@ -1,6 +1,6 @@ goog.provide('ol.control.defaults'); -goog.require('goog.array'); +goog.require('ol.Collection'); goog.require('ol.control.Attribution'); goog.require('ol.control.Logo'); goog.require('ol.control.Zoom'); @@ -9,14 +9,13 @@ goog.require('ol.control.Zoom'); /** * @param {ol.control.DefaultsOptions=} opt_options Defaults options. * @param {Array.=} opt_controls Additional controls. - * @return {Array.} Controls. + * @return {ol.Collection} Controls. */ ol.control.defaults = function(opt_options, opt_controls) { var options = goog.isDef(opt_options) ? opt_options : {}; - /** @type {Array.} */ - var controls = []; + var controls = new ol.Collection(); var attributionControl = goog.isDef(options.attribution) ? options.attribution : true; @@ -43,7 +42,7 @@ ol.control.defaults = function(opt_options, opt_controls) { } if (goog.isDef(opt_controls)) { - goog.array.extend(controls, opt_controls); + controls.extend(opt_controls); } return controls; diff --git a/src/ol/map.js b/src/ol/map.js index c1864078e5..e929730ecc 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -247,6 +247,12 @@ ol.Map = function(options) { this.handleBrowserEvent, false, this); this.registerDisposable(mouseWheelHandler); + /** + * @type {ol.Collection} + * @private + */ + this.controls_ = optionsInternal.controls; + /** * @type {ol.Collection} * @private @@ -320,15 +326,13 @@ ol.Map = function(options) { // is "defined" already. this.setValues(optionsInternal.values); - if (goog.isDef(optionsInternal.controls)) { - goog.array.forEach(optionsInternal.controls, - /** - * @param {ol.control.Control} control Control. - */ - function(control) { - control.setMap(this); - }, this); - } + this.controls_.forEach( + /** + * @param {ol.control.Control} control Control. + */ + function(control) { + control.setMap(this); + }, this); }; goog.inherits(ol.Map, ol.Object); @@ -434,6 +438,14 @@ ol.Map.prototype.getCoordinateFromPixel = function(pixel) { }; +/** + * @return {ol.Collection} Controls. + */ +ol.Map.prototype.getControls = function() { + return this.controls_; +}; + + /** * Get feature information for a pixel on the map. * @@ -997,7 +1009,7 @@ ol.Map.prototype.withFrozenRendering = function(f, opt_obj) { /** - * @typedef {{controls: Array., + * @typedef {{controls: ol.Collection, * interactions: ol.Collection, * rendererConstructor: * function(new: ol.renderer.Map, Element, ol.Map), @@ -1074,8 +1086,17 @@ ol.Map.createOptionsInternal = function(options) { } } - var controls = goog.isDef(options.controls) ? - options.controls : ol.control.defaults(); + var controls; + if (goog.isDef(options.controls)) { + if (goog.isArray(options.controls)) { + controls = new ol.Collection(goog.array.clone(options.controls)); + } else { + goog.asserts.assertInstanceof(options.controls, ol.Collection); + controls = options.controls; + } + } else { + controls = ol.control.defaults(); + } var interactions = goog.isDef(options.interactions) ? options.interactions : ol.interaction.defaults(); From be86b83b678bcc7bd40e7fb13201a507f6d6d105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 4 Jun 2013 07:09:25 +0200 Subject: [PATCH 2/3] Add addControl and removeControl methods to ol.Map --- src/ol/map.exports | 2 ++ src/ol/map.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/ol/map.exports b/src/ol/map.exports index 8523ff1658..71fe519081 100644 --- a/src/ol/map.exports +++ b/src/ol/map.exports @@ -1,4 +1,5 @@ @exportClass ol.Map ol.MapOptions +@exportProperty ol.Map.prototype.addControl @exportProperty ol.Map.prototype.addLayer @exportProperty ol.Map.prototype.addPreRenderFunction @exportProperty ol.Map.prototype.addPreRenderFunctions @@ -6,6 +7,7 @@ @exportProperty ol.Map.prototype.getFeatures @exportProperty ol.Map.prototype.getInteractions @exportProperty ol.Map.prototype.getRenderer +@exportProperty ol.Map.prototype.removeControl @exportProperty ol.Map.prototype.removeLayer @exportProperty ol.Map.prototype.updateSize diff --git a/src/ol/map.js b/src/ol/map.js index e929730ecc..d08576f636 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -338,6 +338,18 @@ ol.Map = function(options) { goog.inherits(ol.Map, ol.Object); +/** + * Add the given control to the map. + * @param {ol.control.Control} control Control. + */ +ol.Map.prototype.addControl = function(control) { + var controls = this.getControls(); + goog.asserts.assert(goog.isDef(controls)); + controls.push(control); + control.setMap(this); +}; + + /** * Adds the given layer to the top of this map. * @param {ol.layer.Layer} layer Layer. @@ -809,6 +821,20 @@ ol.Map.prototype.requestRenderFrame = function() { }; +/** + * Remove the given control from the map. + * @param {ol.control.Control} control Control. + * @return {ol.control.Control|undefined} The removed control of undefined + * if the control was not found. + */ +ol.Map.prototype.removeControl = function(control) { + var controls = this.getControls(); + goog.asserts.assert(goog.isDef(controls)); + control.setMap(null); + return /** @type {ol.control.Control|undefined} */ (controls.remove(control)); +}; + + /** * Removes the given layer from the map. * @param {ol.layer.Layer} layer Layer. From b0dde2ecb422f87708fe16573316696d23218bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 20 Jun 2013 16:10:11 +0200 Subject: [PATCH 3/3] Do not call setMap(null) if control not in map --- src/ol/map.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ol/map.js b/src/ol/map.js index d08576f636..eb8632d0f6 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -830,8 +830,11 @@ ol.Map.prototype.requestRenderFrame = function() { ol.Map.prototype.removeControl = function(control) { var controls = this.getControls(); goog.asserts.assert(goog.isDef(controls)); - control.setMap(null); - return /** @type {ol.control.Control|undefined} */ (controls.remove(control)); + if (goog.isDef(controls.remove(control))) { + control.setMap(null); + return control; + } + return undefined; };