Allow for using a different set of default behaviors.
The default behavior of dragging and wheel zooming feels specific to 2D maps. So I think it makes sense to introduce a special type of controls (ol.control.DefaultControl) that implement default behaviors. This change also re-introduces the Navigatin control, which is a container for the default behaviors that were previously defined in the map handlers. Maybe this control needs to be renamed to Navigatin2D in the future, and there could be a different Navigation control for 3D maps.
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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.<string>}
|
||||
*/
|
||||
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.<ol.control.Control>|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();
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
50
src/ol/control/DefaultControl.js
Normal file
50
src/ol/control/DefaultControl.js
Normal file
@@ -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) {};
|
||||
54
src/ol/control/Navigation.js
Normal file
54
src/ol/control/Navigation.js
Normal file
@@ -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);
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user