Jump to content

MediaWiki:Common.js

From Gyaanipedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

/**
 * MediaWiki Common.js - Sort Special:AllPages alphabetically A-Z
 * This script reorganizes the page listing on Special:AllPages to display
 * pages in strict alphabetical order from A to Z
 */

(function() {
    'use strict';
    
    // Only run on Special:AllPages
    if (mw.config.get('wgCanonicalSpecialPageName') !== 'Allpages') {
        return;
    }
    
    // Wait for the page to fully load
    mw.loader.using(['mediawiki.util'], function() {
        $(document).ready(function() {
            sortAllPages();
        });
    });
    
    function sortAllPages() {
        // Find the content area containing the page list
        var $content = $('.mw-allpages-body, .mw-allpages-chunk');
        
        if ($content.length === 0) {
            // Fallback: try to find the list container
            $content = $('.mw-content-ltr ul').first();
        }
        
        if ($content.length === 0) {
            console.log('AllPages sort: Could not find page list container');
            return;
        }
        
        // Get all list items (pages)
        var $lists = $content.find('ul');
        
        $lists.each(function() {
            var $list = $(this);
            var $items = $list.find('> li').detach();
            
            if ($items.length === 0) {
                return;
            }
            
            // Sort items: letters first (A-Z), then numbers
            $items.sort(function(a, b) {
                var textA = $(a).text().trim().toUpperCase();
                var textB = $(b).text().trim().toUpperCase();
                
                // Remove any leading special characters for sorting
                textA = textA.replace(/^[^A-Z0-9]+/i, '');
                textB = textB.replace(/^[^A-Z0-9]+/i, '');
                
                // Check if strings start with a letter or number
                var startsWithLetterA = /^[A-Z]/i.test(textA);
                var startsWithLetterB = /^[A-Z]/i.test(textB);
                
                // If one starts with letter and other with number, letter comes first
                if (startsWithLetterA && !startsWithLetterB) return -1;
                if (!startsWithLetterA && startsWithLetterB) return 1;
                
                // Both start with letters or both start with numbers - normal sort
                if (textA < textB) return -1;
                if (textA > textB) return 1;
                return 0;
            });
            
            // Re-append sorted items
            $items.each(function() {
                $list.append(this);
            });
        });
        
        console.log('AllPages sort: Pages sorted alphabetically A-Z');
    }
})();