Simplify control architecture

This commit is contained in:
Éric Lemoine
2013-02-01 18:10:37 +01:00
parent f735a597fe
commit bde61a0b4b
7 changed files with 37 additions and 69 deletions

View File

@@ -22,8 +22,6 @@ goog.require('goog.events.MouseWheelHandler');
goog.require('goog.events.MouseWheelHandler.EventType');
goog.require('ol.BrowserFeature');
goog.require('ol.Collection');
goog.require('ol.CollectionEvent');
goog.require('ol.CollectionEventType');
goog.require('ol.Color');
goog.require('ol.Coordinate');
goog.require('ol.Extent');
@@ -230,17 +228,6 @@ ol.Map = function(mapOptions) {
this.handleBrowserEvent, false, this);
this.registerDisposable(mouseWheelHandler);
/**
* @type {ol.Collection}
* @private
*/
this.controls_ = mapOptionsInternal.controls;
goog.events.listen(this.controls_, ol.CollectionEventType.ADD,
this.handleControlsAdd_, false, this);
goog.events.listen(this.controls_, ol.CollectionEventType.REMOVE,
this.handleControlsRemove_, false, this);
/**
* @type {ol.Collection}
* @private
@@ -299,7 +286,9 @@ ol.Map = function(mapOptions) {
// this gives the map an initial size
this.handleBrowserWindowResize();
this.controls_.forEach(
/** @type {Array.<ol.control.Control>} */
var controls = mapOptionsInternal.controls;
goog.array.forEach(controls,
/**
* @param {ol.control.Control} control Control.
*/
@@ -387,14 +376,6 @@ ol.Map.prototype.getTarget = function() {
};
/**
* @return {ol.Collection} Controls.
*/
ol.Map.prototype.getControls = function() {
return this.controls_;
};
/**
* @param {ol.Pixel} pixel Pixel.
* @return {ol.Coordinate} Coordinate.
@@ -523,26 +504,6 @@ ol.Map.prototype.handleBrowserEvent = function(browserEvent, opt_type) {
};
/**
* @param {ol.CollectionEvent} collectionEvent Collection event.
* @private
*/
ol.Map.prototype.handleControlsAdd_ = function(collectionEvent) {
var control = /** @type {ol.control.Control} */ (collectionEvent.elem);
control.setMap(this);
};
/**
* @param {ol.CollectionEvent} collectionEvent Collection event.
* @private
*/
ol.Map.prototype.handleControlsRemove_ = function(collectionEvent) {
var control = /** @type {ol.control.Control} */ (collectionEvent.elem);
control.setMap(null);
};
/**
* @param {ol.MapBrowserEvent} mapBrowserEvent The event to handle.
*/
@@ -849,7 +810,7 @@ ol.Map.prototype.withFrozenRendering = function(f, opt_obj) {
/**
* @typedef {{controls: ol.Collection,
* @typedef {{controls: Array.<ol.control.Control>,
* interactions: ol.Collection,
* rendererConstructor:
* function(new: ol.renderer.Map, Element, ol.Map),
@@ -915,14 +876,9 @@ ol.Map.createOptionsInternal = function(mapOptions) {
}
/**
* @type {ol.Collection}
* @type {Array.<ol.control.Control>}
*/
var controls;
if (goog.isDef(mapOptions.controls)) {
controls = mapOptions.controls;
} else {
controls = ol.Map.createControls_(mapOptions);
}
var controls = ol.Map.createControls_(mapOptions);
/**
* @type {ol.Collection}
@@ -953,22 +909,29 @@ ol.Map.createOptionsInternal = function(mapOptions) {
/**
* @private
* @param {ol.MapOptions} mapOptions Map options.
* @return {ol.Collection} Controls.
* @return {Array.<ol.control.Control>} Controls.
*/
ol.Map.createControls_ = function(mapOptions) {
/** @type {Array.<ol.control.Control>} */
var controls = [];
var controls = new ol.Collection();
var attributionControl = goog.isDef(mapOptions.attributionControl) ?
mapOptions.attributionControl : true;
if (attributionControl) {
controls.push(new ol.control.Attribution({}));
}
controls.push(new ol.control.Attribution({}));
var zoomDelta = goog.isDef(mapOptions.zoomDelta) ?
mapOptions.zoomDelta : 4;
controls.push(new ol.control.Zoom({
delta: zoomDelta
}));
var zoomControl = goog.isDef(mapOptions.zoomControl) ?
mapOptions.zoomControl : true;
if (zoomControl) {
var zoomDelta = goog.isDef(mapOptions.zoomDelta) ?
mapOptions.zoomDelta : 4;
controls.push(new ol.control.Zoom({
delta: zoomDelta
}));
}
return controls;
};