/* * Copyright (c) 2011 OJC Technologies. All rights reserved. * @author: Jake Smith */ function ProximityHelper(pageX, pageY) { this.x = pageX; this.y = pageY; this.ABOVE = 1; this.BELOW = 2; this.RIGHT = 3; this.LEFT = 4; this.WITHIN = 0; this.ERR = -1; this.getInfoFromRelation = function(terminalNodes) { var relation, properNode, index, firstNode, lastNode; firstNode = terminalNodes[0]; lastNode = terminalNodes[terminalNodes.length - 1]; if (this.clickedAbove(firstNode)) { relation = this.ABOVE; properNode = firstNode; index = 0; } else if (this.clickedToLeft(terminalNodes)) { relation = this.LEFT; properNode = firstNode; index = 0; } else if (this.clickedBelow(lastNode)) { relation = this.BELOW; properNode = lastNode; index = properNode.nodeValue.length; } else { relation = this.RIGHT; properNode = lastNode; index = properNode.nodeValue.length; } return new Array(relation, properNode, index); } //-----------------------// // "Private" functions // //-----------------------// this.clickedAbove = function(textNode) { var bounds, top; bounds = this.getBoundingRectFor(textNode); top = Math.round(bounds.top); return this.y < top; } this.clickedBelow = function(textNode) { var bounds, bottom; bounds = this.getBoundingRectFor(textNode); bottom = Math.round(bounds.bottom); return this.y > bottom; } this.getBoundingRectFor = function(textNode) { var span, bounds; span = makeTempSpan(textNode); textNode.parentNode.replaceChild(span, textNode); bounds = span.getBoundingClientRect(); span.parentNode.replaceChild(textNode, span); return bounds; } this.clickedToLeft = function(terminalNodes) { var i, j, rects, r, tempSpan, parent, currNode; for (i = 0; i < terminalNodes.length; i += 1) { currNode = terminalNodes[i]; parent = currNode.parentNode; tempSpan = makeTempSpan(currNode); parent.replaceChild(tempSpan, currNode); rects = tempSpan.getClientRects(); for (j = 0; j < rects.length; j += 1) { r = rects[j]; if (inVerticalBounds(r, this.y)) { if (toLeftOf(r, this.x)) { parent.replaceChild(currNode, tempSpan); return true; } } } parent.replaceChild(currNode, tempSpan); } return false; } } // ProximityHelper(pageX, pageY) function inVerticalBounds(rect, y) { var top, bottom top = Math.round(rect.top) + window.scrollY; bottom = Math.round(rect.bottom) + window.scrollY; return top < y && y < bottom; } function toLeftOf(rect, x) { var leftEdge = rect.left + window.scrollX; return x < leftEdge; }