MediaWiki:Common.js: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
No edit summary
Line 770: Line 770:
         }
         }
     }
     }
    function fetchAndAppendPrintBlock(pageTitle) {
        if (!pageTitle) return;
        const 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',
            origin: '*' // safe for CORS if needed
        }).done(function (res) {
            const html = res?.parse?.text?.['*'];
            if (!html) return;
            const $tmp = $('<div>').html(html);
            const $printBlock = $tmp.find('.print-only').first();
            if ($printBlock.length) {
            $('#show-article').append($printBlock);
            }
        }).fail(function () {
            // silently ignore; we just won't have the print layout
        });
    }


     function openModal(cardElement, event) {
     function openModal(cardElement, event) {
         event.stopPropagation(); // Prevent the event from bubbling up
         event.stopPropagation();  
         console.log("openModal function called.");
         console.log("openModal function called.");
        // ⬇️ NEW: detect the entry page title from the clicked card and fetch print layout
        var pageTitle = $(cardElement).data('page'); // set by ShowEntries
        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');
         showArticleWrapper.css('display', 'block');
         showArticleWrapper.css('display', 'block');
Line 1,046: Line 1,084:
     // Print current entry only
     // Print current entry only
     $('#print-button').on('click', function () {
     $('#print-button').on('click', function () {
    $('body').addClass('print-entry-only');
        $('body').addClass('print-entry-only');
    window.print();
        requestAnimationFrame(() => {
 
            requestAnimationFrame(() => { window.print(); });
    // remove the class after print (covers most browsers)
        });
    setTimeout(function(){ $('body').removeClass('print-entry-only'); }, 500);
     });
     });


    // optional: also remove class when print dialog closes (Firefox/Chromium support)
     window.onafterprint = function () {
     window.onafterprint = function () {
    $('body').removeClass('print-entry-only');
        $('body').removeClass('print-entry-only');
     };
     };


Navigation menu