1 | /*! |
---|
2 | * jquery.event.drag.live - v 2.2 |
---|
3 | * Copyright (c) 2010 Three Dub Media - http://threedubmedia.com |
---|
4 | * Open Source MIT License - http://threedubmedia.com/code/license |
---|
5 | */ |
---|
6 | // Created: 2010-06-07 |
---|
7 | // Updated: 2012-05-21 |
---|
8 | // REQUIRES: jquery 1.7.x, event.drag 2.2 |
---|
9 | |
---|
10 | ;(function( $ ){ |
---|
11 | |
---|
12 | // local refs (increase compression) |
---|
13 | var $event = $.event, |
---|
14 | // ref the special event config |
---|
15 | drag = $event.special.drag, |
---|
16 | // old drag event add method |
---|
17 | origadd = drag.add, |
---|
18 | // old drag event teradown method |
---|
19 | origteardown = drag.teardown; |
---|
20 | |
---|
21 | // allow events to bubble for delegation |
---|
22 | drag.noBubble = false; |
---|
23 | |
---|
24 | // the namespace for internal live events |
---|
25 | drag.livekey = "livedrag"; |
---|
26 | |
---|
27 | // new drop event add method |
---|
28 | drag.add = function( obj ){ |
---|
29 | // call the old method |
---|
30 | origadd.apply( this, arguments ); |
---|
31 | // read the data |
---|
32 | var data = $.data( this, drag.datakey ); |
---|
33 | // bind the live "draginit" delegator |
---|
34 | if ( !data.live && obj.selector ){ |
---|
35 | data.live = true; |
---|
36 | $event.add( this, "draginit."+ drag.livekey, drag.delegate ); |
---|
37 | } |
---|
38 | }; |
---|
39 | |
---|
40 | // new drop event teardown method |
---|
41 | drag.teardown = function(){ |
---|
42 | // call the old method |
---|
43 | origteardown.apply( this, arguments ); |
---|
44 | // read the data |
---|
45 | var data = $.data( this, drag.datakey ) || {}; |
---|
46 | // bind the live "draginit" delegator |
---|
47 | if ( data.live ){ |
---|
48 | // remove the "live" delegation |
---|
49 | $event.remove( this, "draginit."+ drag.livekey, drag.delegate ); |
---|
50 | data.live = false; |
---|
51 | } |
---|
52 | }; |
---|
53 | |
---|
54 | // identify potential delegate elements |
---|
55 | drag.delegate = function( event ){ |
---|
56 | // local refs |
---|
57 | var elems = [], target, |
---|
58 | // element event structure |
---|
59 | events = $.data( this, "events" ) || {}; |
---|
60 | // query live events |
---|
61 | $.each( events || [], function( key, arr ){ |
---|
62 | // no event type matches |
---|
63 | if ( key.indexOf("drag") !== 0 ) |
---|
64 | return; |
---|
65 | $.each( arr || [], function( i, obj ){ |
---|
66 | // locate the element to delegate |
---|
67 | target = $( event.target ).closest( obj.selector, event.currentTarget )[0]; |
---|
68 | // no element found |
---|
69 | if ( !target ) |
---|
70 | return; |
---|
71 | // add an event handler |
---|
72 | $event.add( target, obj.origType+'.'+drag.livekey, obj.origHandler || obj.handler, obj.data ); |
---|
73 | // remember new elements |
---|
74 | if ( $.inArray( target, elems ) < 0 ) |
---|
75 | elems.push( target ); |
---|
76 | }); |
---|
77 | }); |
---|
78 | // if there are no elements, break |
---|
79 | if ( !elems.length ) |
---|
80 | return false; |
---|
81 | // return the matched results, and clenup when complete |
---|
82 | return $( elems ).bind("dragend."+ drag.livekey, function(){ |
---|
83 | $event.remove( this, "."+ drag.livekey ); // cleanup delegation |
---|
84 | }); |
---|
85 | }; |
---|
86 | |
---|
87 | })( jQuery ); |
---|