From 3314668a0c8e873dd68ec9928f663110648f2d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 14 Oct 2015 16:25:36 +0200 Subject: [PATCH] Streamline mouse event handling between View and Item. Consolidating code and making View#onMouseDown/Up/Move/... events work. --- src/core/Emitter.js | 2 +- src/item/Item.js | 92 ++++++++++++--------------------------------- src/view/View.js | 86 ++++++++++++++++++++++++++++++++---------- 3 files changed, 91 insertions(+), 89 deletions(-) diff --git a/src/core/Emitter.js b/src/core/Emitter.js index 3fc80423..43911bca 100644 --- a/src/core/Emitter.js +++ b/src/core/Emitter.js @@ -31,7 +31,7 @@ var Emitter = { handlers.push(func); // See if this is the first handler that we're attaching, // and call install if defined. - if (entry && entry.install && handlers.length == 1) + if (entry && entry.install && handlers.length === 1) entry.install.call(this, type); } } diff --git a/src/item/Item.js b/src/item/Item.js index ab0eb38e..2f9d2dde 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -125,79 +125,33 @@ var Item = Base.extend(Emitter, /** @lends Item# */{ return hasProps; }, - _events: new function() { - - // Flags defining which native events are required by which Paper events - // as required for counting amount of necessary natives events. - // The mapping is native -> virtual - var mouseFlags = { - mousedown: { - mousedown: 1, - mousedrag: 1, - click: 1, - doubleclick: 1 - }, - mouseup: { - mouseup: 1, - mousedrag: 1, - click: 1, - doubleclick: 1 - }, - mousemove: { - mousedrag: 1, - mousemove: 1, - mouseenter: 1, - mouseleave: 1 - } - }; - - // Entry for all mouse events in the _events list - var mouseEvent = { - install: function(type) { - // If the view requires counting of installed mouse events, - // increase the counters now according to mouseFlags - var counters = this.getView()._eventCounters; - if (counters) { - for (var key in mouseFlags) { - counters[key] = (counters[key] || 0) - + (mouseFlags[key][type] || 0); - } - } - }, - uninstall: function(type) { - // If the view requires counting of installed mouse events, - // decrease the counters now according to mouseFlags - var counters = this.getView()._eventCounters; - if (counters) { - for (var key in mouseFlags) - counters[key] -= mouseFlags[key][type] || 0; - } - } - }; - - return Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick', + _events: Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick', 'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'], - function(name) { - this[name] = mouseEvent; - }, { - onFrame: { - install: function() { - this._animateItem(true); - }, - uninstall: function() { - this._animateItem(false); - } + function(name) { + this[name] = { + install: function(type) { + this.getView()._installEvent(type); }, - // Only for external sources, e.g. Raster - onLoad: {} - } - ); - }, + uninstall: function(type) { + this.getView()._uninstallEvent(type); + } + }; + }, { + onFrame: { + install: function() { + this.getView()._animateItem(this, true); + }, - _animateItem: function(animate) { - this.getView()._animateItem(this, animate); - }, + uninstall: function() { + this.getView()._animateItem(this, false); + } + }, + + // Only for external sources, e.g. Raster + onLoad: {} + } + ), _serialize: function(options, dictionary) { var props = {}, diff --git a/src/view/View.js b/src/view/View.js index 75a16d22..c40d8769 100644 --- a/src/view/View.js +++ b/src/view/View.js @@ -149,27 +149,29 @@ var View = Base.extend(Emitter, /** @lends View# */{ return true; }, - /** - * @namespace - * @ignore - */ - _events: { - /** - * @namespace - * @ignore - */ - onFrame: { - install: function() { - this.play(); - }, + _events: Base.each(['onResize', 'onMouseDown', 'onMouseUp', 'onMouseMove'], + function(name) { + this[name] = { + install: function(type) { + this._installEvent(type); + }, - uninstall: function() { - this.pause(); + uninstall: function(type) { + this._uninstallEvent(type); + } + }; + }, { + onFrame: { + install: function() { + this.play(); + }, + + uninstall: function() { + this.pause(); + } } - }, - - onResize: {} - }, + } + ), // These are default values for event related properties on the prototype. // Writing item._count++ does not change the defaults, it creates / updates @@ -828,12 +830,58 @@ new function() { // Injection scope for mouse events on the browser load: updateFocus }); + // Flags defining which native events are required by which Paper events + // as required for counting amount of necessary natives events. + // The mapping is native -> virtual + var mouseFlags = { + mousedown: { + mousedown: 1, + mousedrag: 1, + click: 1, + doubleclick: 1 + }, + mouseup: { + mouseup: 1, + mousedrag: 1, + click: 1, + doubleclick: 1 + }, + mousemove: { + mousedrag: 1, + mousemove: 1, + mouseenter: 1, + mouseleave: 1 + } + }; + return { _viewEvents: viewEvents, // To be defined in subclasses _handleEvent: function(/* type, point, event */) {}, + _installEvent: function(type) { + // If the view requires counting of installed mouse events, + // increase the counters now according to mouseFlags + var counters = this._eventCounters; + if (counters) { + for (var key in mouseFlags) { + counters[key] = (counters[key] || 0) + + (mouseFlags[key][type] || 0); + } + } + }, + + _uninstallEvent: function(type) { + // If the view requires counting of installed mouse events, + // decrease the counters now according to mouseFlags + var counters = this._eventCounters; + if (counters) { + for (var key in mouseFlags) + counters[key] -= mouseFlags[key][type] || 0; + } + }, + statics: { /** * Loops through all views and sets the focus on the first