Posts Tagged ‘dnd’

February 9th, 2009 4 Comments »
Tags: , , ,

Category: News, Screencasts

iGoogle-like drag-and-drop for Shindig (ala Dojo)

My consulting firm has been spending a good bit of time augmenting and providing developer advice for a really interesting startup based out of Berkeley, CA called Life360; in case Life360 sounds familiar, it might be because they were one of the first place teams in Google’s Android Developer Challenge a while back. In short, the crew out in Berkeley is developing an incredible developer platform for “taking care of your family’s what-ifs”, and it’s been a lot of fun getting to be in on all of the discussions and working with the amazing talent on board there to see it happening.

One of the most recent tasks we’ve been working on is an iGoogle-like drag-and-drop container for gadgets. There’s a really nice project out there called Shindig that implements the gadget specification, but there’s little to be had in the way of a nice sophisticated container that gives you drag-and-drop operations, a custom title bar, and a maximized view. Well, after getting chunks of the new application platform wrapped up for some investment pitches in the coming weeks, we decided that it would be really cool to give some of this code back to the OSS community since we’ve benefited so much from Shindig, the LAMP stack, and a number of useful utilities here and there.

90 sec (6.4MB) QuickTime file of dnd in action across multiple browsers

4 min (13.5MB) QuickTime file of installing Shindig and using the container extensions

You can read the blog post on for the details as well as watch some screencasts that walks you through downloading Shindig and putting the files to use, but I did want to take just a moment to make brief mention of a couple of the technical considerations that immediately come to mind:

  • When doing anything like this, you need infrastructure. For this part of the project, Dojo streamlined tons of things like setting up event handlers, styling nodes, getting coordinates on the screen, abstracting a lightweight fabric for drag-and-drop operations, and so forth. Currently, the code uses AOL’s CDN to keep things nice and simple, but you’d definitely want to use the build tools to consolidate and minify the code when it’s game time.
  • When an IFRAME is inserted into the DOM, it (re)loads. So, when you’re dragging an IFRAME all over the screen, its location in the DOM doesn’t change and it doesn’t continually reload because it’s just being positioned absolutely. It’s when the drag event ends that you’re faced with the reload event because you want to move it into its new column (versus trying to maintain the geometry of never manipulating the DOM tree and keeping track of everything in a snap-to-grid fashion.) Anyhow, that’s the approach we took. The one reload event that takes place when the gadget being drug reaches its final destination seemed more reasonable than all of that geometry. Other than the frame getting drug around, no other IFRAMES ever get removed and reinserted, so they never have to reload.
  • For security reasons, Shindig renders gadgets inside of their own individual IFRAMEs versus rendering gadgets in an element like a DIV. That may not sound like a big deal, but it does bring up a few more implementation details worth noting. The first one that comes to mind is that when you’re dragging a DIV that contains an IFRAME, it’s not uncommon for the mouse to occasionally slide ever so slightly into the IFRAME. Unfortunately, the IFRAME then captures mouse events, and then drag-and-drop starts to get choppy and wig out a bit. You can work around this by creating a transparent overlay and placing it on top of the IFRAME during drag events so that the IFRAME can never capture those mouse events. Overlays also come in handy when you popup menus on the titlebar and want them to disappear when the user clicks anywhere else on the screen (including in other IFRAMEs.) One day, hopefully, Caja will kill those IFRAMEs, which will also probably help performance a bit (especially on IE.)
  • There are a few peculiarities that arise when you remove the last element from a container like a DIV, so one way you can deal with this is to ensure it always has a minimum height and width by placing an element in there that spans the width of the column and has a minimal height so that it can accept drag events.
  • As always, there are always a few special cases, so you have to do a little extra bookkeeping here and there. Hopefully the code is commented well enough that it’s not hard to follow. All in all, it’s only about 150 lines for the drag and drop mechanics, including whitespace.

Life360 would love to have you go take a look at their site and rate some of their ideas, so why not head on over there, browse around, and leave some feedback on what they’re doing. Helping startups that have good intentions is the cool thing to do, right?