MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 1,308: Line 1,308:


       var id = this.id;
       var id = this.id;
       // If the main [print] is clicked, just toggle the chooser and stop.
       // If the main [print] is clicked, just toggle the chooser and stop.
       if (id === "print-button") {
       if (id === "print-button") {
Line 1,313: Line 1,314:
         $chooser.toggle();
         $chooser.toggle();


         // 👇 add these lines
         // push the title row down when options are visible
         var $article = $("#show-article");
         var $article = $("#show-article");
         if ($chooser.is(":visible")) {
         if ($chooser.is(":visible")) {
Line 1,320: Line 1,321:
           $article.removeClass("print-opts-open");
           $article.removeClass("print-opts-open");
         }
         }
         return; // <-- don't start printing yet
         return; // <-- don't start printing yet
       }
       }


       // otherwise it's one of the two choices; proceed with your existing flow…
       // otherwise it's one of the two choices; proceed with the print flow…
       var $btn = $(this);
       var $btn = $(this);
       if ($btn.data("busy")) return;
       if ($btn.data("busy")) return;
Line 1,335: Line 1,335:
       preloadFontForPrint();
       preloadFontForPrint();


       var title = window.currentEntryTitle; // e.g. "090"
      // 1) FIRST: if the page already has a .print-only (Entry page), use it and bail.
      var localPrintOnly = $(".print-only").first();
      console.log("[print] local .print-only present:", localPrintOnly.length);
 
      if (localPrintOnly.length) {
        console.log("[print] using local .print-only on this page");
        // optional: hide chooser when starting print
        $("#print-chooser").hide();
        $("#show-article").removeClass("print-opts-open");
        buildIframeAndPrint(localPrintOnly.prop("outerHTML"));
        return; // done; no fetch needed
      }
 
      // 2) OTHERWISE: we’re in modal/home list flow, fetch by title
       var title =
        window.currentEntryTitle ||
        (mw.config && mw.config.get && mw.config.get("wgPageName"));
      console.log("[print] currentEntryTitle / wgPageName:", title);
 
       if (!title) {
       if (!title) {
         console.warn("[print] no currentEntryTitle");
         console.warn(
          "[print] no title available; falling back to window.print()"
        );
         window.print();
         window.print();
         $btn.data("busy", false);
         $btn.data("busy", false);
Line 1,347: Line 1,367:
       console.log("[print] fetching page HTML:", pageUrlFresh);
       console.log("[print] fetching page HTML:", pageUrlFresh);


       // Decide source: if this page already contains a .print-only (Entry page),
       $.get(pageUrlFresh)
      // use it directly. Otherwise fetch (modal/homepage flow).
        .done(function (html) {
      var localPrintOnly = $(".print-only").first();
          var $tmp = $("<div>").html(html);
          var $print = $tmp.find(".print-only").first();
          console.log(
            "[print] .print-only found in fetched page:",
            $print.length
          );
          if (!$print.length) {
            console.warn("[print] no .print-only found; fallback print()");
            window.print();
            $btn.data("busy", false);
            return;
          }
          $("#print-chooser").hide();
          $("#show-article").removeClass("print-opts-open");
          buildIframeAndPrint($print.prop("outerHTML"));
        })
        .fail(function (xhr) {
          console.warn(
            "[print] fetch failed:",
            xhr && xhr.status,
            xhr && xhr.statusText
          );
          window.print();
          $("#print-button").data("busy", false);
        });


       if (localPrintOnly.length) {
       // ---- helper scoped to this click so it can see borderPref and $btn ----
        console.log("[print] using local .print-only on this page");
        // optional: hide chooser when starting print
        $("#print-chooser").hide();
        $("#show-article").removeClass("print-opts-open");
        buildIframeAndPrint(localPrintOnly.prop("outerHTML"));
      } else {
        console.log("[print] fetching page HTML:", pageUrlFresh);
        $.get(pageUrlFresh)
          .done(function (html) {
            var $tmp = $("<div>").html(html);
            var $print = $tmp.find(".print-only").first();
            console.log(
              "[print] .print-only found in fetched page:",
              $print.length
            );
            if (!$print.length) {
              console.warn("[print] no .print-only found; fallback print");
              window.print();
              $btn.data("busy", false);
              return;
            }
            // optional: hide chooser when starting print
            $("#print-chooser").hide();
            $("#show-article").removeClass("print-opts-open");
            buildIframeAndPrint($print.prop("outerHTML"));
          })
          .fail(function (xhr) {
            console.warn(
              "[print] fetch failed:",
              xhr && xhr.status,
              xhr && xhr.statusText
            );
            window.print();
            $("#print-button").data("busy", false);
          });
      }
       function buildIframeAndPrint(printHtml) {
       function buildIframeAndPrint(printHtml) {
         // Build hidden iframe
         // Build hidden iframe
Line 1,446: Line 1,454:
         // Apply border preference (class on <html>)
         // Apply border preference (class on <html>)
         (function applyBorderPreference() {
         (function applyBorderPreference() {
           var root = doc.documentElement;
           var rootHtml = doc.documentElement;
           if (borderPref === "without") {
           if (borderPref === "without") {
             if (root.classList) root.classList.add("print-no-border");
             if (rootHtml.classList) rootHtml.classList.add("print-no-border");
             else if (!/\bprint-no-border\b/.test(root.className))
             else if (!/\bprint-no-border\b/.test(rootHtml.className))
               root.className += " print-no-border";
               rootHtml.className += " print-no-border";
           } else {
           } else {
             if (root.classList) root.classList.remove("print-no-border");
             if (rootHtml.classList)
              rootHtml.classList.remove("print-no-border");
             else
             else
               root.className = root.className.replace(
               rootHtml.className = rootHtml.className.replace(
                 /\bprint-no-border\b/g,
                 /\bprint-no-border\b/g,
                 ""
                 ""

Navigation menu