13.02.2013 Views

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

WEB STANDARDS CREATIVITY

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Then we can concern ourselves with the vertical distance between the ghost<br />

and the static module. To do this, we measure the difference between the top<br />

of the ghost and the top of the static module, and then compare it with the<br />

minimum value already found. If the distance to the current static module is<br />

less than the current minimum value, that becomes the new minimum value.<br />

After all the dragHotspots entries have been checked against, we know<br />

whether there is a suitable location for the dragged module to be inserted at,<br />

and if so, where.<br />

If there was a suitable location, we need to show this to the users, so that they<br />

know where the dragged module will be inserted if they release the mouse<br />

button. To do this, we create a new element called ghostMarker, which is simply<br />

a thin, square block that sits at the top of the candidate insertion point.<br />

The ghost marker is an absolutely positioned child of the body element, so to<br />

position it, we use the coordinates of the closest static module. As you can<br />

see in Figure 9-14, with a bit of CSS styling, the ghost marker gives a good<br />

indication of where the dragged module is going to be inserted.<br />

To make it easy for later insertion of the dragged module, we also create a<br />

new property of ghostMarker that records which static module it is marking:<br />

ghostMarker.marked.<br />

If there wasn’t a suitable location for the dragged module to be inserted—the<br />

user didn’t drag the ghost close enough to one of the static modules—then<br />

we actually remove ghostMarker. So if the user moved the ghost from a suitable<br />

location to an unsuitable one, it won’t still indicate that he can reposition<br />

the dragged module.<br />

The last operation in mousemoveDocument() again stops the default action that the browser would normally perform for a<br />

mousemove, preventing unwanted actions from occurring while the user is dragging.<br />

The repositioned module<br />

When the user has decided where he wants to place the dragged module, or that he doesn’t want to move it, he will release<br />

the mouse button. This is captured by mouseupDocument():<br />

function mouseupDocument()<br />

{<br />

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

var ghost = document.getElementById("ghost");<br />

if (ghost != null)<br />

{<br />

ghost.parentNode.removeChild(ghost);<br />

}<br />

var ghostMarker = document.getElementById("ghostMarker");<br />

if (ghostMarker != null)<br />

{<br />

if (!hasClass(ghostMarker.marked, "module"))<br />

{<br />

Figure 9-14. The ghost marker showing where<br />

the dragged module will be inserted<br />

chapter 9 Creating Dynamic Interfaces Using JavaScript<br />

237

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

Saved successfully!

Ooh no, something went wrong!