Merge branch 'bug7141' of http://github.com/csnover/jquery into csnover-bug7141
[jquery.git] / src / queue.js
1 (function( jQuery ) {
2
3 jQuery.extend({
4         queue: function( elem, type, data ) {
5                 if ( !elem ) {
6                         return;
7                 }
8
9                 type = (type || "fx") + "queue";
10                 var q = jQuery.data( elem, type );
11
12                 // Speed up dequeue by getting out quickly if this is just a lookup
13                 if ( !data ) {
14                         return q || [];
15                 }
16
17                 if ( !q || jQuery.isArray(data) ) {
18                         q = jQuery.data( elem, type, jQuery.makeArray(data) );
19
20                 } else {
21                         q.push( data );
22                 }
23
24                 return q;
25         },
26
27         dequeue: function( elem, type ) {
28                 type = type || "fx";
29
30                 var queue = jQuery.queue( elem, type ), fn = queue.shift();
31
32                 // If the fx queue is dequeued, always remove the progress sentinel
33                 if ( fn === "inprogress" ) {
34                         fn = queue.shift();
35                 }
36
37                 if ( fn ) {
38                         // Add a progress sentinel to prevent the fx queue from being
39                         // automatically dequeued
40                         if ( type === "fx" ) {
41                                 queue.unshift("inprogress");
42                         }
43
44                         fn.call(elem, function() {
45                                 jQuery.dequeue(elem, type);
46                         });
47                 }
48         }
49 });
50
51 jQuery.fn.extend({
52         queue: function( type, data ) {
53                 if ( typeof type !== "string" ) {
54                         data = type;
55                         type = "fx";
56                 }
57
58                 if ( data === undefined ) {
59                         return jQuery.queue( this[0], type );
60                 }
61                 return this.each(function( i ) {
62                         var queue = jQuery.queue( this, type, data );
63
64                         if ( type === "fx" && queue[0] !== "inprogress" ) {
65                                 jQuery.dequeue( this, type );
66                         }
67                 });
68         },
69         dequeue: function( type ) {
70                 return this.each(function() {
71                         jQuery.dequeue( this, type );
72                 });
73         },
74
75         // Based off of the plugin by Clint Helfers, with permission.
76         // http://blindsignals.com/index.php/2009/07/jquery-delay/
77         delay: function( time, type ) {
78                 time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
79                 type = type || "fx";
80
81                 return this.queue( type, function() {
82                         var elem = this;
83                         setTimeout(function() {
84                                 jQuery.dequeue( elem, type );
85                         }, time );
86                 });
87         },
88
89         clearQueue: function( type ) {
90                 return this.queue( type || "fx", [] );
91         }
92 });
93
94 })( jQuery );