queue.js 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
define( [
	"./core",
	"./data/var/dataPriv",
	"./deferred",
	"./callbacks"
], function( jQuery, dataPriv ) {

"use strict";

jQuery.extend( {
Nicolas Widart's avatar
Nicolas Widart committed
11 12 13 14 15
	queue: function( elem, type, data ) {
		var queue;

		if ( elem ) {
			type = ( type || "fx" ) + "queue";
16
			queue = dataPriv.get( elem, type );
Nicolas Widart's avatar
Nicolas Widart committed
17 18 19

			// Speed up dequeue by getting out quickly if this is just a lookup
			if ( data ) {
20 21
				if ( !queue || Array.isArray( data ) ) {
					queue = dataPriv.access( elem, type, jQuery.makeArray( data ) );
Nicolas Widart's avatar
Nicolas Widart committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
				} else {
					queue.push( data );
				}
			}
			return queue || [];
		}
	},

	dequeue: function( elem, type ) {
		type = type || "fx";

		var queue = jQuery.queue( elem, type ),
			startLength = queue.length,
			fn = queue.shift(),
			hooks = jQuery._queueHooks( elem, type ),
			next = function() {
				jQuery.dequeue( elem, type );
			};

		// If the fx queue is dequeued, always remove the progress sentinel
		if ( fn === "inprogress" ) {
			fn = queue.shift();
			startLength--;
		}

		if ( fn ) {

			// Add a progress sentinel to prevent the fx queue from being
			// automatically dequeued
			if ( type === "fx" ) {
				queue.unshift( "inprogress" );
			}

55
			// Clear up the last queue stop function
Nicolas Widart's avatar
Nicolas Widart committed
56 57 58 59 60 61 62 63 64
			delete hooks.stop;
			fn.call( elem, next, hooks );
		}

		if ( !startLength && hooks ) {
			hooks.empty.fire();
		}
	},

65
	// Not public - generate a queueHooks object, or return the current one
Nicolas Widart's avatar
Nicolas Widart committed
66 67
	_queueHooks: function( elem, type ) {
		var key = type + "queueHooks";
68 69 70 71 72
		return dataPriv.get( elem, key ) || dataPriv.access( elem, key, {
			empty: jQuery.Callbacks( "once memory" ).add( function() {
				dataPriv.remove( elem, [ type + "queue", key ] );
			} )
		} );
Nicolas Widart's avatar
Nicolas Widart committed
73
	}
74
} );
Nicolas Widart's avatar
Nicolas Widart committed
75

76
jQuery.fn.extend( {
Nicolas Widart's avatar
Nicolas Widart committed
77 78 79 80 81 82 83 84 85 86
	queue: function( type, data ) {
		var setter = 2;

		if ( typeof type !== "string" ) {
			data = type;
			type = "fx";
			setter--;
		}

		if ( arguments.length < setter ) {
87
			return jQuery.queue( this[ 0 ], type );
Nicolas Widart's avatar
Nicolas Widart committed
88 89 90 91
		}

		return data === undefined ?
			this :
92
			this.each( function() {
Nicolas Widart's avatar
Nicolas Widart committed
93 94
				var queue = jQuery.queue( this, type, data );

95
				// Ensure a hooks for this queue
Nicolas Widart's avatar
Nicolas Widart committed
96 97
				jQuery._queueHooks( this, type );

98
				if ( type === "fx" && queue[ 0 ] !== "inprogress" ) {
Nicolas Widart's avatar
Nicolas Widart committed
99 100
					jQuery.dequeue( this, type );
				}
101
			} );
Nicolas Widart's avatar
Nicolas Widart committed
102 103
	},
	dequeue: function( type ) {
104
		return this.each( function() {
Nicolas Widart's avatar
Nicolas Widart committed
105
			jQuery.dequeue( this, type );
106
		} );
107
	},
Nicolas Widart's avatar
Nicolas Widart committed
108 109 110
	clearQueue: function( type ) {
		return this.queue( type || "fx", [] );
	},
111

Nicolas Widart's avatar
Nicolas Widart committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	// Get a promise resolved when queues of a certain type
	// are emptied (fx is the type by default)
	promise: function( type, obj ) {
		var tmp,
			count = 1,
			defer = jQuery.Deferred(),
			elements = this,
			i = this.length,
			resolve = function() {
				if ( !( --count ) ) {
					defer.resolveWith( elements, [ elements ] );
				}
			};

		if ( typeof type !== "string" ) {
			obj = type;
			type = undefined;
		}
		type = type || "fx";

132 133
		while ( i-- ) {
			tmp = dataPriv.get( elements[ i ], type + "queueHooks" );
Nicolas Widart's avatar
Nicolas Widart committed
134 135 136 137 138 139 140 141
			if ( tmp && tmp.empty ) {
				count++;
				tmp.empty.add( resolve );
			}
		}
		resolve();
		return defer.promise( obj );
	}
142 143 144 145
} );

return jQuery;
} );