MediaWiki:Common.js

From AltDic Alpha

Note: After saving, 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)
  • Internet Explorer: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
$( document ).ready( function () {
    var page = '#input_4';
    var subpage = '#input_5';
    
    // Function to fetch and populate subpages
    function updateSubpageDropdown(fullParentPageTitle) {
        $(subpage).empty();
        
        var parentTitleWithoutNamespace = fullParentPageTitle.replace(/^.+?:/, '');

        // Use jQuery.ajax for robust parameter handling
        $.ajax({
            url: mw.config.get( 'wgScriptPath' ) + '/api.php',
            data: {
                action: 'query',
                format: 'json',
                list: 'allpages',
                apnamespace: 3000,
                aplimit: 500,
                apprefix: parentTitleWithoutNamespace + '/'
            },
            dataType: 'json',
            success: function (data) {
                if (data.query && data.query.allpages) {
                    var subpages = data.query.allpages;
                    $(subpage).append($('<option>', {
                        value: '',
                        text: 'Select a subpage'
                    }));
                    $.each(subpages, function(key, val) {
                        // Display just the subpage name relative to the parent
                        var subpageDisplayName = val.title.replace('Concepts:' + parentTitleWithoutNamespace + '/', '');
                        
                        $(subpage).append($('<option>', {
                            value: val.title,
                            text: subpageDisplayName
                        }));
                    });
                }
            },
            error: function(xhr, status, error) {
                console.error("API request failed: " + status + ", " + error);
                // Optional: Add an error message to the dropdown
                $(subpage).append($('<option>', {
                    value: '',
                    text: 'Error loading subpages'
                }));
            }
        });
    }

    // Event listener for the first dropdown (remains the same)
    $(page).change(function() {
        var selectedParent = $(this).val();
        if (selectedParent) {
            updateSubpageDropdown(selectedParent);
        } else {
            $(subpage).empty();
        }
    });
});