Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Join the Playtest on Steam Now: SpiritVale

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 379: Line 379:
     tipEl.setAttribute("aria-hidden", "true");
     tipEl.setAttribute("aria-hidden", "true");


    // Minimal inline styling so it works even without CSS.
     tipEl.style.position = "fixed";
     tipEl.style.position = "fixed";
     tipEl.style.zIndex = "99999";
     tipEl.style.zIndex = "99999";
Line 422: Line 421:
   }
   }


  function positionTip(el) {
    if (!tipEl || !el) return;
    var r = el.getBoundingClientRect();
    tipEl.style.left = "0px";
    tipEl.style.top = "0px";
    tipEl.style.display = "block";
    tipEl.style.pointerEvents = pinned ? "auto" : "none";
    var tr = tipEl.getBoundingClientRect();
    var pad = 8;
    var gap = 8;
    var x = r.left + (r.width / 2) - (tr.width / 2);
    x = clamp(x, pad, window.innerWidth - tr.width - pad);
    var y = r.bottom + gap;
    if (y + tr.height > window.innerHeight - pad) {
      y = r.top - tr.height - gap;
    }
    y = clamp(y, pad, window.innerHeight - tr.height - pad);
    tipEl.style.left = Math.round(x) + "px";
    tipEl.style.top = Math.round(y) + "px";
  }
  // Position relative to a screen point (preferred on touch + desktop hover)
   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);


    // Prefer ABOVE the finger/cursor
     var y = clientY - tr.height - gap;
     var y = clientY - tr.height - gap;
 
     if (y < pad) y = clientY + gap;
    // If not enough room above, drop below
     if (y < pad) {
      y = clientY + gap;
    }
     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);
    // default positioning (can be overridden by point positioning)
    positionTip(el);
   }
   }


Line 519: Line 482:
   }
   }


   // Hover in/out (desktop) — position ABOVE cursor
   // 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;
      if (!activeEl) return;
 
       var el = closestDef(e.target);
       var el = closestDef(e.target);
       if (!el || el !== activeEl) return;
       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);


      // Prevent follow-up click from toggling it off immediately.
       e.preventDefault();
       e.preventDefault();
       e.stopPropagation();
       e.stopPropagation();
Line 585: Line 579:


   // Click behavior:
   // Click behavior:
   // - If data-sv-def-link is set, navigate (future-ready).
   // - If link exists, navigate.
   // - Otherwise toggle a pinned tooltip.
   // - 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);


      // Click outside closes a pinned tooltip
       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);
      // For mouse clicks, anchor to the click point (feels better than element-anchor)
       positionTipPoint(e.clientX, e.clientY);
       positionTipPoint(e.clientX, e.clientY);
     },
     },
Line 624: Line 619:
   );
   );


  // Close on Escape
   document.addEventListener(
   document.addEventListener(
     "keydown",
     "keydown",
     function (e) {
     function (e) {
       if (e.key === "Escape" && (pinned || activeEl)) {
       if (e.key === "Escape" && (pinned || activeEl)) hide();
        hide();
      }
     },
     },
     true
     true
   );
   );


  // Reposition if visible
   window.addEventListener(
   window.addEventListener(
     "resize",
     "resize",
     function () {
     function () {
       if (tipEl && tipEl.style.display === "block") {
       if (tipEl && tipEl.style.display === "block" && lastPoint) {
         if (lastPoint) positionTipPoint(lastPoint.x, lastPoint.y);
         positionTipPoint(lastPoint.x, lastPoint.y);
        else if (activeEl) positionTip(activeEl);
       }
       }
     },
     },
Line 650: Line 640:
     "scroll",
     "scroll",
     function () {
     function () {
       if (tipEl && tipEl.style.display === "block") {
       if (tipEl && tipEl.style.display === "block" && lastPoint) {
         if (lastPoint) positionTipPoint(lastPoint.x, lastPoint.y);
         positionTipPoint(lastPoint.x, lastPoint.y);
        else if (activeEl) positionTip(activeEl);
       }
       }
     },
     },