From 9061daa935087084d031c00e431d7a65467b8579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 16 Nov 2011 23:41:47 +0100 Subject: [PATCH] Simplify event handling code. --- src/ui/CanvasView.js | 31 ++++++++++++++++++++++++------- src/ui/MouseEvent.js | 13 ------------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/ui/CanvasView.js b/src/ui/CanvasView.js index 227061e0..c17a4637 100644 --- a/src/ui/CanvasView.js +++ b/src/ui/CanvasView.js @@ -81,6 +81,23 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ doubleClick, clickTime; + function callEvent(type, point, target, event, bubble) { + var item = target, + mouseEvent, + called = false; + while (item) { + if (item.responds(type)) { + if (!mouseEvent) + mouseEvent = new MouseEvent(type, point, target, event); + called = item.fire(type, mouseEvent) || called; + if (called && (!bubble || mouseEvent._stopped)) + break; + } + item = item.getParent(); + } + return called; + } + function handleEvent(view, type, event, point) { if (view._eventCounters[type]) { var hit = view._project.hitTest(point, hitOptions), @@ -89,7 +106,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ // If we have a downItem with a mousedrag event, do not send // mousemove events to any item while we're dragging. if (type != 'mousemove' || !downItem) - new MouseEvent(type, point, item, event)._call(); + callEvent(type, point, item, event); return item; } } @@ -111,11 +128,11 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ // If we had a mousedrag event locking mousemove events and are over // another item, send it a mousemove event now if (hasDrag && item != downItem) - new MouseEvent('mousemove', point, item, event)._call(); + callEvent('mousemove', point, item, event); if (item == downItem) { clickTime = Date.now(); - new MouseEvent(doubleClick ? 'doubleclick' : 'click', downPoint, - overItem, event)._call(); + callEvent(doubleClick ? 'doubleclick' : 'click', downPoint, + overItem, event); doubleClick = false; } downItem = null; @@ -125,12 +142,12 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ _onMouseMove: function(event, point) { // Call the mousedrag event first if an item was clicked earlier if (downItem) - new MouseEvent('mousedrag', point, downItem, event)._call(); + callEvent('mousedrag', point, downItem, event); var item = handleEvent(this, 'mousemove', event, point); if (item != overItem) { - new MouseEvent('mouseleave', point, overItem, event)._call(); + callEvent('mouseleave', point, overItem, event); overItem = item; - new MouseEvent('mouseenter', point, item, event)._call(); + callEvent('mouseenter', point, item, event); } } }; diff --git a/src/ui/MouseEvent.js b/src/ui/MouseEvent.js index 60e50d95..dc8031e9 100644 --- a/src/ui/MouseEvent.js +++ b/src/ui/MouseEvent.js @@ -36,18 +36,5 @@ var MouseEvent = this.MouseEvent = Event.extend(/** @lends MouseEvent# */{ + ', target: ' + this.target + ', modifiers: ' + this.getModifiers() + ' }'; - }, - - // TODO: Move to Event perhaps? - _call: function(bubble) { - var item = this.target, - called = false; - while (item) { - called = item.fire(this.type, this) || called; - if (called && (!bubble || this._stopped)) - break; - item = item.getParent(); - } - return called; } });