69 lines
2.2 KiB
JavaScript
69 lines
2.2 KiB
JavaScript
// glossary — hover tooltip for .gls-sym usages (prose links and MathJax \class nodes)
|
|
(function () {
|
|
var tip = null;
|
|
|
|
function ensureTip() {
|
|
if (tip === null) {
|
|
tip = document.createElement("div");
|
|
tip.className = "gls-float";
|
|
tip.style.display = "none";
|
|
document.body.appendChild(tip);
|
|
}
|
|
return tip;
|
|
}
|
|
|
|
function keyOf(el) {
|
|
if (el.dataset && el.dataset.glsKey) return el.dataset.glsKey;
|
|
for (var i = 0; i < el.classList.length; i++) {
|
|
var c = el.classList[i];
|
|
if (c.indexOf("gls-key-") === 0) return c.slice(8);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function show(target) {
|
|
var key = keyOf(target);
|
|
if (!key) return;
|
|
var entry = document.getElementById("gls-" + key);
|
|
var t = ensureTip();
|
|
if (entry) {
|
|
// for a main entry, don't drag every property into the popup
|
|
var node = entry.cloneNode(true);
|
|
node.querySelectorAll(".gls-prop-entry").forEach(function (n) { n.remove(); });
|
|
t.innerHTML = node.innerHTML;
|
|
} else if (window.__glsData && window.__glsData[key]) {
|
|
// entry lives on another page (book chapters render separately)
|
|
t.innerHTML = window.__glsData[key];
|
|
} else {
|
|
return;
|
|
}
|
|
t.style.display = "block";
|
|
if (window.MathJax && window.MathJax.typesetPromise) {
|
|
window.MathJax.typesetPromise([t]).catch(function () {});
|
|
}
|
|
var r = target.getBoundingClientRect();
|
|
var x = r.left + window.scrollX;
|
|
var y = r.bottom + window.scrollY + 6;
|
|
// keep it on screen horizontally
|
|
var w = t.offsetWidth;
|
|
var maxX = window.scrollX + document.documentElement.clientWidth - w - 12;
|
|
t.style.left = Math.max(window.scrollX + 8, Math.min(x, maxX)) + "px";
|
|
t.style.top = y + "px";
|
|
}
|
|
|
|
function hide() {
|
|
if (tip !== null) tip.style.display = "none";
|
|
}
|
|
|
|
document.addEventListener("mouseover", function (e) {
|
|
var el = e.target.closest ? e.target.closest(".gls-sym") : null;
|
|
if (el) show(el);
|
|
});
|
|
document.addEventListener("mouseout", function (e) {
|
|
var el = e.target.closest ? e.target.closest(".gls-sym") : null;
|
|
if (el) hide();
|
|
});
|
|
document.addEventListener("click", hide);
|
|
window.addEventListener("scroll", hide, { passive: true });
|
|
})();
|