MediaWiki:Common.js: Difference between revisions

From Hidden Mickey Wiki

No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 92: Line 92:


   mw.loader.using(['mediawiki.util'], function () {
   mw.loader.using(['mediawiki.util'], function () {
     $(function () {
 
     function addSecondDropdown() {
       var $ul = $('.navbar-nav.navbar-right').first();
       var $ul = $('.navbar-nav.navbar-right').first();
       if (!$ul.length) {
       if (!$ul.length) return;
        console.warn('Navbar <ul> not found');
        return;
      }


       // avoid duplicate
       // Avoid duplicates
       if ($ul.find('li.my-extra-dropdown').length) {
       if ($ul.find('li.my-extra-dropdown').length) return;
        return;
      }


       // Build new dropdown
       // Create dropdown <li>
       var $li = $('<li>', { 'class': 'dropdown my-extra-dropdown' });
       var $li = $('<li>', { 'class': 'dropdown my-extra-dropdown' });
       var $a = $('<a>', {
       var $a = $('<a>', {
Line 123: Line 119:
       $li.append($a).append($menu);
       $li.append($a).append($menu);


       // --- Placement options ---
       // Insert AFTER the first dropdown, at the same level
      // Put new dropdown immediately AFTER the original
       var $firstDropdown = $ul.children('li.dropdown').first();
       var $firstDropdown = $ul.children('li.dropdown').first();
       if ($firstDropdown.length) {
       if ($firstDropdown.length) {
Line 131: Line 126:
         $ul.append($li);
         $ul.append($li);
       }
       }
     });
     }
 
    // Wait until navbar exists
    var attempts = 0, maxAttempts = 50;
    var interval = setInterval(function () {
      if ($('.navbar-nav.navbar-right').length) {
        clearInterval(interval);
        addSecondDropdown();
      } else if (++attempts > maxAttempts) {
        clearInterval(interval);
        console.warn('Navbar not found, could not insert second dropdown.');
      }
    }, 100);
 
   });
   });
})();
})();

Revision as of 14:29, 19 September 2025

/* Any JavaScript here will be loaded for all users on every page load. */
// JavaScript code to save checkbox state and restore it when the page loads
$(document).ready(function() {
    // Function to save the state of checkboxes to localStorage
    function saveCheckboxState() {
        $('input[type="checkbox"]').each(function() {
            localStorage.setItem($(this).attr('id'), $(this).prop('checked'));
        });
    }

    // Function to load the state of checkboxes from localStorage
    function loadCheckboxState() {
        $('input[type="checkbox"]').each(function() {
            const savedState = localStorage.getItem($(this).attr('id'));
            if (savedState !== null) {
                $(this).prop('checked', savedState === 'true');
            }
        });
    }

    // Load the saved checkbox state when the page is loaded
    loadCheckboxState();

    // Save the checkbox state whenever a checkbox is changed
    $('input[type="checkbox"]').change(function() {
        saveCheckboxState();
    });
});

/* Adjust the search box width
$(document).ready(function () {
    $('#searchInput').css('width', '600px'); // Adjust width as needed
}); */

// "Where To" dropdown
$(function () {
    // Prevent duplicate
    if ($('#mw-settings-dropdown').length) return;

    // Create container
    var $container = $('<div id="mw-settings-dropdown"></div>');
    var $button = $('<button>Where To? ▾</button>');
    var $list = $('<ul></ul>');

    // Define menu items
    var menuItems = [
        { title: 'Disneyland', page: 'Disneyland' },
        { title: 'California Adventure', page: 'California Adventure' },
        { title: 'Disneyland Resort', page: 'Disneyland Resort' }
    ];

    // Populate dropdown
    menuItems.forEach(function(item) {
        var $li = $('<li></li>');
        var $a = $('<a></a>').attr('href', mw.util.getUrl(item.page)).text(item.title);
        $li.append($a);
        $list.append($li);
    });

    $container.append($button).append($list);

    // Insert into navbar-right if exists
    var $navbar = $('#mw-navbar-right');
    if ($navbar.length) {
        // Prepend = place at far left of navbar-right
        $navbar.prepend($container);
    } else {
        // fallback: absolute top-right
        $container.css({ position: 'absolute', top: '10px', right: '10px' });
        $('body').append($container);
    }

    // Toggle dropdown
    $button.on('click', function(e) {
        $list.toggle();
        e.stopPropagation();
    });

    // Close dropdown when clicking outside
    $(document).on('click', function() {
        $list.hide();
    });

    $container.on('click', function(e) {
        e.stopPropagation();
    });
});

// Second dropdown
(function () {
  'use strict';

  mw.loader.using(['mediawiki.util'], function () {

    function addSecondDropdown() {
      var $ul = $('.navbar-nav.navbar-right').first();
      if (!$ul.length) return;

      // Avoid duplicates
      if ($ul.find('li.my-extra-dropdown').length) return;

      // Create dropdown <li>
      var $li = $('<li>', { 'class': 'dropdown my-extra-dropdown' });
      var $a = $('<a>', {
        href: '#',
        'class': 'dropdown-toggle',
        'data-toggle': 'dropdown',
        role: 'button',
        'aria-haspopup': 'true',
        'aria-expanded': 'false',
        html: 'More <span class="caret"></span>'
      });
      var $menu = $('<ul>', { 'class': 'dropdown-menu' });

      $menu.append($('<li>').append($('<a>', { href: mw.util.getUrl('Special:RecentChanges'), text: 'Recent changes' })));
      $menu.append($('<li>').append($('<a>', { href: mw.util.getUrl('Special:Random'), text: 'Random page' })));
      $menu.append($('<li>').append($('<a>', { href: mw.util.getUrl('Help:Contents'), text: 'Help' })));

      $li.append($a).append($menu);

      // Insert AFTER the first dropdown, at the same level
      var $firstDropdown = $ul.children('li.dropdown').first();
      if ($firstDropdown.length) {
        $firstDropdown.after($li);
      } else {
        $ul.append($li);
      }
    }

    // Wait until navbar exists
    var attempts = 0, maxAttempts = 50;
    var interval = setInterval(function () {
      if ($('.navbar-nav.navbar-right').length) {
        clearInterval(interval);
        addSecondDropdown();
      } else if (++attempts > maxAttempts) {
        clearInterval(interval);
        console.warn('Navbar not found, could not insert second dropdown.');
      }
    }, 100);

  });
})();

// Add Edit Source to user dropdown
mw.loader.using('mediawiki.util', function () {
    // Add a new entry into the user dropdown
    mw.util.addPortletLink(
        'p-personal',                        // user menu ID
        mw.util.getUrl( mw.config.get('wgPageName'), { action: 'edit' } ), 
        'Edit Source',
        'pt-editsource'
    );
});