diff --git a/src/ol.js b/src/ol.js index 8c936f0329..2ff8dfea9a 100644 --- a/src/ol.js +++ b/src/ol.js @@ -2,6 +2,7 @@ goog.provide("ol"); goog.require('ol.base'); goog.require('ol.bounds'); +goog.require('ol.control.Navigation'); goog.require('ol.control.Attribution'); goog.require('ol.control.Zoom'); goog.require('ol.handler.Drag'); diff --git a/src/ol/Map.js b/src/ol/Map.js index cadf228e04..b4dd0d3c8b 100644 --- a/src/ol/Map.js +++ b/src/ol/Map.js @@ -88,6 +88,12 @@ ol.Map = function() { * @type {Array} */ this.controls_ = null; + + /** + * @private + * @type {ol.control.DefaultControl} + */ + this.defaultControl_ = null; /** * @private @@ -157,7 +163,7 @@ ol.Map.DEFAULT_TILE_SIZE = 256; @const @type {Array.} */ -ol.Map.DEFAULT_CONTROLS = ["attribution", "zoom"]; +ol.Map.CONTROLS = ["navigation", "attribution", "zoom"]; /** * @return {ol.Loc} Map center in map projection. @@ -443,6 +449,20 @@ ol.Map.prototype.addLayers = function(layers) { }; +/** + * @returns {ol.control.DefaultControl} + */ +ol.Map.prototype.getDefaultControl = function() { + return this.defaultControl_; +}; + +/** + * @param {ol.control.DefaultControl} control + */ +ol.Map.prototype.setDefaultControl = function(control) { + this.defaultControl_ = control; +}; + /** * @param {Array.|undefined} opt_controls */ @@ -483,7 +503,7 @@ ol.Map.prototype.setContainer = function(container) { this.createRenderer(); //TODO Controls could be set earlier, but we need to deal with content that // controls place on overlays. - this.setControls(ol.Map.DEFAULT_CONTROLS); + this.setControls(ol.Map.CONTROLS); // conditionally render this.conditionallyRender(); }; diff --git a/src/ol/control/Control.js b/src/ol/control/Control.js index eaa1c693d6..b6d1264ac9 100644 --- a/src/ol/control/Control.js +++ b/src/ol/control/Control.js @@ -31,7 +31,7 @@ ol.control.Control = function(opt_autoActivate) { /** * @type {boolean} active - * @private + * @protected */ this.active_ = false; @@ -42,6 +42,7 @@ ol.control.Control = function(opt_autoActivate) { */ this.autoActivate_ = goog.isDef(opt_autoActivate) ? opt_autoActivate : false; + }; /** diff --git a/src/ol/control/DefaultControl.js b/src/ol/control/DefaultControl.js new file mode 100644 index 0000000000..683171017d --- /dev/null +++ b/src/ol/control/DefaultControl.js @@ -0,0 +1,50 @@ +goog.provide('ol.control.DefaultControl'); + +goog.require('ol.control.Control'); + +/** + * @constructor + * @extends {ol.control.Control} + * @param {boolean|undefined} opt_autoActivate + */ +ol.control.DefaultControl = function(opt_autoActivate) { + goog.base(this, opt_autoActivate); + + /** + * Activate this control when it is added to a map. Default is true. + * + * @type {boolean} autoActivate + */ + this.autoActivate_ = + goog.isDef(opt_autoActivate) ? opt_autoActivate : true; + +}; +goog.inherits(ol.control.DefaultControl, ol.control.Control); + +/** @inheritDoc */ +ol.control.DefaultControl.prototype.activate = function() { + var active = goog.base(this, 'activate'); + if (active) { + this.map_.setDefaultControl(this); + } + return active; +}; + +/** @inheritDoc */ +ol.control.DefaultControl.prototype.deactivate = function() { + var inactive = goog.base(this, 'deactivate'); + if (inactive) { + this.map_.setDefaultControl(null); + } + return inactive; +}; + +/** + * @param {ol.events.MapEvent} e + */ +ol.control.DefaultControl.prototype.defaultDrag = function(e) {}; + +/** + * @param {ol.events.MapEvent} e + */ +ol.control.DefaultControl.prototype.defaultMouseWheel = function(e) {}; diff --git a/src/ol/control/Navigation.js b/src/ol/control/Navigation.js new file mode 100644 index 0000000000..9255b412aa --- /dev/null +++ b/src/ol/control/Navigation.js @@ -0,0 +1,54 @@ +goog.provide('ol.control.Navigation'); + +goog.require('ol.control.DefaultControl'); +goog.require('ol.Map'); + +/** + * @constructor + * @extends {ol.control.DefaultControl} + * @param {boolean|undefined} opt_autoActivate + */ +ol.control.Navigation = function(opt_autoActivate) { + goog.base(this, opt_autoActivate); +}; +goog.inherits(ol.control.Navigation, ol.control.DefaultControl); + + +/** + * @inheritDoc + */ +ol.control.Navigation.prototype.defaultDrag = function(e) { + if (ol.ENABLE_DRAG_HANDLER) { + var deltaX = /** @type {number} */ e.deltaX; + var deltaY = /** @type {number} */ e.deltaY; + this.map_.moveByViewportPx(deltaX, deltaY); + } +}; + +/** + * @inheritDoc + */ +ol.control.Navigation.prototype.defaultMouseWheel = function(e) { + if (ol.ENABLE_MOUSEWHEEL_HANDLER) { + var me = this, + originalE = e.originalEvent; + if (originalE.deltaY === 0 || me.zoomBlocked_) { + return; + } + me.zoomBlocked_ = window.setTimeout(function() { + me.zoomBlocked_ = null; + }, 200); + + var map = me.map_, + step = originalE.deltaY / Math.abs(originalE.deltaY), + position = goog.style.getRelativePosition(originalE, + map.getViewport()); + map.setZoom(map.getZoom() - step, position); + + // We don't want the page to scroll. + // (MouseWheelEvent is a originalEvent) + e.preventDefault(); + } +}; + +ol.control.addControl('navigation', ol.control.Navigation); diff --git a/src/ol/handler/Drag.js b/src/ol/handler/Drag.js index ecd97a992f..0b04294823 100644 --- a/src/ol/handler/Drag.js +++ b/src/ol/handler/Drag.js @@ -95,25 +95,19 @@ ol.handler.Drag.prototype.handleDragStart = function(e) { ol.handler.Drag.prototype.handleDrag = function(e) { this.states_.dragged = true; var newE = new ol.events.MapEvent(ol.events.MapEventType.DRAG, e); - newE.delementaX = e.clientX - this.prevX_; - newE.delementaY = e.clientY - this.prevY_; + newE.deltaX = e.clientX - this.prevX_; + newE.deltaY = e.clientY - this.prevY_; this.prevX_ = e.clientX; this.prevY_ = e.clientY; var rt = goog.events.dispatchEvent(this.map_, newE); if (rt) { - this.defaultDrag(newE); + var defaultControl = this.map_.getDefaultControl(); + if (defaultControl) { + defaultControl.defaultDrag(newE); + } } }; -/** - * @param {ol.events.MapEvent} e - */ -ol.handler.Drag.prototype.defaultDrag = function(e) { - var delementaX = /** @type {number} */ e.delementaX; - var delementaY = /** @type {number} */ e.delementaY; - this.map_.moveByViewportPx(delementaX, delementaY); -}; - /** * @param {goog.fx.DragEvent} e */ diff --git a/src/ol/handler/MapHandler.js b/src/ol/handler/MapHandler.js index 188fd03288..243380cc5f 100644 --- a/src/ol/handler/MapHandler.js +++ b/src/ol/handler/MapHandler.js @@ -4,7 +4,7 @@ * Type definitions and base class for map event handlers that share states, * listen for events on a map related dom element (usually the map's viewport), * dispatch events to an ol.Map instance, and optionally perform default - * actions on an ol.Map instance. + * actions on the map's default control. */ goog.provide('ol.handler.states'); diff --git a/src/ol/handler/MouseWheel.js b/src/ol/handler/MouseWheel.js index 48b483294c..042fd7423e 100644 --- a/src/ol/handler/MouseWheel.js +++ b/src/ol/handler/MouseWheel.js @@ -47,28 +47,9 @@ ol.handler.MouseWheel.prototype.handleMouseWheel = function(e) { var newE = new ol.events.MapEvent(ol.events.MapEventType.MOUSEWHEEL, e); var rt = goog.events.dispatchEvent(this.map_, newE); if (rt) { - this.defaultMouseWheel(e); + var defaultControl = this.map_.getDefaultControl(); + if (defaultControl) { + defaultControl.defaultMouseWheel(newE); + } } -}; - -/** - * @param {goog.events.MouseWheelEvent} e - */ -ol.handler.MouseWheel.prototype.defaultMouseWheel = function(e) { - var me = this; - if (e.deltaY === 0 || me.zoomBlocked_) { - return; - } - me.zoomBlocked_ = window.setTimeout(function() { - me.zoomBlocked_ = null; - }, 200); - - var map = me.map_, - step = e.deltaY / Math.abs(e.deltaY); - map.setZoom(map.getZoom() - step, - goog.style.getRelativePosition(e, this.element_)); - - // We don't want the page to scroll. - // (MouseWheelEvent is a BrowserEvent) - e.preventDefault(); -}; +}; \ No newline at end of file