From ebad701751689b773afe29d1e3c4532c7810d02a Mon Sep 17 00:00:00 2001 From: John Resig Date: Sun, 6 Dec 2009 17:19:33 -0500 Subject: Split the queue code out from data.js into a dedicated queue.js file (also split tests accordingly). --- src/queue.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/queue.js (limited to 'src/queue.js') diff --git a/src/queue.js b/src/queue.js new file mode 100644 index 000000000..e636ffb41 --- /dev/null +++ b/src/queue.js @@ -0,0 +1,78 @@ +jQuery.extend({ + queue: function( elem, type, data ) { + if ( !elem ) { return; } + + type = (type || "fx") + "queue"; + var q = jQuery.data( elem, type ); + + // Speed up dequeue by getting out quickly if this is just a lookup + if ( !data ) { return q || []; } + + if ( !q || jQuery.isArray(data) ) { + q = jQuery.data( elem, type, jQuery.makeArray(data) ); + } else { + q.push( data ); + } + return q; + }, + + dequeue: function( elem, type ){ + type = type || "fx"; + + var queue = jQuery.queue( elem, type ), fn = queue.shift(); + + // If the fx queue is dequeued, always remove the progress sentinel + if ( fn === "inprogress" ) { fn = queue.shift(); } + + if ( fn ) { + // Add a progress sentinel to prevent the fx queue from being + // automatically dequeued + if ( type == "fx" ) { queue.unshift("inprogress"); } + + fn.call(elem, function() { jQuery.dequeue(elem, type); }); + } + } +}); + +jQuery.fn.extend({ + queue: function(type, data){ + if ( typeof type !== "string" ) { + data = type; + type = "fx"; + } + + if ( data === undefined ) { + return jQuery.queue( this[0], type ); + } + return this.each(function(i, elem){ + var queue = jQuery.queue( this, type, data ); + + if ( type == "fx" && queue[0] !== "inprogress" ) { + jQuery.dequeue( this, type ); + } + }); + }, + dequeue: function(type){ + return this.each(function(){ + jQuery.dequeue( this, type ); + }); + }, + + // Based off of the plugin by Clint Helfers, with permission. + // http://blindsignals.com/index.php/2009/07/jquery-delay/ + delay: function( time, type ) { + time = jQuery.fx ? jQuery.fx.speeds[time] || time : time; + type = type || "fx"; + + return this.queue( type, function() { + var elem = this; + setTimeout(function() { + jQuery.dequeue( elem, type ); + }, time ); + }); + }, + + clearQueue: function(type){ + return this.queue( type || "fx", [] ); + } +}); -- cgit v1.2.3