diff life_calendar/tooltip.js @ 81:256b8df1c686

add life_calendar
author paulo
date Fri, 17 Jun 2016 22:24:17 -0700
parents
children a0fdf2cf1d53
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/life_calendar/tooltip.js	Fri Jun 17 22:24:17 2016 -0700
     1.3 @@ -0,0 +1,58 @@
     1.4 +// Based on http://matthias-schuetz.js.org/tooltip.js/
     1.5 +
     1.6 +var _tt_options = {
     1.7 +	tooltipId: "tooltip",
     1.8 +	offsetDefault: 15
     1.9 +};
    1.10 +
    1.11 +function getTooltipElm() {
    1.12 +	return document.querySelector("#" + _tt_options.tooltipId);
    1.13 +}
    1.14 +
    1.15 +function adjustTooltip(evt, tooltipElm, title, text) {
    1.16 +	var offset = _tt_options.offsetDefault;
    1.17 +	var scrollY = window.scrollY || window.pageYOffset;
    1.18 +	var scrollX = window.scrollX || window.pageXOffset;
    1.19 +	var tooltipTop = evt.pageY + offset;
    1.20 +	var tooltipLeft = evt.pageX + offset;
    1.21 +
    1.22 +	tooltipTop = (tooltipTop - scrollY + tooltipElm.offsetHeight + 20 >= window.innerHeight ? (tooltipTop - tooltipElm.offsetHeight - 20) : tooltipTop);
    1.23 +	tooltipLeft = (tooltipLeft - scrollX + tooltipElm.offsetWidth + 20 >= window.innerWidth ? (tooltipLeft - tooltipElm.offsetWidth - 20) : tooltipLeft);
    1.24 +
    1.25 +	tooltipElm.style.top = tooltipTop + "px";
    1.26 +	tooltipElm.style.left = tooltipLeft + "px";
    1.27 +
    1.28 +	setTooltipText(tooltipElm, title, text);
    1.29 +}
    1.30 +
    1.31 +function removeTooltip() {
    1.32 +	document.querySelector("body").removeChild(getTooltipElm());
    1.33 +}
    1.34 +
    1.35 +function createTooltip(evt, title, text) {
    1.36 +	var tooltipElm = getTooltipElm();
    1.37 +
    1.38 +	if (!tooltipElm) {
    1.39 +		tooltipElm = document.createElement("div");
    1.40 +		tooltipElm.appendChild(document.createElement("h1"));
    1.41 +		tooltipElm.appendChild(document.createElement("h2"));
    1.42 +
    1.43 +		tooltipElm.style.position = "absolute";
    1.44 +		tooltipElm.setAttribute("id", _tt_options.tooltipId);
    1.45 +
    1.46 +		document.querySelector("body").appendChild(tooltipElm);
    1.47 +	}
    1.48 +
    1.49 +	adjustTooltip(evt, tooltipElm, title, text);
    1.50 +}
    1.51 +
    1.52 +function setTooltipText(tooltipElm, title, text) {
    1.53 +	var eTitle = tooltipElm.children[0];
    1.54 +	var eText = tooltipElm.children[1];
    1.55 +	if (eTitle && title) {
    1.56 +		eTitle.textContent = title;
    1.57 +	}
    1.58 +	if (eText && text) {
    1.59 +		eText.textContent = text;
    1.60 +	}
    1.61 +}