Net web server - BC452Part VIBonus Chapters} else if (evt.offsetX || evt.offsetY)
Sunday, January 20th, 2008BC452Part VIBonus Chapters} else if (evt.offsetX || evt.offsetY) { offsetX = evt.offsetX - ((evt.offsetX < -2) ? 0 : document.body.scrollLeft); offsetY = evt.offsetY - ((evt.offsetY < -2) ? 0 : document.body.scrollTop); } setBGColor(selectedStateLabel, yellow ); return false; } } The dragIt()function, compact as it is, provides the main action in the application by keep- ing a selected state object under the cursor as the user moves the mouse. This function iscalled repeatedly by the mousemoveevents, although the actual event handling methodol- ogy varies with platform (precisely the same way as with engage(), as shown previously). Regardless of the event property detected, event coordinates (minus the previously pre- served offsets) are passed the shiftTo()function in the API. Before the dragging action branch of the function ends, the event object s cancelBubbleproperty is set to true. In truth, only the IE4+ and W3C DOM event objects have such a prop- erty, but assigning a value to a nonexistent object property for NN4 does no harm. It s impor- tant that this function operate as quickly as possible, because it must execute with eachmousemoveevent. Canceling event bubbling helps in a way, but more important, the cancella- tion allows the mousemoveevent to be used for other purposes, as described in a moment. // move div on mousemovefunction dragIt(evt) { evt = (evt) ? evt : event; if (selectedObj) { if (evt.pageX) { shiftTo(selectedObj, (evt.pageX - offsetX), (evt.pageY offsetY)); } else if (evt.clientX || evt.clientY) { shiftTo(selectedObj, (evt.clientX - offsetX), (evt.clientY offsetY)); } evt.cancelBubble = true; return false; } } When a user drops the currently selected map object, the release()function invokes theonTarget()function to find out if the current location of the map is within range of thedesired destination. If it is in range, the background color of the state label object is set togreen, and the done property of the selected state s database entry is set to true. One addi- tional test (the isDone()function call) looks to see if all the done properties are truein thedatabase. If so, the congratsobject is shown. But if the object is not in the right place, thelabel reverts to its original red color. In case the user moves a state that was previously okay, its database entry is also adjusted. No matter what the outcome, however, the user hasdropped the map, so key global variables are set to nulland the layer order for the item isset to zero (bottom of the heap) so that it doesn t interfere with the next selected map. One more condition is possible in the release()function. As shown later in the initializationfunction, the documentobject s onmousemoveevent handler is assigned to the release() function (to compare the onmousemoveevents for the state maps go to dragIt()). The rea- soning behind this document-level event assignment is that no matter how streamlined thedragging function may be, it is possible for the user to move the mouse so fast that the mapcan t keep up. At that point, mousemoveevents are firing at the document(or other object,