2011-11-11 12:29:28 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
|
|
|
* http://paperjs.org/
|
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var Callback = {
|
|
|
|
attach: function(type, func) {
|
|
|
|
// If an object literal is passed, attach all callbacks defined in it
|
2011-11-11 13:49:31 -05:00
|
|
|
if (typeof type !== 'string') {
|
2011-11-11 12:29:28 -05:00
|
|
|
return Base.each(type, function(value, key) {
|
|
|
|
this.attach(key, value);
|
|
|
|
}, this);
|
|
|
|
}
|
2011-11-11 13:49:31 -05:00
|
|
|
var entry = this._eventTypes[type];
|
|
|
|
if (!entry)
|
|
|
|
return this;
|
2011-11-11 12:29:28 -05:00
|
|
|
var handlers = this._handlers = this._handlers || {};
|
|
|
|
handlers = handlers[type] = handlers[type] || [];
|
|
|
|
if (handlers.indexOf(func) == -1) { // Not added yet, add it now
|
|
|
|
// See if this is the first handler that we're attaching, and
|
|
|
|
// call install if defined.
|
|
|
|
if (entry.install && !handlers.length)
|
|
|
|
entry.install.call(this);
|
|
|
|
handlers.push(func);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
detach: function(type, func) {
|
|
|
|
// If an object literal is passed, detach all callbacks defined in it
|
2011-11-11 13:49:31 -05:00
|
|
|
if (typeof type !== 'string') {
|
2011-11-11 12:29:28 -05:00
|
|
|
return Base.each(type, function(value, key) {
|
|
|
|
this.detach(key, value);
|
|
|
|
}, this);
|
|
|
|
}
|
2011-11-11 13:49:31 -05:00
|
|
|
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.
|
2011-11-11 12:29:28 -05:00
|
|
|
if (!handlers.length) {
|
|
|
|
delete this._handlers[type];
|
|
|
|
if (entry.uninstall)
|
|
|
|
entry.uninstall.call(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
fire: function(type, param) {
|
|
|
|
// Returns true if fired, false otherwise
|
|
|
|
var handlers = this._handlers && this._handlers[type];
|
|
|
|
if (!handlers)
|
|
|
|
return false;
|
|
|
|
Base.each(handlers, function(func) {
|
|
|
|
func.call(this, param);
|
|
|
|
}, this);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2011-11-11 13:49:31 -05:00
|
|
|
responds: function(type) {
|
|
|
|
return this._handlers && this._handlers[type];
|
|
|
|
},
|
|
|
|
|
2011-11-11 12:29:28 -05:00
|
|
|
statics: {
|
|
|
|
inject: function(/* src, ... */) {
|
|
|
|
for (var i = 0, l = arguments.length; i < l; i++) {
|
|
|
|
var src = arguments[i],
|
|
|
|
events = src._events;
|
|
|
|
if (events) {
|
2011-11-11 13:49:31 -05:00
|
|
|
// 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];
|
2011-11-11 12:29:28 -05:00
|
|
|
};
|
2011-11-11 13:49:31 -05:00
|
|
|
src['set' + part] = function(func) {
|
2011-11-11 12:29:28 -05:00
|
|
|
if (func) {
|
|
|
|
this.attach(type, func);
|
|
|
|
} else {
|
2011-11-11 13:49:31 -05:00
|
|
|
this.detach(type, this[name]);
|
2011-11-11 12:29:28 -05:00
|
|
|
}
|
2011-11-11 13:49:31 -05:00
|
|
|
this[name] = func;
|
2011-11-11 12:29:28 -05:00
|
|
|
};
|
|
|
|
});
|
2011-11-11 13:49:31 -05:00
|
|
|
src._eventTypes = types;
|
2011-11-11 12:29:28 -05:00
|
|
|
}
|
|
|
|
this.base(src);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|