concept drag in category javascript

appears as: drag, drag
Prototype and Scriptaculous in Action

This is an excerpt from Manning's book Prototype and Scriptaculous in Action.

HTML forms are a mainstay of web application development, and, in the pre-Ajax era, presented the only serious way to elicit information from a user. With Ajax, other input mechanisms, such as drag and drop, can be used as part of a conversation between the browser and the server, but forms still have a very important role to play. Prototype provides a set of utilities for working with HTML forms, which we’ll cover in chapter 11.

Few usability experts would argue that among the most ubiquitous and useful user interface interactions is the technique of drag and drop. Sometimes also termed click and drag, drag and drop is an easily adopted metaphor for moving items around an application, performing various activities, or creating associations between items. It has been a part of the graphical user interface for just about as long as that interaction paradigm has been in existence (introduced commercially with the first Macintosh systems in 1984). Unless you’ve spent your entire programming life staring at a command line, the concept of drag and drop is bound to be a natural and customary part of your everyday computer life.

Even so, you probably don’t actually think too much about the steps involved in dragging and dropping, so we’ll go over a typical drag-and-drop interaction, introducing terms that Scriptaculous uses and that we will utilize within this chapter. The particular interaction we will examine is deleting a file by dragging it to the desktop Trash (or Recycle Bin, or whatever your desktop calls it). This interaction, seemingly universally used as a demonstration of drag and drop, emphasizes the desktop metaphor, as little is more natural to a user than picking up a piece of paper and throwing it in the trash bin next to the desk (though usually there’s no drag-and-drop metaphor for crumpling up the paper beforehand).

7.4. Sorting by drag and drop

In the previous section, we used drag and drop for one possible interaction type: to reorder, or sort, some thumbnail images. Because of an oversight (completely intentional, of course) in the code of that page, we observed an interesting side effect. If we dragged a thumbnail out of and back into the same droppable target, it was removed from that parent and reattached as a child at the end of the children list for that element. This gave us a sort of “poor man’s sort” capability. Not ideal from a usability standpoint, but it worked after a fashion.

But this ability to shift the position of elements around leads us to a broader question: why not just implement a sort by moving elements around (via drag and drop) within the parent element rather than dragging them to a separate drop target? Apparently, the authors of Scriptaculous asked the same question, and the result was the Sortable library.

Figure 12.8. Drag operation 3—slides can be reordered within the editor using drag and drop