jQuery: The Write Less, Do More JavaScript Library

Changeset 3989

Show
Ignore:
Timestamp:
11/30/07 21:36:49 (8 months ago)
Author:
davidserduke
Message:

Added enchancement for #1994 by adding two parameters to .stop() which give additional functionality. The first parameter clearQueue will clear the queue on the necessary DOM elements so all animation will stop. The second parameter will cause the currently playing animation to immediately complete including reseting original styles on show and hide and calling the callback function. If no parameters are passed it will work as it always did.

While adding unit testing I noticed the stop() unit test wasn't working correctly because the element was hidden so I fixed it and added more unit tests around the new functionality. I also added a cursor:pointer to the css (because for a long time I didn't know they were clickable).

Location:
trunk/jquery
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/fx.js

    r3958 r3989  
    147147    }, 
    148148 
    149     stop: function(){ 
     149    stop: function(clearQueue, gotoEnd){ 
    150150        var timers = jQuery.timers; 
    151151 
    152         return this.each(function(){ 
    153             for ( var i = 0; i < timers.length; i++ ) 
    154                 if ( timers[i].elem == this ) 
    155                     timers.splice(i--, 1); 
    156         }).dequeue(); 
     152        if (clearQueue) 
     153            this.queue([]); 
     154 
     155        this.each(function(){ 
     156            // go in reverse order so anything added to the queue during the loop is ignored 
     157            for ( var i = timers.length - 1; i >= 0; i-- ) 
     158                if ( timers[i].elem == this ) { 
     159                    if (gotoEnd) 
     160                        // force the next step to be the last 
     161                        timers[i](true); 
     162                    timers.splice(i, 1); 
     163                } 
     164        }); 
     165 
     166        // start the next in the queue if the last step wasn't forced 
     167        if (!gotoEnd) 
     168            this.dequeue(); 
     169 
     170        return this; 
    157171    } 
    158172 
     
    270284 
    271285        var self = this; 
    272         function t(){ 
    273             return self.step(); 
     286        function t(gotoEnd){ 
     287            return self.step(gotoEnd); 
    274288        } 
    275289 
     
    323337 
    324338    // Each step of an animation 
    325     step: function(){ 
     339    step: function(gotoEnd){ 
    326340        var t = (new Date()).getTime(); 
    327341 
    328         if ( t > this.options.duration + this.startTime ) { 
     342        if ( gotoEnd || t > this.options.duration + this.startTime ) { 
    329343            this.now = this.end; 
    330344            this.pos = this.state = 1; 
  • trunk/jquery/test/data/testsuite.css

    r2515 r3989  
    1111h2.pass { background-color: green; } 
    1212h2.fail { background-color: red; } 
     13 
     14ol#tests > li > strong { cursor:pointer; } 
    1315 
    1416div#fx-tests h4 { 
  • trunk/jquery/test/unit/fx.js

    r3839 r3989  
    5555    expect(3); 
    5656    stop(); 
    57     reset(); 
    58  
    59     var foo = $("#foo")[0]; 
    60     var h = foo.style.height; 
    61  
    62     $("#foo").slideUp(1000); 
     57 
     58    var $foo = $("#nothiddendiv"); 
     59    var w = 0; 
     60    $foo.hide().width(200).width(); 
     61 
     62    $foo.animate({ width:'show' }, 1000); 
    6363    setTimeout(function(){ 
    64         var nh = foo.style.height; 
    65         ok( nh != h, "An animation occurred " + nh + " " + h ); 
    66         $("#foo").stop(); 
    67  
    68         nh = foo.style.height; 
    69         ok( nh != h, "Stop didn't reset the animation " + nh + " " + h ); 
     64        var nw = $foo.width(); 
     65        ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); 
     66        $foo.stop(); 
     67 
     68        nw = $foo.width(); 
     69        ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); 
    7070        setTimeout(function(){ 
    71             equals( nh, foo.style.height, "The animation didn't continue" ); 
     71            equals( nw, $foo.width(), "The animation didn't continue" ); 
    7272            start(); 
    7373        }, 100); 
     
    7575}); 
    7676 
     77test("stop() - several in queue", function() { 
     78    expect(4); 
     79    stop(); 
     80 
     81    var $foo = $("#nothiddendiv"); 
     82    var w = 0; 
     83    $foo.hide().width(200).width(); 
     84 
     85    $foo.animate({ width:'show' }, 1000); 
     86    $foo.animate({ width:'hide' }, 1000); 
     87    $foo.animate({ width:'show' }, 1000); 
     88    setTimeout(function(){ 
     89        equals( $foo.queue().length, 3, "All 3 still in the queue" ); 
     90        var nw = $foo.width(); 
     91        ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); 
     92        $foo.stop(); 
     93 
     94        nw = $foo.width(); 
     95        ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); 
     96        equals( $foo.queue().length, 2, "The next animation continued" ); 
     97        $foo.stop(true); 
     98        start(); 
     99    }, 100); 
     100}); 
     101 
     102test("stop(clearQueue)", function() { 
     103    expect(4); 
     104    stop(); 
     105 
     106    var $foo = $("#nothiddendiv"); 
     107    var w = 0; 
     108    $foo.hide().width(200).width(); 
     109 
     110    $foo.animate({ width:'show' }, 1000); 
     111    $foo.animate({ width:'hide' }, 1000); 
     112    $foo.animate({ width:'show' }, 1000); 
     113    setTimeout(function(){ 
     114        var nw = $foo.width(); 
     115        ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); 
     116        $foo.stop(true); 
     117 
     118        nw = $foo.width(); 
     119        ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px"); 
     120 
     121        equals( $foo.queue().length, 0, "The animation queue was cleared" ); 
     122        setTimeout(function(){ 
     123            equals( nw, $foo.width(), "The animation didn't continue" ); 
     124            start(); 
     125        }, 100); 
     126    }, 100); 
     127}); 
     128 
     129test("stop(clearQueue, gotoEnd)", function() { 
     130    expect(3); 
     131    stop(); 
     132 
     133    var $foo = $("#nothiddendiv"); 
     134    var w = 0; 
     135    $foo.hide().width(200).width(); 
     136 
     137    $foo.animate({ width:'show' }, 1000); 
     138    $foo.animate({ width:'hide' }, 1000); 
     139    $foo.animate({ width:'show' }, 1000); 
     140    $foo.animate({ width:'hide' }, 1000); 
     141    setTimeout(function(){ 
     142        var nw = $foo.width(); 
     143        ok( nw != w, "An animation occurred " + nw + "px " + w + "px"); 
     144        $foo.stop(false, true); 
     145 
     146        nw = $foo.width(); 
     147        equals( nw, 200, "Stop() reset the animation" ); 
     148 
     149        setTimeout(function(){ 
     150            equals( $foo.queue().length, 3, "The next animation continued" ); 
     151            $foo.stop(true); 
     152            start(); 
     153        }, 100); 
     154    }, 100); 
     155}); 
     156 
    77157test("toggle()", function() { 
    78158    expect(3); 
    79159    var x = $("#foo"); 
    80     ok( x.is(":visible") ); 
     160    ok( x.is(":visible"), "is visible" ); 
    81161    x.toggle(); 
    82     ok( x.is(":hidden") ); 
     162    ok( x.is(":hidden"), "is hidden" ); 
    83163    x.toggle(); 
    84     ok( x.is(":visible") ); 
     164    ok( x.is(":visible"), "is visible again" ); 
    85165}); 
    86166