Emulated click events on mouse devices

The map already emits emulated `click` and `dblclick` events on touch and pointer devices. With this commit the map emits emulated `click` and `dblclick` events on mouse devices as well.
This commit is contained in:
Éric Lemoine
2013-10-29 15:52:53 +01:00
parent 68752729ab
commit 0c212fdcb5

View File

@@ -143,13 +143,13 @@ ol.MapBrowserEventHandler = function(map) {
* @type {Array.<number>} * @type {Array.<number>}
* @private * @private
*/ */
this.listenerKeys_ = null; this.dragListenerKeys_ = null;
/** /**
* @type {Array.<number>} * @type {goog.events.Key}
* @private * @private
*/ */
this.dragListenerKeys_ = null; this.mousedownListenerKey_ = null;
/** /**
* @type {Array.<number>} * @type {Array.<number>}
@@ -164,14 +164,11 @@ ol.MapBrowserEventHandler = function(map) {
this.down_ = null; this.down_ = null;
var element = this.map_.getViewport(); var element = this.map_.getViewport();
this.listenerKeys_ = [
goog.events.listen(element, this.mousedownListenerKey_ = goog.events.listen(element,
[goog.events.EventType.CLICK, goog.events.EventType.DBLCLICK], goog.events.EventType.MOUSEDOWN,
this.click_, false, this), this.handleMouseDown_, false, this);
goog.events.listen(element,
goog.events.EventType.MOUSEDOWN,
this.handleMouseDown_, false, this)
];
// touch events // touch events
this.touchListenerKeys_ = [ this.touchListenerKeys_ = [
goog.events.listen(element, [ goog.events.listen(element, [
@@ -196,19 +193,22 @@ goog.inherits(ol.MapBrowserEventHandler, goog.events.EventTarget);
* @param {goog.events.BrowserEvent} browserEvent Browser event. * @param {goog.events.BrowserEvent} browserEvent Browser event.
* @private * @private
*/ */
ol.MapBrowserEventHandler.prototype.click_ = function(browserEvent) { ol.MapBrowserEventHandler.prototype.emulateClick_ = function(browserEvent) {
if (!this.dragged_) { if (this.clickTimeoutId_ !== 0) {
var newEvent; // double-click
var type = browserEvent.type; goog.global.clearTimeout(this.clickTimeoutId_);
if (type == goog.events.EventType.DBLCLICK) { this.clickTimeoutId_ = 0;
newEvent = new ol.MapBrowserEvent( var newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DBLCLICK, this.map_, browserEvent); ol.MapBrowserEvent.EventType.DBLCLICK, this.map_, browserEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
} else { } else {
newEvent = new ol.MapBrowserEvent( // click
this.clickTimeoutId_ = goog.global.setTimeout(goog.bind(function() {
this.clickTimeoutId_ = 0;
var newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.CLICK, this.map_, browserEvent); ol.MapBrowserEvent.EventType.CLICK, this.map_, browserEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
} }, this), 250);
} }
}; };
@@ -219,13 +219,15 @@ ol.MapBrowserEventHandler.prototype.click_ = function(browserEvent) {
*/ */
ol.MapBrowserEventHandler.prototype.handleMouseUp_ = function(browserEvent) { ol.MapBrowserEventHandler.prototype.handleMouseUp_ = function(browserEvent) {
if (this.down_) { if (this.down_) {
this.down_ = null;
goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey); goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey);
this.dragListenerKeys_ = null; this.dragListenerKeys_ = null;
if (this.dragged_) { if (this.dragged_) {
var newEvent = new ol.MapBrowserEvent( var newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DRAGEND, this.map_, browserEvent); ol.MapBrowserEvent.EventType.DRAGEND, this.map_, browserEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
this.down_ = null;
} else {
this.emulateClick_(browserEvent);
} }
} }
}; };
@@ -239,18 +241,16 @@ ol.MapBrowserEventHandler.prototype.handleMouseDown_ = function(browserEvent) {
var newEvent = new ol.MapBrowserEvent( var newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DOWN, this.map_, browserEvent); ol.MapBrowserEvent.EventType.DOWN, this.map_, browserEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
if (!this.down_) { this.down_ = browserEvent;
this.down_ = browserEvent; this.dragged_ = false;
this.dragged_ = false; this.dragListenerKeys_ = [
this.dragListenerKeys_ = [ goog.events.listen(goog.global.document, goog.events.EventType.MOUSEMOVE,
goog.events.listen(goog.global.document, goog.events.EventType.MOUSEMOVE, this.handleMouseMove_, false, this),
this.handleMouseMove_, false, this), goog.events.listen(goog.global.document, goog.events.EventType.MOUSEUP,
goog.events.listen(goog.global.document, goog.events.EventType.MOUSEUP, this.handleMouseUp_, false, this)
this.handleMouseUp_, false, this) ];
]; // prevent browser image dragging with the dom renderer
// prevent browser image dragging with the dom renderer browserEvent.preventDefault();
browserEvent.preventDefault();
}
}; };
@@ -328,24 +328,7 @@ ol.MapBrowserEventHandler.prototype.handleTouchEnd_ = function(browserEvent) {
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
if (!this.dragged_) { if (!this.dragged_) {
goog.asserts.assert(!goog.isNull(this.down_)); goog.asserts.assert(!goog.isNull(this.down_));
if (this.clickTimeoutId_ !== 0) { this.emulateClick_(this.down_);
// double-click
goog.global.clearTimeout(this.clickTimeoutId_);
this.clickTimeoutId_ = 0;
newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DBLCLICK, this.map_, this.down_);
this.dispatchEvent(newEvent);
this.down_ = null;
} else {
// click
this.clickTimeoutId_ = goog.global.setTimeout(goog.bind(function() {
this.clickTimeoutId_ = 0;
newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.CLICK, this.map_, this.down_);
this.dispatchEvent(newEvent);
this.down_ = null;
}, this), 250);
}
} }
}; };
@@ -354,9 +337,9 @@ ol.MapBrowserEventHandler.prototype.handleTouchEnd_ = function(browserEvent) {
* FIXME empty description for jsdoc * FIXME empty description for jsdoc
*/ */
ol.MapBrowserEventHandler.prototype.disposeInternal = function() { ol.MapBrowserEventHandler.prototype.disposeInternal = function() {
if (!goog.isNull(this.listenerKeys_)) { if (!goog.isNull(this.mousedownListenerKey_)) {
goog.array.forEach(this.listenerKeys_, goog.events.unlistenByKey); goog.events.unlistenByKey(this.mousedownListenerKey_);
this.listenerKeys_ = null; this.mousedownListenerKey_ = null;
} }
if (!goog.isNull(this.dragListenerKeys_)) { if (!goog.isNull(this.dragListenerKeys_)) {
goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey); goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey);