From b3bf57c8e9471e0b47d74f4dd650bbd80eea4dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 16 Nov 2011 22:59:39 +0100 Subject: [PATCH] Implement Item#onMouseEnter / #onMouseLeave. --- src/ui/CanvasView.js | 25 ++++++++++--------------- src/ui/MouseEvent.js | 13 +++++++++++++ 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/ui/CanvasView.js b/src/ui/CanvasView.js index 04631133..367aaa98 100644 --- a/src/ui/CanvasView.js +++ b/src/ui/CanvasView.js @@ -74,28 +74,18 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ tolerance: 0 }; - function callEvent(item, event, bubble) { - var called = false; - while (item) { - called = item.fire(event.type, event) || called; - if (called && (!bubble || event._stopped)) - break; - item = item.getParent(); - } - return called; - } - function handleEvent(view, type, event, point) { if (view._eventCounters[type]) { var hit = view._project.hitTest(point, hitOptions); if (hit && hit.item) { - callEvent(hit.item, new MouseEvent(type, point, - hit.item, event), false); - return hit; + new MouseEvent(type, point, hit.item, event)._call(); + return hit.item; } } } + var overItem = null; + return { _onMouseDown: function(event, point) { handleEvent(this, 'mousedown', event, point); @@ -106,7 +96,12 @@ var CanvasView = View.extend(/** @lends CanvasView# */{ }, _onMouseMove: function(event, point) { - handleEvent(this, 'mousemove', event, point); + var item = handleEvent(this, 'mousemove', event, point); + if (item != overItem) { + new MouseEvent('mouseleave', point, overItem, event)._call(); + overItem = item; + new MouseEvent('mouseenter', point, item, event)._call(); + } } }; }); diff --git a/src/ui/MouseEvent.js b/src/ui/MouseEvent.js index dc8031e9..60e50d95 100644 --- a/src/ui/MouseEvent.js +++ b/src/ui/MouseEvent.js @@ -36,5 +36,18 @@ 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; } });