From 28f8367f1163330c6424b3aeecb38e1978cc04d9 Mon Sep 17 00:00:00 2001 From: jos Date: Fri, 17 Apr 2015 14:21:50 +0200 Subject: [PATCH] Fixed the touch and release events being fired twice each time. Fixed a bug with dragging in the InteractionHandler --- lib/hammerUtil.js | 22 ++++++++++++++++++++-- lib/network/modules/InteractionHandler.js | 4 ++-- lib/timeline/Core.js | 5 +++++ lib/timeline/component/ItemSet.js | 2 +- test/timeline.html | 8 ++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/hammerUtil.js b/lib/hammerUtil.js index 5aae601e..66f9dfb1 100644 --- a/lib/hammerUtil.js +++ b/lib/hammerUtil.js @@ -7,14 +7,23 @@ var Hammer = require('./module/hammer'); */ exports.onTouch = function (hammer, callback) { callback.inputHandler = function (event) { - if (event.isFirst) { + if (event.isFirst && !isTouching) { callback(event); + + isTouching = true; + setTimeout(function () { + isTouching = false; + }, 0); } }; hammer.on('hammer.input', callback.inputHandler); }; +// isTouching is true while a touch action is being emitted +// this is a hack to prevent `touch` from being fired twice +var isTouching = false; + /** * Register a release event, taking place after a gesture * @param {Hammer} hammer A hammer instance @@ -22,13 +31,22 @@ exports.onTouch = function (hammer, callback) { */ exports.onRelease = function (hammer, callback) { callback.inputHandler = function (event) { - if (event.isFinal) { + if (event.isFinal && !isReleasing) { callback(event); + + isReleasing = true; + setTimeout(function () { + isReleasing = false; + }, 0); } }; return hammer.on('hammer.input', callback.inputHandler); }; +// isReleasing is true while a release action is being emitted +// this is a hack to prevent `release` from being fired twice +var isReleasing = false; + /** * Unregister a touch event, taking place before a gesture diff --git a/lib/network/modules/InteractionHandler.js b/lib/network/modules/InteractionHandler.js index d6432c55..ffb6658e 100644 --- a/lib/network/modules/InteractionHandler.js +++ b/lib/network/modules/InteractionHandler.js @@ -217,7 +217,7 @@ class InteractionHandler { this.drag.translation = util.extend({},this.body.view.translation); // copy the object this.drag.nodeId = undefined; - this.selectionHandler._generateClickEvent('dragStart',pointer); + this.selectionHandler._generateClickEvent('dragStart', this.drag.pointer); if (node !== undefined && this.options.dragNodes === true) { this.drag.nodeId = node.id; @@ -324,7 +324,7 @@ class InteractionHandler { else { this.body.emitter.emit('_requestRedraw'); } - this.selectionHandler._generateClickEvent('dragEnd',pointer); + this.selectionHandler._generateClickEvent('dragEnd',this.drag.pointer); } diff --git a/lib/timeline/Core.js b/lib/timeline/Core.js index 5e4085b0..ddd96f1e 100644 --- a/lib/timeline/Core.js +++ b/lib/timeline/Core.js @@ -140,6 +140,11 @@ Core.prototype._create = function (container) { me.emit('touch', event); }.bind(this)); + // emulate a release event (emitted after a pan, pinch, tap, or press) + hammerUtil.onRelease(this.hammer, function (event) { + me.emit('release', event); + }.bind(this)); + function onMouseWheel(event) { if (me.isActive()) { me.emit('mousewheel', event); diff --git a/lib/timeline/component/ItemSet.js b/lib/timeline/component/ItemSet.js index dbe0fa1d..104c8057 100644 --- a/lib/timeline/component/ItemSet.js +++ b/lib/timeline/component/ItemSet.js @@ -1546,7 +1546,7 @@ ItemSet.prototype.groupFromTarget = function(event) { //} // - var clientY = event.gesture.center.clientY; + var clientY = event.center.clientY; for (var i = 0; i < this.groupIds.length; i++) { var groupId = this.groupIds[i]; var group = this.groups[groupId]; diff --git a/test/timeline.html b/test/timeline.html index 6baa872d..595ffac7 100644 --- a/test/timeline.html +++ b/test/timeline.html @@ -151,6 +151,14 @@ console.log('select', selection); }); +// timeline.on('touch', function (event) { +// console.log('touch', event, Object.keys(event)); +// }); +// +// timeline.on('release', function (event) { +// console.log('release', event, Object.keys(event)); +// }); + /* timeline.on('rangechange', function (range) { console.log('rangechange', range);