* .jshintrc: Updated to include more strict options that match our code conventions. Also separated into 3 groups: - stricter (curly, eqeqeq etc.) - laxer (smarttabs, laxbreak) - envrionment (browser) * .jshintignore: Updated to include more third-party/upstream files that should not be linted. * Most of it is just routine cleanup, a few notable points: - jquery.autoEllipsis: Removed unused variable $protectedText. - jquery.arrowSteps.js: Remove <!-- --> and language="javascript" that hasn't been needed for almost a decade. - jquery.byteLimit: Use dashToCamel key for .data(), this already happens internally in jQuery data(), since data storage should use keys that are usable as identifiers. The dashed versions are to populate these from data-attribute-names, which then becomes data.attributeNames. jQuery data() takes both forms as convenience. - jquery.client.js: To avoid a rewrite of it, allowing unexpected assignments (boss) and eval (evil) in the functions that use that. Left as it is for now, could use a rewrite later. - jquery.color.js: Tolerate unexpected assignment for now (boss). Left as it is for now, should perhaps be refactored later. Also re-ordered per jshint/jslint to put definition before invocation. This option can be disabled, but then it doesn't warn for invoking undefined functions (or typos) at all. - jquery.expandableField.js: Remove empty switch/case. - jquery.localize.js: Alias mw global. - jquery.suggestions.js: Use e.which; jQuery.Event normalizes e.keyCode etc. - jquery.tablesorter.js: Alias mw global. - jquery.textSelection.js: Fix leakage of variable in global scope of var "i" and "j". - mediawiki.util.test.js: Fixed implied global `pCustom`. * Review with -w for your own sanity. Change-Id: Ia972f79539a96a38357ec4e92b0b6e38cc301681
76 lines
2.3 KiB
JavaScript
76 lines
2.3 KiB
JavaScript
( function ( $ ) {
|
|
/**
|
|
* Function that escapes spaces in event names. This is needed because
|
|
* "_delayedBind-foo bar-1000" refers to two events
|
|
*/
|
|
function encodeEvent( event ) {
|
|
return event.replace( /-/g, '--' ).replace( / /g, '-' );
|
|
}
|
|
|
|
$.fn.extend( {
|
|
/**
|
|
* Bind a callback to an event in a delayed fashion.
|
|
* In detail, this means that the callback will be called a certain
|
|
* time after the event fires, but the timer is reset every time
|
|
* the event fires.
|
|
* @param timeout Number of milliseconds to wait
|
|
* @param event Name of the event (string)
|
|
* @param data Data to pass to the event handler (optional)
|
|
* @param callback Function to call
|
|
*/
|
|
delayedBind: function ( timeout, event, data, callback ) {
|
|
if ( arguments.length === 3 ) {
|
|
// Shift optional parameter down
|
|
callback = data;
|
|
data = undefined;
|
|
}
|
|
var encEvent = encodeEvent( event );
|
|
return this.each( function () {
|
|
var that = this;
|
|
// Bind the top half
|
|
// Do this only once for every (event, timeout) pair
|
|
if ( !( $(this).data( '_delayedBindBound-' + encEvent + '-' + timeout ) ) ) {
|
|
$(this).data( '_delayedBindBound-' + encEvent + '-' + timeout, true );
|
|
$(this).bind( event, function () {
|
|
var timerID = $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout );
|
|
// Cancel the running timer
|
|
if ( timerID !== null ) {
|
|
clearTimeout( timerID );
|
|
}
|
|
timerID = setTimeout( function () {
|
|
$(that).trigger( '_delayedBind-' + encEvent + '-' + timeout );
|
|
}, timeout );
|
|
$(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout, timerID );
|
|
} );
|
|
}
|
|
|
|
// Bottom half
|
|
$(this).bind( '_delayedBind-' + encEvent + '-' + timeout, data, callback );
|
|
} );
|
|
},
|
|
|
|
/**
|
|
* Cancel the timers for delayed events on the selected elements.
|
|
*/
|
|
delayedBindCancel: function ( timeout, event ) {
|
|
var encEvent = encodeEvent( event );
|
|
return this.each( function () {
|
|
var timerID = $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout );
|
|
if ( timerID !== null ) {
|
|
clearTimeout( timerID );
|
|
}
|
|
} );
|
|
},
|
|
|
|
/**
|
|
* Unbind an event bound with delayedBind()
|
|
*/
|
|
delayedBindUnbind: function ( timeout, event, callback ) {
|
|
var encEvent = encodeEvent( event );
|
|
return this.each( function () {
|
|
$(this).unbind( '_delayedBind-' + encEvent + '-' + timeout, callback );
|
|
} );
|
|
}
|
|
} );
|
|
|
|
}( jQuery ) );
|