Docs: Update remaining HTTP URLs to HTTPS
[jquery.git] / src / queue.js
blob306dbcff808db6d7740aa78f3e6449690170c3ab
1 import { jQuery } from "./core.js";
2 import { dataPriv } from "./data/var/dataPriv.js";
4 import "./deferred.js";
5 import "./callbacks.js";
7 jQuery.extend( {
8         queue: function( elem, type, data ) {
9                 var queue;
11                 if ( elem ) {
12                         type = ( type || "fx" ) + "queue";
13                         queue = dataPriv.get( elem, type );
15                         // Speed up dequeue by getting out quickly if this is just a lookup
16                         if ( data ) {
17                                 if ( !queue || Array.isArray( data ) ) {
18                                         queue = dataPriv.set( elem, type, jQuery.makeArray( data ) );
19                                 } else {
20                                         queue.push( data );
21                                 }
22                         }
23                         return queue || [];
24                 }
25         },
27         dequeue: function( elem, type ) {
28                 type = type || "fx";
30                 var queue = jQuery.queue( elem, type ),
31                         startLength = queue.length,
32                         fn = queue.shift(),
33                         hooks = jQuery._queueHooks( elem, type ),
34                         next = function() {
35                                 jQuery.dequeue( elem, type );
36                         };
38                 // If the fx queue is dequeued, always remove the progress sentinel
39                 if ( fn === "inprogress" ) {
40                         fn = queue.shift();
41                         startLength--;
42                 }
44                 if ( fn ) {
46                         // Add a progress sentinel to prevent the fx queue from being
47                         // automatically dequeued
48                         if ( type === "fx" ) {
49                                 queue.unshift( "inprogress" );
50                         }
52                         // Clear up the last queue stop function
53                         delete hooks.stop;
54                         fn.call( elem, next, hooks );
55                 }
57                 if ( !startLength && hooks ) {
58                         hooks.empty.fire();
59                 }
60         },
62         // Not public - generate a queueHooks object, or return the current one
63         _queueHooks: function( elem, type ) {
64                 var key = type + "queueHooks";
65                 return dataPriv.get( elem, key ) || dataPriv.set( elem, key, {
66                         empty: jQuery.Callbacks( "once memory" ).add( function() {
67                                 dataPriv.remove( elem, [ type + "queue", key ] );
68                         } )
69                 } );
70         }
71 } );
73 jQuery.fn.extend( {
74         queue: function( type, data ) {
75                 var setter = 2;
77                 if ( typeof type !== "string" ) {
78                         data = type;
79                         type = "fx";
80                         setter--;
81                 }
83                 if ( arguments.length < setter ) {
84                         return jQuery.queue( this[ 0 ], type );
85                 }
87                 return data === undefined ?
88                         this :
89                         this.each( function() {
90                                 var queue = jQuery.queue( this, type, data );
92                                 // Ensure a hooks for this queue
93                                 jQuery._queueHooks( this, type );
95                                 if ( type === "fx" && queue[ 0 ] !== "inprogress" ) {
96                                         jQuery.dequeue( this, type );
97                                 }
98                         } );
99         },
100         dequeue: function( type ) {
101                 return this.each( function() {
102                         jQuery.dequeue( this, type );
103                 } );
104         },
105         clearQueue: function( type ) {
106                 return this.queue( type || "fx", [] );
107         },
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         }
139 } );
141 export { jQuery, jQuery as $ };