Make Item#removeOn() calls work with new event callback mechanism.

This commit is contained in:
Jürg Lehni 2011-11-11 20:41:09 +01:00
parent 78d1ce1540
commit 3dfb4d3ae5
2 changed files with 25 additions and 46 deletions

View file

@ -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 // TODO: implement Item#removeOnFrame
return Base.each(['down', 'drag', 'up', 'move'], function(name) { return Base.each(['down', 'drag', 'up', 'move'], function(name) {
this['removeOn' + Base.capitalize(name)] = function() { this['removeOn' + Base.capitalize(name)] = function() {
@ -1986,12 +1946,10 @@ var Item = this.Item = Base.extend(/** @lends Item# */{
removeOn: function(obj) { removeOn: function(obj) {
for (var name in obj) { for (var name in obj) {
if (obj[name]) { if (obj[name]) {
sets[name][this.getId()] = this; var key = 'mouse' + name,
// Since the drag set gets cleared in up, we need to make sets = Tool._removeSets = Tool._removeSets || {};
// sure it's installed too sets[key] = sets[key] || {};
if (name === 'drag') sets[key][this.getId()] = this;
installHandler('up');
installHandler(name);
} }
} }
return this; return this;

View file

@ -328,6 +328,27 @@ var Tool = this.Tool = PaperScopeItem.extend(Callback, /** @lends Tool# */{
onHandleEvent: function(type, pt, event) { onHandleEvent: function(type, pt, event) {
// Update global reference to this scope. // Update global reference to this scope.
paper = 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; var called = false;
switch (type) { switch (type) {
case 'mousedown': case 'mousedown':