queue.js 3.0 KB

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