reconsider the way we specify controls on the map #273

Merged
merged 10 commits into from Mar 6, 2013
+266 −213
Split
View
@@ -4,6 +4,7 @@ goog.require('ol.Map');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.control.ScaleLineUnits');
+goog.require('ol.control.defaults');
goog.require('ol.layer.TileLayer');
goog.require('ol.projection');
goog.require('ol.source.TiledWMS');
@@ -24,11 +25,16 @@ var layers = new ol.Collection([
]);
var map = new ol.Map({
+ controls: ol.control.defaults({
+ scaleLine: true,
+ // FIXME The typecast here is only needed if the example is compiled
+ scaleLineOptions: /** @type {ol.control.ScaleLineOptions} */ ({
+ units: ol.control.ScaleLineUnits.DEGREES
+ })
+ }),
layers: layers,
// The OSgeo server does not set cross origin headers, so we cannot use WebGL
renderers: [ol.RendererHint.CANVAS, ol.RendererHint.DOM],
- scaleLineControl: true,
- scaleLineUnits: ol.control.ScaleLineUnits.DEGREES,
target: 'map',
view: new ol.View2D({
projection: ol.projection.getFromCode('EPSG:4326'),
View
@@ -5,6 +5,7 @@ goog.require('ol.Geolocation');
goog.require('ol.Map');
goog.require('ol.RendererHints');
goog.require('ol.View2D');
+goog.require('ol.control.defaults');
goog.require('ol.layer.TileLayer');
goog.require('ol.source.MapQuestOpenAerial');
@@ -13,9 +14,11 @@ var layer = new ol.layer.TileLayer({
source: new ol.source.MapQuestOpenAerial()
});
var map = new ol.Map({
+ controls: ol.control.defaults({
+ scaleLine: true
+ }),
layers: new ol.Collection([layer]),
renderers: ol.RendererHints.createFromQueryData(),
- scaleLineControl: true,
target: 'map',
view: new ol.View2D({
center: new ol.Coordinate(0, 0),
View
@@ -5,6 +5,7 @@ goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.animation');
goog.require('ol.control.MousePosition');
+goog.require('ol.control.defaults');
goog.require('ol.easing');
goog.require('ol.interaction.Keyboard');
goog.require('ol.layer.TileLayer');
@@ -26,22 +27,28 @@ var view = new ol.View2D({
zoom: 1
});
+var domMousePosition = new ol.control.MousePosition({
+ coordinateFormat: ol.Coordinate.toStringHDMS,
+ projection: ol.projection.getFromCode('EPSG:4326'),
+ target: document.getElementById('domMousePosition'),
+ undefinedHTML: ' '
+});
var domMap = new ol.Map({
+ controls: ol.control.defaults({}, [domMousePosition]),
layers: new ol.Collection([layer]),
renderer: ol.RendererHint.DOM,
target: 'domMap',
view: view
});
-var domMousePosition = new ol.control.MousePosition({
+var webglMousePosition = new ol.control.MousePosition({
coordinateFormat: ol.Coordinate.toStringHDMS,
projection: ol.projection.getFromCode('EPSG:4326'),
- target: document.getElementById('domMousePosition'),
+ target: document.getElementById('webglMousePosition'),
undefinedHTML: ' '
});
-domMousePosition.setMap(domMap);
-
var webglMap = new ol.Map({
+ controls: ol.control.defaults({}, [webglMousePosition]),
renderer: ol.RendererHint.WEBGL,
target: 'webglMap'
});
@@ -50,15 +57,15 @@ if (webglMap !== null) {
webglMap.bindTo('view', domMap);
}
-var webglMousePosition = new ol.control.MousePosition({
+
+var canvasMousePosition = new ol.control.MousePosition({
coordinateFormat: ol.Coordinate.toStringHDMS,
projection: ol.projection.getFromCode('EPSG:4326'),
- target: document.getElementById('webglMousePosition'),
- undefinedHTML: ' '
+ target: document.getElementById('canvasMousePosition'),
+ undefinedHtml: ' '
});
-webglMousePosition.setMap(webglMap);
-
var canvasMap = new ol.Map({
+ controls: ol.control.defaults({}, [canvasMousePosition]),
renderer: ol.RendererHint.CANVAS,
target: 'canvasMap'
});
@@ -67,14 +74,6 @@ if (canvasMap !== null) {
canvasMap.bindTo('view', domMap);
}
-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() {
layer.setBrightness(0);
View
@@ -22,7 +22,6 @@ var layers = new ol.Collection([
var map = new ol.Map({
layers: layers,
renderers: ol.RendererHints.createFromQueryData(),
- scaleLineControl: true,
target: 'map',
view: new ol.View2D({
center: new ol.Coordinate(0, 0),
View
@@ -1,24 +1,11 @@
@exportObjectLiteral ol.MapOptions
-@exportObjectLiteralProperty ol.MapOptions.attributionControl boolean|undefined
-@exportObjectLiteralProperty ol.MapOptions.doubleClickZoom boolean|undefined
-@exportObjectLiteralProperty ol.MapOptions.dragPan boolean|undefined
+@exportObjectLiteralProperty ol.MapOptions.controls Array.<ol.control.Control>|undefined
@exportObjectLiteralProperty ol.MapOptions.interactions ol.Collection|undefined
-@exportObjectLiteralProperty ol.MapOptions.keyboard boolean|undefined
-@exportObjectLiteralProperty ol.MapOptions.keyboardPanOffset number|undefined
@exportObjectLiteralProperty ol.MapOptions.layers ol.Collection|undefined
-@exportObjectLiteralProperty ol.MapOptions.mouseWheelZoom boolean|undefined
@exportObjectLiteralProperty ol.MapOptions.renderer ol.RendererHint|undefined
@exportObjectLiteralProperty ol.MapOptions.renderers Array.<ol.RendererHint>|undefined
-@exportObjectLiteralProperty ol.MapOptions.scaleLineControl boolean|undefined
-@exportObjectLiteralProperty ol.MapOptions.scaleLineUnits ol.control.ScaleLineUnits|undefined
-@exportObjectLiteralProperty ol.MapOptions.shiftDragZoom boolean|undefined
@exportObjectLiteralProperty ol.MapOptions.target Element|string
-@exportObjectLiteralProperty ol.MapOptions.touchPan boolean|undefined
-@exportObjectLiteralProperty ol.MapOptions.touchRotate boolean|undefined
-@exportObjectLiteralProperty ol.MapOptions.touchZoom boolean|undefined
@exportObjectLiteralProperty ol.MapOptions.view ol.IView|undefined
-@exportObjectLiteralProperty ol.MapOptions.zoomControl boolean|undefined
-@exportObjectLiteralProperty ol.MapOptions.zoomDelta number|undefined
@exportObjectLiteral ol.View2DOptions
@exportObjectLiteralProperty ol.View2DOptions.center ol.Coordinate|undefined
@@ -59,6 +46,14 @@
@exportObjectLiteralProperty ol.control.AttributionOptions.map ol.Map|undefined
@exportObjectLiteralProperty ol.control.AttributionOptions.target Element|undefined
+@exportObjectLiteral ol.control.DefaultsOptions
+@exportObjectLiteralProperty ol.control.DefaultsOptions.attribution boolean|undefined
+@exportObjectLiteralProperty ol.control.DefaultsOptions.attributionOptions ol.control.AttributionOptions|undefined
+@exportObjectLiteralProperty ol.control.DefaultsOptions.scaleLine boolean|undefined
+@exportObjectLiteralProperty ol.control.DefaultsOptions.scaleLineOptions ol.control.ScaleLineOptions|undefined
+@exportObjectLiteralProperty ol.control.DefaultsOptions.zoom boolean|undefined
+@exportObjectLiteralProperty ol.control.DefaultsOptions.zoomOptions ol.control.ZoomOptions|undefined
+
@exportObjectLiteral ol.control.ScaleLineOptions
@exportObjectLiteralProperty ol.control.ScaleLineOptions.map ol.Map|undefined
@exportObjectLiteralProperty ol.control.ScaleLineOptions.minWidth number|undefined
@@ -77,6 +72,17 @@
@exportObjectLiteralProperty ol.control.ZoomOptions.map ol.Map|undefined
@exportObjectLiteralProperty ol.control.ZoomOptions.target Element|undefined
+@exportObjectLiteral ol.interaction.DefaultOptions
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.doubleClickZoom boolean|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.dragPan boolean|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.keyboard boolean|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.keyboardPanOffset number|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.mouseWheelZoom boolean|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.shiftDragZoom boolean|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.touchPan boolean|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.touchRotate boolean|undefined
+@exportObjectLiteralProperty ol.interaction.DefaultOptions.touchZoom boolean|undefined
+
@exportObjectLiteral ol.layer.LayerOptions
@exportObjectLiteralProperty ol.layer.LayerOptions.brightness number|undefined
@exportObjectLiteralProperty ol.layer.LayerOptions.contrast number|undefined
View
@@ -100,6 +100,17 @@ ol.Collection.prototype.clear = function() {
/**
+ * @param {Array} arr Array.
+ */
+ol.Collection.prototype.extend = function(arr) {
+ var i;
+ for (i = 0; i < arr.length; ++i) {
+ this.push(arr[i]);
+ }
+};
+
+
+/**
* @param {Function} f Function.
* @param {Object=} opt_obj Object.
*/
@@ -21,10 +21,11 @@ goog.require('ol.source.Source');
/**
* @constructor
* @extends {ol.control.Control}
- * @param {ol.control.AttributionOptions} attributionOptions Attribution
- * options.
+ * @param {ol.control.AttributionOptions=} opt_options Options.
*/
-ol.control.Attribution = function(attributionOptions) {
+ol.control.Attribution = function(opt_options) {
+
+ var options = goog.isDef(opt_options) ? opt_options : {};
this.ulElement_ = goog.dom.createElement(goog.dom.TagName.UL);
@@ -34,8 +35,8 @@ ol.control.Attribution = function(attributionOptions) {
goog.base(this, {
element: element,
- map: attributionOptions.map,
- target: attributionOptions.target
+ map: options.map,
+ target: options.target
});
/**
@@ -0,0 +1 @@
+@exportSymbol ol.control.defaults ol.control.defaults
View
@@ -0,0 +1,51 @@
+goog.provide('ol.control.defaults');
+
+goog.require('goog.array');
+goog.require('ol.control.Attribution');
+goog.require('ol.control.ScaleLine');
+goog.require('ol.control.Zoom');
+
+
+/**
+ * @param {ol.control.DefaultsOptions=} opt_options Options.
+ * @param {Array.<ol.control.Control>=} opt_controls Additional controls.
+ * @return {Array.<ol.control.Control>} Controls.
+ */
+ol.control.defaults = function(opt_options, opt_controls) {
+
+ var options = goog.isDef(opt_options) ? opt_options : {};
+
+ /** @type {Array.<ol.control.Control>} */
+ var controls = [];
+
+ var attributionControl = goog.isDef(options.attribution) ?
+ options.attribution : true;
+ if (attributionControl) {
+ var attributionControlOptions = goog.isDef(options.attributionOptions) ?
+ options.attributionOptions : undefined;
+ controls.push(new ol.control.Attribution(attributionControlOptions));
+ }
+
+ var scaleLineControl = goog.isDef(options.scaleLine) ?
+ options.scaleLine : false;
+ if (scaleLineControl) {
+ var scaleLineOptions = goog.isDef(options.scaleLineOptions) ?
+ options.scaleLineOptions : undefined;
+ controls.push(new ol.control.ScaleLine(scaleLineOptions));
+ }
+
+ var zoomControl = goog.isDef(options.zoom) ?
+ options.zoom : true;
+ if (zoomControl) {
+ var zoomControlOptions = goog.isDef(options.zoomControlOptions) ?
+ options.zoomControlOptions : undefined;
+ controls.push(new ol.control.Zoom(zoomControlOptions));
+ }
+
+ if (goog.isDef(opt_controls)) {
+ goog.array.extend(controls, opt_controls);
+ }
+
+ return controls;
+
+};
@@ -23,39 +23,40 @@ goog.require('ol.projection');
/**
* @constructor
* @extends {ol.control.Control}
- * @param {ol.control.MousePositionOptions} mousePositionOptions Mouse position
- * options.
+ * @param {ol.control.MousePositionOptions=} opt_options Options.
*/
-ol.control.MousePosition = function(mousePositionOptions) {
+ol.control.MousePosition = function(opt_options) {
+
+ var options = goog.isDef(opt_options) ? opt_options : {};
var element = goog.dom.createDom(goog.dom.TagName.DIV, {
'class': 'ol-mouse-position'
});
goog.base(this, {
element: element,
- map: mousePositionOptions.map,
- target: mousePositionOptions.target
+ map: options.map,
+ target: options.target
});
/**
* @private
* @type {ol.Projection|undefined}
*/
- this.projection_ = mousePositionOptions.projection;
+ this.projection_ = options.projection;
/**
* @private
* @type {ol.CoordinateFormatType|undefined}
*/
- this.coordinateFormat_ = mousePositionOptions.coordinateFormat;
+ this.coordinateFormat_ = options.coordinateFormat;
/**
* @private
* @type {string}
*/
- this.undefinedHTML_ = goog.isDef(mousePositionOptions.undefinedHTML) ?
- mousePositionOptions.undefinedHTML : '';
+ this.undefinedHTML_ = goog.isDef(options.undefinedHTML) ?
+ options.undefinedHTML : '';
/**
* @private
@@ -19,9 +19,11 @@ ol.control.ZOOM_DURATION = 250;
/**
* @constructor
* @extends {ol.control.Control}
- * @param {ol.control.ZoomOptions} zoomOptions Zoom options.
+ * @param {ol.control.ZoomOptions=} opt_options Options.
*/
-ol.control.Zoom = function(zoomOptions) {
+ol.control.Zoom = function(opt_options) {
+
+ var options = goog.isDef(opt_options) ? opt_options : {};
var inElement = goog.dom.createDom(goog.dom.TagName.A, {
'href': '#zoomIn',
@@ -46,15 +48,15 @@ ol.control.Zoom = function(zoomOptions) {
goog.base(this, {
element: element,
- map: zoomOptions.map,
- target: zoomOptions.target
+ map: options.map,
+ target: options.target
});
/**
* @type {number}
* @private
*/
- this.delta_ = goog.isDef(zoomOptions.delta) ? zoomOptions.delta : 1;
+ this.delta_ = goog.isDef(options.delta) ? options.delta : 1;
};
goog.inherits(ol.control.Zoom, ol.control.Control);
@@ -0,0 +1 @@
+@exportSymbol ol.interaction.defaults ol.interaction.defaults
Oops, something went wrong.