Time to go with the flow and use the on()/off()/emit() pattern for events.

This commit is contained in:
Jürg Lehni 2014-10-08 14:57:56 +02:00
parent 2be48bab0b
commit c7c49d4091
10 changed files with 43 additions and 43 deletions

View file

@ -16,19 +16,19 @@
* @private
*/
var Emitter = {
attach: function(type, func) {
on: function(type, func) {
// If an object literal is passed, attach all callbacks defined in it
if (typeof type !== 'string') {
Base.each(type, function(value, key) {
this.attach(key, value);
this.on(key, value);
}, this);
return;
}
var entry = this._eventTypes[type];
if (entry) {
var handlers = this._handlers = this._handlers || {};
var handlers = this._callbacks = this._callbacks || {};
handlers = handlers[type] = handlers[type] || [];
if (handlers.indexOf(func) == -1) { // Not added yet, add it now
if (handlers.indexOf(func) === -1) { // Not added yet, add it now
handlers.push(func);
// See if this is the first handler that we're attaching, and
// call install if defined.
@ -38,26 +38,26 @@ var Emitter = {
}
},
detach: function(type, func) {
off: function(type, func) {
// If an object literal is passed, detach all callbacks defined in it
if (typeof type !== 'string') {
Base.each(type, function(value, key) {
this.detach(key, value);
this.off(key, value);
}, this);
return;
}
var entry = this._eventTypes[type],
handlers = this._handlers && this._handlers[type],
handlers = this._callbacks && this._callbacks[type],
index;
if (entry && handlers) {
// See if this is the last handler that we're detaching (or if we
// are detaching all handlers), and call uninstall if defined.
if (!func || (index = handlers.indexOf(func)) != -1
&& handlers.length == 1) {
if (!func || (index = handlers.indexOf(func)) !== -1
&& handlers.length === 1) {
if (entry.uninstall)
entry.uninstall.call(this, type);
delete this._handlers[type];
} else if (index != -1) {
delete this._callbacks[type];
} else if (index !== -1) {
// Just remove this one handler
handlers.splice(index, 1);
}
@ -65,15 +65,15 @@ var Emitter = {
},
once: function(type, func) {
this.attach(type, function() {
this.on(type, function() {
func.apply(this, arguments);
this.detach(type, func);
this.off(type, func);
});
},
fire: function(type, event) {
emit: function(type, event) {
// Returns true if fired, false otherwise
var handlers = this._handlers && this._handlers[type];
var handlers = this._callbacks && this._callbacks[type];
if (!handlers)
return false;
var args = [].slice.call(arguments, 1),
@ -91,16 +91,16 @@ var Emitter = {
},
responds: function(type) {
return !!(this._handlers && this._handlers[type]);
return !!(this._callbacks && this._callbacks[type]);
},
// Install jQuery-style aliases to our event handler methods
on: '#attach',
off: '#detach',
trigger: '#fire',
// Keep deprecated methods around from previous Callback interface.
attach: '#on',
detach: '#off',
fire: '#emit',
_installEvents: function(install) {
var handlers = this._handlers,
var handlers = this._callbacks,
key = install ? 'install' : 'uninstall';
for (var type in handlers) {
if (handlers[type].length > 0) {
@ -142,9 +142,9 @@ var Emitter = {
// Detach the previous event, if there was one.
var prev = this[name];
if (prev)
this.detach(type, prev);
this.off(type, prev);
if (func)
this.attach(type, func);
this.on(type, func);
this[name] = func;
};
});

View file

@ -39,9 +39,9 @@ var PaperScopeItem = Base.extend(Emitter, /** @lends PaperScopeItem# */{
return false;
var prev = this._scope[this._reference];
if (prev && prev !== this)
prev.fire('deactivate');
prev.emit('deactivate');
this._scope[this._reference] = this;
this.fire('activate', prev);
this.emit('activate', prev);
return true;
},

View file

@ -410,7 +410,7 @@ Base.exports.PaperScript = (function() {
view.setOnResize(res.onResize);
// Fire resize event directly, so any user
// defined resize handlers are called.
view.fire('resize', {
view.emit('resize', {
size: view.size,
delta: new Point()
});

View file

@ -312,7 +312,7 @@ var Raster = Item.extend(/** @lends Raster# */{
if (view) {
paper = view._scope;
that.setImage(image);
that.fire('load');
that.emit('load');
view.update();
}
}

View file

@ -215,7 +215,7 @@ new function() {
// http://www.w3.org/TR/SVG/struct.html#ImageElement
image: function (node) {
var raster = new Raster(getValue(node, 'href', true));
raster.attach('load', function() {
raster.on('load', function() {
var size = getSize(node, 'width', 'height');
this.setSize(size);
// Since x and y start from the top left of an image, add

View file

@ -347,7 +347,7 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
}
}
return this.responds(type)
&& this.fire(type, new ToolEvent(this, type, event));
&& this.emit(type, new ToolEvent(this, type, event));
},
_handleEvent: function(type, point, event) {

View file

@ -200,7 +200,7 @@ var Component = Base.extend(Emitter, /** @lends Component# */{
meta.value || 'value'));
},
click: function() {
that.fire('click');
that.emit('click');
}
}
});
@ -224,9 +224,9 @@ var Component = Base.extend(Emitter, /** @lends Component# */{
this._className = className;
// Attach default 'change' even that delegates to the palette.
this.attach('change', function(value) {
this.on('change', function(value) {
if (!this._dontFire)
palette.fire('change', this, this._name, value);
palette.emit('change', this, this._name, value);
});
this._dontFire = true;
// Now that everything is set up, copy over values fro, props.
@ -309,7 +309,7 @@ var Component = Base.extend(Emitter, /** @lends Component# */{
if (this._value !== value) {
this._value = value;
if (!this._dontFire)
this.fire('change', this.getValue());
this.emit('change', this.getValue());
}
},

View file

@ -95,7 +95,7 @@ var Key = new function() {
// Update global reference to this scope.
paper = scope;
// Call the onKeyDown or onKeyUp handler if present
tool.fire(type, new KeyEvent(down, key, character, event));
tool.emit(type, new KeyEvent(down, key, character, event));
if (view)
view.update();
}

View file

@ -149,7 +149,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
// Calculate delta if lastPoint was passed
lastPoint ? point.subtract(lastPoint) : null);
}
if (obj.fire(type, mouseEvent) && mouseEvent.isStopped) {
if (obj.emit(type, mouseEvent) && mouseEvent.isStopped) {
// Call preventDefault() on native event if mouse event was
// handled here.
event.preventDefault();
@ -327,7 +327,7 @@ CanvasView.inject(new function() {
}
});
// Use new Base() to convert into a Base object, for #toString()
view.fire('frame', new Base({
view.emit('frame', new Base({
delta: frameDuration,
time: frameDuration * count,
count: count

View file

@ -153,7 +153,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
this._element = this._project = null;
// Remove all onFrame handlers.
// TODO: Shouldn't we remove all handlers, automatically
this.detach('frame');
this.off('frame');
this._animate = false;
this._frameItems = {};
return true;
@ -212,7 +212,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
this._before = now;
this._handlingFrame = true;
// Use new Base() to convert into a Base object, for #toString()
this.fire('frame', new Base({
this.emit('frame', new Base({
// Time elapsed since last redraw in seconds:
delta: delta,
// Time since first call of frame() in seconds:
@ -237,12 +237,12 @@ var View = Base.extend(Emitter, /** @lends View# */{
count: 0
};
if (++this._frameItemCount === 1)
this.attach('frame', this._handleFrameItems);
this.on('frame', this._handleFrameItems);
} else {
delete items[item._id];
if (--this._frameItemCount === 0) {
// If this is the last one, just stop animating straight away.
this.detach('frame', this._handleFrameItems);
this.off('frame', this._handleFrameItems);
}
}
},
@ -251,7 +251,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
_handleFrameItems: function(event) {
for (var i in this._frameItems) {
var entry = this._frameItems[i];
entry.item.fire('frame', new Base(event, {
entry.item.emit('frame', new Base(event, {
// Time since first call of frame() in seconds:
time: entry.time += event.delta,
count: entry.count++
@ -348,7 +348,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
this._setViewSize(size);
this._bounds = null; // Force recalculation
// Call onResize handler on any size change
this.fire('resize', {
this.emit('resize', {
size: size,
delta: delta
});
@ -629,7 +629,7 @@ var View = Base.extend(Emitter, /** @lends View# */{
* // When the user presses the mouse,
* // detach the frame handler from the view:
* function onMouseDown(event) {
* view.detach('frame');
* view.off('frame');
* }
*/
/**