From d066ad775d0097d4ea2e367e6fe0cd05a3dad267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 16 Nov 2011 23:18:27 +0100 Subject: [PATCH] Implement Item#onClick and #onDoubleClick. --- src/ui/CanvasView.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ui/CanvasView.js b/src/ui/CanvasView.js index 367aaa98..a2d09d72 100644 --- a/src/ui/CanvasView.js +++ b/src/ui/CanvasView.js @@ -84,15 +84,30 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ } } - var overItem = null; + var downPoint, + downItem, + overItem, + doubleClick, + clickTime; return { _onMouseDown: function(event, point) { - handleEvent(this, 'mousedown', event, point); + var item = handleEvent(this, 'mousedown', event, point); + // See if we're clicking again on the same item, within the + // double-click time. Firefox uses 300ms as the max time difference: + doubleClick = downItem == item && Date.now() - clickTime < 300; + downItem = item; + downPoint = point; }, _onMouseUp: function(event, point) { - handleEvent(this, 'mouseup', event, point); + var item = handleEvent(this, 'mouseup', event, point); + if (item == downItem) { + clickTime = Date.now(); + new MouseEvent(doubleClick ? 'doubleclick' : 'click', downPoint, + overItem, event)._call(); + doubleClick = false; + } }, _onMouseMove: function(event, point) {