diff options
author | David Bonner <dbonner@cogolabs.com> | 2012-10-15 17:20:33 -0400 |
---|---|---|
committer | Rick Waldron <waldron.rick@gmail.com> | 2012-10-15 17:20:33 -0400 |
commit | 9104a3d5f2a1bb72b89ebdbad3f0bfdb2ea8ab74 (patch) | |
tree | 5f145091dacd9348725f20fe6e6fa9535ad9c3ef | |
parent | d6c84c5bb55814df0063e04b12c4143d07cba3f7 (diff) | |
download | jquery-9104a3d5f2a1bb72b89ebdbad3f0bfdb2ea8ab74.tar.gz jquery-9104a3d5f2a1bb72b89ebdbad3f0bfdb2ea8ab74.zip |
Deprecate fn toggle Closes gh-970 https://github.com/jquery/2012-dev-summit/issues/24
-rw-r--r-- | src/deprecated.js | 26 | ||||
-rw-r--r-- | src/event.js | 26 | ||||
-rw-r--r-- | test/unit/deprecated.js | 122 | ||||
-rw-r--r-- | test/unit/event.js | 82 |
4 files changed, 127 insertions, 129 deletions
diff --git a/src/deprecated.js b/src/deprecated.js index af2b6223d..c5fa375d4 100644 --- a/src/deprecated.js +++ b/src/deprecated.js @@ -60,4 +60,30 @@ jQuery.sub = function() { return jQuerySub; }; +jQuery.fn.toggle = function( fn ) { + // Save reference to arguments for access in closure + var args = arguments, + guid = fn.guid || jQuery.guid++, + i = 0, + toggler = function( event ) { + // Figure out which function to execute + var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i; + jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 ); + + // Make sure that clicks stop + event.preventDefault(); + + // and execute the function + return args[ lastToggle ].apply( this, arguments ) || false; + }; + + // link all the functions, so any of them can unbind this click handler + toggler.guid = guid; + while ( i < args.length ) { + args[ i++ ].guid = guid; + } + + return this.click( toggler ); +}; + })(); diff --git a/src/event.js b/src/event.js index 153045ac5..91ec7d757 100644 --- a/src/event.js +++ b/src/event.js @@ -980,32 +980,6 @@ jQuery.fn.extend({ } }, - toggle: function( fn ) { - // Save reference to arguments for access in closure - var args = arguments, - guid = fn.guid || jQuery.guid++, - i = 0, - toggler = function( event ) { - // Figure out which function to execute - var lastToggle = ( jQuery._data( this, "lastToggle" + fn.guid ) || 0 ) % i; - jQuery._data( this, "lastToggle" + fn.guid, lastToggle + 1 ); - - // Make sure that clicks stop - event.preventDefault(); - - // and execute the function - return args[ lastToggle ].apply( this, arguments ) || false; - }; - - // link all the functions, so any of them can unbind this click handler - toggler.guid = guid; - while ( i < args.length ) { - args[ i++ ].guid = guid; - } - - return this.click( toggler ); - }, - hover: function( fnOver, fnOut ) { return this.mouseenter( fnOver ).mouseleave( fnOut || fnOver ); } diff --git a/test/unit/deprecated.js b/test/unit/deprecated.js index 0f77040c1..15fdf85cb 100644 --- a/test/unit/deprecated.js +++ b/test/unit/deprecated.js @@ -1,28 +1,104 @@ +if ( jQuery.browser ) { module("deprecated"); -// Start jQuery.browser tests -if ( jQuery.browser && jQuery.uaMatch ) { - if ( jQuery.get && !isLocal ) { - asyncTest( "browser", function() { - jQuery.get( "data/ua.txt", function( data ) { - var uas = data.split( "\n" ); - expect( (uas.length - 1) * 2 ); - - jQuery.each(uas, function() { - var parts = this.split( "\t" ), - agent = parts[2], - ua; - - if ( agent ) { - ua = jQuery.uaMatch( agent ); - equal( ua.browser, parts[0], "browser (" + agent + ")" ); - equal( ua.version, parts[1], "version (" + agent + ")" ); - } - }); - - start(); +if ( jQuery.get && !isLocal ) { + asyncTest( "browser", function() { + jQuery.get( "data/ua.txt", function( data ) { + var uas = data.split( "\n" ); + expect( (uas.length - 1) * 2 ); + + jQuery.each(uas, function() { + var parts = this.split( "\t" ), + agent = parts[2], + ua; + + if ( agent ) { + ua = jQuery.uaMatch( agent ); + equal( ua.browser, parts[0], "browser (" + agent + ")" ); + equal( ua.version, parts[1], "version (" + agent + ")" ); + } }); + + start(); + }); + }); +} + +test("toggle(Function, Function, ...)", function() { + expect(16); + + var count = 0, + fn1 = function(e) { count++; }, + fn2 = function(e) { count--; }, + preventDefault = function(e) { e.preventDefault(); }, + link = jQuery("#mark"); + link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click(); + equal( count, 1, "Check for toggle(fn, fn)" ); + + jQuery("#firstp").toggle(function () { + equal(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" ); + }, function() {}).trigger("click", [ 1, 2, 3 ]); + + var first = 0; + jQuery("#simon1").one("click", function() { + ok( true, "Execute event only once" ); + jQuery(this).toggle(function() { + equal( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); + }, function() { + equal( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); }); - } + return false; + }).click().click().click(); + + var turn = 0; + var fns = [ + function(){ + turn = 1; + }, + function(){ + turn = 2; + }, + function(){ + turn = 3; + } + ]; + + var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] ); + $div.click(); + equal( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1"); + $div.click(); + equal( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2"); + $div.click(); + equal( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3"); + $div.click(); + equal( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1"); + $div.click(); + equal( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2"); + + $div.unbind("click",fns[0]); + var data = jQuery._data( $div[0], "events" ); + ok( !data, "Unbinding one function from toggle unbinds them all"); + + // manually clean up detached elements + $div.remove(); + + // Test Multi-Toggles + var a = [], b = []; + $div = jQuery("<div/>"); + $div.toggle(function(){ a.push(1); }, function(){ a.push(2); }); + $div.click(); + deepEqual( a, [1], "Check that a click worked." ); + + $div.toggle(function(){ b.push(1); }, function(){ b.push(2); }); + $div.click(); + deepEqual( a, [1,2], "Check that a click worked with a second toggle." ); + deepEqual( b, [1], "Check that a click worked with a second toggle." ); + + $div.click(); + deepEqual( a, [1,2,1], "Check that a click worked with a second toggle, second click." ); + deepEqual( b, [1,2], "Check that a click worked with a second toggle, second click." ); + + // manually clean up detached elements + $div.remove(); +}); } -// End of jQuery.browser tests diff --git a/test/unit/event.js b/test/unit/event.js index 48ba2df91..667920475 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -1449,84 +1449,6 @@ test("jQuery.Event.currentTarget", function(){ .off( "click" ); }); -test("toggle(Function, Function, ...)", function() { - expect(16); - - var count = 0, - fn1 = function(e) { count++; }, - fn2 = function(e) { count--; }, - preventDefault = function(e) { e.preventDefault(); }, - link = jQuery("#mark"); - link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click(); - equal( count, 1, "Check for toggle(fn, fn)" ); - - jQuery("#firstp").toggle(function () { - equal(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" ); - }, function() {}).trigger("click", [ 1, 2, 3 ]); - - var first = 0; - jQuery("#simon1").one("click", function() { - ok( true, "Execute event only once" ); - jQuery(this).toggle(function() { - equal( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); - }, function() { - equal( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" ); - }); - return false; - }).click().click().click(); - - var turn = 0; - var fns = [ - function(){ - turn = 1; - }, - function(){ - turn = 2; - }, - function(){ - turn = 3; - } - ]; - - var $div = jQuery("<div> </div>").toggle( fns[0], fns[1], fns[2] ); - $div.click(); - equal( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1"); - $div.click(); - equal( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2"); - $div.click(); - equal( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3"); - $div.click(); - equal( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1"); - $div.click(); - equal( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2"); - - $div.unbind("click",fns[0]); - var data = jQuery._data( $div[0], "events" ); - ok( !data, "Unbinding one function from toggle unbinds them all"); - - // manually clean up detached elements - $div.remove(); - - // Test Multi-Toggles - var a = [], b = []; - $div = jQuery("<div/>"); - $div.toggle(function(){ a.push(1); }, function(){ a.push(2); }); - $div.click(); - deepEqual( a, [1], "Check that a click worked." ); - - $div.toggle(function(){ b.push(1); }, function(){ b.push(2); }); - $div.click(); - deepEqual( a, [1,2], "Check that a click worked with a second toggle." ); - deepEqual( b, [1], "Check that a click worked with a second toggle." ); - - $div.click(); - deepEqual( a, [1,2,1], "Check that a click worked with a second toggle, second click." ); - deepEqual( b, [1,2], "Check that a click worked with a second toggle, second click." ); - - // manually clean up detached elements - $div.remove(); -}); - test(".live()/.die()", function() { expect(66); @@ -2360,7 +2282,7 @@ test( "delegated event with delegateTarget-relative selector", function() { .end() .find("a").click().end() .find("#ul0").off(); - + // Non-positional selector (#12383) markup = markup.wrap("<div />").parent(); markup @@ -2375,7 +2297,7 @@ test( "delegated event with delegateTarget-relative selector", function() { ok( true, "li.test is below the delegation point." ); }) .find("#a0_0").click(); - + markup.remove(); }); |