The map now has a collection of controls

This commit is contained in:
Éric Lemoine
2013-06-04 07:08:43 +02:00
parent 79980bc7be
commit b85520f504
3 changed files with 39 additions and 19 deletions

View File

@@ -43,8 +43,8 @@
/** /**
* Object literal with config options for the map. * Object literal with config options for the map.
* @typedef {Object} ol.MapOptions * @typedef {Object} ol.MapOptions
* @property {Array.<ol.control.Control>|undefined} controls Controls initially * @property {ol.Collection|Array.<ol.control.Control>|undefined} controls
* added to the map. * Controls initially added to the map.
* @property {ol.Collection|undefined} interactions Interactions. * @property {ol.Collection|undefined} interactions Interactions.
* @property {Array.<ol.layer.Layer>|ol.Collection|undefined} layers Layers. * @property {Array.<ol.layer.Layer>|ol.Collection|undefined} layers Layers.
* @property {ol.RendererHint|undefined} renderer Renderer. * @property {ol.RendererHint|undefined} renderer Renderer.

View File

@@ -1,6 +1,6 @@
goog.provide('ol.control.defaults'); goog.provide('ol.control.defaults');
goog.require('goog.array'); goog.require('ol.Collection');
goog.require('ol.control.Attribution'); goog.require('ol.control.Attribution');
goog.require('ol.control.Logo'); goog.require('ol.control.Logo');
goog.require('ol.control.Zoom'); goog.require('ol.control.Zoom');
@@ -9,14 +9,13 @@ goog.require('ol.control.Zoom');
/** /**
* @param {ol.control.DefaultsOptions=} opt_options Defaults options. * @param {ol.control.DefaultsOptions=} opt_options Defaults options.
* @param {Array.<ol.control.Control>=} opt_controls Additional controls. * @param {Array.<ol.control.Control>=} opt_controls Additional controls.
* @return {Array.<ol.control.Control>} Controls. * @return {ol.Collection} Controls.
*/ */
ol.control.defaults = function(opt_options, opt_controls) { ol.control.defaults = function(opt_options, opt_controls) {
var options = goog.isDef(opt_options) ? opt_options : {}; var options = goog.isDef(opt_options) ? opt_options : {};
/** @type {Array.<ol.control.Control>} */ var controls = new ol.Collection();
var controls = [];
var attributionControl = goog.isDef(options.attribution) ? var attributionControl = goog.isDef(options.attribution) ?
options.attribution : true; options.attribution : true;
@@ -43,7 +42,7 @@ ol.control.defaults = function(opt_options, opt_controls) {
} }
if (goog.isDef(opt_controls)) { if (goog.isDef(opt_controls)) {
goog.array.extend(controls, opt_controls); controls.extend(opt_controls);
} }
return controls; return controls;

View File

@@ -247,6 +247,12 @@ ol.Map = function(options) {
this.handleBrowserEvent, false, this); this.handleBrowserEvent, false, this);
this.registerDisposable(mouseWheelHandler); this.registerDisposable(mouseWheelHandler);
/**
* @type {ol.Collection}
* @private
*/
this.controls_ = optionsInternal.controls;
/** /**
* @type {ol.Collection} * @type {ol.Collection}
* @private * @private
@@ -320,15 +326,13 @@ ol.Map = function(options) {
// is "defined" already. // is "defined" already.
this.setValues(optionsInternal.values); this.setValues(optionsInternal.values);
if (goog.isDef(optionsInternal.controls)) { this.controls_.forEach(
goog.array.forEach(optionsInternal.controls, /**
/** * @param {ol.control.Control} control Control.
* @param {ol.control.Control} control Control. */
*/ function(control) {
function(control) { control.setMap(this);
control.setMap(this); }, this);
}, this);
}
}; };
goog.inherits(ol.Map, ol.Object); 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. * Get feature information for a pixel on the map.
* *
@@ -997,7 +1009,7 @@ ol.Map.prototype.withFrozenRendering = function(f, opt_obj) {
/** /**
* @typedef {{controls: Array.<ol.control.Control>, * @typedef {{controls: ol.Collection,
* interactions: ol.Collection, * interactions: ol.Collection,
* rendererConstructor: * rendererConstructor:
* function(new: ol.renderer.Map, Element, ol.Map), * function(new: ol.renderer.Map, Element, ol.Map),
@@ -1074,8 +1086,17 @@ ol.Map.createOptionsInternal = function(options) {
} }
} }
var controls = goog.isDef(options.controls) ? var controls;
options.controls : ol.control.defaults(); 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) ? var interactions = goog.isDef(options.interactions) ?
options.interactions : ol.interaction.defaults(); options.interactions : ol.interaction.defaults();