Merge pull request #175 from elemoine/control
Simplify control architecture
This commit is contained in:
@@ -42,12 +42,13 @@ var domMap = new ol.Map({
|
||||
view: view
|
||||
});
|
||||
|
||||
domMap.getControls().push(new ol.control.MousePosition({
|
||||
var domMousePosition = new ol.control.MousePosition({
|
||||
coordinateFormat: ol.Coordinate.toStringHDMS,
|
||||
projection: ol.Projection.getFromCode('EPSG:4326'),
|
||||
target: document.getElementById('domMousePosition'),
|
||||
undefinedHTML: ' '
|
||||
}));
|
||||
});
|
||||
domMousePosition.setMap(domMap);
|
||||
|
||||
var webglMap = new ol.Map({
|
||||
renderer: ol.RendererHint.WEBGL,
|
||||
@@ -58,12 +59,13 @@ if (webglMap !== null) {
|
||||
webglMap.bindTo('view', domMap);
|
||||
}
|
||||
|
||||
webglMap.getControls().push(new ol.control.MousePosition({
|
||||
var webglMousePosition = new ol.control.MousePosition({
|
||||
coordinateFormat: ol.Coordinate.toStringHDMS,
|
||||
projection: ol.Projection.getFromCode('EPSG:4326'),
|
||||
target: document.getElementById('webglMousePosition'),
|
||||
undefinedHTML: ' '
|
||||
}));
|
||||
});
|
||||
webglMousePosition.setMap(webglMap);
|
||||
|
||||
var canvasMap = new ol.Map({
|
||||
renderer: ol.RendererHint.CANVAS,
|
||||
@@ -74,12 +76,13 @@ if (canvasMap !== null) {
|
||||
canvasMap.bindTo('view', domMap);
|
||||
}
|
||||
|
||||
canvasMap.getControls().push(new ol.control.MousePosition({
|
||||
var canvasMousePosition = new ol.control.MousePosition({
|
||||
coordinateFormat: ol.Coordinate.toStringHDMS,
|
||||
projection: ol.Projection.getFromCode('EPSG:4326'),
|
||||
target: document.getElementById('canvasMousePosition'),
|
||||
undefinedHtml: ' '
|
||||
}));
|
||||
});
|
||||
canvasMousePosition.setMap(canvasMap);
|
||||
|
||||
var keyboardInteraction = new ol.interaction.Keyboard();
|
||||
keyboardInteraction.addCallback('0', function() {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
@exportObjectLiteral ol.MapOptions
|
||||
@exportObjectLiteralProperty ol.MapOptions.controls ol.Collection|undefined
|
||||
@exportObjectLiteralProperty ol.MapOptions.attributionControl boolean|undefined
|
||||
@exportObjectLiteralProperty ol.MapOptions.doubleClickZoom boolean|undefined
|
||||
@exportObjectLiteralProperty ol.MapOptions.dragPan boolean|undefined
|
||||
@exportObjectLiteralProperty ol.MapOptions.interactions ol.Collection|undefined
|
||||
@@ -13,6 +13,7 @@
|
||||
@exportObjectLiteralProperty ol.MapOptions.shiftDragZoom boolean|undefined
|
||||
@exportObjectLiteralProperty ol.MapOptions.target Element|string
|
||||
@exportObjectLiteralProperty ol.MapOptions.view ol.IView|undefined
|
||||
@exportObjectLiteralProperty ol.MapOptions.zoomControl boolean|undefined
|
||||
@exportObjectLiteralProperty ol.MapOptions.zoomDelta number|undefined
|
||||
|
||||
@exportObjectLiteral ol.View2DOptions
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
@exportClass ol.control.Attribution ol.control.AttributionOptions
|
||||
@exportProperty ol.control.Attribution.prototype.setMap
|
||||
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
@exportClass ol.control.MousePosition ol.control.MousePositionOptions
|
||||
@exportProperty ol.control.MousePosition.prototype.setMap
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
@exportClass ol.control.Zoom ol.control.ZoomOptions
|
||||
|
||||
@exportProperty ol.control.Zoom.prototype.setMap
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
@exportClass ol.Map ol.MapOptions
|
||||
@exportProperty ol.Map.prototype.addPreRenderFunction
|
||||
@exportProperty ol.Map.prototype.addPreRenderFunctions
|
||||
@exportProperty ol.Map.prototype.getControls
|
||||
@exportProperty ol.Map.prototype.getInteractions
|
||||
|
||||
@exportSymbol ol.RendererHint
|
||||
|
||||
@@ -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.
|
||||
@@ -522,26 +503,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.
|
||||
*/
|
||||
@@ -848,7 +809,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),
|
||||
@@ -914,14 +875,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}
|
||||
@@ -952,22 +908,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({}));
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user