From e238dea3ac06866c20017e7e2d2bedffa0bb03bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 11 Nov 2011 19:49:31 +0100 Subject: [PATCH] More work to make new events mechanisms work for Tool too. --- src/core/Callback.js | 68 +++++++++++++++++++++++++---------------- src/core/PaperScript.js | 7 ++--- src/tool/Tool.js | 36 +++++++++------------- src/ui/View.js | 6 ++-- 4 files changed, 61 insertions(+), 56 deletions(-) diff --git a/src/core/Callback.js b/src/core/Callback.js index 0ae85796..e0cacf35 100644 --- a/src/core/Callback.js +++ b/src/core/Callback.js @@ -16,14 +16,15 @@ var Callback = { attach: function(type, func) { - var entry = this._events[type]; // If an object literal is passed, attach all callbacks defined in it - if (!entry) { + if (typeof type !== 'string') { return Base.each(type, function(value, key) { this.attach(key, value); }, this); } - // Otherwise, attach the event now + var entry = this._eventTypes[type]; + if (!entry) + return this; var handlers = this._handlers = this._handlers || {}; handlers = handlers[type] = handlers[type] || []; if (handlers.indexOf(func) == -1) { // Not added yet, add it now @@ -37,20 +38,23 @@ var Callback = { }, detach: function(type, func) { - var entry = this._events[type]; // If an object literal is passed, detach all callbacks defined in it - if (!entry) { + if (typeof type !== 'string') { return Base.each(type, function(value, key) { this.detach(key, value); }, this); } - // Otherwise, detach the event now - var handlers = this._handlers && this._handlers[type], - index = handlers && handlers.indexOf(func) || -1; - if (index != -1) { - handlers.splice(index, 1); - // See if this is the last handler that we're detaching, and - // call uninstall if defined. + var entry = this._eventTypes[type], + handlers = this._handlers && this._handlers[type], + index; + if (entry && handlers) { + if (!func) { // Remove all + handlers = []; + } else if ((index = handlers.indexOf(func)) != -1) { + handlers.splice(index, 1); + } + // See if this is the last handler that we're detaching, and call + // uninstall if defined. if (!handlers.length) { delete this._handlers[type]; if (entry.uninstall) @@ -60,14 +64,6 @@ var Callback = { return this; }, - detachAll: function(type) { - return Base.each(this._handlers && this._handlers[type] || [], - function(func) { - this.detach(type, func); - }, - this); - }, - fire: function(type, param) { // Returns true if fired, false otherwise var handlers = this._handlers && this._handlers[type]; @@ -79,26 +75,44 @@ var Callback = { return true; }, + responds: function(type) { + return this._handlers && this._handlers[type]; + }, + statics: { inject: function(/* src, ... */) { for (var i = 0, l = arguments.length; i < l; i++) { var src = arguments[i], events = src._events; if (events) { - Base.each(events, function(entry, type) { - var part = Base.capitalize(type); - src['getOn' + part] = function() { - return this['_on' + part]; + // events can either be an object literal or an array of + // strings describing the on*-names. + // We need to map lowercased event types to the event + // entries represented by these on*-names in _events. + var types = {}; + Base.each(events, function(entry, key) { + var isString = typeof entry === 'string', + name = isString ? entry : key, + part = Base.capitalize(name), + type = name.substring(2).toLowerCase(); + // Map the event type name to the event entry. + types[type] = isString ? {} : entry; + // Create getters and setters for the property + // with the on*-name name: + name = '_' + name; + src['get' + part] = function() { + return this[name]; }; - src['setOn' + part] = function(func) { + src['set' + part] = function(func) { if (func) { this.attach(type, func); } else { - this.detach(type, this['_on' + part]); + this.detach(type, this[name]); } - this['_on' + part] = func; + this[name] = func; }; }); + src._eventTypes = types; } this.base(src); } diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 44c47d5b..25f77bce 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -169,10 +169,7 @@ var PaperScript = this.PaperScript = new function() { (function() { var onEditOptions, onSelect, onDeselect, onReselect, onMouseDown, onMouseUp, onMouseDrag, onMouseMove, onKeyDown, onKeyUp, - onFrame, onResize, - handlers = [ 'onEditOptions', 'onSelect', 'onDeselect', - 'onReselect', 'onMouseDown', 'onMouseUp', 'onMouseDrag', - 'onMouseMove', 'onKeyDown', 'onKeyUp']; + onFrame, onResize; res = eval(compile(code)); if (tool) { // We could do this instead to avoid eval(), but it's longer @@ -186,7 +183,7 @@ var PaperScript = this.PaperScript = new function() { // tool.onMouseMove = onMouseMove; // tool.onKeyDown = onKeyDown; // tool.onKeyUp = onKeyUp; - Base.each(handlers, function(key) { + Base.each(tool._events, function(key) { tool[key] = eval(key); }); } diff --git a/src/tool/Tool.js b/src/tool/Tool.js index 73b27a06..cfb673b4 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -46,9 +46,12 @@ * path.add(event.point); * } */ -var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ +var Tool = this.Tool = PaperScopeItem.extend(Callback, /** @lends Tool# */{ _list: 'tools', _reference: 'tool', + _events: [ 'onEditOptions', 'onSelect', 'onDeselect', 'onReselect', + 'onMouseDown', 'onMouseUp', 'onMouseDrag', 'onMouseMove', + 'onKeyDown', 'onKeyUp' ], // DOCS: rewrite Tool constructor explanation initialize: function() { @@ -329,10 +332,8 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ switch (type) { case 'mousedown': this.updateEvent(type, pt, null, null, true, false, false); - if (this.onMouseDown) { - this.onMouseDown(new ToolEvent(this, type, event)); - called = true; - } + if (this.responds(type)) + called = this.fire(type, new ToolEvent(this, type, event)); break; case 'mousedrag': // In order for idleInterval drag events to work, we need to not @@ -347,10 +348,8 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ matchMaxDistance = false; while (this.updateEvent(type, pt, this.minDistance, this.maxDistance, false, needsChange, matchMaxDistance)) { - if (this.onMouseDrag) { - this.onMouseDrag(new ToolEvent(this, type, event)); - called = true; - } + if (this.responds(type)) + called = this.fire(type, new ToolEvent(this, type, event)); needsChange = true; matchMaxDistance = true; } @@ -361,17 +360,14 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ if ((this._point.x != pt.x || this._point.y != pt.y) && this.updateEvent('mousedrag', pt, this.minDistance, this.maxDistance, false, false, false)) { - if (this.onMouseDrag) { - this.onMouseDrag(new ToolEvent(this, type, event)); - called = true; - } + if (this.responds('mousedrag')) + called = this.fire('mousedrag', + new ToolEvent(this, type, event)); } this.updateEvent(type, pt, null, this.maxDistance, false, false, false); - if (this.onMouseUp) { - this.onMouseUp(new ToolEvent(this, type, event)); - called = true; - } + if (this.responds(type)) + called = this.fire(type, new ToolEvent(this, type, event)); // Start with new values for 'mousemove' this.updateEvent(type, pt, null, null, true, false, false); this._firstMove = true; @@ -379,10 +375,8 @@ var Tool = this.Tool = PaperScopeItem.extend(/** @lends Tool# */{ case 'mousemove': while (this.updateEvent(type, pt, this.minDistance, this.maxDistance, this._firstMove, true, false)) { - if (this.onMouseMove) { - this.onMouseMove(new ToolEvent(this, type, event)); - called = true; - } + if (this.responds(type)) + called = this.fire(type, new ToolEvent(this, type, event)); this._firstMove = false; } break; diff --git a/src/ui/View.js b/src/ui/View.js index d49755d2..4007bfab 100644 --- a/src/ui/View.js +++ b/src/ui/View.js @@ -27,7 +27,7 @@ var View = this.View = PaperScopeItem.extend(Callback, /** @lends View# */{ _list: 'views', _reference: 'view', _events: { - frame: { + onFrame: { install: function() { /*#*/ if (options.browser) { var that = this, @@ -75,7 +75,7 @@ var View = this.View = PaperScopeItem.extend(Callback, /** @lends View# */{ } }, - resize: {} + onResize: {} }, /** @@ -210,7 +210,7 @@ var View = this.View = PaperScopeItem.extend(Callback, /** @lends View# */{ this._canvas = this._domEvents = null; // Removing all onFrame handlers makes the _onFrameCallback handler stop // automatically through its uninstall method. - this.detachAll('frame'); + this.detach('frame'); return true; },