Replace the navigation control by two controls, namely DragPan and MouseWheelZoom. DragPan is the map default drag control. MouseWheelZoom is the map default mouse wheel control.

This commit is contained in:
Éric Lemoine
2012-07-13 08:57:40 +02:00
parent f4532f1548
commit c2a30f4ac4
9 changed files with 108 additions and 96 deletions

View File

@@ -2,7 +2,8 @@ goog.provide("ol");
goog.require('ol.base');
goog.require('ol.bounds');
goog.require('ol.control.Navigation');
goog.require('ol.control.DragPan');
goog.require('ol.control.MouseWheelZoom');
goog.require('ol.control.Attribution');
goog.require('ol.control.Zoom');
goog.require('ol.handler.Drag');

View File

@@ -91,9 +91,15 @@ ol.Map = function() {
/**
* @private
* @type {ol.control.DefaultControl}
* @type {ol.control.Control}
*/
this.defaultControl_ = null;
this.defaultDragControl_ = null;
/**
* @private
* @type {ol.control.Control}
*/
this.defaultMouseWheelControl_ = null;
/**
* @private
@@ -163,7 +169,7 @@ ol.Map.DEFAULT_TILE_SIZE = 256;
@const
@type {Array.<string>}
*/
ol.Map.CONTROLS = ["navigation", "attribution", "zoom"];
ol.Map.CONTROLS = ["dragpan", "mousewheelzoom", "attribution", "zoom"];
/**
* @return {ol.Loc} Map center in map projection.
@@ -450,17 +456,35 @@ ol.Map.prototype.addLayers = function(layers) {
/**
* @returns {ol.control.DefaultControl}
* @param {ol.control.Control} control
*/
ol.Map.prototype.getDefaultControl = function() {
return this.defaultControl_;
ol.Map.prototype.setDefaultDragControl = function(control) {
this.defaultDragControl_ = control;
};
/**
* @param {ol.control.DefaultControl} control
* @param {ol.events.MapEvent} e
*/
ol.Map.prototype.setDefaultControl = function(control) {
this.defaultControl_ = control;
ol.Map.prototype.handleDragEvent = function(e) {
if (!goog.isNull(this.defaultDragControl_)) {
this.defaultDragControl_.handleEvent(e);
}
};
/**
* @param {ol.control.Control} control
*/
ol.Map.prototype.setDefaultMouseWheelControl = function(control) {
this.defaultMouseWheelControl_ = control;
};
/**
* @param {ol.events.MapEvent} e
*/
ol.Map.prototype.handleMouseWheelEvent = function(e) {
if (!goog.isNull(this.defaultMouseWheelControl_)) {
this.defaultMouseWheelControl_.handleEvent(e);
}
};
/**

View File

@@ -1,50 +0,0 @@
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) {};

41
src/ol/control/DragPan.js Normal file
View File

@@ -0,0 +1,41 @@
/**
* @fileoverview Drag Pan Control.
*
* This control registers itself in the map as the default drag control.
*/
goog.provide('ol.control.DragPan');
goog.require('ol.control.Control');
/**
* @constructor
* @extends {ol.control.Control}
* @param {boolean|undefined} opt_autoActivate
*/
ol.control.DragPan = function(opt_autoActivate) {
goog.base(this, opt_autoActivate);
};
goog.inherits(ol.control.DragPan, ol.control.Control);
/**
* @param {ol.Map} map
*/
ol.control.DragPan.prototype.setMap = function(map) {
goog.base(this, 'setMap', map);
this.map_.setDefaultDragControl(this);
};
/**
* @param {ol.events.MapEvent} e
*/
ol.control.DragPan.prototype.handleEvent = function(e) {
// FIXME do we want to test ENABLE_DRAG_HANDLER here?
if (ol.ENABLE_DRAG_HANDLER) {
var deltaX = /** @type {number} */ e.deltaX;
var deltaY = /** @type {number} */ e.deltaY;
this.map_.moveByViewportPx(deltaX, deltaY);
}
};
ol.control.addControl('dragpan', ol.control.DragPan);

View File

@@ -1,34 +1,36 @@
goog.provide('ol.control.Navigation');
/**
* @fileoverview Mouse Wheel Zoom Control.
*
* This control registers itself in the map as the default mouse wheel control.
*/
goog.require('ol.control.DefaultControl');
goog.require('ol.Map');
goog.provide('ol.control.MouseWheelZoom');
goog.require('ol.control.Control');
/**
* @constructor
* @extends {ol.control.DefaultControl}
* @extends {ol.control.Control}
* @param {boolean|undefined} opt_autoActivate
*/
ol.control.Navigation = function(opt_autoActivate) {
ol.control.MouseWheelZoom = function(opt_autoActivate) {
goog.base(this, opt_autoActivate);
};
goog.inherits(ol.control.Navigation, ol.control.DefaultControl);
goog.inherits(ol.control.MouseWheelZoom, ol.control.Control);
/**
* @inheritDoc
* @param {ol.Map} map
*/
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);
}
ol.control.MouseWheelZoom.prototype.setMap = function(map) {
goog.base(this, 'setMap', map);
this.map_.setDefaultMouseWheelControl(this);
};
/**
* @inheritDoc
* @param {ol.events.MapEvent} e
*/
ol.control.Navigation.prototype.defaultMouseWheel = function(e) {
ol.control.MouseWheelZoom.prototype.handleEvent = function(e) {
// FIXME do we want to test ENABLE_DRAG_HANDLER here?
if (ol.ENABLE_MOUSEWHEEL_HANDLER) {
var me = this,
originalE = e.originalEvent;
@@ -51,4 +53,4 @@ ol.control.Navigation.prototype.defaultMouseWheel = function(e) {
}
};
ol.control.addControl('navigation', ol.control.Navigation);
ol.control.addControl('mousewheelzoom', ol.control.MouseWheelZoom);

View File

@@ -4,7 +4,8 @@
* Provides a class for listening to drag sequences on a DOM element and
* dispatching dragstart, drag and dragend events to a map object.
*
* The default behavior for the drag event is moving the map.
* Calls handleDragEvent on the map if default is not prevented by
* listeners.
*/
goog.provide('ol.handler.Drag');
@@ -101,10 +102,7 @@ ol.handler.Drag.prototype.handleDrag = function(e) {
this.prevY_ = e.clientY;
var rt = goog.events.dispatchEvent(this.map_, newE);
if (rt) {
var defaultControl = this.map_.getDefaultControl();
if (defaultControl) {
defaultControl.defaultDrag(newE);
}
this.map_.handleDragEvent(newE);
}
};

View File

@@ -4,7 +4,8 @@
* Provides a class for listening to mousewheel events on a DOM element
* and dispatching mousewheel events to a map instance.
*
* The default behavior for the mousewheel event is zooming the map.
* Calls handleMouseWheelEvent on the map if default is not prevented by
* listeners.
*/
goog.provide('ol.handler.MouseWheel');
@@ -47,9 +48,6 @@ 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) {
var defaultControl = this.map_.getDefaultControl();
if (defaultControl) {
defaultControl.defaultMouseWheel(newE);
}
this.map_.handleMouseWheelEvent(newE);
}
};
};