paulo@81: // Based on http://matthias-schuetz.js.org/tooltip.js/ paulo@81: paulo@81: var _tt_options = { paulo@81: tooltipId: "tooltip", paulo@81: offsetDefault: 15 paulo@81: }; paulo@81: paulo@81: function getTooltipElm() { paulo@81: return document.querySelector("#" + _tt_options.tooltipId); paulo@81: } paulo@81: paulo@86: function adjustTooltip(evt, tooltipElm, title, text, subtext) { paulo@81: var offset = _tt_options.offsetDefault; paulo@81: var scrollY = window.scrollY || window.pageYOffset; paulo@81: var scrollX = window.scrollX || window.pageXOffset; paulo@81: var tooltipTop = evt.pageY + offset; paulo@81: var tooltipLeft = evt.pageX + offset; paulo@81: paulo@81: tooltipTop = (tooltipTop - scrollY + tooltipElm.offsetHeight + 20 >= window.innerHeight ? (tooltipTop - tooltipElm.offsetHeight - 20) : tooltipTop); paulo@81: tooltipLeft = (tooltipLeft - scrollX + tooltipElm.offsetWidth + 20 >= window.innerWidth ? (tooltipLeft - tooltipElm.offsetWidth - 20) : tooltipLeft); paulo@81: paulo@81: tooltipElm.style.top = tooltipTop + "px"; paulo@81: tooltipElm.style.left = tooltipLeft + "px"; paulo@81: paulo@86: setTooltipText(tooltipElm, title, text, subtext); paulo@81: } paulo@81: paulo@81: function removeTooltip() { paulo@81: document.querySelector("body").removeChild(getTooltipElm()); paulo@81: } paulo@81: paulo@86: function createTooltip(evt, title, text, subtext) { paulo@81: var tooltipElm = getTooltipElm(); paulo@81: paulo@81: if (!tooltipElm) { paulo@81: tooltipElm = document.createElement("div"); paulo@81: tooltipElm.appendChild(document.createElement("h1")); paulo@81: tooltipElm.appendChild(document.createElement("h2")); paulo@86: tooltipElm.appendChild(document.createElement("h3")); paulo@81: paulo@86: tooltipElm.style.position = "absolute"; tooltipElm.setAttribute("id", _tt_options.tooltipId); paulo@81: paulo@81: document.querySelector("body").appendChild(tooltipElm); paulo@81: } paulo@81: paulo@86: adjustTooltip(evt, tooltipElm, title, text, subtext); paulo@81: } paulo@81: paulo@86: function setTooltipText(tooltipElm, title, text, subtext) { paulo@81: var eTitle = tooltipElm.children[0]; paulo@81: var eText = tooltipElm.children[1]; paulo@86: var eSubText = tooltipElm.children[2]; paulo@81: if (eTitle && title) { paulo@81: eTitle.textContent = title; paulo@81: } paulo@81: if (eText && text) { paulo@81: eText.textContent = text; paulo@81: } paulo@86: if (eSubText) { paulo@86: eSubText.textContent = subtext; paulo@86: } paulo@81: }