BlueGit

timfa bluegit

Files

bluegit / internal / server / static / tip.js

tip.js

tip.js 3.3 KiB Download Raw
1// Info-tooltips (i): tonen bij hover, toetsenbordfocus en als het veld in
2// hetzelfde label focus heeft (CSS). Op touch opent een tik de uitleg;
3// tik ernaast of Escape sluit hem. Zichtbare .tip-b blijft in het
4// viewport (fixed, geklemd of omgeklapt).
5(function () {
6 var GAP = 8;
7
8 function closeAll(except) {
9 document.querySelectorAll(".tip.open").forEach(function (t) {
10 if (t !== except) {
11 t.classList.remove("open");
12 var b = t.querySelector(".tip-i");
13 if (b) { b.setAttribute("aria-expanded", "false"); }
14 }
15 });
16 }
17
18 function placeBody(body, anchor) {
19 body.classList.remove("above");
20 body.style.position = "fixed";
21 body.style.left = "0px";
22 body.style.top = "0px";
23 body.style.right = "auto";
24 var rect = anchor.getBoundingClientRect();
25 var w = body.offsetWidth;
26 var h = body.offsetHeight;
27 var vw = document.documentElement.clientWidth;
28 var vh = document.documentElement.clientHeight;
29 var left = rect.left;
30 var top = rect.bottom + GAP;
31 var above = false;
32 if (top + h > vh - GAP && rect.top - GAP - h >= GAP) {
33 top = rect.top - GAP - h;
34 above = true;
35 }
36 if (left + w > vw - GAP) {
37 left = vw - GAP - w;
38 }
39 if (left < GAP) {
40 left = GAP;
41 }
42 if (top + h > vh - GAP) {
43 top = Math.max(GAP, vh - GAP - h);
44 }
45 if (top < GAP) {
46 top = GAP;
47 }
48 body.classList.toggle("above", above);
49 body.style.left = Math.round(left) + "px";
50 body.style.top = Math.round(top) + "px";
51 }
52
53 function clearBody(body) {
54 body.classList.remove("above");
55 body.style.position = "";
56 body.style.left = "";
57 body.style.top = "";
58 body.style.right = "";
59 }
60
61 function repositionTips() {
62 document.querySelectorAll(".tip-b").forEach(function (body) {
63 if (getComputedStyle(body).display === "none") {
64 clearBody(body);
65 return;
66 }
67 var tip = body.closest(".tip");
68 var anchor = (tip && tip.querySelector(".tip-i")) || body.parentElement;
69 if (anchor) {
70 placeBody(body, anchor);
71 }
72 });
73 }
74
75 function schedulePlace() {
76 requestAnimationFrame(repositionTips);
77 }
78
79 document.addEventListener("click", function (e) {
80 var btn = e.target.closest && e.target.closest(".tip-i");
81 if (!btn) { closeAll(null); schedulePlace(); return; }
82 e.preventDefault();
83 var tip = btn.closest(".tip");
84 var open = !tip.classList.contains("open");
85 closeAll(tip);
86 tip.classList.toggle("open", open);
87 btn.setAttribute("aria-expanded", open ? "true" : "false");
88 schedulePlace();
89 });
90 document.addEventListener("keydown", function (e) {
91 if (e.key === "Escape") { closeAll(null); schedulePlace(); return; }
92 var btn = e.target.closest && e.target.closest(".tip-i");
93 if (btn && (e.key === "Enter" || e.key === " ")) { e.preventDefault(); btn.click(); }
94 });
95 document.addEventListener("pointerover", function (e) {
96 if (e.target.closest && e.target.closest(".tip, .tiphost")) {
97 schedulePlace();
98 }
99 });
100 document.addEventListener("focusin", function (e) {
101 if (e.target.closest && e.target.closest(".tip, .tiphost")) {
102 schedulePlace();
103 }
104 });
105 window.addEventListener("scroll", repositionTips, true);
106 window.addEventListener("resize", repositionTips);
107})();