You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.2 KiB

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