Emulate click and dblclick on touch devices

This commit is contained in:
Éric Lemoine
2013-10-16 18:20:52 +02:00
parent fe3440a83d
commit 70eb5a5f13
+25 -17
View File
@@ -3,6 +3,7 @@ goog.provide('ol.MapBrowserEvent.EventType');
goog.provide('ol.MapBrowserEventHandler'); goog.provide('ol.MapBrowserEventHandler');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.asserts');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.events.BrowserEvent'); goog.require('goog.events.BrowserEvent');
goog.require('goog.events.EventTarget'); goog.require('goog.events.EventTarget');
@@ -121,20 +122,18 @@ ol.MapBrowserEventHandler = function(map) {
*/ */
this.map_ = map; this.map_ = map;
/**
* @type {number}
* @private
*/
this.clickTimeoutId_ = 0;
/** /**
* @type {boolean} * @type {boolean}
* @private * @private
*/ */
this.dragged_ = false; this.dragged_ = false;
/**
* Timestamp for the first click of a double click. Will be set back to 0
* as soon as a double click is detected.
* @type {?number}
* @private
*/
this.timestamp_ = null;
/** /**
* @type {Array.<number>} * @type {Array.<number>}
* @private * @private
@@ -199,7 +198,7 @@ ol.MapBrowserEventHandler.prototype.click_ = function(browserEvent) {
if (!this.dragged_) { if (!this.dragged_) {
var newEvent; var newEvent;
var type = browserEvent.type; var type = browserEvent.type;
if (this.timestamp_ === 0 || type == goog.events.EventType.DBLCLICK) { if (type == goog.events.EventType.DBLCLICK) {
newEvent = new ol.MapBrowserEvent( newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DBLCLICK, this.map_, browserEvent); ol.MapBrowserEvent.EventType.DBLCLICK, this.map_, browserEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
@@ -326,17 +325,26 @@ ol.MapBrowserEventHandler.prototype.handleTouchEnd_ = function(browserEvent) {
ol.MapBrowserEvent.EventType.TOUCHEND, this.map_, browserEvent); ol.MapBrowserEvent.EventType.TOUCHEND, this.map_, browserEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
if (!this.dragged_) { if (!this.dragged_) {
var now = goog.now(); goog.asserts.assert(!goog.isNull(this.down_));
if (!this.timestamp_ || now - this.timestamp_ > 250) { if (this.clickTimeoutId_ !== 0) {
this.timestamp_ = now; // 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 { } else {
this.timestamp_ = 0; // click
} this.clickTimeoutId_ = goog.global.setTimeout(goog.bind(function() {
if (!goog.isNull(this.down_)) { this.clickTimeoutId_ = 0;
this.click_(this.down_); newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.CLICK, this.map_, this.down_);
this.dispatchEvent(newEvent);
this.down_ = null;
}, this), 250);
} }
} }
this.down_ = null;
}; };