MediaWiki:Common.js: Difference between revisions

    From UNITApedia
    (Created page with "→‎Any JavaScript here will be loaded for all users on every page load.: mw.loader.using(['mediawiki.util'], function () { $(function () { // Build the link styled as a button var $pdfLink = $('<a>') .text('📄 Export this page to PDF') .attr('href', '#') .css({ display: 'inline-block', padding: '0.5em 1em', 'font-size': '1em', 'background-color': '#007bff', color: '#fff', 'text-decoration': '...")
     
    No edit summary
     
    (13 intermediate revisions by 2 users not shown)
    Line 1: Line 1:
    /* Any JavaScript here will be loaded for all users on every page load. */
    mw.loader.using('mediawiki.util', function () {
    mw.loader.using(['mediawiki.util'], function () {
       $(function () {
       $(function () {
         // Build the link styled as a button
         // 📄 PDF Export Button (for InternationalisationForms pages)
         var $pdfLink = $('<a>')
         var cats = mw.config.get('wgCategories') || [];
          .text('📄 Export this page to PDF')
        var targetCat = 'InternationalisationForms';
          .attr('href', '#')
          .css({
            display: 'inline-block',
            padding: '0.5em 1em',
            'font-size': '1em',
            'background-color': '#007bff',
            color: '#fff',
            'text-decoration': 'none',
            'border-radius': '4px',
            margin: '0.5em'
          })
          .on('click', function (e) {
            e.preventDefault();
            window.print();  // triggers browser print dialog (Save as PDF)
          });


         // Insert into the page: choose where you like, e.g. top of content
         if (cats.indexOf(targetCat) !== -1) {
        $('#content').before(
          var $pdfLink = $('<a>')
          $('<div>')
            .text('📄 Export this page to PDF')
            .attr('id', 'export-to-pdf-container')
            .attr('href', '#')
            .css({ 'text-align': 'right', padding: '0.5em' })
            .css({
            .append($pdfLink)
              display: 'inline-block',
         );
              padding: '0.39em 0.77em',
              'font-size': '0.82em',
              'background-color': '#19529E',
              color: '#fff',
              'text-decoration': 'none',
              'border-radius': '3px',
              margin: '0.39em'
            })
            .click(function (e) {
              e.preventDefault();
              window.print();
            });
     
          $('#content').before(
            $('<div>')
              .attr('id', 'export-to-pdf-container')
              .css({ 'text-align': 'right', padding: '0.5em' })
              .append($pdfLink)
          );
        }
     
        // 🧰 Hide toolbox for anonymous users
        if (mw.config.get('wgUserName') === null) {
          $('#p-tb').hide();
          $('#p-toolbox, #vector-page-tools, #vector-page-tools-dropdown').hide();
        }
     
        // 🧼 Hide “Switch to old look” link
        function hideSkinSwitcher() {
          $('a').filter(function () {
            return $(this).text().trim() === 'Switch to old look';
          }).closest('li, span, div').hide();
         }
     
        hideSkinSwitcher();
        const observer = new MutationObserver(hideSkinSwitcher);
        observer.observe(document.body, { childList: true, subtree: true });
     
        // 🌳 COLLAPSIBLE TREE TABLE (expand/collapse rows based on parent)
        $(".treetable .tree-row").each(function () {
          const id = $(this).data("id");
          const parent = $(this).data("parent");
     
          if (parent) {
            $(this).addClass("child-of-" + parent.replace(/\s/g, "-")).hide();
          } else {
            $(this).addClass("root-row").css("cursor", "pointer");
          }
        });
     
        $(".treetable .root-row").on("click", function () {
          const id = $(this).data("id").replace(/\s/g, "-");
          $(".child-of-" + id).toggle();
        });
       });
       });
    });
    });

    Latest revision as of 11:37, 24 July 2025

    mw.loader.using('mediawiki.util', function () {
      $(function () {
        // 📄 PDF Export Button (for InternationalisationForms pages)
        var cats = mw.config.get('wgCategories') || [];
        var targetCat = 'InternationalisationForms';
    
        if (cats.indexOf(targetCat) !== -1) {
          var $pdfLink = $('<a>')
            .text('📄 Export this page to PDF')
            .attr('href', '#')
            .css({
              display: 'inline-block',
              padding: '0.39em 0.77em',
              'font-size': '0.82em',
              'background-color': '#19529E',
              color: '#fff',
              'text-decoration': 'none',
              'border-radius': '3px',
              margin: '0.39em'
            })
            .click(function (e) {
              e.preventDefault();
              window.print();
            });
    
          $('#content').before(
            $('<div>')
              .attr('id', 'export-to-pdf-container')
              .css({ 'text-align': 'right', padding: '0.5em' })
              .append($pdfLink)
          );
        }
    
        // 🧰 Hide toolbox for anonymous users
        if (mw.config.get('wgUserName') === null) {
          $('#p-tb').hide();
          $('#p-toolbox, #vector-page-tools, #vector-page-tools-dropdown').hide();
        }
    
        // 🧼 Hide “Switch to old look” link
        function hideSkinSwitcher() {
          $('a').filter(function () {
            return $(this).text().trim() === 'Switch to old look';
          }).closest('li, span, div').hide();
        }
    
        hideSkinSwitcher();
        const observer = new MutationObserver(hideSkinSwitcher);
        observer.observe(document.body, { childList: true, subtree: true });
    
        // 🌳 COLLAPSIBLE TREE TABLE (expand/collapse rows based on parent)
        $(".treetable .tree-row").each(function () {
          const id = $(this).data("id");
          const parent = $(this).data("parent");
    
          if (parent) {
            $(this).addClass("child-of-" + parent.replace(/\s/g, "-")).hide();
          } else {
            $(this).addClass("root-row").css("cursor", "pointer");
          }
        });
    
        $(".treetable .root-row").on("click", function () {
          const id = $(this).data("id").replace(/\s/g, "-");
          $(".child-of-" + id).toggle();
        });
      });
    });