Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit 59783bac45668b2df33c68c4bdf5295b10859daa 1 parent a8eab23
Michael Helgeson authored
View
595 event.drop/jquery.event.drop.js
@@ -1,298 +1,299 @@
-/*!
- * jquery.event.drop - v 2.1.0
- * Copyright (c) 2010 Three Dub Media - http://threedubmedia.com
- * Open Source MIT License - http://threedubmedia.com/code/license
- */
-// Created: 2008-06-04
-// Updated: 2010-09-03
-// REQUIRES: jquery 1.4.2+, event.drag 2.1+
-
-;(function($){ // secure $ jQuery alias
-
-// Events: drop, dropstart, dropend
-
-// add the jquery instance method
-$.fn.drop = function( str, arg, opts ){
- // figure out the event type
- var type = typeof str == "string" ? str : "",
- // figure out the event handler...
- fn = $.isFunction( str ) ? str : $.isFunction( arg ) ? arg : null;
- // fix the event type
- if ( type.indexOf("drop") !== 0 )
- type = "drop"+ type;
- // were options passed
- opts = ( str == fn ? arg : opts ) || {};
- // trigger or bind event handler
- return fn ? this.bind( type, opts, fn ) : this.trigger( type );
-};
-
-// DROP MANAGEMENT UTILITY
-// returns filtered drop target elements, caches their positions
-$.drop = function( opts ){
- opts = opts || {};
- // safely set new options...
- drop.multi = opts.multi === true ? Infinity :
- opts.multi === false ? 1 : !isNaN( opts.multi ) ? opts.multi : drop.multi;
- drop.delay = opts.delay || drop.delay;
- drop.tolerance = $.isFunction( opts.tolerance ) ? opts.tolerance :
- opts.tolerance === null ? null : drop.tolerance;
- drop.mode = opts.mode || drop.mode || 'intersect';
-};
-
-// local refs (increase compression)
-var $event = $.event,
-$special = $event.special,
-// configure the drop special event
-drop = $.event.special.drop = {
-
- // these are the default settings
- multi: 1, // allow multiple drop winners per dragged element
- delay: 20, // async timeout delay
- mode: 'overlap', // drop tolerance mode
-
- // internal cache
- targets: [],
-
- // the key name for stored drop data
- datakey: "dropdata",
-
- // count bound related events
- add: function( obj ){
- // read the interaction data
- var data = $.data( this, drop.datakey );
- // count another realted event
- data.related += 1;
- },
-
- // forget unbound related events
- remove: function(){
- $.data( this, drop.datakey ).related -= 1;
- },
-
- // configure the interactions
- setup: function(){
- // check for related events
- if ( $.data( this, drop.datakey ) )
- return;
- // initialize the drop element data
- var data = {
- related: 0,
- active: [],
- anyactive: 0,
- winner: 0,
- location: {}
- };
- // store the drop data on the element
- $.data( this, drop.datakey, data );
- // store the drop target in internal cache
- drop.targets.push( this );
- },
-
- // destroy the configure interaction
- teardown: function(){
- // check for related events
- if ( $.data( this, drop.datakey ).related )
- return;
- // remove the stored data
- $.removeData( this, drop.datakey );
- // reference the targeted element
- var element = this;
- // remove from the internal cache
- drop.targets = $.grep( drop.targets, function( target ){
- return ( target !== element );
- });
- },
-
- // shared event handler
- handler: function( event, dd ){
- // local vars
- var results, $targets;
- // make sure the right data is available
- if ( !dd )
- return;
- // handle various events
- switch ( event.type ){
- // draginit, from $.event.special.drag
- case 'mousedown': // DROPINIT >>
- case 'touchstart': // DROPINIT >>
- // collect and assign the drop targets
- $targets = $( drop.targets );
- if ( typeof dd.drop == "string" )
- $targets = $targets.filter( dd.drop );
- // reset drop data winner properties
- $targets.each(function(){
- var data = $.data( this, drop.datakey );
- data.active = [];
- data.anyactive = 0;
- data.winner = 0;
- });
- // set available target elements
- dd.droppable = $targets;
- // activate drop targets for the initial element being dragged
- $special.drag.hijack( event, "dropinit", dd );
- break;
- // drag, from $.event.special.drag
- case 'mousemove': // TOLERATE >>
- case 'touchmove': // TOLERATE >>
- drop.event = event; // store the mousemove event
- if ( !drop.timer )
- // monitor drop targets
- drop.tolerate( dd );
- break;
- // dragend, from $.event.special.drag
- case 'mouseup': // DROP >> DROPEND >>
- case 'touchend': // DROP >> DROPEND >>
- drop.timer = clearTimeout( drop.timer ); // delete timer
- if ( dd.propagates ){
- $special.drag.hijack( event, "drop", dd );
- $special.drag.hijack( event, "dropend", dd );
- }
- break;
-
- }
- },
-
- // returns the location positions of an element
- locate: function( elem, index ){
- var data = $.data( elem, drop.datakey ),
- $elem = $( elem ),
- posi = $elem.offset() || {},
- height = $elem.outerHeight(),
- width = $elem.outerWidth(),
- location = {
- elem: elem,
- width: width,
- height: height,
- top: posi.top,
- left: posi.left,
- right: posi.left + width,
- bottom: posi.top + height
- };
- // drag elements might not have dropdata
- if ( data ){
- data.location = location;
- data.index = index;
- data.elem = elem;
- }
- return location;
- },
-
- // test the location positions of an element against another OR an X,Y coord
- contains: function( target, test ){ // target { location } contains test [x,y] or { location }
- return ( ( test[0] || test.left ) >= target.left && ( test[0] || test.right ) <= target.right
- && ( test[1] || test.top ) >= target.top && ( test[1] || test.bottom ) <= target.bottom );
- },
-
- // stored tolerance modes
- modes: { // fn scope: "$.event.special.drop" object
- // target with mouse wins, else target with most overlap wins
- 'intersect': function( event, proxy, target ){
- return this.contains( target, [ event.pageX, event.pageY ] ) ? // check cursor
- 1e9 : this.modes.overlap.apply( this, arguments ); // check overlap
- },
- // target with most overlap wins
- 'overlap': function( event, proxy, target ){
- // calculate the area of overlap...
- return Math.max( 0, Math.min( target.bottom, proxy.bottom ) - Math.max( target.top, proxy.top ) )
- * Math.max( 0, Math.min( target.right, proxy.right ) - Math.max( target.left, proxy.left ) );
- },
- // proxy is completely contained within target bounds
- 'fit': function( event, proxy, target ){
- return this.contains( target, proxy ) ? 1 : 0;
- },
- // center of the proxy is contained within target bounds
- 'middle': function( event, proxy, target ){
- return this.contains( target, [ proxy.left + proxy.width * .5, proxy.top + proxy.height * .5 ] ) ? 1 : 0;
- }
- },
-
- // sort drop target cache by by winner (dsc), then index (asc)
- sort: function( a, b ){
- return ( b.winner - a.winner ) || ( a.index - b.index );
- },
-
- // async, recursive tolerance execution
- tolerate: function( dd ){
- // declare local refs
- var i, drp, drg, data, arr, len, elem,
- // interaction iteration variables
- x = 0, ia, end = dd.interactions.length,
- // determine the mouse coords
- xy = [ drop.event.pageX, drop.event.pageY ],
- // custom or stored tolerance fn
- tolerance = drop.tolerance || drop.modes[ drop.mode ];
- // go through each passed interaction...
- do if ( ia = dd.interactions[x] ){
- // check valid interaction
- if ( !ia )
- return;
- // initialize or clear the drop data
- ia.drop = [];
- // holds the drop elements
- arr = [];
- len = ia.droppable.length;
- // determine the proxy location, if needed
- if ( tolerance )
- drg = drop.locate( ia.proxy );
- // reset the loop
- i = 0;
- // loop each stored drop target
- do if ( elem = ia.droppable[i] ){
- data = $.data( elem, drop.datakey );
- drp = data.location;
- if ( !drp ) continue;
- // find a winner: tolerance function is defined, call it
- data.winner = tolerance ? tolerance.call( drop, drop.event, drg, drp )
- // mouse position is always the fallback
- : drop.contains( drp, xy ) ? 1 : 0;
- arr.push( data );
- } while ( ++i < len ); // loop
- // sort the drop targets
- arr.sort( drop.sort );
- // reset the loop
- i = 0;
- // loop through all of the targets again
- do if ( data = arr[ i ] ){
- // winners...
- if ( data.winner && ia.drop.length < drop.multi ){
- // new winner... dropstart
- if ( !data.active[x] && !data.anyactive ){
- // check to make sure that this is not prevented
- if ( $special.drag.hijack( drop.event, "dropstart", dd, x, data.elem )[0] !== false ){
- data.active[x] = 1;
- data.anyactive += 1;
- }
- // if false, it is not a winner
- else
- data.winner = 0;
- }
- // if it is still a winner
- if ( data.winner )
- ia.drop.push( data.elem );
- }
- // losers...
- else if ( data.active[x] && data.anyactive == 1 ){
- // former winner... dropend
- $special.drag.hijack( drop.event, "dropend", dd, x, data.elem );
- data.active[x] = 0;
- data.anyactive -= 1;
- }
- } while ( ++i < len ); // loop
- } while ( ++x < end ) // loop
- // check if the mouse is still moving or is idle
- if ( drop.last && xy[0] == drop.last.pageX && xy[1] == drop.last.pageY )
- delete drop.timer; // idle, don't recurse
- else // recurse
- drop.timer = setTimeout(function(){
- drop.tolerate( dd );
- }, drop.delay );
- // remember event, to compare idleness
- drop.last = drop.event;
- }
-
-};
-
-// share the same special event configuration with related events...
-$special.dropinit = $special.dropstart = $special.dropend = drop;
-
+/*!
+ * jquery.event.drop - v 2.1.0
+ * Copyright (c) 2010 Three Dub Media - http://threedubmedia.com
+ * Open Source MIT License - http://threedubmedia.com/code/license
+ */
+// Created: 2008-06-04
+// Updated: 2010-09-03
+// REQUIRES: jquery 1.4.2+, event.drag 2.1+
+
+;(function($){ // secure $ jQuery alias
+
+// Events: drop, dropstart, dropend
+
+// add the jquery instance method
+$.fn.drop = function( str, arg, opts ){
+ // figure out the event type
+ var type = typeof str == "string" ? str : "",
+ // figure out the event handler...
+ fn = $.isFunction( str ) ? str : $.isFunction( arg ) ? arg : null;
+ // fix the event type
+ if ( type.indexOf("drop") !== 0 )
+ type = "drop"+ type;
+ // were options passed
+ opts = ( str == fn ? arg : opts ) || {};
+ // trigger or bind event handler
+ return fn ? this.bind( type, opts, fn ) : this.trigger( type );
+};
+
+// DROP MANAGEMENT UTILITY
+// returns filtered drop target elements, caches their positions
+$.drop = function( opts ){
+ opts = opts || {};
+ // safely set new options...
+ drop.multi = opts.multi === true ? Infinity :
+ opts.multi === false ? 1 : !isNaN( opts.multi ) ? opts.multi : drop.multi;
+ drop.delay = opts.delay || drop.delay;
+ drop.tolerance = $.isFunction( opts.tolerance ) ? opts.tolerance :
+ opts.tolerance === null ? null : drop.tolerance;
+ drop.mode = opts.mode || drop.mode || 'intersect';
+};
+
+// local refs (increase compression)
+var $event = $.event,
+$special = $event.special,
+// configure the drop special event
+drop = $.event.special.drop = {
+
+ // these are the default settings
+ multi: 1, // allow multiple drop winners per dragged element
+ delay: 20, // async timeout delay
+ mode: 'overlap', // drop tolerance mode
+
+ // internal cache
+ targets: [],
+
+ // the key name for stored drop data
+ datakey: "dropdata",
+
+ // count bound related events
+ add: function( obj ){
+ // read the interaction data
+ var data = $.data( this, drop.datakey );
+ // count another realted event
+ data.related += 1;
+ },
+
+ // forget unbound related events
+ remove: function(){
+ $.data( this, drop.datakey ).related -= 1;
+ },
+
+ // configure the interactions
+ setup: function(){
+ // check for related events
+ if ( $.data( this, drop.datakey ) )
+ return;
+ // initialize the drop element data
+ var data = {
+ related: 0,
+ active: [],
+ anyactive: 0,
+ winner: 0,
+ location: {}
+ };
+ // store the drop data on the element
+ $.data( this, drop.datakey, data );
+ // store the drop target in internal cache
+ drop.targets.push( this );
+ },
+
+ // destroy the configure interaction
+ teardown: function(){
+ var data = $.data( this, drop.datakey ) || {};
+ // check for related events
+ if ( data.related )
+ return;
+ // remove the stored data
+ $.removeData( this, drop.datakey );
+ // reference the targeted element
+ var element = this;
+ // remove from the internal cache
+ drop.targets = $.grep( drop.targets, function( target ){
+ return ( target !== element );
+ });
+ },
+
+ // shared event handler
+ handler: function( event, dd ){
+ // local vars
+ var results, $targets;
+ // make sure the right data is available
+ if ( !dd )
+ return;
+ // handle various events
+ switch ( event.type ){
+ // draginit, from $.event.special.drag
+ case 'mousedown': // DROPINIT >>
+ case 'touchstart': // DROPINIT >>
+ // collect and assign the drop targets
+ $targets = $( drop.targets );
+ if ( typeof dd.drop == "string" )
+ $targets = $targets.filter( dd.drop );
+ // reset drop data winner properties
+ $targets.each(function(){
+ var data = $.data( this, drop.datakey );
+ data.active = [];
+ data.anyactive = 0;
+ data.winner = 0;
+ });
+ // set available target elements
+ dd.droppable = $targets;
+ // activate drop targets for the initial element being dragged
+ $special.drag.hijack( event, "dropinit", dd );
+ break;
+ // drag, from $.event.special.drag
+ case 'mousemove': // TOLERATE >>
+ case 'touchmove': // TOLERATE >>
+ drop.event = event; // store the mousemove event
+ if ( !drop.timer )
+ // monitor drop targets
+ drop.tolerate( dd );
+ break;
+ // dragend, from $.event.special.drag
+ case 'mouseup': // DROP >> DROPEND >>
+ case 'touchend': // DROP >> DROPEND >>
+ drop.timer = clearTimeout( drop.timer ); // delete timer
+ if ( dd.propagates ){
+ $special.drag.hijack( event, "drop", dd );
+ $special.drag.hijack( event, "dropend", dd );
+ }
+ break;
+
+ }
+ },
+
+ // returns the location positions of an element
+ locate: function( elem, index ){
+ var data = $.data( elem, drop.datakey ),
+ $elem = $( elem ),
+ posi = $elem.offset() || {},
+ height = $elem.outerHeight(),
+ width = $elem.outerWidth(),
+ location = {
+ elem: elem,
+ width: width,
+ height: height,
+ top: posi.top,
+ left: posi.left,
+ right: posi.left + width,
+ bottom: posi.top + height
+ };
+ // drag elements might not have dropdata
+ if ( data ){
+ data.location = location;
+ data.index = index;
+ data.elem = elem;
+ }
+ return location;
+ },
+
+ // test the location positions of an element against another OR an X,Y coord
+ contains: function( target, test ){ // target { location } contains test [x,y] or { location }
+ return ( ( test[0] || test.left ) >= target.left && ( test[0] || test.right ) <= target.right
+ && ( test[1] || test.top ) >= target.top && ( test[1] || test.bottom ) <= target.bottom );
+ },
+
+ // stored tolerance modes
+ modes: { // fn scope: "$.event.special.drop" object
+ // target with mouse wins, else target with most overlap wins
+ 'intersect': function( event, proxy, target ){
+ return this.contains( target, [ event.pageX, event.pageY ] ) ? // check cursor
+ 1e9 : this.modes.overlap.apply( this, arguments ); // check overlap
+ },
+ // target with most overlap wins
+ 'overlap': function( event, proxy, target ){
+ // calculate the area of overlap...
+ return Math.max( 0, Math.min( target.bottom, proxy.bottom ) - Math.max( target.top, proxy.top ) )
+ * Math.max( 0, Math.min( target.right, proxy.right ) - Math.max( target.left, proxy.left ) );
+ },
+ // proxy is completely contained within target bounds
+ 'fit': function( event, proxy, target ){
+ return this.contains( target, proxy ) ? 1 : 0;
+ },
+ // center of the proxy is contained within target bounds
+ 'middle': function( event, proxy, target ){
+ return this.contains( target, [ proxy.left + proxy.width * .5, proxy.top + proxy.height * .5 ] ) ? 1 : 0;
+ }
+ },
+
+ // sort drop target cache by by winner (dsc), then index (asc)
+ sort: function( a, b ){
+ return ( b.winner - a.winner ) || ( a.index - b.index );
+ },
+
+ // async, recursive tolerance execution
+ tolerate: function( dd ){
+ // declare local refs
+ var i, drp, drg, data, arr, len, elem,
+ // interaction iteration variables
+ x = 0, ia, end = dd.interactions.length,
+ // determine the mouse coords
+ xy = [ drop.event.pageX, drop.event.pageY ],
+ // custom or stored tolerance fn
+ tolerance = drop.tolerance || drop.modes[ drop.mode ];
+ // go through each passed interaction...
+ do if ( ia = dd.interactions[x] ){
+ // check valid interaction
+ if ( !ia )
+ return;
+ // initialize or clear the drop data
+ ia.drop = [];
+ // holds the drop elements
+ arr = [];
+ len = ia.droppable.length;
+ // determine the proxy location, if needed
+ if ( tolerance )
+ drg = drop.locate( ia.proxy );
+ // reset the loop
+ i = 0;
+ // loop each stored drop target
+ do if ( elem = ia.droppable[i] ){
+ data = $.data( elem, drop.datakey );
+ drp = data.location;
+ if ( !drp ) continue;
+ // find a winner: tolerance function is defined, call it
+ data.winner = tolerance ? tolerance.call( drop, drop.event, drg, drp )
+ // mouse position is always the fallback
+ : drop.contains( drp, xy ) ? 1 : 0;
+ arr.push( data );
+ } while ( ++i < len ); // loop
+ // sort the drop targets
+ arr.sort( drop.sort );
+ // reset the loop
+ i = 0;
+ // loop through all of the targets again
+ do if ( data = arr[ i ] ){
+ // winners...
+ if ( data.winner && ia.drop.length < drop.multi ){
+ // new winner... dropstart
+ if ( !data.active[x] && !data.anyactive ){
+ // check to make sure that this is not prevented
+ if ( $special.drag.hijack( drop.event, "dropstart", dd, x, data.elem )[0] !== false ){
+ data.active[x] = 1;
+ data.anyactive += 1;
+ }
+ // if false, it is not a winner
+ else
+ data.winner = 0;
+ }
+ // if it is still a winner
+ if ( data.winner )
+ ia.drop.push( data.elem );
+ }
+ // losers...
+ else if ( data.active[x] && data.anyactive == 1 ){
+ // former winner... dropend
+ $special.drag.hijack( drop.event, "dropend", dd, x, data.elem );
+ data.active[x] = 0;
+ data.anyactive -= 1;
+ }
+ } while ( ++i < len ); // loop
+ } while ( ++x < end ) // loop
+ // check if the mouse is still moving or is idle
+ if ( drop.last && xy[0] == drop.last.pageX && xy[1] == drop.last.pageY )
+ delete drop.timer; // idle, don't recurse
+ else // recurse
+ drop.timer = setTimeout(function(){
+ drop.tolerate( dd );
+ }, drop.delay );
+ // remember event, to compare idleness
+ drop.last = drop.event;
+ }
+
+};
+
+// share the same special event configuration with related events...
+$special.dropinit = $special.dropstart = $special.dropend = drop;
+
})(jQuery); // confine scope
View
168 event.drop/jquery.event.drop.live.js
@@ -1,82 +1,88 @@
-/*!
- * jquery.event.drop.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-13
-// REQUIRES: jquery 1.4.2+, event.drag 2.1+, event.drop 2.1+
-
-;(function($){ // secure $ jQuery alias
-
-// local refs (increase compression)
-var $event = $.event,
-// ref the drop special event config
-drop = $event.special.drop,
-// old drop event add method
-origadd = drop.add,
-// old drop event teradown method
-origteardown = drop.teardown;
-
-// the namespace for internal live events
-drop.livekey = "livedrop";
-
-// new drop event add method
-drop.add = function( obj ){
- // call the old method
- origadd.apply( this, arguments );
- // read the data
- var data = $.data( this, drop.datakey );
- // bind the live "dropinit" delegator
- if ( !data.live && obj.selector ){
- data.live = true;
- $event.add( this, "dropinit."+ drop.livekey, drop.delegate );
- }
-};
-
-// new drop event teardown method
-drop.teardown = function(){
- // call the old method
- origteardown.apply( this, arguments );
- // remove the "live" delegation
- $event.remove( this, "dropinit", drop.delegate );
-};
-
-// identify potential delegate elements
-drop.delegate = function( event, dd ){
- // local refs
- var elems = [], $targets,
- // element event structure
- events = $.data( this, "events" ) || {};
- // query live events
- $.each( events.live || [], function( i, obj ){
- // no event type matches
- if ( obj.preType.indexOf("drop") !== 0 )
- return;
- // locate the elements to delegate
- $targets = $( event.currentTarget ).find( obj.selector );
- // no element found
- if ( !$targets.length )
- return;
- // take each target...
- $targets.each(function(){
- // add an event handler
- $event.add( this, obj.origType +'.'+ drop.livekey, obj.origHandler, obj.data );
- // remember new elements
- if ( $.inArray( this, elems ) < 0 )
- elems.push( this );
- });
- });
- // may not exist when artifically triggering dropinit event
- if ( dd )
- // clean-up after the interaction ends
- $event.add( dd.drag, "dragend."+drop.livekey, function(){
- $.each( elems.concat( this ), function(){
- $event.remove( this, '.'+ drop.livekey );
- });
- });
- //drop.delegates.push( elems );
- return elems.length ? $( elems ) : false;
-};
-
+/*!
+ * jquery.event.drop.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-13
+// REQUIRES: jquery 1.4.2+, event.drag 2.1+, event.drop 2.1+
+
+;(function($){ // secure $ jQuery alias
+
+// local refs (increase compression)
+var $event = $.event,
+// ref the drop special event config
+drop = $event.special.drop,
+// old drop event add method
+origadd = drop.add,
+// old drop event teradown method
+origteardown = drop.teardown;
+
+// the namespace for internal live events
+drop.livekey = "livedrop";
+
+// new drop event add method
+drop.add = function( obj ){
+ // call the old method
+ origadd.apply( this, arguments );
+ // read the data
+ var data = $.data( this, drop.datakey );
+ // bind the live "dropinit" delegator
+ if ( !data.live && obj.selector ){
+ data.live = true;
+ $event.add( this, "dropinit."+ drop.livekey, drop.delegate );
+ }
+};
+
+// new drop event teardown method
+drop.teardown = function(){
+ // call the old method
+ origteardown.apply( this, arguments );
+ // read the data
+ var data = $.data( this, drop.datakey ) || {};
+ // remove the live "dropinit" delegator
+ if ( data.live ){
+ // remove the "live" delegation
+ $event.remove( this, "dropinit", drop.delegate );
+ data.live = false;
+ }
+};
+
+// identify potential delegate elements
+drop.delegate = function( event, dd ){
+ // local refs
+ var elems = [], $targets,
+ // element event structure
+ events = $.data( this, "events" ) || {};
+ // query live events
+ $.each( events.live || [], function( i, obj ){
+ // no event type matches
+ if ( obj.preType.indexOf("drop") !== 0 )
+ return;
+ // locate the elements to delegate
+ $targets = $( event.currentTarget ).find( obj.selector );
+ // no element found
+ if ( !$targets.length )
+ return;
+ // take each target...
+ $targets.each(function(){
+ // add an event handler
+ $event.add( this, obj.origType +'.'+ drop.livekey, obj.origHandler, obj.data );
+ // remember new elements
+ if ( $.inArray( this, elems ) < 0 )
+ elems.push( this );
+ });
+ });
+ // may not exist when artifically triggering dropinit event
+ if ( dd )
+ // clean-up after the interaction ends
+ $event.add( dd.drag, "dragend."+drop.livekey, function(){
+ $.each( elems.concat( this ), function(){
+ $event.remove( this, '.'+ drop.livekey );
+ });
+ });
+ //drop.delegates.push( elems );
+ return elems.length ? $( elems ) : false;
+};
+
})( jQuery ); // confine scope
View
18 event.drop/test/binding.js 100644 → 100755
@@ -1,9 +1,11 @@
-test("Event Binding",function(){
+module("Event Binding");
+
+$.each(['dropinit','dropstart','drop','dropend'],function( i, type ){
- expect( 36 );
-
- $.each(['dropinit','dropstart','drop','dropend'],function( i, type ){
+ test('"'+ type +'"',function(){
+ expect( 9 );
+
// make sure the event handler gets bound to the element
var $elem = $('<div />'),
elem = $elem[0],
@@ -12,15 +14,15 @@ test("Event Binding",function(){
count += 1;
};
- ok( $elem.bind( type, fn )[0] == elem, ".bind('"+ type +"', fn )" );
+ ok( $elem.bind( type, fn )[0] == elem, '.bind("'+ type +'", fn )' );
ok( $.data( elem, $.event.special.drop.datakey ), "drop 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( $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.drop.datakey ), "drop data removed" );
View
236 event.drop/test/handlers.js 100644 → 100755
@@ -1,116 +1,130 @@
-test("Event Handlers",function(){
-
- expect( 24 );
+;(function(){
- // create the markup for the test
- var $drag = $('<div class="drag"/>')
- .appendTo( document.body )
- .css({
- position: 'absolute',
- top: 0,
- left: 0,
- height: 100,
- width: 100
- })
- .bind("dragend",{ drop:'.drop' },function( event, dd ){
- same( dd.drop, dragend, "drop (dragend)" );
- }),
- $drop = $('<div class="drop"/><div class="extra"/>')
- .appendTo( document.body )
- .css({
- position: 'absolute',
- top: 0,
- left: 0,
- height: 100,
- width: 100
- })
- .bind("dropinit dropstart drop dropend",function( event, dd ){
- counts[ event.type ] += 1;
- return returned[ event.type ];
- }),
- counts, returned, dragend,
- reset = function(){
- $.drop({ mode:'overlap', multi:false });
- counts = { dropinit:0, dropstart:0, drop:0, dropend:0 };
- returned = {};
- };
-
- // test DROPINIT FALSE
- ok( true, 'DROPINIT returns false...' );
- // test prep
- reset();
- returned.dropinit = false;
- dragend = [];
- // simulate a partial drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
- // check counts
- equals( counts.dropinit, 1, "dropinit");
- equals( counts.dropstart, 0, "dropstart");
- equals( counts.drop, 0, "drop");
- equals( counts.dropend, 0, "dropend");
+ module("Event Handlers");
- // test DROPSTART FALSE
- ok( true, 'DROPSTART returns false...' );
- // test prep
- reset();
- returned.dropstart = false;
- dragend = [];
- // simulate a partial drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
- // check counts
- equals( counts.dropinit, 1, "dropinit");
- equals( counts.dropstart, 1, "dropstart");
- equals( counts.drop, 0, "drop");
- equals( counts.dropend, 0, "dropend");
+ // a simple re-usable test harness object
+ var obj = {
+ init: function( opts ){
+ obj.$drag = $('<div class="drag"/>')
+ .appendTo( document.body )
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .bind("dragend",{ drop:'.drop' },function( event, dd ){
+ same( dd.drop, obj.dragend, "drop (dragend)" );
+ });
+ obj.$drop = $('<div class="drop"/><div class="extra"/>')
+ .appendTo( document.body )
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .bind("dropinit dropstart drop dropend",function( event, dd ){
+ obj[ event.type ] += 1;
+ return obj.returned[ event.type ];
+ });
+ $.extend( obj, { dropinit:0, dropstart:0, drop:0, dropend:0 });
+ $.drop({ mode:'overlap', multi:false });
+ obj.returned = {};
+ obj.dragend = null;
+ },
+ mouse: function(){
+ var start = {
+ pageX: Math.round( Math.random() * 90 ) + 5,
+ pageY: Math.round( Math.random() * 90 ) + 5
+ },
+ end = {
+ pageX: Math.round( Math.random() * 90 ) + start.pageX,
+ pageY: Math.round( Math.random() * 90 ) + start.pageY
+ };
+ // simulate a complete mouse drag
+ obj.$drag
+ .fire("mousedown", start )
+ .fire("mousemove", end )
+ .fire("mouseup", end )
+ .fire("click", end );
+ },
+ done: function( ms ){
+ obj.$drag.remove();
+ obj.$drop.remove();
+ start();
+ }
+ };
- // test DROP FALSE
- ok( true, 'DROP returns false...' );
- // test prep
- reset();
- returned.drop = false;
- dragend = [];
- // simulate a partial drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
- // check counts
- equals( counts.dropinit, 1, "dropinit");
- equals( counts.dropstart, 1, "dropstart");
- equals( counts.drop, 1, "drop");
- equals( counts.dropend, 0, "dropend");
-
- // test DROPINIT returns elements
- ok( true, 'DROPINIT returns elements...' );
- // test prep
- reset();
- returned.dropinit = $drop.eq(1);
- dragend = [ $drop[1] ];
- // simulate a partial drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
- // check counts
- equals( counts.dropinit, 1, "dropinit");
- equals( counts.dropstart, 1, "dropstart");
- equals( counts.drop, 1, "drop");
- equals( counts.dropend, 1, "dropend");
+ asyncTest('"dropinit" return false',function(){
+ expect( 5 );
+ // test prep
+ obj.init();
+ obj.returned['dropinit'] = false;
+ obj.dragend = [];
+ // simulate a partial drag
+ obj.mouse();
+ // check counts
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 0, "dropstart");
+ equals( obj.drop, 0, "drop");
+ equals( obj.dropend, 0, "dropend");
+ // continue
+ obj.done();
+ });
+
+ asyncTest('"dropstart" return false',function(){
+ expect( 5 );
+ // test prep
+ obj.init();
+ obj.returned['dropstart'] = false;
+ obj.dragend = [];
+ // simulate a partial drag
+ obj.mouse();
+ // check counts
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 1, "dropstart");
+ equals( obj.drop, 0, "drop");
+ equals( obj.dropend, 0, "dropend");
+ // continue
+ obj.done();
+ });
- // test clean-up
- setTimeout(function(){
- $drag.remove();
- $drop.remove();
- }, 20 );
-});
+ asyncTest('"drop" return false',function(){
+ expect( 5 );
+ // test prep
+ obj.init();// test DROP FALSE
+ obj.returned['drop'] = false;
+ obj.dragend = [];
+ // simulate a partial drag
+ obj.mouse();
+ // check counts
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 1, "dropstart");
+ equals( obj.drop, 1, "drop");
+ equals( obj.dropend, 0, "dropend");
+ // continue
+ obj.done();
+ });
+
+ asyncTest('"dropinit" return elements',function(){
+ expect( 5 );
+ // test prep
+ obj.init();
+ obj.returned['dropinit'] = obj.$drop.eq(1);
+ obj.dragend = [ obj.$drop[1] ];
+ // simulate a partial drag
+ obj.mouse();
+ // check counts
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 1, "dropstart");
+ equals( obj.drop, 1, "drop");
+ equals( obj.dropend, 1, "dropend");
+ // continue
+ obj.done();
+ });
+
+})();
View
57 event.drop/test/index.html 100644 → 100755
@@ -1,27 +1,32 @@
-<!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml">
-<head>
-<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-<link href="./inc/qunit.css" rel="stylesheet" />
-<script src="./inc/jquery-1.4.2.js"></script>
-<script src="./inc/qunit.js"></script>
-<script src="./inc/jquery.fire.js"></script>
-<script src="./inc/jquery.event.drag.js"></script>
-<script src="./inc/jquery.event.drop.js"></script>
-<script src="./requirements.js"></script>
-<script src="./binding.js"></script>
-<script src="./method.js"></script>
-<script src="./tolerance.js"></script>
-<script src="./interaction.js"></script>
-<script src="./properties.js"></script>
-<script src="./handlers.js"></script>
-<script src="./live.js"></script>
-<title>ThreeDubMedia &middot; jquery.event.drop.js</title>
-</head>
-<body>
-<h1 id="qunit-header">jquery.event.drop.js Unit Tests</h1>
-<h2 id="qunit-banner"></h2>
-<h2 id="qunit-userAgent"></h2>
-<ol id="qunit-tests"></ol>
-</body>
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<link href="../../img/favicon.ico" rel="shortcut icon" />
+<link href="../../jquery/qunit.css" rel="stylesheet" />
+<script src="../../jquery/jquery.js"></script>
+<script src="../../jquery/qunit.js"></script>
+<script src="../../fire/jquery.fire.js"></script>
+<script src="../../event.drag/jquery.event.drag.js"></script>
+<script src="../../event.drag/jquery.event.drag.live.js"></script>
+<script src="../jquery.event.drop.js"></script>
+<script src="../jquery.event.drop.live.js"></script>
+<script src="./requirements.js"></script>
+<script src="./binding.js"></script>
+<script src="./method.js"></script>
+<script src="./tolerance.js"></script>
+<script src="./interaction.js"></script>
+<script src="./touch.js"></script>
+<script src="./properties.js"></script>
+<script src="./handlers.js"></script>
+<script src="./live.js"></script>
+
+<title>ThreeDubMedia &middot; jQuery.event.drop.js</title>
+</head>
+<body>
+<h1 id="qunit-header">jQuery.event.drop.js Unit Tests</h1>
+<h2 id="qunit-banner"></h2>
+<h2 id="qunit-userAgent"></h2>
+<ol id="qunit-tests"></ol>
+</body>
</html>
View
234 event.drop/test/interaction.js 100644 → 100755
@@ -1,144 +1,134 @@
-test("Interaction Options",function(){
-
- expect( 36 );
-
- // create the markup for the test
- var $drag = $('<div />')
- .css({
- position: 'absolute',
- top: 0,
- left: 0,
- height: 100,
- width: 100
- })
- .appendTo( document.body )
- .bind("draginit dragstart drag dragend click", function( event ){
- counts[ event.type ] += 1;
- }),
- $drop = $('<div />')
- .css({
- position: 'absolute',
- top: 0,
- left: 0,
- height: 100,
- width: 100
- })
- .appendTo( document.body )
- .bind("dropinit dropstart drop dropend",function( event ){
- counts[ event.type ] += 1;
- }),
- counts,
- reset = function( opts ){
- counts = {
- draginit:0, dragstart:0, drag:0, dragend:0,
- dropinit:0, dropstart:0, drop:0, dropend:0,
- click:0
- };
- $.extend( $drag.data( $.event.special.drag.datakey ), $.event.special.drag.defaults, opts );
- };
+;(function(){
+ module("Mouse Interaction");
+
+ // a simple re-usable test harness object
+ var obj = {
+ init: function( opts ){
+ obj.$drag = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("draginit dragstart drag dragend click", opts || {}, function( event ){
+ obj[ event.type ] += 1;
+ });
+ obj.$drop = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("dropinit dropstart drop dropend",function( event ){
+ obj[ event.type ] += 1;
+ });
+ $.extend( obj, {
+ draginit:0, dragstart:0, drag:0, dragend:0,
+ dropinit:0, dropstart:0, drop:0, dropend:0,
+ click:0
+ });
+ },
+ mouse: function(){
+ var start = {
+ pageX: Math.round( Math.random() * 90 ) + 5,
+ pageY: Math.round( Math.random() * 90 ) + 5
+ },
+ end = {
+ pageX: Math.round( Math.random() * 90 ) + start.pageX,
+ pageY: Math.round( Math.random() * 90 ) + start.pageY
+ };
+ // simulate a complete mouse drag
+ obj.$drag
+ .fire("mousedown", start )
+ .fire("mousemove", end )
+ .fire("mouseup", end )
+ .fire("click", end );
+ },
+ done: function(){
+ obj.$drag.remove();
+ obj.$drop.remove();
+ start();
+ }
+ };
- setTimeout(function(){
- ok( true, "Drag and Drop defaults...");
+ asyncTest('Drag and Drop defaults',function(){
// prep DEFAULT interaction
- reset();
+ obj.init();
// simulate DEFAULT interaction
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
+ obj.mouse();
// inspect results
- equals( counts.draginit, 1, "draginit");
- equals( counts.dragstart, 1, "dragstart");
- equals( counts.drag, 1, "drag");
- equals( counts.dragend, 1, "dragend");
- equals( counts.dropinit, 1, "dropinit");
- equals( counts.dropstart, 1, "dropstart");
- equals( counts.drop, 1, "drop");
- equals( counts.dropend, 1, "dropend");
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 1, "dropstart");
+ equals( obj.drop, 1, "drop");
+ equals( obj.dropend, 1, "dropend");
// continue
- start();
- }, 20 );
- stop();
+ obj.done();
+ });
- setTimeout(function(){
- ok( true, 'Drag "drop" option (false)...');
+ asyncTest('Drag "drop" option (false)',function(){
// prep interaction
- reset({ drop:false });
+ obj.init({ drop:false });
// simulate drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
+ obj.mouse();
// inspect results
- equals( counts.draginit, 1, "draginit");
- equals( counts.dragstart, 1, "dragstart");
- equals( counts.drag, 1, "drag");
- equals( counts.dragend, 1, "dragend");
- equals( counts.dropinit, 0, "dropinit");
- equals( counts.dropstart, 0, "dropstart");
- equals( counts.drop, 0, "drop");
- equals( counts.dropend, 0, "dropend");
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 0, "dropinit");
+ equals( obj.dropstart, 0, "dropstart");
+ equals( obj.drop, 0, "drop");
+ equals( obj.dropend, 0, "dropend");
// continue
- start();
- }, 20 );
- stop();
+ obj.done();
+ });
- setTimeout(function(){
- ok( true, 'Drag "drop" option (unmatched)...');
+ asyncTest('Drag "drop" option (unmatched)',function(){
// prep interaction
- reset({ drop:'body' });
+ obj.init({ drop:'body' });
// simulate drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
+ obj.mouse();
// inspect results
- equals( counts.draginit, 1, "draginit");
- equals( counts.dragstart, 1, "dragstart");
- equals( counts.drag, 1, "drag");
- equals( counts.dragend, 1, "dragend");
- equals( counts.dropinit, 0, "dropinit");
- equals( counts.dropstart, 0, "dropstart");
- equals( counts.drop, 0, "drop");
- equals( counts.dropend, 0, "dropend");
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 0, "dropinit");
+ equals( obj.dropstart, 0, "dropstart");
+ equals( obj.drop, 0, "drop");
+ equals( obj.dropend, 0, "dropend");
// continue
- start();
- }, 20 );
- stop();
-
- setTimeout(function(){
- ok( true, 'Drag "drop" option (matched)...');
+ obj.done();
+ });
+
+ asyncTest('Drag "drop" option (matched)',function(){
// prep interaction
- reset({ drop:'div' });
+ obj.init({ drop:'div' });
// simulate drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
+ obj.mouse();
// inspect results
- equals( counts.draginit, 1, "draginit");
- equals( counts.dragstart, 1, "dragstart");
- equals( counts.drag, 1, "drag");
- equals( counts.dragend, 1, "dragend");
- equals( counts.dropinit, 1, "dropinit");
- equals( counts.dropstart, 1, "dropstart");
- equals( counts.drop, 1, "drop");
- equals( counts.dropend, 1, "dropend");
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 1, "dropstart");
+ equals( obj.drop, 1, "drop");
+ equals( obj.dropend, 1, "dropend");
// continue
- start();
- }, 20 );
- stop();
-
- // test clean-up
- setTimeout(function(){
- $drag.remove();
- $drop.remove();
- }, 20 );
+ obj.done();
+ });
-});
+})();
View
102 event.drop/test/live.js 100644 → 100755
@@ -1,48 +1,60 @@
-test("Live Delegation",function(){
-
- expect( 0 );
-
- // set up the delegation
- $('.drop-live').live("dropinit dropstart drop dropend",function( event ){
- counts[ event.type ] += 1;
- equals( this, $drop[0], event.type+" target" );
- });
- // local refs
- var counts = { dropinit:0, dropstart:0, drop:0, dropend:0 },
- // add a div to test the delegation
- $drop = $('<div class="drop-live" />')
- .css({
- position: 'absolute',
- top: 0, left: 0,
- height: 100, width: 100
- })
- .appendTo( document.body ),
- // add a dragger
- $drag = $('<div class="drag-live" />')
- .css({
- position: 'absolute',
- top: 0, left: 0,
- height: 100, width: 100
- })
- .appendTo( document.body )
- .drag(function(){ },{ drop:'.drop-live' });
-
- // check triggering of the event handlers
- $.each(["dropend","drop","dropstart","dropinit"],function( i, type ){
- $drop.trigger( type );
- equals( counts[ type ], 1, "triggered "+ type );
- counts[ type ] = 0;
- });
+module("Live Delegation");
+
+$.each(["dropinit","dropstart","drop","dropend"],function( i, type ){
+
+ test('"'+ type+'"',function(){
+
+ expect( i ? 5 : 1 );
+
+ if ( !i ){
+ ok( true, 'Not supported for this event type.');
+ return;
+ }
+
+ // set up the delegation
+ $('.drop').live( type, function( event ){
+ count += 1;
+ equals( this, $drop[0], event.type+" target" );
+ });
+ // local refs
+ var count = 0,
+ // add a div to test the delegation
+ $drop = $('<div class="drop" />')
+ .css({
+ position: 'absolute',
+ top: 0, left: 0,
+ height: 100, width: 100
+ })
+ .appendTo( document.body ),
+ // add a dragger
+ $drag = $('<div class="drag" />')
+ .css({
+ position: 'absolute',
+ top: 0, left: 0,
+ height: 100, width: 100
+ })
+ .appendTo( document.body )
+ .drag(function(){ });
+
+ // check triggering of the event handlers
+ ok( $drop.trigger( type ), '.trigger("'+ type +'")');
+ equals( count, 1, "event was triggered");
+
+ // simulate a complete drag
+ $drag
+ .fire("mousedown",{ pageX:50, pageY:50 })
+ .fire("mousemove",{ pageX:51, pageY:51 })
+ .fire("mouseup",{ pageX:51, pageY:51 })
+ .fire("click",{ pageX:51, pageY:51 });
+
+ // check the event handler counts
+ equals( count, 2, "event was delegated");
+
+ // remove delegation
+ $('.drop').die( type );
+ $drag.remove();
+ $drop.remove();
- // simulate a complete drag
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
-
- // check the event handler counts
- $.each(["dropend","drop","dropstart","dropinit"],function( i, type ){
- equals( counts[ type ], 1, "delegated "+ type );
});
+
});
View
63 event.drop/test/method.js 100644 → 100755
@@ -1,9 +1,11 @@
-test("Instance and Static Methods",function(){
-
- expect( 29 );
-
- $.each(['init','start','','end'],function( i, type ){
-
+module("Drop Method");
+
+$.each(['init','start','','end'],function( i, type ){
+
+ test('"drop'+ type +'"',function(){
+
+ expect( 9 );
+
// make sure the event handler gets bound to the element
var $elem = $('<div />'),
elem = $elem[0],
@@ -12,39 +14,50 @@ test("Instance and Static Methods",function(){
count += 1;
};
- ok( $elem.drop( type, fn )[0] == elem, ".drop("+( type ? "'"+ type +"'," : "" )+" fn )" );
+ ok( $elem.drop( type, fn )[0] == elem, ".drop("+( type ? '"'+ type +'",' : "" )+" fn )" );
ok( $.data( elem, $.event.special.drop.datakey ), "drop data exists" );
ok( $.data( elem, "events" ), "event data exists" );
- ok( $.data( elem, "events" )[ 'drop'+type ][0], 'drop'+ type +" event handler added" );
+ ok( $.data( elem, "events" )[ 'drop'+type ][0], '"drop'+ type +'" event handler added' );
- ok( $elem.drop( type )[0] == elem, ".drop("+( type ? "'"+ type +"'," : "" )+")" );
+ ok( $elem.drop( type )[0] == elem, ".drop("+( type ? '"'+ type +'"' : "" )+")" );
ok( count == 1, "handler was triggered");
+
+ ok( $elem.unbind( "drop"+ type )[0] == elem, '.unbind("drop'+ type +'")' );
+ ok( !$.data( elem, "events" ), "event data removed" );
+ ok( !$.data( elem, $.event.special.drag.datakey ), "drag data removed" );
$elem.remove();
});
-
+
+});
+
+test('$.drop()',function(){
+
+ expect( 4 );
+
// call the static method with settings
- var fn = function(){};
- $.drop({
+ var drop = $.event.special.drop,
+ def = {
+ multi: drop.multi,
+ delay: drop.delay,
+ mode: drop.mode,
+ tolerance: null
+ },
+ opts = {
multi: 99,
delay: 88,
mode: 77,
- tolerance: fn
- });
+ tolerance: function(){}
+ };
+ $.drop( opts );
- ok( $.event.special.drop.multi == 99, "$.drop() static method..." );
- ok( $.event.special.drop.multi == 99, "multi option was set" );
- ok( $.event.special.drop.delay == 88, "delay option was set" );
- ok( $.event.special.drop.mode == 77, "mode option was set" );
- ok( $.event.special.drop.tolerance == fn, "tolerance option was set" );
+ ok( drop.multi == opts.multi, "multi option was set" );
+ ok( drop.delay == opts.delay, "delay option was set" );
+ ok( drop.mode == opts.mode, "mode option was set" );
+ ok( drop.tolerance == opts.tolerance, "tolerance option was set" );
// restore defaults
- $.drop({
- multi: 1,
- delay: 20,
- mode: 'overlap',
- tolerance: null
- });
+ $.drop( def );
});
View
122 event.drop/test/properties.js 100644 → 100755
@@ -1,66 +1,62 @@
-test("Callback Properties",function(){
-
- expect( 18 );
- // create the markup for the test
- var $drag = $('<div class="drag"/>')
- .appendTo( document.body )
- .css({
- position: 'absolute',
- top: 0,
- left: 0,
- height: 100,
- width: 100
- })
- .bind("draginit",function( ev, dd ){
- same( dd.drop, [], "drop (draginit)" );
- same( dd.available, [], "available (draginit)" );
- })
- .bind("dragstart",function( ev, dd ){
- same( dd.drop, [], "drop (dragstart)" );
- same( dd.available, $drop.toArray(), "available (dragstart)" );
- })
- .bind("drag",function( ev, dd ){
- same( dd.drop, [], "drop (drag)" );
- same( dd.available, $drop.toArray(), "available (drag)" );
- })
- .bind("dragend",function( ev, dd ){
- same( dd.drop, $drop.eq(0).toArray(), "drop (dragend)" );
- same( dd.available, $drop.toArray(), "available (dragend)" );
- $drag.remove();
- $drop.remove();
- })
- $drop = $('<div class="drop"/><div class="drop"/>')
- .appendTo( document.body )
- .css({
- position: 'absolute',
- top: 0,
- left: 0,
- height: 100,
- width: 100
- })
- .bind("dropinit",function( ev, dd ){
- same( dd.drop, [], "drop (dropinit)" );
- same( dd.available, [], "available (dropinit)" );
- })
- .bind("dropstart",function( ev, dd ){
- same( dd.drop, [], "drop (dropstart)" );
- same( dd.available, $drop.toArray(), "available (dropstart)" );
- })
- .bind("drop",function( ev, dd ){
- same( dd.drop, $drop.eq(0).toArray(), "drop (drop)" );
- same( dd.available, $drop.toArray(), "available (drop)" );
- })
- .bind("dropend",function( ev, dd ){
- same( dd.drop, $drop.eq(0).toArray(), "drop (dropend)" );
- same( dd.available, $drop.toArray(), "available (dropend)" );
- });
+module("Callback Properties");
+
+$.each(["init","start","","end"],function( i, type ){
- $.drop({ mode:'overlap', multi:false });
- // simulate a complete drag and drop
- $drag
- .fire("mousedown",{ clientX:50, clientY:50 })
- .fire("mousemove",{ clientX:51, clientY:51 })
- .fire("mouseup",{ clientX:51, clientY:51 })
- .trigger("click");
+ test('"drop'+ type +'" callback',function(){
+
+ expect( i ? 10 : 12 );
+
+ // create the markup for the test
+ var $drag = $('<div class="drag"/>')
+ .appendTo( document.body )
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .bind("draginit",function( ev, dd ){
+ same( dd.drop, [], 'draginit: "drop"' );
+ same( dd.available, [], 'draginit: "available"' );
+ })
+ .bind("dragstart",function( ev, dd ){
+ same( dd.drop, [], 'dragstart: "drop"' );
+ same( dd.available, available, 'dragstart: "available"' );
+ })
+ .bind("drag",function( ev, dd ){
+ same( dd.drop, [], 'drag: "drop"' );
+ same( dd.available, available, 'drag: "available"' );
+ })
+ .bind("dragend",function( ev, dd ){
+ same( dd.drop, drop, 'dragend: "drop"' );
+ same( dd.available, available, 'dragend: "available"' );
+ $drag.remove();
+ $drop.remove();
+ }),
+ $drop = $('<div class="drop"/><div class="drop"/>')
+ .appendTo( document.body )
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .drop( type, function( ev, dd ){
+ same( dd.drop, i < 2 ? [] : drop, 'drop'+ type +': "drop"' );
+ same( dd.available, i < 1 ? [] : available, 'drop'+ type +': "available"' );
+ }),
+ available = $drop.toArray(),
+ drop = $drop.eq(0).toArray();
+
+ $.drop({ mode:'overlap', multi:false });
+ // simulate a complete drag and drop
+ $drag
+ .fire("mousedown",{ pageX:50, pageY:50 })
+ .fire("mousemove",{ pageX:51, pageY:51 })
+ .fire("mouseup",{ pageX:51, pageY:51 })
+ .trigger("click");
+ });
});
View
55 event.drop/test/requirements.js 100644 → 100755
@@ -1,20 +1,37 @@
-module("EVENT.DROP");
-
-test("Requirements",function(){
-
- expect( 9 );
-
- // make sure the right jquery is included
- ok( window.jQuery, "jQuery exists" );
- ok( parseFloat( jQuery([]).jquery ) >= 1.4, "jQuery version is 1.4 or greater" );
- ok( jQuery.event.special.drag, "DRAG special event is defined" );
-
- // make sure the event interface is complete
- ok( jQuery.event.special.dropinit, "DROPINIT special event is defined" );
- ok( jQuery.event.special.dropstart, "DROPSTART special event is defined" );
- ok( jQuery.event.special.drop, "DROP special event is defined" );
- ok( jQuery.event.special.dropend, "DROPEND special event is defined" );
- ok( jQuery([]).drop, "$('...').drop() method is defined" );
- ok( jQuery.drop, "$.drop() method is defined" );
-
+module("Requirements");
+
+test("jQuery",function(){
+
+ expect( 2 );
+
+ // make sure the right jquery is included
+ ok( window.jQuery, "jQuery exists" );
+ ok( parseFloat( jQuery([]).jquery ) >= 1.5, "jQuery version is 1.5 or greater" );
+});
+
+test("$.event.special.drag",function(){
+
+ expect( 5 );
+
+ // make sure the event interface is complete
+ ok( jQuery.event.special.draginit, '"draginit" special event is defined' );
+ ok( jQuery.event.special.dragstart, '"dragstart" special event is defined' );
+ ok( jQuery.event.special.drag, '"drag" special event is defined' );
+ ok( jQuery.event.special.dragend, '"dragend" special event is defined' );
+ ok( jQuery([]).drag, "$.fn.drag method is defined" );
+
+});
+
+test("Installation",function(){
+
+ expect( 6 );
+
+ // make sure the event interface is complete
+ ok( jQuery.event.special.dropinit, '"dropinit" special event is defined' );
+ ok( jQuery.event.special.dropstart, '"dropstart" special event is defined' );
+ ok( jQuery.event.special.drop, '"drop" special event is defined' );
+ ok( jQuery.event.special.dropend, '"dropend" special event is defined' );
+ ok( jQuery([]).drop, '$.fn.drop method is defined' );
+ ok( jQuery.drop, '$.drop method is defined' );
+
});
View
311 event.drop/test/tolerance.js 100644 → 100755
@@ -1,156 +1,189 @@
-test("Tolerance Modes",function(){
-
- expect( 130 );
+;(function(){
+
+ module("Tolerance Mode");
- // create the markup for the test
- var $drop = $('<div />')
- .css({
- position: 'absolute',
- top: 200,
- left: 200,
- height: 100,
- width: 100
- })
- .appendTo( document.body )
- .bind("dropinit",function( event ){
- if ( multidrop )
- return $( this )
- .clone( true )
- .addClass('clone')
- .appendTo( document.body )
- .add( this );
- })
- .bind("drop",function( event ){
- count += 1;
- }),
- $drag = $('<div />')
- .css({
- position: 'absolute',
- top: 100,
- left: 100,
- height: 100,
- width: 100
- })
- .appendTo( document.body )
- .bind("draginit", function( event ){
- if ( multidrag )
- return $( this )
- .clone( true )
- .addClass('clone')
- .appendTo( document.body )
- .add( this );
- })
- .bind("dragstart",function( ev, dd ){
- if ( proxy )
- return $( this ).clone()
- .addClass('clone')
- .appendTo( document.body );
- })
- .bind("drag",function( ev, dd ){
- if ( move )
- $( dd.proxy ).css({
- top: dd.offsetY,
- left: dd.offsetX
+ // re-usable test harness object
+ var obj = {
+ init: function( opts ){
+ // the drop element
+ obj.$drop = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 200,
+ left: 200,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("dropinit",function( event ){
+ if ( obj.multidrop )
+ return $( this )
+ .clone( true )
+ .addClass('clone')
+ .appendTo( document.body )
+ .add( this );
+ })
+ .bind("drop",function( event ){
+ obj.count += 1;
});
- })
- .bind("dragend",function( ev, dd ){
- var $div = $( this ).css({
- top: 100,
- left: 100
- });
- $( document.body ).find('.clone').remove();
- }),
- // local refs
- count, move, proxy, multidrag, multidrop;
+ // the drag element
+ obj.$drag = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 100,
+ left: 100,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("draginit", function( event ){
+ if ( obj.multidrag )
+ return $( this )
+ .clone( true )
+ .addClass('clone')
+ .appendTo( document.body )
+ .add( this );
+ })
+ .bind("dragstart",function( ev, dd ){
+ if ( obj.proxy )
+ return $( this ).clone()
+ .addClass('clone')
+ .appendTo( document.body );
+ })
+ .bind("drag",function( ev, dd ){
+ if ( obj.move )
+ $( dd.proxy ).css({
+ top: dd.offsetY,
+ left: dd.offsetX
+ });
+ })
+ .bind("dragend",function( ev, dd ){
+ var $div = $( this ).css({
+ top: 100,
+ left: 100
+ });
+ $( document.body ).find('.clone').remove();
+ });
+ // reset test vars
+ obj.count = 0;
+ obj.move = obj.proxy = obj.multidrag = obj.multidrop = null;
+ },
+ done: function(){
+ obj.$drag.remove();
+ obj.$drop.remove();
+ start();
+ }
+ };
- // test MOUSE mode
- setTimeout(function(){
-
- // test each mode...
- $.each({
- 'mouse':{
- 'overlap':[0,0,0,0,0],
- 'middle':[0,0,0,0,0],
- 'fit':[0,0,0,0,0],
- 'mouse':[1,1,1,2,4],
- 'away':[0,0,0,0,0]
- },
- 'overlap':{
- 'overlap':[0,1,1,2,4],
- 'middle':[0,1,1,2,4],
- 'fit':[0,1,1,2,4],
- 'mouse':[0,0,0,0,0],
- 'away':[0,0,0,0,0]
- },
- 'middle':{
- 'overlap':[0,0,0,0,0],
- 'middle':[0,1,1,2,4],
- 'fit':[0,1,1,2,4],
- 'mouse':[0,0,0,0,0],
- 'away':[0,0,0,0,0]
- },
- 'fit':{
- 'overlap':[0,0,0,0,0],
- 'middle':[0,0,0,0,0],
- 'fit':[0,1,1,2,4],
- 'mouse':[0,0,0,0,0],
- 'away':[0,0,0,0,0]
- },
- 'intersect':{
- 'overlap':[0,1,1,2,4],
- 'middle':[0,1,1,2,4],
- 'fit':[0,1,1,2,4],
- 'mouse':[1,1,1,2,4],
- 'away':[0,0,0,0,0]
+ // async iterator
+ function asyncEach( obj, func, after ){
+ // local refs
+ var arr = [], i = 0, len;
+ // translate into a secondary array
+ $.each( obj, function( index, value ){
+ len = arr.push({ that:this, index:index, value:value });
+ });
+ function next(){
+ var data = arr[ i++ ], ret;
+ if ( i <= len ){
+ if ( data )
+ ret = func.call( data.that, data.index, data.value, next );
+ if ( ret === false )
+ done();
}
- }, function( mode, expected ){
- // execute interaction variants
- ok( true, mode.toUpperCase() +' tolerance...');
+ else done();
+ };
+ function done(){
+ if ( after )
+ after();
+ };
+ next();
+ }
+
+ // test each mode...
+ $.each({
+ 'mouse':{
+ 'overlap':[0,0,0,0,0],
+ 'middle':[0,0,0,0,0],
+ 'fit':[0,0,0,0,0],
+ 'mouse':[1,1,1,2,4],
+ 'away':[0,0,0,0,0]
+ },
+ 'overlap':{
+ 'overlap':[0,1,1,2,4],
+ 'middle':[0,1,1,2,4],
+ 'fit':[0,1,1,2,4],
+ 'mouse':[0,0,0,0,0],
+ 'away':[0,0,0,0,0]
+ },
+ 'middle':{
+ 'overlap':[0,0,0,0,0],
+ 'middle':[0,1,1,2,4],
+ 'fit':[0,1,1,2,4],
+ 'mouse':[0,0,0,0,0],
+ 'away':[0,0,0,0,0]
+ },
+ 'fit':{
+ 'overlap':[0,0,0,0,0],
+ 'middle':[0,0,0,0,0],
+ 'fit':[0,1,1,2,4],
+ 'mouse':[0,0,0,0,0],
+ 'away':[0,0,0,0,0]
+ },
+ 'intersect':{
+ 'overlap':[0,1,1,2,4],
+ 'middle':[0,1,1,2,4],
+ 'fit':[0,1,1,2,4],
+ 'mouse':[1,1,1,2,4],
+ 'away':[0,0,0,0,0]
+ }
+ }, function( mode, expected ){
+
+ // test each mode
+ asyncTest('"'+ mode +'"',function(){
+
+ expect( 25 );
+
// test prep
- $.drop({ mode:mode, multi:true });
+ obj.init();
+ $.drop({
+ mode:mode,
+ multi:true
+ });
// drag to each position
- $.each({
- 'overlap':{ clientX:33, clientY:33 },
- 'middle':{ clientX:66, clientY:66 },
- 'fit':{ clientX:100, clientY:100 },
- 'mouse':{ clientX:250, clientY:250 },
- 'away':{ clientX:-1, clientY:-1 }
- }, function( where, coord ){
+ asyncEach({
+ 'overlap':{ pageX:33, pageY:33 },
+ 'middle':{ pageX:66, pageY:66 },
+ 'fit':{ pageX:100, pageY:100 },
+ 'mouse':{ pageX:250, pageY:250 },
+ 'away':{ pageX:-1, pageY:-1 }
+ }, function( where, coord, next ){
// execute interaction variants
- $.each([
+ asyncEach([
'dropped '+ where +' (no motion)',
'dropped '+ where +' (drag motion)',
'dropped '+ where +' (proxy motion)',
'dropped '+ where +' (multi drag)',
'dropped '+ where +' (multi drop)'
- ], function( i, msg ){
+ ], function( i, msg, next ){
// set-up
- count = 0;
- move = ( i > 0 );
- proxy = ( i > 1 );
- multidrag = ( i > 2 );
- multidrop = ( i > 3 );
- // simulate a complete drag
- $drag
- .fire("mousedown",{ clientX:0, clientY:0 })
+ obj.count = 0;
+ obj.move = ( i > 0 );
+ obj.proxy = ( i > 1 );
+ obj.multidrag = ( i > 2 );
+ obj.multidrop = ( i > 3 );
+ // simulate a partial drag
+ obj.$drag
+ .fire("mousedown",{ pageX:0, pageY:0 })
.fire("mousemove", coord )
.fire("mouseup", coord )
- .trigger("click");
- // check counts
- equals( count, expected[where][i], msg );
- });
- });
+ .fire("click", coord );
+ equals( obj.count, expected[where][i], msg );
+ next();
+ }, next );
+ }, obj.done );
});
- // continue
- start();
- }, 20 );
- stop();
+
+ });
- // test clean-up
- setTimeout(function(){
- $drag.remove();
- $drop.remove();
- }, 20 );
-
-});
-
+})();
View
140 event.drop/test/touch.js
@@ -0,0 +1,140 @@
+;(function(){
+
+ module("Touch Interaction");
+
+ if ( !document.createEvent ){
+ test("Touch Simulation Not Supported",function(){
+ ok( true, 'This browser does not support "document.createEvent" and cannot simulate touch events.');
+ });
+ return;
+ }
+
+ // a simple re-usable test harness object
+ var obj = {
+ init: function( opts ){
+ obj.$drag = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("draginit dragstart drag dragend click", opts || {}, function( event ){
+ obj[ event.type ] += 1;
+ });
+ obj.$drop = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("dropinit dropstart drop dropend",function( event ){
+ obj[ event.type ] += 1;
+ });
+ $.extend( obj, {
+ draginit:0, dragstart:0, drag:0, dragend:0,
+ dropinit:0, dropstart:0, drop:0, dropend:0,
+ click:0
+ });
+ },
+ touch: function(){
+ var start = {
+ pageX: Math.round( Math.random() * 90 ) + 5,
+ pageY: Math.round( Math.random() * 90 ) + 5
+ },
+ end = {
+ pageX: Math.round( Math.random() * 90 ) + start.pageX,
+ pageY: Math.round( Math.random() * 90 ) + start.pageY
+ };
+ // simulate a complete mouse drag
+ obj.$drag
+ .fire("touchstart", start )
+ .fire("touchmove", end )
+ .fire("touchend", end )
+ .fire("click", end );
+ },
+ done: function(){
+ obj.$drag.remove();
+ obj.$drop.remove();
+ start();
+ }
+ };
+
+ asyncTest('Drag and Drop defaults',function(){
+ // prep DEFAULT interaction
+ obj.init();
+ // simulate DEFAULT interaction
+ obj.touch();
+ // inspect results
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 1, "dropstart");
+ equals( obj.drop, 1, "drop");
+ equals( obj.dropend, 1, "dropend");
+ // continue
+ obj.done();
+ });
+
+ asyncTest('Drag "drop" option (false)',function(){
+ // prep interaction
+ obj.init({ drop:false });
+ // simulate drag
+ obj.touch();
+ // inspect results
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 0, "dropinit");
+ equals( obj.dropstart, 0, "dropstart");
+ equals( obj.drop, 0, "drop");
+ equals( obj.dropend, 0, "dropend");
+ // continue
+ obj.done();
+ });
+
+ asyncTest('Drag "drop" option (unmatched)',function(){
+ // prep interaction
+ obj.init({ drop:'body' });
+ // simulate drag
+ obj.touch();
+ // inspect results
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 0, "dropinit");
+ equals( obj.dropstart, 0, "dropstart");
+ equals( obj.drop, 0, "drop");
+ equals( obj.dropend, 0, "dropend");
+ // continue
+ obj.done();
+ });
+
+ asyncTest('Drag "drop" option (matched)',function(){
+ // prep interaction
+ obj.init({ drop:'div' });
+ // simulate drag
+ obj.touch();
+ // inspect results
+ equals( obj.draginit, 1, "draginit");
+ equals( obj.dragstart, 1, "dragstart");
+ equals( obj.drag, 1, "drag");
+ equals( obj.dragend, 1, "dragend");
+ equals( obj.dropinit, 1, "dropinit");
+ equals( obj.dropstart, 1, "dropstart");
+ equals( obj.drop, 1, "drop");
+ equals( obj.dropend, 1, "dropend");
+ // continue
+ obj.done();
+ });
+
+})();
Please sign in to comment.
Something went wrong with that request. Please try again.