Localisation updates from http://translatewiki.net.
[mediawiki.git] / resources / jquery / jquery.delayedBind.js
blobd84ee267a4db210991a1f7976d4d4aa57da3b22f
1 (function( $ ) {
2 /**
3  * Function that escapes spaces in event names. This is needed because
4  * "_delayedBind-foo bar-1000" refers to two events
5  */
6 function encodeEvent( event ) {
7         return event.replace( /-/g, '--' ).replace( / /g, '-' );
10 $.fn.extend( {
11         /**
12          * Bind a callback to an event in a delayed fashion.
13          * In detail, this means that the callback will be called a certain
14          * time after the event fires, but the timer is reset every time
15          * the event fires.
16          * @param timeout Number of milliseconds to wait
17          * @param event Name of the event (string)
18          * @param data Data to pass to the event handler (optional)
19          * @param callback Function to call
20          */
21         delayedBind: function( timeout, event, data, callback ) {
22                 if ( arguments.length == 3 ) {
23                         // Shift optional parameter down
24                         callback = data;
25                         data = undefined;
26                 }
27                 var encEvent = encodeEvent( event );
28                 return this.each( function() {
29                         var that = this;
30                         // Bind the top half
31                         // Do this only once for every (event, timeout) pair
32                         if (  !( $(this).data( '_delayedBindBound-' + encEvent + '-' + timeout ) ) ) {
33                                 $(this).data( '_delayedBindBound-' + encEvent + '-' + timeout, true );
34                                 $(this).bind( event, function() {
35                                         var timerID = $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout );
36                                         // Cancel the running timer
37                                         if ( typeof timerID != 'undefined' )
38                                                 clearTimeout( timerID );
39                                         timerID = setTimeout( function() {
40                                                 $(that).trigger( '_delayedBind-' + encEvent + '-' + timeout );
41                                         }, timeout );
42                                         $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout, timerID );
43                                 } );
44                         }
45                         
46                         // Bottom half
47                         $(this).bind( '_delayedBind-' + encEvent + '-' + timeout, data, callback );
48                 } );
49         },
50         
51         /**
52          * Cancel the timers for delayed events on the selected elements.
53          */
54         delayedBindCancel: function( timeout, event ) {
55                 var encEvent = encodeEvent( event );
56                 return this.each( function() {
57                         var timerID = $(this).data( '_delayedBindTimerID-' + encEvent + '-' + timeout );
58                         if ( typeof timerID != 'undefined' )
59                                 clearTimeout( timerID );
60                 } );
61         },
62         
63         /**
64          * Unbind an event bound with delayedBind()
65          */
66         delayedBindUnbind: function( timeout, event, callback ) {
67                 var encEvent = encodeEvent( event );
68                 return this.each( function() {
69                         $(this).unbind( '_delayedBind-' + encEvent + '-' + timeout, callback );
70                 } );
71         }
72 } );
73 } )( jQuery );