Have View#_handleEvent() also return true if event is handled.

And use it to call preventDefault() if either tool or view handle events.
This commit is contained in:
Jürg Lehni 2016-01-13 17:53:39 +01:00
parent 6d768f559a
commit 8b0340e6df
3 changed files with 39 additions and 24 deletions

View file

@ -58,11 +58,6 @@ var DomEvent = /** @lends DomEvent */{
// Remove target offsets from page coordinates // Remove target offsets from page coordinates
return DomEvent.getPoint(event).subtract(DomElement.getOffset( return DomEvent.getPoint(event).subtract(DomElement.getOffset(
target || DomEvent.getTarget(event))); target || DomEvent.getTarget(event)));
},
stop: function(event) {
event.stopPropagation();
event.preventDefault();
} }
}; };

View file

@ -278,6 +278,12 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* } * }
*/ */
/**
* Private method to handle tool-events.
*
* @return true if at least one event handler was called, false otherwise.
*/
_handleEvent: function(type, event, point) { _handleEvent: function(type, event, point) {
// Update global reference to this scope. // Update global reference to this scope.
paper = this._scope; paper = this._scope;
@ -372,9 +378,6 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
} }
} }
} }
// Prevent default if mouse event was handled.
if (called)
event.preventDefault();
return called; return called;
} }
/** /**

View file

@ -732,10 +732,11 @@ new function() { // Injection scope for mouse events on the browser
function handleEvent(type, view, event, point) { function handleEvent(type, view, event, point) {
var eventType = type === 'mousemove' && mouseDown ? 'mousedrag' : type, var eventType = type === 'mousemove' && mouseDown ? 'mousedrag' : type,
project = paper.project, project = paper.project,
tool = view._scope.tool; tool = view._scope.tool,
called = false;
function handle(obj) { function handle(obj) {
obj._handleEvent(eventType, event, point); called = obj._handleEvent(eventType, event, point) || called;
} }
if (!point) if (!point)
@ -749,6 +750,10 @@ new function() { // Injection scope for mouse events on the browser
handle(view); handle(view);
if (tool) if (tool)
handle(tool); handle(tool);
// Prevent default if at least one mouse event handler was called, to
// prevent scrolling on touch devices.
if (called)
event.preventDefault();
// In the end we always call update(), which only updates the view if // In the end we always call update(), which only updates the view if
// anything has changed in the above calls. // anything has changed in the above calls.
view.update(); view.update();
@ -872,7 +877,14 @@ new function() { // Injection scope for mouse events on the browser
* with support for bubbling (event-propagation). * with support for bubbling (event-propagation).
*/ */
var downPoint, var called = false, // Keep track of whether at least one handler was called
// Event fallbacks for "virutal" events, e.g. if an item doesn't respond
// to doubleclick, fall back to click:
fallbacks = {
doubleclick: 'click',
mousedrag: 'mousemove'
},
downPoint,
lastPoint, lastPoint,
downItem, downItem,
overItem, overItem,
@ -880,13 +892,8 @@ new function() { // Injection scope for mouse events on the browser
clickItem, clickItem,
clickTime, clickTime,
dblClick, dblClick,
overView, overView;
// Event fallbacks for "virutal" events, e.g. if an item doesn't respond
// to doubleclick, fall back to click:
fallbacks = {
doubleclick: 'click',
mousedrag: 'mousemove'
};
// Returns true if event was stopped, false otherwise, whether handler was // Returns true if event was stopped, false otherwise, whether handler was
// called or not! // called or not!
@ -904,9 +911,12 @@ new function() { // Injection scope for mouse events on the browser
// Calculate delta if prevPoint was passed // Calculate delta if prevPoint was passed
prevPoint ? point.subtract(prevPoint) : null); prevPoint ? point.subtract(prevPoint) : null);
} }
if (obj.emit(type, mouseEvent)) {
called = true;
// Bail out if propagation is stopped // Bail out if propagation is stopped
if (obj.emit(type, mouseEvent) && mouseEvent.stopped) if (mouseEvent.stopped)
return true; return true;
}
} else { } else {
var fallback = fallbacks[type]; var fallback = fallbacks[type];
if (fallback) if (fallback)
@ -923,7 +933,12 @@ new function() { // Injection scope for mouse events on the browser
return false; return false;
} }
// Returns true if event was stopped, false otherwise, whether handler was
// called or not!
function emitEvents(view, item, type, event, point, prevPoint) { function emitEvents(view, item, type, event, point, prevPoint) {
// Set called to false, so it will reflect if the following calls to
// emitEvent() have at least called one handler.
called = false;
// First handle the drag-item and its parents, through bubbling. // First handle the drag-item and its parents, through bubbling.
return (dragItem && emitEvent(dragItem, type, event, point, return (dragItem && emitEvent(dragItem, type, event, point,
prevPoint) prevPoint)
@ -968,8 +983,10 @@ new function() { // Injection scope for mouse events on the browser
_viewEvents: viewEvents, _viewEvents: viewEvents,
/** /**
* Returns true if event was stopped, false otherwise, whether handler * Private method to handle view and item events.
* was called or not! *
* @return true if at least one event handler was called, false
* otherwise.
*/ */
_handleEvent: function(type, event, point) { _handleEvent: function(type, event, point) {
// Run the hit-test first // Run the hit-test first
@ -1038,7 +1055,7 @@ new function() { // Injection scope for mouse events on the browser
} }
} }
lastPoint = point; lastPoint = point;
return stopped; return called;
}, },
_installEvent: function(type) { _installEvent: function(type) {