Add dragging flag to MapBrowserEvent

This commit is contained in:
tsauerwein
2015-01-30 09:15:35 +01:00
parent 3abcbcf377
commit f2518e4c08
2 changed files with 49 additions and 19 deletions

View File

@@ -92,6 +92,12 @@ oli.MapBrowserEvent.prototype.originalEvent;
oli.MapBrowserEvent.prototype.pixel; oli.MapBrowserEvent.prototype.pixel;
/**
* @type {boolean}
*/
oli.MapBrowserEvent.prototype.dragging;
/** /**
* @interface * @interface

View File

@@ -30,9 +30,11 @@ goog.require('ol.pointer.PointerEventHandler');
* @param {string} type Event type. * @param {string} type Event type.
* @param {ol.Map} map Map. * @param {ol.Map} map Map.
* @param {goog.events.BrowserEvent} browserEvent Browser event. * @param {goog.events.BrowserEvent} browserEvent Browser event.
* @param {boolean=} opt_dragging Is the map currently being dragged?
* @param {?olx.FrameState=} opt_frameState Frame state. * @param {?olx.FrameState=} opt_frameState Frame state.
*/ */
ol.MapBrowserEvent = function(type, map, browserEvent, opt_frameState) { ol.MapBrowserEvent = function(type, map, browserEvent, opt_dragging,
opt_frameState) {
goog.base(this, type, map, opt_frameState); goog.base(this, type, map, opt_frameState);
@@ -61,6 +63,12 @@ ol.MapBrowserEvent = function(type, map, browserEvent, opt_frameState) {
*/ */
this.coordinate = map.getCoordinateFromPixel(this.pixel); this.coordinate = map.getCoordinateFromPixel(this.pixel);
/**
* @type {boolean}
* @api stable
*/
this.dragging = goog.isDef(opt_dragging) ? opt_dragging : false;
}; };
goog.inherits(ol.MapBrowserEvent, ol.MapEvent); goog.inherits(ol.MapBrowserEvent, ol.MapEvent);
@@ -96,11 +104,14 @@ ol.MapBrowserEvent.prototype.stopPropagation = function() {
* @param {string} type Event type. * @param {string} type Event type.
* @param {ol.Map} map Map. * @param {ol.Map} map Map.
* @param {ol.pointer.PointerEvent} pointerEvent Pointer event. * @param {ol.pointer.PointerEvent} pointerEvent Pointer event.
* @param {boolean=} opt_dragging Is the map currently being dragged?
* @param {?olx.FrameState=} opt_frameState Frame state. * @param {?olx.FrameState=} opt_frameState Frame state.
*/ */
ol.MapBrowserPointerEvent = function(type, map, pointerEvent, opt_frameState) { ol.MapBrowserPointerEvent = function(type, map, pointerEvent, opt_dragging,
opt_frameState) {
goog.base(this, type, map, pointerEvent.browserEvent, opt_frameState); goog.base(this, type, map, pointerEvent.browserEvent, opt_dragging,
opt_frameState);
/** /**
* @const * @const
@@ -139,7 +150,7 @@ ol.MapBrowserEventHandler = function(map) {
* @type {boolean} * @type {boolean}
* @private * @private
*/ */
this.dragged_ = false; this.dragging_ = false;
/** /**
* @type {Array.<number>} * @type {Array.<number>}
@@ -304,22 +315,24 @@ ol.MapBrowserEventHandler.prototype.handlePointerUp_ = function(pointerEvent) {
ol.MapBrowserEvent.EventType.POINTERUP, this.map_, pointerEvent); ol.MapBrowserEvent.EventType.POINTERUP, this.map_, pointerEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
// We emulate click events on left mouse button click, touch contact, and pen
// contact. isMouseActionButton returns true in these cases (evt.button is set
// to 0).
// See http://www.w3.org/TR/pointerevents/#button-states
if (!this.dragging_ && this.isMouseActionButton_(pointerEvent)) {
goog.asserts.assert(!goog.isNull(this.down_));
this.emulateClick_(this.down_);
}
goog.asserts.assert(this.activePointers_ >= 0); goog.asserts.assert(this.activePointers_ >= 0);
if (this.activePointers_ === 0) { if (this.activePointers_ === 0) {
goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey); goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey);
this.dragListenerKeys_ = null; this.dragListenerKeys_ = null;
this.dragging_ = false;
this.down_ = null;
goog.dispose(this.documentPointerEventHandler_); goog.dispose(this.documentPointerEventHandler_);
this.documentPointerEventHandler_ = null; this.documentPointerEventHandler_ = null;
} }
// We emulate click event on left mouse button click, touch contact, and pen
// contact. isMouseActionButton returns true in these cases (evt.button is set
// to 0).
// See http://www.w3.org/TR/pointerevents/#button-states
if (!this.dragged_ && this.isMouseActionButton_(pointerEvent)) {
goog.asserts.assert(!goog.isNull(this.down_));
this.emulateClick_(this.down_);
}
}; };
@@ -350,7 +363,6 @@ ol.MapBrowserEventHandler.prototype.handlePointerDown_ =
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
this.down_ = pointerEvent; this.down_ = pointerEvent;
this.dragged_ = false;
if (goog.isNull(this.dragListenerKeys_)) { if (goog.isNull(this.dragListenerKeys_)) {
/* Set up a pointer event handler on the `document`, /* Set up a pointer event handler on the `document`,
@@ -399,11 +411,11 @@ ol.MapBrowserEventHandler.prototype.handlePointerMove_ =
// the exact same coordinates of the pointerdown event. To avoid a // the exact same coordinates of the pointerdown event. To avoid a
// 'false' touchmove event to be dispatched , we test if the pointer // 'false' touchmove event to be dispatched , we test if the pointer
// effectively moved. // effectively moved.
if (pointerEvent.clientX != this.down_.clientX || if (this.isMoving_(pointerEvent)) {
pointerEvent.clientY != this.down_.clientY) { this.dragging_ = true;
this.dragged_ = true;
var newEvent = new ol.MapBrowserPointerEvent( var newEvent = new ol.MapBrowserPointerEvent(
ol.MapBrowserEvent.EventType.POINTERDRAG, this.map_, pointerEvent); ol.MapBrowserEvent.EventType.POINTERDRAG, this.map_, pointerEvent,
this.dragging_);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
} }
@@ -422,8 +434,20 @@ ol.MapBrowserEventHandler.prototype.handlePointerMove_ =
* @private * @private
*/ */
ol.MapBrowserEventHandler.prototype.relayEvent_ = function(pointerEvent) { ol.MapBrowserEventHandler.prototype.relayEvent_ = function(pointerEvent) {
var dragging = !goog.isNull(this.down_) && this.isMoving_(pointerEvent);
this.dispatchEvent(new ol.MapBrowserPointerEvent( this.dispatchEvent(new ol.MapBrowserPointerEvent(
pointerEvent.type, this.map_, pointerEvent)); pointerEvent.type, this.map_, pointerEvent, dragging));
};
/**
* @param {ol.pointer.PointerEvent} pointerEvent Pointer event.
* @return {boolean}
* @private
*/
ol.MapBrowserEventHandler.prototype.isMoving_ = function(pointerEvent) {
return pointerEvent.clientX != this.down_.clientX ||
pointerEvent.clientY != this.down_.clientY;
}; };