Offset: allow offset setter to throw for disconnected elements
[jquery.git] / src / queue.js
blob235439fa6f0424a8c5ff1a69ef5956ac7a22711a
1 define([
2         "./core",
3         "./data/var/dataPriv",
4         "./deferred",
5         "./callbacks"
6 ], function( jQuery, dataPriv ) {
8 jQuery.extend({
9         queue: function( elem, type, data ) {
10                 var queue;
12                 if ( elem ) {
13                         type = ( type || "fx" ) + "queue";
14                         queue = dataPriv.get( elem, type );
16                         // Speed up dequeue by getting out quickly if this is just a lookup
17                         if ( data ) {
18                                 if ( !queue || jQuery.isArray( data ) ) {
19                                         queue = dataPriv.access( elem, type, jQuery.makeArray(data) );
20                                 } else {
21                                         queue.push( data );
22                                 }
23                         }
24                         return queue || [];
25                 }
26         },
28         dequeue: function( elem, type ) {
29                 type = type || "fx";
31                 var queue = jQuery.queue( elem, type ),
32                         startLength = queue.length,
33                         fn = queue.shift(),
34                         hooks = jQuery._queueHooks( elem, type ),
35                         next = function() {
36                                 jQuery.dequeue( elem, type );
37                         };
39                 // If the fx queue is dequeued, always remove the progress sentinel
40                 if ( fn === "inprogress" ) {
41                         fn = queue.shift();
42                         startLength--;
43                 }
45                 if ( fn ) {
47                         // Add a progress sentinel to prevent the fx queue from being
48                         // automatically dequeued
49                         if ( type === "fx" ) {
50                                 queue.unshift( "inprogress" );
51                         }
53                         // Clear up the last queue stop function
54                         delete hooks.stop;
55                         fn.call( elem, next, hooks );
56                 }
58                 if ( !startLength && hooks ) {
59                         hooks.empty.fire();
60                 }
61         },
63         // Not public - generate a queueHooks object, or return the current one
64         _queueHooks: function( elem, type ) {
65                 var key = type + "queueHooks";
66                 return dataPriv.get( elem, key ) || dataPriv.access( elem, key, {
67                         empty: jQuery.Callbacks("once memory").add(function() {
68                                 dataPriv.remove( elem, [ type + "queue", key ] );
69                         })
70                 });
71         }
72 });
74 jQuery.fn.extend({
75         queue: function( type, data ) {
76                 var setter = 2;
78                 if ( typeof type !== "string" ) {
79                         data = type;
80                         type = "fx";
81                         setter--;
82                 }
84                 if ( arguments.length < setter ) {
85                         return jQuery.queue( this[0], type );
86                 }
88                 return data === undefined ?
89                         this :
90                         this.each(function() {
91                                 var queue = jQuery.queue( this, type, data );
93                                 // Ensure a hooks for this queue
94                                 jQuery._queueHooks( this, type );
96                                 if ( type === "fx" && queue[0] !== "inprogress" ) {
97                                         jQuery.dequeue( this, type );
98                                 }
99                         });
100         },
101         dequeue: function( type ) {
102                 return this.each(function() {
103                         jQuery.dequeue( this, type );
104                 });
105         },
106         clearQueue: function( type ) {
107                 return this.queue( type || "fx", [] );
108         },
109         // Get a promise resolved when queues of a certain type
110         // are emptied (fx is the type by default)
111         promise: function( type, obj ) {
112                 var tmp,
113                         count = 1,
114                         defer = jQuery.Deferred(),
115                         elements = this,
116                         i = this.length,
117                         resolve = function() {
118                                 if ( !( --count ) ) {
119                                         defer.resolveWith( elements, [ elements ] );
120                                 }
121                         };
123                 if ( typeof type !== "string" ) {
124                         obj = type;
125                         type = undefined;
126                 }
127                 type = type || "fx";
129                 while ( i-- ) {
130                         tmp = dataPriv.get( elements[ i ], type + "queueHooks" );
131                         if ( tmp && tmp.empty ) {
132                                 count++;
133                                 tmp.empty.add( resolve );
134                         }
135                 }
136                 resolve();
137                 return defer.promise( obj );
138         }
141 return jQuery;