13.02.2013 Views

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ghost.appendChild(dragTarget.cloneNode(true));<br />

ghost.style.left = position[0] + "px";<br />

ghost.style.top = position[1] + "px";<br />

attachEventListener(document, "mousemove", mousemoveDocument, false);<br />

attachEventListener(document, "mouseup", mouseupDocument, false);<br />

event.returnValue = false;<br />

if (typeof event.preventDefault != "undefined")<br />

{<br />

event.preventDefault();<br />

}<br />

return true;<br />

};<br />

After it does the normal event object abstraction, mousedownH2() then goes about finding exactly which element was clicked.<br />

This should be theoretically possible just by referencing the this object, but unfortunately, Internet Explorer doesn’t assign this<br />

object correctly when events are added using the W3C event model. Instead, we have to rely on the event object’s target element<br />

property, which can be inaccurate if there are elements nested inside our event target, but it’s fine here because the h2<br />

is the deepest element in this branch. Naturally, Internet Explorer doesn’t call this element target, as other browsers do; it’s<br />

srcElement in Internet Explorer. So we have to check whether event.target exists (the standard property), and if it doesn’t,<br />

use event.srcElement (the Internet Explorer property).<br />

Once these differences have been sorted out, we create a reference to the parent node of the target element (the div surrounding<br />

the h2) in a global variable called dragTarget, so that we can use it later in other functions.<br />

After the target has been discerned, we then create some global variables that will help track what’s happening as the user<br />

drags the module around. dragOrigin stores the coordinates of the original mousedown event. Whenever an event is fired,<br />

event.clientX and event.clientY record the two coordinates that define the place on a page where an event occurred; in<br />

this case, where the user pushed down the mouse button. It’s important to store this original location, because when the<br />

user moves her mouse, we want to know how far she moved it from where she clicked originally.<br />

A map of the page<br />

dragHotspots is an array that stores the locations of all the possible places you could drag a module. You can think of it as<br />

a sort of map. When users drag a module around, they aren’t changing its position to an arbitrary point on the page; instead,<br />

they are actually changing its position relative to the other modules. So, instead of specifying a module’s destination in terms<br />

of absolute coordinates, we are trying to describe its destination in terms of its location among the other modules: “move<br />

the search module beneath the calendar module, but above the horoscope module.” In order to do this, we need to create<br />

a map of the page indicating where all the other modules are. That way, when the dragged module is moved around the<br />

page, we know whether the dragged module should be above or below each of the static modules. All that calculation is<br />

done by the mousemove handler, though. For now, we’re just concerned with creating the map.<br />

We could create the map inside the mousemove handler, but every time the mouse moved, the calculations would have to<br />

be done again. Creating the map just once—when the mouse button is pressed—is more efficient.<br />

To create the map, we look inside each of the module areas and iterate through all of the divs that have a class of module.<br />

For those divs, we create a new object in the dragHotspots array that holds a reference to the module (element), the horizontal<br />

page offset of the module’s top-left corner (offsetX), and the vertical page offset of the module’s top-left corner<br />

(offsetY). Those last two properties get their values from the getPosition() function, which takes an element and returns<br />

its position:<br />

chapter 9 Creating Dynamic Interfaces Using JavaScript<br />

233

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!