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