4,554
edits
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(); | ||
// | // 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 | // 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; | // 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 | 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); | ||
$.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; | |||
} | |||
$("#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); | |||
}); | |||
// ---- helper scoped to this click so it can see borderPref and $btn ---- | |||
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 | var rootHtml = doc.documentElement; | ||
if (borderPref === "without") { | if (borderPref === "without") { | ||
if ( | if (rootHtml.classList) rootHtml.classList.add("print-no-border"); | ||
else if (!/\bprint-no-border\b/.test( | else if (!/\bprint-no-border\b/.test(rootHtml.className)) | ||
rootHtml.className += " print-no-border"; | |||
} else { | } else { | ||
if ( | if (rootHtml.classList) | ||
rootHtml.classList.remove("print-no-border"); | |||
else | else | ||
rootHtml.className = rootHtml.className.replace( | |||
/\bprint-no-border\b/g, | /\bprint-no-border\b/g, | ||
"" | "" |