From 8b0340e6df29c77775bb1ccffee84e27f50ab765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 13 Jan 2016 17:53:39 +0100 Subject: [PATCH] Have View#_handleEvent() also return true if event is handled. And use it to call preventDefault() if either tool or view handle events. --- src/dom/DomEvent.js | 5 ----- src/tool/Tool.js | 9 ++++++--- src/view/View.js | 49 ++++++++++++++++++++++++++++++--------------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/dom/DomEvent.js b/src/dom/DomEvent.js index 4de38bb0..8352e721 100644 --- a/src/dom/DomEvent.js +++ b/src/dom/DomEvent.js @@ -58,11 +58,6 @@ var DomEvent = /** @lends DomEvent */{ // Remove target offsets from page coordinates return DomEvent.getPoint(event).subtract(DomElement.getOffset( target || DomEvent.getTarget(event))); - }, - - stop: function(event) { - event.stopPropagation(); - event.preventDefault(); } }; diff --git a/src/tool/Tool.js b/src/tool/Tool.js index 4bb7cf26..8a2ed36c 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -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) { // Update global reference to 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; } /** diff --git a/src/view/View.js b/src/view/View.js index 8f2ab672..27c83fd6 100644 --- a/src/view/View.js +++ b/src/view/View.js @@ -732,10 +732,11 @@ new function() { // Injection scope for mouse events on the browser function handleEvent(type, view, event, point) { var eventType = type === 'mousemove' && mouseDown ? 'mousedrag' : type, project = paper.project, - tool = view._scope.tool; + tool = view._scope.tool, + called = false; function handle(obj) { - obj._handleEvent(eventType, event, point); + called = obj._handleEvent(eventType, event, point) || called; } if (!point) @@ -749,6 +750,10 @@ new function() { // Injection scope for mouse events on the browser handle(view); if (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 // anything has changed in the above calls. view.update(); @@ -872,7 +877,14 @@ new function() { // Injection scope for mouse events on the browser * 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, downItem, overItem, @@ -880,13 +892,8 @@ new function() { // Injection scope for mouse events on the browser clickItem, clickTime, dblClick, - 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' - }; + overView; + // Returns true if event was stopped, false otherwise, whether handler was // called or not! @@ -904,9 +911,12 @@ new function() { // Injection scope for mouse events on the browser // Calculate delta if prevPoint was passed prevPoint ? point.subtract(prevPoint) : null); } - // Bail out if propagation is stopped - if (obj.emit(type, mouseEvent) && mouseEvent.stopped) - return true; + if (obj.emit(type, mouseEvent)) { + called = true; + // Bail out if propagation is stopped + if (mouseEvent.stopped) + return true; + } } else { var fallback = fallbacks[type]; if (fallback) @@ -923,7 +933,12 @@ new function() { // Injection scope for mouse events on the browser return false; } + // Returns true if event was stopped, false otherwise, whether handler was + // called or not! 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. return (dragItem && emitEvent(dragItem, type, event, point, prevPoint) @@ -968,8 +983,10 @@ new function() { // Injection scope for mouse events on the browser _viewEvents: viewEvents, /** - * Returns true if event was stopped, false otherwise, whether handler - * was called or not! + * Private method to handle view and item events. + * + * @return true if at least one event handler was called, false + * otherwise. */ _handleEvent: function(type, event, point) { // Run the hit-test first @@ -1038,7 +1055,7 @@ new function() { // Injection scope for mouse events on the browser } } lastPoint = point; - return stopped; + return called; }, _installEvent: function(type) {