MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 1,260: Line 1,260:
   });
   });


   // ==========================
   (function ($) {
  // Softwear — Print (all pages)
  // ==========================
  $(function () {
     var DEBUG = true; // set false to silence console logs
     var DEBUG = true; // set false to silence console logs


Line 1,304: Line 1,301:
     }
     }


     // 5) Main “choice” handler (called by the container click handler below)
     // 5) Build a hidden iframe, inject CSS+HTML, and print
    function handlePrintChoice(id, $btn) {
      if ($btn.data("busy")) {
        if (DEBUG) console.log("[print] busy, ignoring click");
        return;
      }
      $btn.data("busy", true);
 
      var borderPref = id === "print-no-border" ? "without" : "with";
      if (DEBUG) console.log("[print] option chosen:", borderPref);
 
      preloadFontForPrint();
 
      // Prefer local .print-only (Entry page)
      var localPrintOnly = $(".print-only").first();
      if (DEBUG)
        console.log(
          "[print] local .print-only present:",
          localPrintOnly.length
        );
 
      if (localPrintOnly.length) {
        if (DEBUG) console.log("[print] using local .print-only on this page");
        hidePrintUI();
        buildIframeAndPrint(localPrintOnly.prop("outerHTML"), borderPref, $btn);
        return;
      }
 
      // Otherwise fetch by title (modal/home list flow)
      var title =
        window.currentEntryTitle ||
        (window.mw &&
          mw.config &&
          mw.config.get &&
          mw.config.get("wgPageName"));
      if (DEBUG) console.log("[print] currentEntryTitle / wgPageName:", title);
 
      if (!title) {
        console.warn("[print] no title; falling back to window.print()");
        window.print();
        $btn.data("busy", false);
        return;
      }
 
      var pageUrl =
        window.mw && mw.util && mw.util.getUrl
          ? mw.util.getUrl(title)
          : "/wiki/" + String(title);
      var pageUrlFresh = cacheBust(pageUrl);
      if (DEBUG) console.log("[print] fetching page HTML:", pageUrlFresh);
 
      $.get(pageUrlFresh)
        .done(function (html) {
          var $tmp = $("<div>").html(html);
          var $print = $tmp.find(".print-only").first();
          if (DEBUG)
            console.log("[print] .print-only in fetched page:", $print.length);
          if (!$print.length) {
            console.warn(
              "[print] no .print-only in fetched page; window.print()"
            );
            window.print();
            $btn.data("busy", false);
            return;
          }
          hidePrintUI();
          buildIframeAndPrint($print.prop("outerHTML"), borderPref, $btn);
        })
        .fail(function (xhr) {
          console.warn(
            "[print] fetch failed:",
            xhr && xhr.status,
            xhr && xhr.statusText
          );
          window.print();
          $("#print-button").data("busy", false);
        });
    }
 
    // 6) Build a hidden iframe, inject CSS+HTML, and print
     function buildIframeAndPrint(printHtml, borderPref, $btn) {
     function buildIframeAndPrint(printHtml, borderPref, $btn) {
       if (DEBUG) console.log("[print] buildIframeAndPrint: starting");
       if (DEBUG) console.log("[print] buildIframeAndPrint: starting");
Line 1,446: Line 1,364:
         var htmlEl = doc.documentElement;
         var htmlEl = doc.documentElement;
         if (borderPref === "without") {
         if (borderPref === "without") {
           htmlEl.classList
           if (htmlEl.classList) {
             ? htmlEl.classList.add("print-no-border")
             htmlEl.classList.add("print-no-border");
             : (htmlEl.className += " print-no-border");
          } else if (
             (" " + htmlEl.className + " ").indexOf(" print-no-border ") === -1
          ) {
            htmlEl.className += " print-no-border";
          }
         } else {
         } else {
           htmlEl.classList
           if (htmlEl.classList) {
             ? htmlEl.classList.remove("print-no-border")
             htmlEl.classList.remove("print-no-border");
             : (htmlEl.className = htmlEl.className.replace(
          } else {
                /\bprint-no-border\b/g,
             htmlEl.className = htmlEl.className.replace(
                ""
              /\bprint-no-border\b/g,
              ));
              ""
            );
          }
         }
         }
         if (DEBUG) console.log("[print] borderPref applied:", borderPref);
         if (DEBUG) console.log("[print] borderPref applied:", borderPref);
Line 1,474: Line 1,398:
             onlyBr
             onlyBr
           ) {
           ) {
             p.parentNode && p.parentNode.removeChild(p);
             if (p.parentNode) p.parentNode.removeChild(p);
           }
           }
         });
         });
