Rich Waters

Ext, Javascript, Notes/Domino, Ext.nd, Ruby on Rails

Update 4/8/06 - Theres a new article available that dives into writing a custom Draggable Class.

Update 3/7/06 - looks like this is my most popular article and I noticed there’s no direct link to the sample javascript, so here you go. rich.js

For those of you who haven’t seen OpenRico its another AJAX library, with quite a few cool extras. What I’m going to cover here is my first expiriment with Rico and their ‘drag and drop’ functionality. Getting basic drag and drop functionality is extremely easy with this library, and with just a bit of modification you can easily make it fit whatever you could want.

First things first, to use Rico you need to get it: Download Open Rico and Prototype (if you haven’t seen protyotype before definitely check it out, OO Javascript at its best). Rico is a rather large library for just an everyday site so before deploying anything with it I would reccomend cutting out anything your not using and running it through a JS Compressor.

Import the libraries just like any other javascript, find or create a couple of div’s within your html to act as the draggable and the drop targets. Now add an onLoad javascript call (or put it elsewhere, perhaps an onclick if you want someone to activate the drag and drop feature) to

dndMgr.registerDropZone( new Dropzone( ‘your html div’ ) );

dndMgr is an object that Rico creates for you to make creating drag and drop objects easy. With the single line above the specified div becomes a drop target for the draggable objects. Now with one more call, a different div can become a draggable object to be dropped on the first.

dndMgr.registerDraggable( new Draggable(’test-rico-dnd’,'your html div2′) );

Again calling a method of the built in dndMgr object to register a div as a draggable object. Same as before this can be called onLoad, onClick, or in the middle of any other javascript function so you can make it draggable at any point you choose.

To make something happen when an object gets dragged onto a target you need to go a bit more in depth (this is where learnig prototype will come in handy). You need to create your own draggable and Dropzone classes that extend the basic ones provided in Rico. If you look through the Rico classes you’ll notice methods which correspond to the different events that take place, clicking on a draggable object, dragging a draggable object, hovering over a drop target, releasing on a non-drop target, releasing on a drop target, and quite a few more. To create your own functionality override the method in your custom class and add the functionality that you want. Probably the most useful function to override is the Dropzone.accept function, you can use this.htmlElement to access the dropZone and draggableObjects[0].htmlElement to access the first draggable element being dropped on the target.

Here’s a toned down version of what I wrote for work: RouteExample

This page has custom drag and drop objects written to change the effects some, the trash can is the drop target and draggable objects are created on the fly when you press the submit button. Basically we’re allowing the user to create a list of options, if they mess up all they need to is drag the mistake on the trash can (something most people are familiar with due to windows).

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • BleedYellow
  • del.icio.us
  • Technorati
  • Reddit
U Comment, I Follow - Heavily moderated, SPAM will be dealt with.

11 Responses to “Fun with Drag and Drop w/RICO”

  1. Where is the link to the javascript file you said you added?

    Jim

  2. Right at the bottom of the post? It’s
    rich.js, should be able to get to it just fine.

    Rich Waters

  3. Looks like this doesn’t work with Safari.

    Anonymous

  4. Could you be more explanitory on this? perhaps make a very simple example you can provide it’s complete source code with? Thanks so much

    Anonymous

  5. […] After looking at some stats I found that my drag and drop article seems to be getting the most attention so I added a quick update to give a direct link to the sample javascript file. DnD Article […]

    Rich Waters » Blog Archive » Quick Update - Drag & Drop

  6. How can you call the CancelDrag method whenever you want from the

    javascript file.Please let me know

    abhi

  7. If you’re using my example code you should be able to call MyDraggable.cancelDrag() from anywhere else in the file.

    Rich Waters

  8. Hi Rich,

    Im trying to use drag and drop to move items - but only when the ‘handle’ of the item is held and not the whole item.

    So far when you click on the handle, a onMouseDown event uses the ‘new Draggable’ function and allows you to pick it up. However after dropping it I need to somehow de-register it.

    Otherwise if people are using select boxes in the item they tend to drag it accidentally.

    Andy

  9. Great question, it certainly seems popular to only have part of a div be the ‘handle’ that people can grab to drag and Rico makes this pretty easy.

    I would first look at the newer article on writing a custom draggable class.
    http://www.rich-waters.com/blog/2006/04/rico-drag-ndrop-p2-ricodraggable.html

    the key piece you want to look at in the code is the ‘getSingleObjectDragGUI’ method. This method is what Rico uses to get a handle on which object its dragging. I used this to actually create a completely new div which copied the old one and shade the old one out so you get a nice effect.

    What I would do for your above example is give the div or whatever element is the ‘handle’ an id and register that id as the draggable element, then write a custom getSingleObjectDragGUI which returns either the parent element if that works or whichever specific element contains the whole piece you want to drag.

    Hope that makes sense…

    Rich Waters

  10. The examples all work great. Thanks. But when I try to implement my own stuff in Firefox, the mouse and item being dragged are always offset by the size of the browser’s toolbar area. That is, the viewport offset isn’t getting calculated correctly or something.

    E.G:

    ^

    Bryan

  11. [oops — the < in my example messed things up. here it is again…]

    E.G:

    ^ < - mouse poitner

    [] <- div being dragged

    I’ve tried all kinds of combinations of postion:relative/absolute in both outer container divs and my draggables, but no difference at all.

    Any idea what the cause of this might be or how to fix it? Thanks.

    Bryan

Leave a Reply