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