Enhances ajaxSetup so that it can take an optional target option, in which case targe...
[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 ),
31                         fn = queue.shift();
32
33                 // If the fx queue is dequeued, always remove the progress sentinel
34                 if ( fn === "inprogress" ) {
35                         fn = queue.shift();
36                 }
37
38                 if ( fn ) {
39                         // Add a progress sentinel to prevent the fx queue from being
40                         // automatically dequeued
41                         if ( type === "fx" ) {
42                                 queue.unshift("inprogress");
43                         }
44
45                         fn.call(elem, function() {
46                                 jQuery.dequeue(elem, type);
47                         });
48                 }
49
50                 if ( !queue.length ) {
51                         jQuery.removeData( elem, type + "queue", true );
52                 }
53         }
54 });
55
56 jQuery.fn.extend({
57         queue: function( type, data ) {
58                 if ( typeof type !== "string" ) {
59                         data = type;
60                         type = "fx";
61                 }
62
63                 if ( data === undefined ) {
64                         return jQuery.queue( this[0], type );
65                 }
66                 return this.each(function( i ) {
67                         var queue = jQuery.queue( this, type, data );
68
69                         if ( type === "fx" && queue[0] !== "inprogress" ) {
70                                 jQuery.dequeue( this, type );
71                         }
72                 });
73         },
74         dequeue: function( type ) {
75                 return this.each(function() {
76                         jQuery.dequeue( this, type );
77                 });
78         },
79
80         // Based off of the plugin by Clint Helfers, with permission.
81         // http://blindsignals.com/index.php/2009/07/jquery-delay/
82         delay: function( time, type ) {
83                 time = jQuery.fx ? jQuery.fx.speeds[time] || time : time;
84                 type = type || "fx";
85
86                 return this.queue( type, function() {
87                         var elem = this;
88                         setTimeout(function() {
89                                 jQuery.dequeue( elem, type );
90                         }, time );
91                 });
92         },
93
94         clearQueue: function( type ) {
95                 return this.queue( type || "fx", [] );
96         }
97 });
98
99 })( jQuery );