Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery's drag and drop - reverse engineering Facebook's uiTokenizer on 'Arts & Interests'

I'm reverse engineering facebook's uiTokenizer using $.special.event.drag and $.special.event.drop and jQuery because jQuery UI's sortable / draggable is heavy and slow.

My question I think is related to the tolerance math for deciding when to inject the placeholder element.

I've cleaned up a lot of this code since I first wrote it but the premise is still there and should work for this question.

Here is the code and logic: http://jsfiddle.net/JoshuaIRL/kf9Qt/ for testing.

The area I need somebody to look at would be the "tolerance" area of the $.drop function if you do find for it.

 $.drop({       mode:'intersect',        tolerance: function( event, proxy, target ){            var testV = event.pageY > ( target.top + target.height / 2 );             $.data(target.elem, "drop+reorder", testV ? "insertAfter" : "insertBefore" );                return this.contains( target, [ event.pageX, event.pageY ] );        }  }); 

Facebook's seems to do an "insertAfter" when your actual cursor hits another element.

I'm thinking this might be the heart of it as well:

if ( drop && $(drop).is('.uiToken') && ( drop != dd.current || method != dd.method ) ){           $( this )[ method ]( drop );       dd.current = drop;       dd.method = method;       dd.update();      refreshTokens(clickOffset);      placeholder.insertAfter($(this)); } 

Also, I need to figure out how to ensure I'm not over-binding my elements with this stupid open-ended $.drop() method ... :-\

Any help you may offer will be greatly appreciated.

UPDATE: To answer your comments... If you go to the JSFiddle link and you drag an element over another element... It should immediately bump it out of the way in all directions. To see a comparison from mine versus Facebook's, checkout theirs at https://www.facebook.com//favorites then click on "Edit" button.

Whether or not the "Tolerance" method is what's wrong, their interests list is what I'm looking to duplicate. I don't even know if the Tolerance area is what's wrong, I want to achieve what they are doing.

I hope this helps!

like image 579
Joshua F. Rountree Avatar asked Feb 22 '11 14:02

Joshua F. Rountree


1 Answers

It looks like you are just determining if the draggables right intersects the targets left, and if the draggables top intersects the targets bottom (with some tolerance).

There are 2 ways I would go about this:

  1. Find all intersect elements and find the largest area - in this case the left square of the image below.
  2. If the draggable has the same dimensions as the droppable the 1/4 overlap would suffice - in this case the right square.

enter image description here

The second is by far the easiest to implement but is no means as general as the first. So by having a quick look through your code, I would loop through all the "possible" droppable elements determine the area of the overlap and target the largest.

like image 193
martin Avatar answered Oct 04 '22 11:10

martin