Permalink
Browse files

updated "drag" unit tests, fix for live plugin with jquery 1.5

  • Loading branch information...
1 parent 59783ba commit 521afaa7db93b5ebb0e9f603fadaaac38710b35a Michael Helgeson committed Jan 19, 2011
@@ -1,76 +1,82 @@
-/*!
- * jquery.event.drag.live - v 2.1.0
- * Copyright (c) 2010 Three Dub Media - http://threedubmedia.com
- * Open Source MIT License - http://threedubmedia.com/code/license
- */
-// Created: 2010-06-07
-// Updated: 2010-09-03
-// REQUIRES: jquery 1.4.2+, event.drag 2.1+
-
-;(function( $ ){
-
-// local refs (increase compression)
-var $event = $.event,
-// ref the special event config
-drag = $event.special.drag,
-// old drag event add method
-origadd = drag.add,
-// old drag event teradown method
-origteardown = drag.teardown;
-
-// the namespace for internal live events
-drag.livekey = "livedrag";
-
-// new drop event add method
-drag.add = function( obj ){
- // call the old method
- origadd.apply( this, arguments );
- // read the data
- var data = $.data( this, drag.datakey );
- // bind the live "dropinit" delegator
- if ( !data.live && obj.selector ){
- data.live = true;
- $event.add( this, "draginit."+ drag.livekey, drag.delegate );
- }
-};
-
-// new drop event teardown method
-drag.teardown = function(){
- // call the old method
- origteardown.apply( this, arguments );
- // remove the "live" delegation
- $event.remove( this, "draginit", drag.delegate );
-};
-
-// identify potential delegate elements
-drag.delegate = function( event ){
- // local refs
- var elems = [], target,
- // element event structure
- events = $.data( this, "events" ) || {};
- // query live events
- $.each( events.live || [], function( i, obj ){
- // no event type matches
- if ( obj.preType.indexOf("drag") !== 0 )
- return;
- // locate the element to delegate
- target = $( event.target ).closest( obj.selector, event.currentTarget )[0];
- // no element found
- if ( !target )
- return;
- // add an event handler
- $event.add( target, obj.origType+'.'+drag.livekey, obj.origHandler, obj.data );
- // remember new elements
- if ( $.inArray( target, elems ) < 0 )
- elems.push( target );
- });
- // if there are no elements, break
- if ( !elems.length )
- return false;
- // return the matched results, and clenup when complete
- return $( elems ).bind("dragend."+ drag.livekey, function(){
- $event.remove( this, "."+ drag.livekey ); // cleanup delegation
- });
-};
-
+/*!
+ * jquery.event.drag.live - v 2.1.0
+ * Copyright (c) 2010 Three Dub Media - http://threedubmedia.com
+ * Open Source MIT License - http://threedubmedia.com/code/license
+ */
+// Created: 2010-06-07
+// Updated: 2010-09-03
+// REQUIRES: jquery 1.4.2+, event.drag 2.1+
+
+;(function( $ ){
+
+// local refs (increase compression)
+var $event = $.event,
+// ref the special event config
+drag = $event.special.drag,
+// old drag event add method
+origadd = drag.add,
+// old drag event teradown method
+origteardown = drag.teardown;
+
+// the namespace for internal live events
+drag.livekey = "livedrag";
+
+// new drop event add method
+drag.add = function( obj ){
+ // call the old method
+ origadd.apply( this, arguments );
+ // read the data
+ var data = $.data( this, drag.datakey );
+ // bind the live "draginit" delegator
+ if ( !data.live && obj.selector ){
+ data.live = true;
+ $event.add( this, "draginit."+ drag.livekey, drag.delegate );
+ }
+};
+
+// new drop event teardown method
+drag.teardown = function(){
+ // call the old method
+ origteardown.apply( this, arguments );
+ // read the data
+ var data = $.data( this, drag.datakey ) || {};
+ // bind the live "draginit" delegator
+ if ( data.live ){
+ // remove the "live" delegation
+ $event.remove( this, "draginit."+ drag.livekey, drag.delegate );
+ data.live = false;
+ }
+};
+
+// identify potential delegate elements
+drag.delegate = function( event ){
+ // local refs
+ var elems = [], target,
+ // element event structure
+ events = $.data( this, "events" ) || {};
+ // query live events
+ $.each( events.live || [], function( i, obj ){
+ // no event type matches
+ if ( obj.preType.indexOf("drag") !== 0 )
+ return;
+ // locate the element to delegate
+ target = $( event.target ).closest( obj.selector, event.currentTarget )[0];
+ // no element found
+ if ( !target )
+ return;
+ // add an event handler
+ $event.add( target, obj.origType+'.'+drag.livekey, obj.origHandler, obj.data );
+ // remember new elements
+ if ( $.inArray( target, elems ) < 0 )
+ elems.push( target );
+ });
+ // if there are no elements, break
+ if ( !elems.length )
+ return false;
+ // return the matched results, and clenup when complete
+ return $( elems ).bind("dragend."+ drag.livekey, function(){
+ $event.remove( this, "."+ drag.livekey ); // cleanup delegation
+ });
+};
+
})( jQuery );
View
52 event.drag/test/binding.js 100644 → 100755
@@ -1,9 +1,11 @@
-test("Event Binding",function(){
-
- expect( 72 );
-
- $.each(['draginit','dragstart','drag','dragend'],function( i, type ){
-
+module("Event Binding");
+
+$.each(['draginit','dragstart','drag','dragend'],function( i, type ){
+
+ test('"'+ type +'"',function(){
+
+ expect( 27 );
+
// make sure the event handler gets bound to the element
var $elem = $('<div />'),
elem = $elem[0],
@@ -20,29 +22,41 @@ test("Event Binding",function(){
drop: 44,
click: 33
},
+ def = $.event.special[ type ].defaults,
data;
- ok( $elem.bind( type, fn )[0] == elem, ".bind('"+ type +"', fn )" );
- ok( $.data( elem, $.event.special.drag.datakey ), "drag data exists" );
+ ok( $elem.bind( type, fn )[0] == elem, '.bind("'+ type +'", fn )' );
+ ok( data = $.data( elem, $.event.special.drag.datakey ), "drag data exists" );
ok( $.data( elem, "events" ), "event data exists" );
- ok( $.data( elem, "events" )[ type ][0], type +" event handler added" );
+ ok( $.data( elem, "events" )[ type ][0], '"'+ type +'" event handler added' );
- ok( $elem.trigger( type )[0] == elem, ".trigger('"+ type +"')" );
+ ok( data.which == def.which, '"which" default stored' );
+ ok( data.distance == def.distance, '"distance" default stored' );
+ ok( data.not == def.not, '"not" default stored' );
+ ok( data.handle == def.handle, '"handle" default stored' );
+ ok( data.relative == def.relative, '"relative" default stored' );
+ ok( data.drop == def.drop, '"drop" default stored' );
+ ok( data.click == def.click, '"click" default stored' );
+
+ ok( $elem.trigger( type )[0] == elem, '.trigger("'+ type +'")' );
ok( count == 1, "handler was triggered");
- ok( $elem.unbind( type )[0] == elem, ".unbind('"+ type +"')" );
+ ok( $elem.unbind( type )[0] == elem, '.unbind("'+ type +'")' );
ok( !$.data( elem, "events" ), "event data removed" );
ok( !$.data( elem, $.event.special.drag.datakey ), "drag data removed" );
- ok( $elem.bind( type, opts, fn )[0] == elem, ".bind('"+ type +"', data, fn )" );
+ ok( $elem.bind( type, opts, fn )[0] == elem, '.bind("'+ type +'", data, fn )' );
ok( data = $.data( elem, $.event.special.drag.datakey ), "drag data exists" );
- ok( data.which == opts.which, "'which' option stored" );
- ok( data.distance == opts.distance, "'distance' option stored" );
- ok( data.not == opts.not, "'not' option stored" );
- ok( data.handle == opts.handle, "'handle' option stored" );
- ok( data.relative == opts.relative, "'relative' option stored" );
- ok( data.drop == opts.drop, "'drop' option stored" );
- ok( data.click == opts.click, "'click'option stored" );
+ ok( $.data( elem, "events" ), "event data exists" );
+ ok( $.data( elem, "events" )[ type ][0], '"drag'+ type +'" event handler added' );
+
+ ok( data.which == opts.which, '"which" option stored' );
+ ok( data.distance == opts.distance, '"distance" option stored' );
+ ok( data.not == opts.not, '"not" option stored' );
+ ok( data.handle == opts.handle, '"handle" option stored' );
+ ok( data.relative == opts.relative, '"relative" option stored' );
+ ok( data.drop == opts.drop, '"drop" option stored' );
+ ok( data.click == opts.click, '"click" option stored' );
$elem.remove();
Oops, something went wrong. Retry.

0 comments on commit 521afaa

Please sign in to comment.