4,396
edits
No edit summary |
No edit summary |
||
Line 769: | Line 769: | ||
window.open(url, '_blank').focus(); | window.open(url, '_blank').focus(); | ||
} | } | ||
} | } | ||
Line 799: | Line 774: | ||
event.stopPropagation(); | event.stopPropagation(); | ||
var pageTitle = $(cardElement).data('page') || null; | |||
var pageTitle = $(cardElement).data('page'); | window.currentEntryTitle = pageTitle; | ||
var isRelatedArticle = $(cardElement).hasClass('related-article'); | var isRelatedArticle = $(cardElement).hasClass('related-article'); | ||
Line 1,080: | Line 1,049: | ||
// Print current entry only | // Print current entry only | ||
$('#print-button').on('click', function () { | $('#print-button').on('click', function () { | ||
$(' | var pageTitle = window.currentEntryTitle; | ||
if (!pageTitle) { window.print(); return; } | |||
var api = mw.util.wikiScript('api'); | |||
// fetch rendered entry HTML | |||
$.getJSON(api, { | |||
action: 'parse', | |||
page: pageTitle, | |||
prop: 'text', | |||
format: 'json' | |||
}).done(function (res) { | |||
var html = res && res.parse && res.parse.text && res.parse.text['*']; | |||
if (!html) { window.print(); return; } | |||
var $tmp = $('<div>').html(html); | |||
var $print = $tmp.find('.print-only').first(); | |||
if (!$print.length) { window.print(); return; } | |||
// create hidden iframe | |||
var iframe = document.createElement('iframe'); | |||
iframe.style.position = 'fixed'; | |||
iframe.style.right = '0'; | |||
iframe.style.bottom = '0'; | |||
iframe.style.width = '0'; | |||
iframe.style.height = '0'; | |||
iframe.style.border = '0'; | |||
document.body.appendChild(iframe); | |||
// load print CSS and the print-only HTML | |||
var doc = iframe.contentDocument || iframe.contentWindow.document; | |||
var printCssUrl = mw.util.getUrl('MediaWiki:Print.css', { action: 'raw', ctype: 'text/css' }); | |||
doc.open(); | |||
doc.write( | |||
'<!doctype html><html><head><meta charset="utf-8">' + | |||
'<title>Print</title>' + | |||
'<link rel="stylesheet" href="' + printCssUrl + '">' + | |||
'</head><body>' + $print.prop('outerHTML') + '</body></html>' | |||
); | |||
doc.close(); | |||
iframe.onload = function () { | |||
iframe.contentWindow.focus(); | |||
iframe.contentWindow.print(); | |||
setTimeout(function () { document.body.removeChild(iframe); }, 500); | |||
}; | |||
}).fail(function () { | |||
window.print(); | window.print(); | ||
}); | }); | ||
}); | }); | ||
// Close modal with Close button | // Close modal with Close button |