MediaWiki:Common.js: Difference between revisions

From Hidden Mickey Wiki

No edit summary
No edit summary
Line 33: Line 33:
});
});


(function () {
/* === Settings dropdown in mw-navbar-left === */
  var box = document.createElement('div');
$(function () {
  box.id = 'mw-test-settings';
    // Avoid duplicates
  box.style.position = 'absolute';
    if ($('#mw-settings-dropdown').length) return;
  box.style.top = '10px';
  box.style.right = '10px';
  box.style.background = 'white';
  box.style.border = '1px solid #ccc';
  box.style.padding = '4px';
  box.style.zIndex = 9999;


  var btn = document.createElement('button');
    // Create container
  btn.textContent = 'Settings ▾';
    var $container = $('<div id="mw-settings-dropdown"></div>');
    var $button = $('<button>Settings ▾</button>');
    var $list = $('<ul></ul>');


  var list = document.createElement('ul');
    // Add menu items
  list.style.listStyle = 'none';
    var menuItems = [
  list.style.margin = '4px 0 0 0';
        { title: 'Preferences', page: 'Special:Preferences' },
  list.style.padding = '0';
        { title: 'Watchlist', page: 'Special:Watchlist' }
  list.style.display = 'none';
    ];


  ['Preferences', 'Watchlist'].forEach(function (txt) {
    menuItems.forEach(function(item) {
    var li = document.createElement('li');
        var $li = $('<li></li>');
    var a = document.createElement('a');
        var $a = $('<a></a>').attr('href', mw.util.getUrl(item.page)).text(item.title);
    a.textContent = txt;
        $li.append($a);
    a.href = '/wiki/Special:' + txt;
        $list.append($li);
    a.style.display = 'block';
    });
    a.style.padding = '4px 8px';
    a.style.textDecoration = 'none';
    a.style.color = 'black';
    li.appendChild(a);
    list.appendChild(li);
  });


  btn.onclick = function () {
    $container.append($button).append($list);
    list.style.display = (list.style.display === 'none') ? 'block' : 'none';
  };


  box.appendChild(btn);
    // Insert into #mw-navbar-left
  box.appendChild(list);
    var $navbar = $('#mw-navbar-left');
  document.body.appendChild(box);
    if ($navbar.length) {
})();
        // Position at the far right
        $container.css({
            display: 'inline-block',
            float: 'right',
            marginRight: '10px',
            position: 'relative'
        });
        $navbar.append($container);
    } else {
        // fallback: append to body
        $container.css({ position: 'absolute', top: '10px', right: '10px' });
        $('body').append($container);
    }


box.id = 'mw-settings-dropdown';
    // Toggle dropdown
    $button.on('click', function(e) {
        $list.toggle();
        e.stopPropagation();
    });
 
    // Close dropdown on outside click
    $(document).on('click', function() {
        $list.hide();
    });
 
    $container.on('click', function(e) {
        e.stopPropagation();
    });
});

Revision as of 15:38, 18 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
});

/* === Settings dropdown in mw-navbar-left === */
$(function () {
    // Avoid duplicates
    if ($('#mw-settings-dropdown').length) return;

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

    // Add menu items
    var menuItems = [
        { title: 'Preferences', page: 'Special:Preferences' },
        { title: 'Watchlist', page: 'Special:Watchlist' }
    ];

    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 #mw-navbar-left
    var $navbar = $('#mw-navbar-left');
    if ($navbar.length) {
        // Position at the far right
        $container.css({
            display: 'inline-block',
            float: 'right',
            marginRight: '10px',
            position: 'relative'
        });
        $navbar.append($container);
    } else {
        // fallback: append to body
        $container.css({ position: 'absolute', top: '10px', right: '10px' });
        $('body').append($container);
    }

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

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

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