Line 1,530: Line 1,454:


         var refs = doc.querySelectorAll(".article-external-reference a[href]");
         var refs = doc.querySelectorAll(".article-external-reference a[href]");
         refs.forEach(function (a) {
         Array.prototype.forEach.call(refs, function (a) {
           var txt = (a.textContent || "").trim();
           var txt = (a.textContent || "").trim();
           var href = a.getAttribute("href") || "";
           var href = a.getAttribute("href") || "";
Line 1,658: Line 1,582:
     }
     }


     // 7) Event wiring
     // 6b) Choice click handler (decides source and calls buildIframeAndPrint)
     // Remove any prior print handlers so we don’t double-bind after AJAX nav etc.
     function handlePrintChoice(id, $btn) {
      if ($btn.data("busy")) {
        if (DEBUG) console.log("[print] busy, ignoring click");
        return;
      }
      $btn.data("busy", true);
 
      var borderPref = id === "print-no-border" ? "without" : "with";
      if (DEBUG) console.log("[print] option chosen:", borderPref);
 
      preloadFontForPrint();
 
      // Prefer local .print-only (Entry page)
      var localPrintOnly = $(".print-only").first();
      if (DEBUG)
        console.log(
          "[print] local .print-only present:",
          localPrintOnly.length
        );
 
      if (localPrintOnly.length) {
        if (DEBUG) console.log("[print] using local .print-only on this page");
        hidePrintUI();
        buildIframeAndPrint(localPrintOnly.prop("outerHTML"), borderPref, $btn);
        return;
      }
 
      // Otherwise fetch by title (modal/home list flow)
      var title =
        window.currentEntryTitle ||
        (window.mw &&
          mw.config &&
          mw.config.get &&
          mw.config.get("wgPageName"));
      if (DEBUG) console.log("[print] currentEntryTitle / wgPageName:", title);
 
      if (!title) {
        console.warn("[print] no title; falling back to window.print()");
        window.print();
        $btn.data("busy", false);
        return;
      }
 
      var pageUrl =
        window.mw && mw.util && mw.util.getUrl
          ? mw.util.getUrl(title)
          : "/wiki/" + String(title);
      var pageUrlFresh = cacheBust(pageUrl);
      if (DEBUG) console.log("[print] fetching page HTML:", pageUrlFresh);
 
      $.get(pageUrlFresh)
        .done(function (html) {
          var $tmp = $("<div>").html(html);
          var $print = $tmp.find(".print-only").first();
          if (DEBUG)
            console.log("[print] .print-only in fetched page:", $print.length);
          if (!$print.length) {
            console.warn(
              "[print] no .print-only in fetched page; window.print()"
            );
            window.print();
            $btn.data("busy", false);
            return;
          }
          hidePrintUI();
          buildIframeAndPrint($print.prop("outerHTML"), borderPref, $btn);
        })
        .fail(function (xhr) {
          console.warn(
            "[print] fetch failed:",
            xhr && xhr.status,
            xhr && xhr.statusText
          );
          window.print();
          $("#print-button").data("busy", false);
        });
    }
 
    // 7) Event wiring — one tolerant handler
     $(document).off("click.print");
     $(document).off("click.print");


     // Single tolerant handler bound to:
     // Container handler:
     // - #print-button     (to toggle chooser)
     // - #print-button toggles chooser
     // - #print-chooser   (so nested <a><button> clicks are caught)
     // - #print-chooser / #print-options capture clicks on the two anchors
    // - #print-options   (legacy container if you still render it)
     $(document).on(
     $(document).on(
       "click.print",
       "click.print",
Line 1,675: Line 1,676:
           var $chooser = ensurePrintChooser();
           var $chooser = ensurePrintChooser();
           $chooser.toggle();
           $chooser.toggle();
           $("#show-article").toggleClass(
           $("#show-article").toggleClass(
             "print-opts-open",
             "print-opts-open",
             $chooser.is(":visible")
             $chooser.is(":visible")
           );
           );
           if (DEBUG)
           if (DEBUG)
             console.log(
             console.log(
Line 1,690: Line 1,689:


         // Otherwise, check if one of the two choice anchors was clicked
         // Otherwise, check if one of the two choice anchors was clicked
         var anchor = e.target.closest("a#print-with-border, a#print-no-border");
         var $choice = $(e.target).closest(
         if (!anchor) return; // click inside container but not on a choice
          "a#print-with-border, a#print-no-border"
        );
         if (!$choice.length) return; // click inside container but not on a choice


         e.preventDefault();
         e.preventDefault();
         var id = anchor.id;
         var id = $choice.attr("id");
         if (DEBUG) console.log("[print] option anchor click:", { id });
         if (DEBUG) console.log("[print] option anchor click:", id);
         handlePrintChoice(id, $(anchor));
         handlePrintChoice(id, $choice);
       }
       }
     );
     );
Line 1,703: Line 1,704:
     $(document).on("click.print", "#close-button", hidePrintUI);
     $(document).on("click.print", "#close-button", hidePrintUI);
     $(document).on("keydown.print", function (e) {
     $(document).on("keydown.print", function (e) {
       if (e.key === "Escape") hidePrintUI();
      // No ES6 keys here; use keyCode for ES5 compatibility
       if (e.keyCode === 27) hidePrintUI(); // ESC
     });
     });
   });
   });

Navigation menu