Catrope has uploaded a new change for review. https://gerrit.wikimedia.org/r/90061
Change subject: Update oojs to 1.0.4 ...................................................................... Update oojs to 1.0.4 Contains EventEmitter fixes Change-Id: Iafc49bd148a50b19e5e9f0a0add056ad858533de --- M modules/oojs/OO.js 1 file changed, 37 insertions(+), 25 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor refs/changes/61/90061/1 diff --git a/modules/oojs/OO.js b/modules/oojs/OO.js index 814da26..7df3b02 100644 --- a/modules/oojs/OO.js +++ b/modules/oojs/OO.js @@ -1,12 +1,12 @@ /*! - * Object Oriented JavaScript Library v1.0.2 + * Object Oriented JavaScript Library v1.0.4-pre (95052a825c) * https://github.com/trevorparscal/oojs * * Copyright 2011-2013 OOJS Team and other contributors. * Released under the MIT license * http://oojs.mit-license.org * - * Date: Thu Jul 25 04:28:57 2013 GMT+0200 (CEST) + * Date: Thu Oct 10 2013 14:19:48 GMT-0700 (PDT) */ ( function ( global ) { @@ -364,6 +364,7 @@ * Event emitter. * * @class OO.EventEmitter + * * @constructor * @property {Object} bindings */ @@ -377,6 +378,8 @@ /** * Add a listener to events of a specific event. * + * If the callback/context are already bound to the event, they will not be bound again. + * * @method * @param {string} event Type of event to listen to * @param {Function} callback Function to call when event occurs @@ -386,21 +389,35 @@ * @chainable */ oo.EventEmitter.prototype.on = function ( event, callback, args, context ) { + var i, bindings, binding; + // Validate callback if ( typeof callback !== 'function' ) { throw new Error( 'Invalid callback. Function or method name expected.' ); } - - // Auto-initialize binding - if ( !( event in this.bindings ) ) { - this.bindings[event] = []; + // Fallback to null context + if ( arguments.length < 4 ) { + context = null; } - + if ( this.bindings.hasOwnProperty( event ) ) { + // Check for duplicate callback and context for this event + bindings = this.bindings[event]; + i = bindings.length; + while ( i-- ) { + binding = bindings[i]; + if ( bindings.callback === callback && bindings.context === context ) { + return this; + } + } + } else { + // Auto-initialize bindings list + bindings = this.bindings[event] = []; + } // Add binding - this.bindings[event].push( { + bindings.push( { 'callback': callback, 'args': args, - 'context': context || null + 'context': context } ); return this; }; @@ -427,10 +444,11 @@ * @method * @param {string} event Type of event to remove listener from * @param {Function} [callback] Listener to remove, omit to remove all + * @param {Object} [context=null] Object used context for callback function or method * @chainable * @throws {Error} Listener argument is not a function */ -oo.EventEmitter.prototype.off = function ( event, callback ) { +oo.EventEmitter.prototype.off = function ( event, callback, context ) { var i, bindings; if ( arguments.length === 1 ) { @@ -446,11 +464,15 @@ // No matching bindings return this; } + // Fallback to null context + if ( arguments.length < 3 ) { + context = null; + } // Remove matching handlers bindings = this.bindings[event]; i = bindings.length; while ( i-- ) { - if ( bindings[i].callback === callback ) { + if ( bindings[i].callback === callback && bindings[i].context === context ) { bindings.splice( i, 1 ); } } @@ -544,6 +566,7 @@ var i, method, callback, event, bindings; if ( methods ) { + // Remove specific connections to the context for ( event in methods ) { method = methods[event]; if ( typeof method === 'string' ) { @@ -556,28 +579,17 @@ } else { callback = method; } - bindings = this.bindings[event]; - i = bindings.length; - while ( i-- ) { - if ( bindings[i].context === context && bindings[i].callback === callback ) { - bindings.splice( i, 1 ); - } - } - if ( bindings.length === 0 ) { - delete this.bindings[event]; - } + this.off( event, callback, context ); } } else { + // Remove all connections to the context for ( event in this.bindings ) { bindings = this.bindings[event]; i = bindings.length; while ( i-- ) { if ( bindings[i].context === context ) { - bindings.splice( i, 1 ); + this.off( event, bindings[i].callback, context ); } - } - if ( bindings.length === 0 ) { - delete this.bindings[event]; } } } -- To view, visit https://gerrit.wikimedia.org/r/90061 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iafc49bd148a50b19e5e9f0a0add056ad858533de Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: master Gerrit-Owner: Catrope <[email protected]> Gerrit-Reviewer: Trevor Parscal <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
