Jump to content

MediaWiki:Common.js

From Bconomy Wiki
Revision as of 15:40, 2 November 2026 by TheCarriedOne (talk | contribs) (Move section trees into the sidebar)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Section trees ({{… sidebar}} templates, class bc-section-nav).
 * Moves the tree on an article into the left sidebar as its own block, with the
 * current page in bold. The in-page box stays as the fallback on narrow screens
 * (see .bc-section-nav--moved in MediaWiki:Common.css). */
mw.loader.using("mediawiki.util").then(function () {
  mw.hook("wikipage.content").add(function ($content) {
    var box = $content[0] && $content[0].querySelector(".bc-section-nav");
    if (!box || document.getElementById("p-bc-section")) {
      return;
    }
    var titleLink = box.querySelector(
      ".bc-section-nav-title a, .bc-section-nav-title strong",
    );
    var label =
      box.getAttribute("aria-label") ||
      (titleLink && titleLink.textContent) ||
      "";
    var before = document.getElementById("p-Getting_started")
      ? "#p-Getting_started"
      : "#p-navigation";
    var portlet = mw.util.addPortlet("p-bc-section", label, before);
    if (!portlet) {
      return;
    }
    var list = portlet.querySelector("ul");
    var addItem = function (link, text, depth) {
      var li = document.createElement("li");
      li.className = "mw-list-item bc-section-depth-" + depth;
      if (link && link.classList.contains("mw-selflink")) {
        li.className += " bc-section-current";
      }
      if (link) {
        var a = document.createElement("a");
        a.href =
          link.getAttribute("href") ||
          mw.util.getUrl(mw.config.get("wgPageName"));
        a.textContent = text;
        li.appendChild(a);
      } else {
        li.className += " bc-section-label";
        var span = document.createElement("span");
        span.textContent = text;
        li.appendChild(span);
      }
      list.appendChild(li);
    };
    if (titleLink) {
      addItem(titleLink, titleLink.textContent, 0);
    }
    box.querySelectorAll("li").forEach(function (item) {
      var depth = 0;
      for (
        var el = item.parentElement;
        el && el !== box;
        el = el.parentElement
      ) {
        if (el.tagName === "UL") {
          depth++;
        }
      }
      var link = item.querySelector(
        ":scope > a, :scope > strong.mw-selflink, :scope > .mw-selflink",
      );
      var text = link
        ? link.textContent
        : item.firstChild && item.firstChild.nodeType === 3
          ? item.firstChild.textContent.trim()
          : "";
      if (text) {
        addItem(link, text, depth);
      }
    });
    box.classList.add("bc-section-nav--moved");
  });
});