MediaWiki:Common.css: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
# Replace the JAVASCRIPT_CODE section with this updated version: | |||
/* Collapsible | |||
/* Collapsible Sidebar Functionality */ | |||
$(document).ready(function() { | $(document).ready(function() { | ||
// | function initCollapsibleSidebar() { | ||
// Define which sections should be collapsible | |||
// | var collapsibleSections = [ | ||
'Documentation', | |||
'Tools & Utilities', | |||
'Community', | |||
'Administrative', | |||
'External Resources' | |||
]; | |||
// Define which sections start collapsed | |||
var startCollapsed = ['Administrative', 'External Resources']; | |||
// Process each portal in the sidebar | |||
$('#mw-panel .portal').each(function() { | $('#mw-panel .portal').each(function() { | ||
var $portal = $(this); | var $portal = $(this); | ||
var $header = $portal.find('h3'); | var $header = $portal.find('h3'); | ||
var $body = $portal.find('.body'); | var $body = $portal.find('.body'); | ||
var headerText = $header.text().trim(); | |||
// Skip | // Skip non-collapsible sections | ||
var portalId = $portal.attr('id'); | var portalId = $portal.attr('id'); | ||
if (portalId === 'p-search' || portalId === 'p-tb' || portalId === 'p-lang') { | if (portalId === 'p-search' || portalId === 'p-tb' || portalId === 'p-lang') { | ||
Line 17: | Line 30: | ||
} | } | ||
// | // Only make specified sections collapsible | ||
if (collapsibleSections.includes(headerText)) { | |||
// Remove any existing toggle buttons | |||
$header.find('.sidebar-toggle').remove(); | |||
// Create toggle button with custom text | |||
var isCollapsed = startCollapsed.includes(headerText); | |||
var toggleText = isCollapsed ? 'Show' : 'Hide'; // Changed from [+]/[-] | |||
var $toggleBtn = $('<span class="sidebar-toggle">' + toggleText + '</span>'); | |||
// Add toggle button to header | // Add toggle button to header | ||
$header.append(' ').append($toggleBtn); | $header.append(' ').append($toggleBtn); | ||
// Set initial state | |||
if (isCollapsed) { | |||
$body.hide(); | |||
$portal.addClass('sidebar-collapsed'); | |||
} | |||
// Add click handler | // Add click handler | ||
$toggleBtn.click(function(e) { | $toggleBtn.off('click').on('click', function(e) { | ||
e.preventDefault(); | e.preventDefault(); | ||
e.stopPropagation(); | e.stopPropagation(); | ||
if ($body.is(':visible')) { | if ($body.is(':visible')) { | ||
$body.slideUp( | $body.slideUp(300); | ||
$toggleBtn.text('[+]'); | $toggleBtn.text('Show'); // Changed from [+] | ||
$portal.addClass('sidebar-collapsed'); | |||
// Save state to localStorage | |||
localStorage.setItem('sidebar-' + headerText, 'collapsed'); | |||
} else { | } else { | ||
$body.slideDown( | $body.slideDown(300); | ||
$toggleBtn.text(' | $toggleBtn.text('Hide'); // Changed from [-] | ||
$portal.removeClass('collapsed'); | $portal.removeClass('sidebar-collapsed'); | ||
// Save state to localStorage | |||
localStorage.setItem('sidebar-' + headerText, 'expanded'); | |||
} | } | ||
}); | }); | ||
// | // Restore saved state from localStorage | ||
var savedState = localStorage.getItem('sidebar-' + headerText); | |||
if (savedState === 'collapsed' && $body.is(':visible')) { | |||
$body.hide(); | $body.hide(); | ||
$toggleBtn.text(' | $toggleBtn.text('Show'); | ||
$portal.addClass('collapsed'); | $portal.addClass('sidebar-collapsed'); | ||
} else if (savedState === 'expanded' && !$body.is(':visible')) { | |||
$body.show(); | |||
$toggleBtn.text('Hide'); | |||
$portal.removeClass('sidebar-collapsed'); | |||
} | } | ||
} | } | ||
Line 52: | Line 86: | ||
} | } | ||
// | // Initialize on page load | ||
initCollapsibleSidebar(); | |||
// | // Re-initialize after AJAX loads (if needed) | ||
$(document).ajaxComplete(function() { | $(document).ajaxComplete(function() { | ||
setTimeout(initCollapsibleSidebar, 100); | |||
}); | }); | ||
}); | }); |
Revision as of 14:37, 20 August 2025
# Replace the JAVASCRIPT_CODE section with this updated version: /* Collapsible Sidebar Functionality */ $(document).ready(function() { function initCollapsibleSidebar() { // Define which sections should be collapsible var collapsibleSections = [ 'Documentation', 'Tools & Utilities', 'Community', 'Administrative', 'External Resources' ]; // Define which sections start collapsed var startCollapsed = ['Administrative', 'External Resources']; // Process each portal in the sidebar $('#mw-panel .portal').each(function() { var $portal = $(this); var $header = $portal.find('h3'); var $body = $portal.find('.body'); var headerText = $header.text().trim(); // Skip non-collapsible sections var portalId = $portal.attr('id'); if (portalId === 'p-search' || portalId === 'p-tb' || portalId === 'p-lang') { return; } // Only make specified sections collapsible if (collapsibleSections.includes(headerText)) { // Remove any existing toggle buttons $header.find('.sidebar-toggle').remove(); // Create toggle button with custom text var isCollapsed = startCollapsed.includes(headerText); var toggleText = isCollapsed ? 'Show' : 'Hide'; // Changed from [+]/[-] var $toggleBtn = $('<span class="sidebar-toggle">' + toggleText + '</span>'); // Add toggle button to header $header.append(' ').append($toggleBtn); // Set initial state if (isCollapsed) { $body.hide(); $portal.addClass('sidebar-collapsed'); } // Add click handler $toggleBtn.off('click').on('click', function(e) { e.preventDefault(); e.stopPropagation(); if ($body.is(':visible')) { $body.slideUp(300); $toggleBtn.text('Show'); // Changed from [+] $portal.addClass('sidebar-collapsed'); // Save state to localStorage localStorage.setItem('sidebar-' + headerText, 'collapsed'); } else { $body.slideDown(300); $toggleBtn.text('Hide'); // Changed from [-] $portal.removeClass('sidebar-collapsed'); // Save state to localStorage localStorage.setItem('sidebar-' + headerText, 'expanded'); } }); // Restore saved state from localStorage var savedState = localStorage.getItem('sidebar-' + headerText); if (savedState === 'collapsed' && $body.is(':visible')) { $body.hide(); $toggleBtn.text('Show'); $portal.addClass('sidebar-collapsed'); } else if (savedState === 'expanded' && !$body.is(':visible')) { $body.show(); $toggleBtn.text('Hide'); $portal.removeClass('sidebar-collapsed'); } } }); } // Initialize on page load initCollapsibleSidebar(); // Re-initialize after AJAX loads (if needed) $(document).ajaxComplete(function() { setTimeout(initCollapsibleSidebar, 100); }); });