From 3dfb4d3ae5e7633eb1a34ea8e93d362f95ef9060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Fri, 11 Nov 2011 20:41:09 +0100 Subject: [PATCH] Make Item#removeOn() calls work with new event callback mechanism. --- src/item/Item.js | 50 ++++-------------------------------------------- src/tool/Tool.js | 21 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 46 deletions(-) diff --git a/src/item/Item.js b/src/item/Item.js index 272485fb..59f57921 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -1935,46 +1935,6 @@ var Item = this.Item = Base.extend(/** @lends Item# */{ * } */ - var sets = { - down: {}, drag: {}, up: {}, move: {} - }; - - function removeAll(set) { - for (var id in set) { - var item = set[id]; - item.remove(); - for (var type in sets) { - var other = sets[type]; - if (other != set && other[item.getId()]) - delete other[item.getId()]; - } - } - } - - function installHandler(name) { - var handler = 'onMouse' + Base.capitalize(name); - // Inject a onMouse handler that performs all the behind the scene magic - // and calls the script's handler at the end, if defined. - var func = paper.tool[handler]; - if (!func || !func._installed) { - var hash = {}; - hash[handler] = function(event) { - // Always clear the drag set on mouseup - if (name === 'up') - sets.drag = {}; - removeAll(sets[name]); - sets[name] = {}; - // Call the script's overridden handler, if defined - if (this.base) - this.base(event); - }; - paper.tool.inject(hash); - // Only install this handler once, and mark it as installed, - // to prevent repeated installing. - paper.tool[handler]._installed = true; - } - } - // TODO: implement Item#removeOnFrame return Base.each(['down', 'drag', 'up', 'move'], function(name) { this['removeOn' + Base.capitalize(name)] = function() { @@ -1986,12 +1946,10 @@ var Item = this.Item = Base.extend(/** @lends Item# */{ removeOn: function(obj) { for (var name in obj) { if (obj[name]) { - sets[name][this.getId()] = this; - // Since the drag set gets cleared in up, we need to make - // sure it's installed too - if (name === 'drag') - installHandler('up'); - installHandler(name); + var key = 'mouse' + name, + sets = Tool._removeSets = Tool._removeSets || {}; + sets[key] = sets[key] || {}; + sets[key][this.getId()] = this; } } return this; diff --git a/src/tool/Tool.js b/src/tool/Tool.js index cfb673b4..4d487b48 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -328,6 +328,27 @@ var Tool = this.Tool = PaperScopeItem.extend(Callback, /** @lends Tool# */{ onHandleEvent: function(type, pt, event) { // Update global reference to this scope. paper = this._scope; + // Handle removeOn* calls first + var sets = Tool._removeSets; + if (sets) { + // Always clear the drag set on mouseup + if (type === 'mouseup') + sets.mousedrag = null; + var set = sets[type]; + if (set) { + for (var id in set) { + var item = set[id]; + for (var key in sets) { + var other = sets[key]; + if (other && other != set && other[item.getId()]) + delete other[item.getId()]; + } + item.remove(); + } + sets[type] = null; + } + } + // Now handle event callbacks var called = false; switch (type) { case 'mousedown':