2013-08-30 16:12:49 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor EventSequencer class.
|
|
|
|
*
|
|
|
|
* @copyright 2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-09-07 00:15:34 +00:00
|
|
|
* EventSequencer class with on-event and after-event listeners.
|
2013-08-30 16:12:49 +00:00
|
|
|
*
|
2013-09-07 00:15:34 +00:00
|
|
|
* After-event listeners are fired as soon as possible after the
|
2013-08-30 16:12:49 +00:00
|
|
|
* corresponding native event. They are similar to the setTimeout(f, 0)
|
|
|
|
* idiom, except that they are guaranteed to execute before any subsequent
|
2013-09-07 00:15:34 +00:00
|
|
|
* on-event listener. Therefore, events are executed in the 'right order'.
|
2013-08-30 16:12:49 +00:00
|
|
|
*
|
|
|
|
* This matters when many events are added to the event queue in one go.
|
|
|
|
* For instance, browsers often queue 'keydown' and 'keypress' in immediate
|
|
|
|
* sequence, so a setTimeout(f, 0) defined in the keydown listener will run
|
|
|
|
* *after* the keypress listener (i.e. in the 'wrong' order). EventSequencer
|
|
|
|
* ensures that this does not happen.
|
|
|
|
*
|
2013-09-07 00:15:34 +00:00
|
|
|
* All listeners receive the jQuery event as an argument. If an on-event
|
|
|
|
* listener needs to pass information to a corresponding after-event listener,
|
|
|
|
* it can do so by adding properties into the jQuery event itself.
|
|
|
|
*
|
|
|
|
* @class ve.EventSequencer
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* To fire after-event listeners promptly, the EventSequencer may need to
|
|
|
|
* listen to some events for which it has no registered on-event or
|
2013-09-16 17:45:06 +00:00
|
|
|
* after-event listeners. For instance, to ensure an after-keydown listener
|
2013-09-07 00:15:34 +00:00
|
|
|
* is be fired before the native keyup action, you must include both
|
|
|
|
* 'keydown' and 'keyup' in the eventNames Array.
|
|
|
|
*
|
2013-08-30 16:12:49 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {string[]} eventNames List of event Names to listen to
|
|
|
|
*/
|
2013-09-16 17:45:06 +00:00
|
|
|
ve.EventSequencer = function VeEventSequencer( eventNames ) {
|
|
|
|
var i, len, eventName, eventSequencer = this;
|
2013-09-07 00:15:34 +00:00
|
|
|
this.$node = null;
|
|
|
|
this.eventNames = eventNames;
|
|
|
|
this.eventHandlers = {};
|
2013-08-30 16:12:49 +00:00
|
|
|
|
2013-09-16 17:45:06 +00:00
|
|
|
/**
|
|
|
|
* Generate an event handler for a specific event
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} eventName The event's name
|
|
|
|
* @returns {Function} An event handler
|
|
|
|
*/
|
|
|
|
function makeEventHandler( eventName ) {
|
|
|
|
return function ( ev ) {
|
|
|
|
return eventSequencer.onEvent( eventName, ev );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-08-30 16:12:49 +00:00
|
|
|
/**
|
|
|
|
* @property {Object[]}
|
|
|
|
* - id {number} Id for setTimeout
|
|
|
|
* - func {Function} Post-event listener
|
|
|
|
* - ev {jQuery.Event} Browser event
|
2013-09-16 17:45:06 +00:00
|
|
|
* - eventName {string} Name, such as keydown
|
|
|
|
*/
|
2013-08-30 16:12:49 +00:00
|
|
|
this.pendingCalls = [];
|
2013-09-07 00:15:34 +00:00
|
|
|
|
|
|
|
/**
|
2013-09-16 17:45:06 +00:00
|
|
|
* @property {Object.<string,Function>}
|
2013-09-07 00:15:34 +00:00
|
|
|
*/
|
|
|
|
this.onListenersForEvent = {};
|
|
|
|
|
|
|
|
/**
|
2013-09-16 17:45:06 +00:00
|
|
|
* @property {Object.<string,Function>}
|
2013-09-07 00:15:34 +00:00
|
|
|
*/
|
|
|
|
this.afterListenersForEvent = {};
|
|
|
|
|
2013-08-30 16:12:49 +00:00
|
|
|
for ( i = 0, len = eventNames.length; i < len; i++ ) {
|
|
|
|
eventName = eventNames[i];
|
2013-09-07 00:15:34 +00:00
|
|
|
this.onListenersForEvent[eventName] = [];
|
|
|
|
this.afterListenersForEvent[eventName] = [];
|
|
|
|
this.eventHandlers[eventName] = makeEventHandler( eventName );
|
2013-08-30 16:12:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-09-07 00:15:34 +00:00
|
|
|
* Attach to a node, to listen to its jQuery events
|
|
|
|
*
|
2013-08-30 16:12:49 +00:00
|
|
|
* @method
|
2013-09-07 00:15:34 +00:00
|
|
|
* @param {jQuery} $node The node to attach to
|
2013-08-30 16:12:49 +00:00
|
|
|
*/
|
2013-09-07 00:15:34 +00:00
|
|
|
ve.EventSequencer.prototype.attach = function ( $node ) {
|
|
|
|
this.detach();
|
|
|
|
this.$node = $node.on( this.eventHandlers );
|
2013-08-30 16:12:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-09-07 00:15:34 +00:00
|
|
|
* Detach from a node (if attached), to stop listen to its jQuery events
|
|
|
|
*
|
2013-08-30 16:12:49 +00:00
|
|
|
* @method
|
|
|
|
*/
|
2013-09-07 00:15:34 +00:00
|
|
|
ve.EventSequencer.prototype.detach = function () {
|
|
|
|
if ( this.$node === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.runPendingCalls();
|
|
|
|
this.$node.off( this.eventHandlers );
|
|
|
|
this.$node = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add listeners to be fired just before the browser native action
|
|
|
|
* @method
|
|
|
|
* @param {Object.<string,Function>} listeners Function for each event
|
|
|
|
*/
|
|
|
|
ve.EventSequencer.prototype.on = function ( listeners ) {
|
|
|
|
var eventName;
|
|
|
|
for ( eventName in listeners ) {
|
|
|
|
this.onListenersForEvent[eventName].push( listeners[eventName] );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add listeners to be fired as soon as possible after the native action
|
|
|
|
* @method
|
|
|
|
* @param {Object.<string,Function>} listeners Function for each event
|
|
|
|
*/
|
|
|
|
ve.EventSequencer.prototype.after = function ( listeners ) {
|
|
|
|
var eventName;
|
|
|
|
for ( eventName in listeners ) {
|
|
|
|
this.afterListenersForEvent[eventName].push( listeners[eventName] );
|
|
|
|
}
|
2013-08-30 16:12:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic listener method which does the sequencing
|
2013-09-07 00:15:34 +00:00
|
|
|
* @private
|
2013-08-30 16:12:49 +00:00
|
|
|
* @method
|
|
|
|
* @param {string} eventName Javascript name of the event, e.g. 'keydown'
|
|
|
|
* @param {jQuery.Event} ev The browser event
|
|
|
|
*/
|
2013-09-07 00:15:34 +00:00
|
|
|
ve.EventSequencer.prototype.onEvent = function ( eventName, ev ) {
|
|
|
|
var i, len, onListener, afterListener, pendingCall;
|
|
|
|
this.runPendingCalls();
|
|
|
|
// Length cache 'len' is required, as an onListener could add another onListener
|
|
|
|
for ( i = 0, len = this.onListenersForEvent[eventName].length; i < len; i++ ) {
|
|
|
|
onListener = this.onListenersForEvent[eventName][i];
|
|
|
|
onListener( ev );
|
2013-08-30 16:12:49 +00:00
|
|
|
}
|
2013-09-07 00:15:34 +00:00
|
|
|
// Length cache 'len' for style only
|
|
|
|
for ( i = 0, len = this.afterListenersForEvent[eventName].length; i < len; i++ ) {
|
|
|
|
afterListener = this.afterListenersForEvent[eventName][i];
|
2013-08-30 16:12:49 +00:00
|
|
|
|
|
|
|
// Create a cancellable pending call
|
|
|
|
// - Create the pendingCall object first
|
|
|
|
// - then create the setTimeout invocation to modify pendingCall.id
|
|
|
|
// - then set pendingCall.id to the setTimeout id, so the call can cancel itself
|
|
|
|
// Must wrap everything in a function call, to create the required closure.
|
2013-09-07 00:15:34 +00:00
|
|
|
pendingCall = { 'func': afterListener, 'id': null, 'ev': ev, 'eventName': eventName };
|
2013-08-30 16:12:49 +00:00
|
|
|
/*jshint loopfunc:true */
|
2013-09-07 00:15:34 +00:00
|
|
|
( function ( pendingCall, ev ) {
|
2013-08-30 16:12:49 +00:00
|
|
|
var id = setTimeout( function () {
|
|
|
|
if ( pendingCall.id === null ) {
|
2013-09-07 00:15:34 +00:00
|
|
|
// clearTimeout seems not always to work immediately
|
|
|
|
return;
|
2013-08-30 16:12:49 +00:00
|
|
|
}
|
|
|
|
pendingCall.id = null;
|
|
|
|
pendingCall.func( ev );
|
|
|
|
} );
|
|
|
|
pendingCall.id = id;
|
2013-09-07 00:15:34 +00:00
|
|
|
} )( pendingCall, ev );
|
2013-08-30 16:12:49 +00:00
|
|
|
/*jshint loopfunc:false */
|
|
|
|
this.pendingCalls.push( pendingCall );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run any pending listeners, and clear the pending queue
|
2013-09-07 00:15:34 +00:00
|
|
|
* @private
|
2013-08-30 16:12:49 +00:00
|
|
|
* @method
|
|
|
|
*/
|
2013-09-07 00:15:34 +00:00
|
|
|
ve.EventSequencer.prototype.runPendingCalls = function () {
|
2013-08-30 16:12:49 +00:00
|
|
|
var i, pendingCall;
|
|
|
|
for ( i = 0; i < this.pendingCalls.length; i++ ) {
|
|
|
|
// Length cache not possible, as a pending call appends another pending call.
|
2013-09-07 00:15:34 +00:00
|
|
|
// It's important that this list remains mutable, in the case that this
|
|
|
|
// function indirectly recurses.
|
2013-08-30 16:12:49 +00:00
|
|
|
pendingCall = this.pendingCalls[i];
|
|
|
|
if ( pendingCall.id === null ) {
|
2013-09-07 00:15:34 +00:00
|
|
|
// the call has already run
|
|
|
|
continue;
|
2013-08-30 16:12:49 +00:00
|
|
|
}
|
|
|
|
clearTimeout( pendingCall.id );
|
|
|
|
pendingCall.id = null;
|
2013-09-07 00:15:34 +00:00
|
|
|
// Force to run now. It's important that we set id to null before running,
|
|
|
|
// so that there's no chance a recursive call will call the listener again.
|
2013-08-30 16:12:49 +00:00
|
|
|
pendingCall.func( pendingCall.ev );
|
|
|
|
}
|
2013-09-07 00:15:34 +00:00
|
|
|
// This is safe because we only ever appended to the list, so it's definitely exhausted
|
|
|
|
// now.
|
|
|
|
this.pendingCalls.length = 0;
|
2013-08-30 16:12:49 +00:00
|
|
|
};
|