/* * Copyright(c) 2011 * OJC Technologies * all rights reserved * @author: Jake Smith */ function FloatingCursor() { this.element = null; this.styleString1 = "position: absolute; color: black;" + " background-color: red;"; this.styleString2 = "position: absolute; color: black;" + " background-color: green;"; this.id = "floatingcursorelem"; this.style = null; this.remove = function() { document.body.removeChild(this.element); this.element = null; } this.addToPageBehind = function(refElem) { var refTop, refRight, rect; rect = refElem.getBoundingClientRect(); refTop = rect.top; refRight = rect.right; this.style = this.styleString1 + "left: " + refRight + "px; top: "+ refTop + "px;"; this.insertFloatingCursor(); } this.addToPageOver = function(refElem) { var refTop, refLeft, rect, parent, prevSib, ch, tNode, tempElem, nodeText; parent = refElem.parentNode; prevSib = refElem.previousSibling; if (prevSib != null && prevSib.nodeType == Node.TEXT_NODE) { // peek back at further-previous element for reference location nodeText = prevSib.nodeValue; ch = nodeText.substring(nodeText.length - 1); tNode = document.createTextNode(ch); prevSib.nodeValue = nodeText.substring(0, nodeText.length - 1); tempElem = document.createElement("span"); tempElem.setAttribute("id", "tempfloatingrefelem"); tempElem.appendChild(tNode); parent.insertBefore(tempElem, refElem); refElem = tempElem; } rect = refElem.getBoundingClientRect(); refTop = rect.top; refLeft = rect.right; this.style = this.styleString2 + "left: " + refLeft + "px; top: "+ refTop + "px;"; this.insertFloatingCursor(); if (ch != null) { // clean up from our earlier peeking tempElem = document.getElementById("tempfloatingrefelem"); tNode = tempElem.childNodes[0]; parent.replaceChild(tNode, tempElem); parent.normalize(); } } //inernal this.insertFloatingCursor = function() { this.element = document.createElement("span"); this.element.setAttribute('id', this.id); this.element.setAttribute('style', this.style); this.element.appendChild(document.createTextNode("\u00a0")); document.body.appendChild(this.element); } }