Zum Inhalt springen


MediaWiki:Common.js

Aus Firestone Idle RPG Wiki

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
/* Das folgende JavaScript wird für alle Benutzer geladen. */
/* ==== Sidebar: klickbare Kopfzeile + Gruppen mit Auf/Zu (Vector-2022) ==== */
(function () {
  if ( mw.config.get('skin') !== 'vector-2022' ) return;

  // --- Helpers --------------------------------------------------------------
  function extractDirective(a) {
    if (!a) return null;
    var href = a.getAttribute('href') || '';
    try { href = new URL(href, location.href).hash || ''; } catch (e) {}
    var s = (href || '').trim().toLowerCase();
    if (s.indexOf('#group:') >= 0) return { kind: 'group', value: decodeURIComponent(s.split('#group:').pop()) };
    if (s.indexOf('#link:')  >= 0) return { kind: 'link',  value: decodeURIComponent(s.split('#link:').pop())  };
    return null;
  }

  // entfernt #link:… / #group:… aus einem <a>
  function stripDirectiveHash(a) {
    if (!a) return;
    var raw = a.getAttribute('href') || '';
    a.setAttribute('href', raw.replace(/#(?:link|group):.*$/i, ''));
  }

  function makeHead(li, keepLink) {
    // Kopf: [Pfeil] [Link oder Text]
    var a = li.querySelector(':scope > a');
    var head = document.createElement('div');
    head.className = 'kr-head';

    var btn = document.createElement('button');
    btn.type = 'button';
    btn.className = 'kr-arrow';
    btn.setAttribute('aria-expanded', 'true');
    head.appendChild(btn);

    if (keepLink && a) {
      stripDirectiveHash(a);        // <<< HIER: #link:… aus der URL entfernen
      head.appendChild(a);
    } else {
      var span = document.createElement('span');
      span.className = 'kr-title';
      span.textContent = (a && a.textContent) || '';
      head.appendChild(span);
      if (a) a.remove();            // kein Link im Kopf behalten
    }

    li.insertBefore(head, li.firstChild);
    return head;
  }

  function buildGroups(ul) {
    var lis = Array.from(ul.querySelectorAll(':scope > li.mw-list-item'));
    var currentGroup = null;

    lis.forEach(function (li) {
      var a = li.querySelector(':scope > a');
      var d = extractDirective(a);

      if (d && d.kind === 'group') {
        // Gruppen-Header (ohne Link)
        li.classList.add('kr-group');
        makeHead(li, false);

        // Unterliste
        var sub = document.createElement('ul');
        sub.className = 'kr-sub';
        li.appendChild(sub);
        currentGroup = sub;

        // Direktiven-Link vollständig aus dem DOM entfernen
        if (a) a.remove();
        return;
      }

      // Normale Einträge in die gerade offene Gruppe schieben
      if (currentGroup && !(d && (d.kind === 'group' || d.kind === 'link'))) {
        currentGroup.appendChild(li);
      }
    });

    // Auf/Zu
    ul.addEventListener('click', function (e) {
      if (e.target.classList.contains('kr-arrow')) {
        var li = e.target.closest('li.kr-group, li.kr-top');
        if (li) {
          var collapsed = li.classList.toggle('is-collapsed');
          e.target.setAttribute('aria-expanded', collapsed ? 'false' : 'true');
        }
      }
    });
  }

  function buildPortlets(root) {
    var lists = root.querySelectorAll('#mw-panel .vector-menu-content-list');

    lists.forEach(function (ul) {
      var items = Array.from(ul.querySelectorAll(':scope > li.mw-list-item'));
      var topIdx = items.findIndex(function (li) {
        var a = li.querySelector(':scope > a');
        var d = extractDirective(a);
        return d && d.kind === 'link';
      });

      if (topIdx >= 0) {
        var top = items[topIdx];
        top.classList.add('kr-top');
        makeHead(top, true); // Link bleibt – aber ohne #link:…

        // Unterliste unter dem Kopf, Rest hinein
        var sub = document.createElement('ul');
        sub.className = 'kr-sub';
        top.appendChild(sub);
        for (var i = topIdx + 1; i < items.length; i++) sub.appendChild(items[i]);

        buildGroups(sub);
      } else {
        buildGroups(ul);
      }

      // Sicherheit: alle verbleibenden Direktiven aus hrefs entfernen
      ul.querySelectorAll('a[href*="#link:"], a[href*="#group:"]').forEach(stripDirectiveHash);

      // Original-Portlet-Heading (H3) ENTFERNEN
      var portlet = ul.closest('.vector-menu');
      if (portlet) {
        var heading = portlet.querySelector(':scope > .vector-menu-heading');
        if (heading) heading.remove();
      }
    });
  }

  function init() {
    var panel = document.getElementById('mw-panel');
    if (!panel) return;
    panel.classList.add('kr-sb');
    buildPortlets(panel);
  }

  mw.hook('wikipage.content').add(init);
  init();
})();