2 * jquery.event.drop.live - v 2.2
3 * Copyright (c) 2010 Three Dub Media - http://threedubmedia.com
4 * Open Source MIT License - http://threedubmedia.com/code/license
8 // REQUIRES: jquery 1.7.x, event.drag 2.2, event.drop 2.2
10 ;(function($){ // secure $ jQuery alias
12 // local refs (increase compression)
14 // ref the drop special event config
15 drop = $event.special.drop,
16 // old drop event add method
18 // old drop event teradown method
19 origteardown = drop.teardown;
21 // allow events to bubble for delegation
22 drop.noBubble = false;
24 // the namespace for internal live events
25 drop.livekey = "livedrop";
27 // new drop event add method
28 drop.add = function( obj ){
29 // call the old method
30 origadd.apply( this, arguments );
32 var data = $.data( this, drop.datakey );
33 // bind the live "dropinit" delegator
34 if ( !data.live && obj.selector ){
36 $event.add( this, "dropinit."+ drop.livekey, drop.delegate );
40 // new drop event teardown method
41 drop.teardown = function(){
42 // call the old method
43 origteardown.apply( this, arguments );
45 var data = $.data( this, drop.datakey ) || {};
46 // remove the live "dropinit" delegator
48 // remove the "live" delegation
49 $event.remove( this, "dropinit", drop.delegate );
54 // identify potential delegate elements
55 drop.delegate = function( event, dd ){
57 var elems = [], $targets,
58 // element event structure
59 events = $.data( this, "events" ) || {};
61 $.each( events || [], function( key, arr ){
62 // no event type matches
63 if ( key.indexOf("drop") !== 0 )
65 $.each( arr, function( i, obj ){
66 // locate the elements to delegate
67 $targets = $( event.currentTarget ).find( obj.selector );
69 if ( !$targets.length )
71 // take each target...
72 $targets.each(function(){
73 // add an event handler
74 $event.add( this, obj.origType +'.'+ drop.livekey, obj.origHandler || obj.handler, obj.data );
75 // remember new elements
76 if ( $.inArray( this, elems ) < 0 )
81 // may not exist when artifically triggering dropinit event
83 // clean-up after the interaction ends
84 $event.add( dd.drag, "dragend."+drop.livekey, function(){
85 $.each( elems.concat( this ), function(){
86 $event.remove( this, '.'+ drop.livekey );
89 //drop.delegates.push( elems );
90 return elems.length ? $( elems ) : false;
93 })( jQuery ); // confine scope