Remove MouseEvent#target hitTest() getter magic again.

Relates to #995
This commit is contained in:
Jürg Lehni 2016-06-17 00:42:40 +02:00
parent 74b22266f7
commit a03631f620
2 changed files with 15 additions and 52 deletions

View file

@ -30,7 +30,7 @@ var MouseEvent = Event.extend(/** @lends MouseEvent# */{
this.type = type; this.type = type;
this.event = event; this.event = event;
this.point = point; this.point = point;
this._target = target; this.target = target;
this.delta = delta; this.delta = delta;
}, },
@ -51,20 +51,6 @@ var MouseEvent = Event.extend(/** @lends MouseEvent# */{
* @type Point * @type Point
*/ */
// DOCS: document MouseEvent#target
/**
* @name MouseEvent#target
* @type Item
*/
getTarget: function() {
// #_target may be a hitTest() function, in which case we need to
// execute and override it the first time #target is requested.
var target = this._target;
if (typeof target === 'function')
target = this._target = target();
return target;
},
// DOCS: document MouseEvent#delta // DOCS: document MouseEvent#delta
/** /**
* @name MouseEvent#delta * @name MouseEvent#delta
@ -77,7 +63,7 @@ var MouseEvent = Event.extend(/** @lends MouseEvent# */{
toString: function() { toString: function() {
return "{ type: '" + this.type return "{ type: '" + this.type
+ "', point: " + this.point + "', point: " + this.point
+ ', target: ' + this.getTarget() + ', target: ' + this.target
+ (this.delta ? ', delta: ' + this.delta : '') + (this.delta ? ', delta: ' + this.delta : '')
+ ', modifiers: ' + this.getModifiers() + ', modifiers: ' + this.getModifiers()
+ ' }'; + ' }';

View file

@ -1196,8 +1196,7 @@ new function() { // Injection scope for event handling on the browser
} }
// Returns true if event was stopped, false otherwise. // Returns true if event was stopped, false otherwise.
function emitMouseEvents(view, hitItem, hitTest, type, event, point, function emitMouseEvents(view, hitItem, type, event, point, prevPoint) {
prevPoint) {
// Before handling events, process removeOn() calls for cleanup. // Before handling events, process removeOn() calls for cleanup.
// NOTE: As soon as there is one event handler receiving mousedrag // NOTE: As soon as there is one event handler receiving mousedrag
// events, non of the removeOnMove() items will be removed while the // events, non of the removeOnMove() items will be removed while the
@ -1219,9 +1218,7 @@ new function() { // Injection scope for event handling on the browser
&& emitMouseEvent(hitItem, null, fallbacks[type] || type, event, && emitMouseEvent(hitItem, null, fallbacks[type] || type, event,
point, prevPoint, dragItem) point, prevPoint, dragItem)
// Lastly handle the mouse events on the view, if we're still here. // Lastly handle the mouse events on the view, if we're still here.
// Choose from the potential targets in the right sequence, with the || emitMouseEvent(view, dragItem || hitItem || view, type, event,
// hitTest() function as the fall-back getter for MouseEvent#target.
|| emitMouseEvent(view, dragItem || hitItem || hitTest, type, event,
point, prevPoint)); point, prevPoint));
} }
@ -1299,7 +1296,12 @@ new function() { // Injection scope for event handling on the browser
// Run the hit-test on items first, but only if we're required to do // Run the hit-test on items first, but only if we're required to do
// so for this given mouse event, see hitItems, #_countItemEvent(): // so for this given mouse event, see hitItems, #_countItemEvent():
var inView = this.getBounds().contains(point), var inView = this.getBounds().contains(point),
hitItem, hit = inView && view._project.hitTest(point, {
tolerance: 0,
fill: true,
stroke: true
});
hitItem = hit && hit.item || null,
// Keep track if view event should be handled, so we can use it // Keep track if view event should be handled, so we can use it
// to decide if tool._handleMouseEvent() shall be called after. // to decide if tool._handleMouseEvent() shall be called after.
handle = false, handle = false,
@ -1308,28 +1310,6 @@ new function() { // Injection scope for event handling on the browser
// mouse event types. // mouse event types.
mouse[type.substr(5)] = true; mouse[type.substr(5)] = true;
// Provide a hit-test function that makes sure to only perform the
// hit-test once, and only when it's actually required. This method
// is passed to emitMouseEvents() and as target to emitMouseEvent(),
// as the fall-back getter for MouseEvent#target.
function hitTest() {
//
if (hitItem === undefined) {
var hit = inView && view._project.hitTest(point, {
tolerance: 0,
fill: true,
stroke: true
});
hitItem = hit && hit.item || null;
}
// Return the target with view as the fall-back, as expected by
// MouseEvent#target.
return hitItem || view;
}
// Execute hitTest right away if we have events relying on hitItem.
if (hitItems)
hitTest();
// Handle mouseenter / leave between items and views first. // Handle mouseenter / leave between items and views first.
if (hitItems && hitItem !== overItem) { if (hitItems && hitItem !== overItem) {
if (overItem) { if (overItem) {
@ -1355,9 +1335,8 @@ new function() { // Injection scope for event handling on the browser
if ((inView || mouse.drag) && !point.equals(lastPoint)) { if ((inView || mouse.drag) && !point.equals(lastPoint)) {
// Handle mousemove even if this is not actually a mousemove // Handle mousemove even if this is not actually a mousemove
// event but the mouse has moved since the last event. // event but the mouse has moved since the last event.
emitMouseEvents(this, hitItem, hitTest, emitMouseEvents(this, hitItem, nativeMove ? type : 'mousemove',
nativeMove ? type : 'mousemove', event, event, point, lastPoint);
point, lastPoint);
handle = true; handle = true;
} }
wasInView = inView; wasInView = inView;
@ -1365,8 +1344,7 @@ new function() { // Injection scope for event handling on the browser
// We emit mousedown only when in the view, and mouseup regardless, // We emit mousedown only when in the view, and mouseup regardless,
// as long as the mousedown event was inside. // as long as the mousedown event was inside.
if (mouse.down && inView || mouse.up && downPoint) { if (mouse.down && inView || mouse.up && downPoint) {
emitMouseEvents(this, hitItem, hitTest, type, event, emitMouseEvents(this, hitItem, type, event, point, downPoint);
point, downPoint);
if (mouse.down) { if (mouse.down) {
// See if we're clicking again on the same item, within the // See if we're clicking again on the same item, within the
// double-click time. Firefox uses 300ms as the max time // double-click time. Firefox uses 300ms as the max time
@ -1383,9 +1361,8 @@ new function() { // Injection scope for event handling on the browser
// not the view. // not the view.
if (!prevented && hitItem === downItem) { if (!prevented && hitItem === downItem) {
clickTime = Date.now(); clickTime = Date.now();
emitMouseEvents(this, hitItem, hitTest, emitMouseEvents(this, hitItem, dblClick ? 'doubleclick'
dblClick ? 'doubleclick' : 'click', event, : 'click', event, point, downPoint);
point, downPoint);
dblClick = false; dblClick = false;
} }
downItem = dragItem = null; downItem = dragItem = null;