MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 769: Line 769:
             window.open(url, '_blank').focus();
             window.open(url, '_blank').focus();
         }
         }
    }
    function fetchAndAppendPrintBlock(pageTitle) {
        if (!pageTitle) return;
        var api = mw.util.wikiScript('api');
        // remove any previous print block so we don't duplicate
        $('#show-article .print-only').remove();
        $.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) return;
            var $tmp = $('<div>').html(html);
            var $printBlock = $tmp.find('.print-only').first();
            if ($printBlock.length) {
            $('#show-article').append($printBlock);
            }
        });
     }
     }


Line 799: Line 774:
         event.stopPropagation();  
         event.stopPropagation();  
          
          
        // ⬇️ NEW: detect the entry page title from the clicked card and fetch print layout
         var pageTitle = $(cardElement).data('page') || null;
         var pageTitle = $(cardElement).data('page'); // set by ShowEntries
         window.currentEntryTitle = pageTitle;
         if (pageTitle) {
            fetchAndAppendPrintBlock(pageTitle);
        } else {
            // if this is a related-article and you someday expose its page, handle here.
            $('#show-article .print-only').remove(); // avoid stale print blocks
        }


         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 () {
         $('body').addClass('print-entry-only');
        var pageTitle = window.currentEntryTitle;
        requestAnimationFrame(function () {
        if (!pageTitle) { window.print(); return; }
             requestAnimationFrame(function () {
 
        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();
            });
         });
         });
     });
     });


    window.onafterprint = function () {
        $('body').removeClass('print-entry-only');
    };


     // Close modal with Close button
     // Close modal with Close button

Navigation menu