MediaWiki:Common.js: Difference between revisions
MediaWiki interface page
More actions
No edit summary |
No edit summary |
||
| Line 379: | Line 379: | ||
tipEl.setAttribute("aria-hidden", "true"); | tipEl.setAttribute("aria-hidden", "true"); | ||
tipEl.style.position = "fixed"; | tipEl.style.position = "fixed"; | ||
tipEl.style.zIndex = "99999"; | tipEl.style.zIndex = "99999"; | ||
| Line 422: | Line 421: | ||
} | } | ||
function positionTipPoint(clientX, clientY) { | function positionTipPoint(clientX, clientY) { | ||
if (!tipEl) return; | if (!tipEl) return; | ||
| Line 465: | Line 436: | ||
x = clamp(x, pad, window.innerWidth - tr.width - pad); | x = clamp(x, pad, window.innerWidth - tr.width - pad); | ||
var y = clientY - tr.height - gap; | var y = clientY - tr.height - gap; | ||
if (y < pad) y = clientY + gap; | |||
if (y < pad) | |||
y = clamp(y, pad, window.innerHeight - tr.height - pad); | y = clamp(y, pad, window.innerHeight - tr.height - pad); | ||
| Line 492: | Line 458: | ||
suppressTitle(el); | suppressTitle(el); | ||
} | } | ||
| Line 519: | Line 482: | ||
} | } | ||
// Hover in/out (desktop) — | // Hover in/out (desktop) — disable rollover if tip is blank | ||
document.addEventListener( | document.addEventListener( | ||
"mouseover", | "mouseover", | ||
function (e) { | function (e) { | ||
if (pinned) return; | if (pinned) return; | ||
var el = closestDef(e.target); | var el = closestDef(e.target); | ||
if (!el) return; | |||
// If we moved onto non-definition content, hide any visible hover tooltip | |||
if (!el) { | |||
if (activeEl) hide(); | |||
return; | |||
} | |||
if (isEntering(el, e.relatedTarget)) return; | if (isEntering(el, e.relatedTarget)) return; | ||
if (activeEl === el) return; | if (activeEl === el) return; | ||
var txt = qTipText(el); | |||
if (!txt) { | |||
// IMPORTANT: no tooltip data -> ensure the old tooltip is not left up | |||
if (activeEl) hide(); | |||
return; | |||
} | |||
show(el); | show(el); | ||
positionTipPoint(e.clientX, e.clientY); | positionTipPoint(e.clientX, e.clientY); | ||
| Line 538: | Line 516: | ||
function (e) { | function (e) { | ||
if (pinned) return; | if (pinned) return; | ||
var el = closestDef(e.target); | var el = closestDef(e.target); | ||
if (!el | if (!el) { | ||
positionTipPoint(e.clientX, e.clientY); | if (activeEl) hide(); | ||
return; | |||
} | |||
if (activeEl && el !== activeEl) return; | |||
var txt = qTipText(el); | |||
if (!txt) { | |||
if (activeEl) hide(); | |||
return; | |||
} | |||
// Track mouse so it never sits under the cursor | |||
if (activeEl) positionTipPoint(e.clientX, e.clientY); | |||
}, | }, | ||
true | true | ||
| Line 559: | Line 550: | ||
// Touch: pin tooltip and position ABOVE the finger press point. | // Touch: pin tooltip and position ABOVE the finger press point. | ||
// If tip is blank, tap does nothing (and closes any pinned tooltip). | |||
document.addEventListener( | document.addEventListener( | ||
"pointerdown", | "pointerdown", | ||
| Line 571: | Line 563: | ||
var txt = qTipText(el); | var txt = qTipText(el); | ||
if (!txt) return; | if (!txt) { | ||
if (pinned) hide(); | |||
return; | |||
} | |||
pinned = true; | pinned = true; | ||
| Line 577: | Line 572: | ||
positionTipPoint(e.clientX, e.clientY); | positionTipPoint(e.clientX, e.clientY); | ||
e.preventDefault(); | e.preventDefault(); | ||
e.stopPropagation(); | e.stopPropagation(); | ||
| Line 585: | Line 579: | ||
// Click behavior: | // Click behavior: | ||
// - If | // - If link exists, navigate. | ||
// - | // - If tip exists, toggle pinned tooltip. | ||
// - If tip is blank, do nothing (and close pinned if clicking another def). | |||
document.addEventListener( | document.addEventListener( | ||
"click", | "click", | ||
| Line 592: | Line 587: | ||
var el = closestDef(e.target); | var el = closestDef(e.target); | ||
if (!el) { | if (!el) { | ||
if (pinned) hide(); | if (pinned) hide(); | ||
| Line 605: | Line 599: | ||
var txt = qTipText(el); | var txt = qTipText(el); | ||
if (!txt) return; | if (!txt) { | ||
if (pinned) hide(); | |||
return; | |||
} | |||
e.preventDefault(); | e.preventDefault(); | ||
| Line 617: | Line 614: | ||
pinned = true; | pinned = true; | ||
show(el); | show(el); | ||
positionTipPoint(e.clientX, e.clientY); | positionTipPoint(e.clientX, e.clientY); | ||
}, | }, | ||
| Line 624: | Line 619: | ||
); | ); | ||
document.addEventListener( | document.addEventListener( | ||
"keydown", | "keydown", | ||
function (e) { | function (e) { | ||
if (e.key === "Escape" && (pinned || activeEl)) | if (e.key === "Escape" && (pinned || activeEl)) hide(); | ||
}, | }, | ||
true | true | ||
); | ); | ||
window.addEventListener( | window.addEventListener( | ||
"resize", | "resize", | ||
function () { | function () { | ||
if (tipEl && tipEl.style.display === "block") { | if (tipEl && tipEl.style.display === "block" && lastPoint) { | ||
positionTipPoint(lastPoint.x, lastPoint.y); | |||
} | } | ||
}, | }, | ||
| Line 650: | Line 640: | ||
"scroll", | "scroll", | ||
function () { | function () { | ||
if (tipEl && tipEl.style.display === "block") { | if (tipEl && tipEl.style.display === "block" && lastPoint) { | ||
positionTipPoint(lastPoint.x, lastPoint.y); | |||
} | } | ||
}, | }, | ||