More MapBrowserEvent improvements

Now we use the browser's native dblclick event on pointer
devices, and we fire dragstart and dragend only if we're really
dragging, and not on mousedown/touchstart and mouseup/touchend.
This commit is contained in:
ahocevar
2012-09-29 18:54:29 +02:00
parent 877971185f
commit 69859b415b
+25 -23
View File
@@ -109,6 +109,8 @@ ol.MapBrowserEventHandler = function(map) {
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} * @type {number}
* @private * @private
*/ */
@@ -126,16 +128,11 @@ ol.MapBrowserEventHandler = function(map) {
*/ */
this.down_ = null; this.down_ = null;
/**
* @type {boolean}
* @private
*/
this.isDblClick_ = false;
var element = this.map_.getViewport(); var element = this.map_.getViewport();
if (!ol.BrowserFeature.HAS_TOUCH) { if (!ol.BrowserFeature.HAS_TOUCH) {
goog.events.listen(element, goog.events.listen(element,
goog.events.EventType.CLICK, this.click_, false, this); [goog.events.EventType.CLICK, goog.events.EventType.DBLCLICK],
this.click_, false, this);
} }
goog.events.listen(element, goog.events.listen(element,
ol.BrowserFeature.HAS_TOUCH ? ol.BrowserFeature.HAS_TOUCH ?
@@ -170,12 +167,13 @@ ol.MapBrowserEventHandler.prototype.touchEnableBrowserEvent_ =
*/ */
ol.MapBrowserEventHandler.prototype.click_ = function(browserEvent) { ol.MapBrowserEventHandler.prototype.click_ = function(browserEvent) {
if (!this.dragged_) { if (!this.dragged_) {
this.touchEnableBrowserEvent_(browserEvent); var newEvent;
var newEvent = new ol.MapBrowserEvent( if (browserEvent.type !== goog.events.EventType.DBLCLICK) {
ol.MapBrowserEvent.EventType.CLICK, this.map_, browserEvent); newEvent = new ol.MapBrowserEvent(
this.dispatchEvent(newEvent); ol.MapBrowserEvent.EventType.CLICK, this.map_, browserEvent);
if (this.isDblClick_) { this.dispatchEvent(newEvent);
this.isDblClick_ = false; }
if (!this.timestamp_) {
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);
@@ -196,7 +194,6 @@ ol.MapBrowserEventHandler.prototype.handleUp_ = function(browserEvent) {
this.timestamp_ = now; this.timestamp_ = now;
} else { } else {
this.timestamp_ = 0; this.timestamp_ = 0;
this.isDblClick_ = true;
} }
if (ol.BrowserFeature.HAS_TOUCH) { if (ol.BrowserFeature.HAS_TOUCH) {
this.click_(this.down_); this.click_(this.down_);
@@ -206,9 +203,11 @@ ol.MapBrowserEventHandler.prototype.handleUp_ = function(browserEvent) {
goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey); goog.array.forEach(this.dragListenerKeys_, goog.events.unlistenByKey);
this.dragListenerKeys_ = null; this.dragListenerKeys_ = null;
this.previous_ = null; this.previous_ = null;
var newEvent = new ol.MapBrowserEvent( if (this.dragged_) {
ol.MapBrowserEvent.EventType.DRAGEND, this.map_, browserEvent); var newEvent = new ol.MapBrowserEvent(
this.dispatchEvent(newEvent); ol.MapBrowserEvent.EventType.DRAGEND, this.map_, browserEvent);
this.dispatchEvent(newEvent);
}
} }
}; };
@@ -219,8 +218,8 @@ ol.MapBrowserEventHandler.prototype.handleUp_ = function(browserEvent) {
*/ */
ol.MapBrowserEventHandler.prototype.handleDown_ = function(browserEvent) { ol.MapBrowserEventHandler.prototype.handleDown_ = function(browserEvent) {
if (!this.previous_) { if (!this.previous_) {
this.down_ = browserEvent;
this.touchEnableBrowserEvent_(browserEvent); this.touchEnableBrowserEvent_(browserEvent);
this.down_ = browserEvent;
this.previous_ = { this.previous_ = {
clientX: browserEvent.clientX, clientX: browserEvent.clientX,
clientY: browserEvent.clientY clientY: browserEvent.clientY
@@ -242,9 +241,6 @@ ol.MapBrowserEventHandler.prototype.handleDown_ = function(browserEvent) {
// prevent browser image dragging on pointer devices // prevent browser image dragging on pointer devices
browserEvent.preventDefault(); browserEvent.preventDefault();
} }
var newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DRAGSTART, this.map_, browserEvent);
this.dispatchEvent(newEvent);
} }
}; };
@@ -254,7 +250,13 @@ ol.MapBrowserEventHandler.prototype.handleDown_ = function(browserEvent) {
* @private * @private
*/ */
ol.MapBrowserEventHandler.prototype.drag_ = function(browserEvent) { ol.MapBrowserEventHandler.prototype.drag_ = function(browserEvent) {
this.dragged_ = true; var newEvent;
if (!this.dragged_) {
this.dragged_ = true;
newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DRAGSTART, this.map_, this.down_);
this.dispatchEvent(newEvent);
}
this.touchEnableBrowserEvent_(browserEvent); this.touchEnableBrowserEvent_(browserEvent);
this.previous_ = { this.previous_ = {
clientX: browserEvent.clientX, clientX: browserEvent.clientX,
@@ -262,7 +264,7 @@ ol.MapBrowserEventHandler.prototype.drag_ = function(browserEvent) {
}; };
// prevent viewport dragging on touch devices // prevent viewport dragging on touch devices
browserEvent.preventDefault(); browserEvent.preventDefault();
var newEvent = new ol.MapBrowserEvent( newEvent = new ol.MapBrowserEvent(
ol.MapBrowserEvent.EventType.DRAG, this.map_, browserEvent); ol.MapBrowserEvent.EventType.DRAG, this.map_, browserEvent);
this.dispatchEvent(newEvent); this.dispatchEvent(newEvent);
}; };