This wiki is still in active development, many areas of the site are unstable or broken.

In the meantime, consider adding content to pages you like!

MediaWiki:Gadget-CollapsibleNav.js: Difference between revisions

From NBITTRPG Wiki
 
mNo edit summary
 
(4 intermediate revisions by the same user not shown)
Line 6: Line 6:
  * @source /core/skins/vector/collapsibleNav.js
  * @source /core/skins/vector/collapsibleNav.js
  */
  */
mw.loader.using('oojs-ui-widgets').then(function() {
( function () {
'use strict';
'use strict';


var desktopSelectors = ['#site-tools', '#page-tools', '#catlinks-sidebar', '#other-languages'];
// Use the same function for all navigation headings - don't repeat
var globalSelectors = ['#site-navigation'];
function toggle( $element, forceSetting ) {
var isCollapsed = $element.parent().is( '.collapsed' );


function toggle($element, forceSetting) {
if (forceSetting == 'expand' && !isCollapsed
var isCollapsed = $element.parent().is('.collapsed');
|| forceSetting == 'close' && isCollapsed) {
 
if (forceSetting == 'expand' && !isCollapsed ||
forceSetting == 'close' && isCollapsed) {
return;
return;
}
}


$.cookie(
$.cookie(
'timeless-nav-' + $element.parent().attr('id'),
'timeless-nav-' + $element.parent().attr( 'id' ),
isCollapsed, {
isCollapsed,
'expires': 30,
{ 'expires': 30, 'path': '/' }
'path': '/'
}
);
);


$element
$element
.parent()
.parent()
.toggleClass('expanded')
.toggleClass( 'expanded' )
.toggleClass('collapsed')
.toggleClass( 'collapsed' )
.find('.mw-portlet-body')
.find( '.mw-portlet-body' )
.slideToggle('fast');
.slideToggle( 'fast' );
isCollapsed = !isCollapsed;
isCollapsed = !isCollapsed;


$element
$element
.find('> a')
.find( '> a' )
.attr({
.attr( {
'aria-pressed': isCollapsed ? 'false' : 'true',
'aria-pressed': isCollapsed ? 'false' : 'true',
'aria-expanded': isCollapsed ? 'false' : 'true'
'aria-expanded': isCollapsed ? 'false' : 'true'
});
} );
}
}


function run(selectors) {
$( function ( $ ) {
$(function($) {
var $headings, tabIndex;
var $headings, tabIndex;


selectors.forEach(function(selector) {
/* General Portal Modification */
// Apply a class to the entire panel to activate styles
$(selector).addClass('collapsible-nav');
$(selector + ' > .sidebar-inner > div > h3:not("#p-navigation-label")').addClass('collapsible-nav-header');


// Use cookie data to restore preferences of what to show and hide
// Apply a class to the entire panel to activate styles
$(selector + ' .mw-portlet')
$( '#site-navigation' ).addClass( 'collapsible-nav' );
.each(function(i) {
$( '#site-navigation > .sidebar-inner > div > h3' ).addClass( 'collapsible-nav-header' );
var id = $(this).attr('id'),
state = $.cookie('timeless-nav-' + id);
// Use cookie data to restore preferences of what to show and hide
$(this).find('ul:first').attr('id', id + '-list');
$( '#site-navigation .mw-portlet' )
// Add anchor tag to heading for better accessibility
.each( function ( i ) {
$(this).find('h3:not("#p-navigation-label")').wrapInner(
var id = $(this).attr( 'id' ),
$('<a>')
state = $.cookie( 'timeless-nav-' + id );
.attr({
$(this).find( 'ul:first' ).attr( 'id', id + '-list' );
href: '#',
// Add anchor tag to heading for better accessibility
'aria-haspopup': 'true',
$( this ).find( 'h3' ).wrapInner(
'aria-controls': id + '-list',
$( '<a>' )
role: 'button'
.attr( {
})
href: '#',
.click(false)
'aria-haspopup': 'true',
);
'aria-controls': id + '-list',
// In the case that we are not showing the new version, let's show the languages by default
role: 'button'
if (
} )
state === 'true' ||
.click( false )
state === null
);
) {
// In the case that we are not showing the new version, let's show the languages by default
$(this)
if (
.addClass('expanded')
state === 'true' ||
.removeClass('collapsed')
state === null  
.find('.mw-portlet-body')
) {
.hide() // bug 34450
$(this)
.show();
.addClass( 'expanded' )
$(this).find('h3:not("#p-navigation-label") > a')
.removeClass( 'collapsed' )
.attr({
.find( '.mw-portlet-body' )
'aria-pressed': 'true',
.hide() // bug 34450
'aria-expanded': 'true'
.show();
});
$(this).find( 'h3 > a' )
} else {
.attr( {
$(this)
'aria-pressed': 'true',
.addClass('collapsed')
'aria-expanded': 'true'
.removeClass('expanded');
} );
$(this).find('h3:not("#p-navigation-label") > a')
} else {
.attr({
$(this)
'aria-pressed': 'false',
.addClass( 'collapsed' )
'aria-expanded': 'false'
.removeClass( 'expanded' );
});
$(this).find( 'h3 > a' )
}
.attr( {
// Re-save cookie
'aria-pressed': 'false',
if (state !== null) {
'aria-expanded': 'false'
$.cookie('timeless-nav-' + $(this).attr('id'), state, {
} );
'expires': 30,
}
'path': '/'
// Re-save cookie
});
if ( state !== null ) {
}
$.cookie( 'timeless-nav-' + $(this).attr( 'id' ), state, { 'expires': 30, 'path': '/' } );
});
}
} );


/* Tab Indexing */
/* Tab Indexing */
// Make it keyboard accessible
$headings = $(selector + ' .mw-portlet > h3:not("#p-navigation-label")');
$headings.attr('tabindex', '0');


// Toggle the selected menu's class and expand or collapse the menu
$headings = $( '#site-navigation .mw-portlet > h3' );
$(selector)
.on('keydown', '.mw-portlet > h3:not("#p-navigation-label")', function(e) {
// Make the space and enter keys act as a click
if (e.which === 13 /* Enter */ || e.which === 32 /* Space */ ) {
toggle($(this));
}
})
.on('mousedown', '.mw-portlet > h3:not("#p-navigation-label")', function(e) {
if (e.which !== 3) { // Right mouse click
toggle($(this));
$(this).blur();
}
return false;
});
});
});
}


var initialState = {};
// Make it keyboard accessible
desktopSelectors.forEach(function(selector) {
$headings.attr( 'tabindex', '0' );
initialState[selector] = $(selector).clone(true);
});


var navigationPortletExpandState = $.cookie('timeless-nav-p-navigation');
// Toggle the selected menu's class and expand or collapse the menu
var toggleText = 'Close All';
$( '#site-navigation' )
if (navigationPortletExpandState !== 'true' && navigationPortletExpandState !== null) {
.on( 'keydown', '.mw-portlet > h3', function ( e ) {
toggleText = 'Expand All';
// Make the space and enter keys act as a click
}
if ( e.which === 13 /* Enter */ || e.which === 32 /* Space */ ) {
$('#p-New_Horizons').prepend("<div id=\"sidebar-collapsible-toggle\">" + toggleText + "</div>");
toggle( $(this) );
$("#sidebar-collapsible-toggle").click(function() {
if ($("#sidebar-collapsible-toggle").text() == "Expand All") {
$("#sidebar-collapsible-toggle").text("Close All");
$('#site-navigation .mw-portlet > h3:not("#p-navigation-label")').each(function(i) {
toggle($(this), 'expand');
});
$.cookie(
'timeless-nav-text-toggle',
true, {
'expires': 30,
'path': '/'
}
}
);
} )
} else {
.on( 'mousedown', '.mw-portlet > h3', function ( e ) {
$("#sidebar-collapsible-toggle").text("Expand All");
if ( e.which !== 3 ) { // Right mouse click
$('#site-navigation .mw-portlet > h3:not("#p-navigation-label")').each(function(i) {
toggle( $(this) );
toggle($(this), 'close');
$(this).blur();
});
$.cookie(
'timeless-nav-text-toggle',
false, {
'expires': 30,
'path': '/'
}
}
);
return false;
} );
});
var navigationPortletExpandState = $.cookie( 'timeless-nav-p-navigation' );
var toggleText = 'Close All';
if (navigationPortletExpandState !== 'true' && navigationPortletExpandState !== null) {
toggleText = 'Expand All';
}
}
});
$( '#site-navigation > .sidebar-inner' ).prepend("<div id=\"sidebar-collapsible-toggle\">" + toggleText + "</div>");
 
$("#sidebar-collapsible-toggle").click(function() {
if ($(window).width() > 1099) {
if($("#sidebar-collapsible-toggle").text() == "Expand All") {
run(desktopSelectors.concat(globalSelectors));
$("#sidebar-collapsible-toggle").text("Close All");
} else {
$( '#site-navigation .mw-portlet > h3' ).each( function ( i ) {
run(globalSelectors);
toggle( $(this), 'expand');
}
} );
 
$.cookie(
function resizeCheck() {
'timeless-nav-text-toggle',
if ($(window).width() > 1099) {
true,
if (!$(desktopSelectors[0]).hasClass('collapsible-nav')) {
{ 'expires': 30, 'path': '/' }
run(desktopSelectors);
);
} else {
$("#sidebar-collapsible-toggle").text("Expand All");
$( '#site-navigation .mw-portlet > h3' ).each( function ( i ) {
toggle( $(this), 'close');
} );
$.cookie(
'timeless-nav-text-toggle',
false,
{ 'expires': 30, 'path': '/' }
);
}
}
} else {
});
if ($(desktopSelectors[0]).hasClass('collapsible-nav')) {
}() );
desktopSelectors.forEach(function(selector) {
$(selector).replaceWith(initialState[selector].clone(true));
});
}
}
}
 
window.onresize = resizeCheck;
});

Latest revision as of 20:43, 8 October 2026

/**
 * Collapsible navigation for Timeless, based on the Collapsible Navigation from Vector.
 * Derived from Dragalia Lost Wiki by Elaeagnifolia (https://dragalialost.wiki/w/User:Elaeagnifolia) 
 * Licensed under CC BY-NC-SA 4.0 (https://creativecommons.org/licenses/by-nc-sa/4.0/)
 * @source https://dragalialost.wiki/w/MediaWiki:Gadget-CollapsibleNav.js
 * @source /core/skins/vector/collapsibleNav.js
 */
( function () {
	'use strict';

	// Use the same function for all navigation headings - don't repeat
	function toggle( $element, forceSetting ) {
		var isCollapsed = $element.parent().is( '.collapsed' );

		if (forceSetting == 'expand' && !isCollapsed
			|| forceSetting == 'close' && isCollapsed) {
			return;
		}

		$.cookie(
			'timeless-nav-' + $element.parent().attr( 'id' ),
			isCollapsed,
			{ 'expires': 30, 'path': '/' }
		);

		$element
			.parent()
			.toggleClass( 'expanded' )
			.toggleClass( 'collapsed' )
			.find( '.mw-portlet-body' )
			.slideToggle( 'fast' );
		isCollapsed = !isCollapsed;

		$element
			.find( '> a' )
			.attr( {
				'aria-pressed': isCollapsed ? 'false' : 'true',
				'aria-expanded': isCollapsed ? 'false' : 'true'
			} );
	}

	$( function ( $ ) {
		var $headings, tabIndex;

		/* General Portal Modification */

		// Apply a class to the entire panel to activate styles
		$( '#site-navigation' ).addClass( 'collapsible-nav' );
		$( '#site-navigation > .sidebar-inner > div > h3' ).addClass( 'collapsible-nav-header' );
		
		// Use cookie data to restore preferences of what to show and hide
		$( '#site-navigation .mw-portlet' )
			.each( function ( i ) {
				var id = $(this).attr( 'id' ),
					state = $.cookie( 'timeless-nav-' + id );
				$(this).find( 'ul:first' ).attr( 'id', id + '-list' );
				// Add anchor tag to heading for better accessibility
				$( this ).find( 'h3' ).wrapInner(
					$( '<a>' )
						.attr( {
							href: '#',
							'aria-haspopup': 'true',
							'aria-controls': id + '-list',
							role: 'button'
						} )
						.click( false )
				);
				// In the case that we are not showing the new version, let's show the languages by default
				if (
					state === 'true' ||
					state === null 
				) {
					$(this)
						.addClass( 'expanded' )
						.removeClass( 'collapsed' )
						.find( '.mw-portlet-body' )
						.hide() // bug 34450
						.show();
					$(this).find( 'h3 > a' )
						.attr( {
							'aria-pressed': 'true',
							'aria-expanded': 'true'
						} );
				} else {
					$(this)
						.addClass( 'collapsed' )
						.removeClass( 'expanded' );
					$(this).find( 'h3 > a' )
						.attr( {
							'aria-pressed': 'false',
							'aria-expanded': 'false'
						} );
				}
				// Re-save cookie
				if ( state !== null ) {
					$.cookie( 'timeless-nav-' + $(this).attr( 'id' ), state, { 'expires': 30, 'path': '/' } );
				}
			} );

		/* Tab Indexing */

		$headings = $( '#site-navigation .mw-portlet > h3' );

		// Make it keyboard accessible
		$headings.attr( 'tabindex', '0' );

		// Toggle the selected menu's class and expand or collapse the menu
		$( '#site-navigation' )
			.on( 'keydown', '.mw-portlet > h3', function ( e ) {
				// Make the space and enter keys act as a click
				if ( e.which === 13 /* Enter */ || e.which === 32 /* Space */ ) {
					toggle( $(this) );
				}
			} )
			.on( 'mousedown', '.mw-portlet > h3', function ( e ) {
				if ( e.which !== 3 ) { // Right mouse click
					toggle( $(this) );
					$(this).blur();
				}
				return false;
			} );
		});
		
		var navigationPortletExpandState = $.cookie( 'timeless-nav-p-navigation' );
		var toggleText = 'Close All';
		if (navigationPortletExpandState !== 'true' && navigationPortletExpandState !== null) {
			toggleText = 'Expand All';
		}
		$( '#site-navigation > .sidebar-inner' ).prepend("<div id=\"sidebar-collapsible-toggle\">" + toggleText + "</div>");
		$("#sidebar-collapsible-toggle").click(function() {
			if($("#sidebar-collapsible-toggle").text() == "Expand All") {
				$("#sidebar-collapsible-toggle").text("Close All");
				$( '#site-navigation .mw-portlet > h3' ).each( function ( i ) {
					toggle( $(this), 'expand');
				} );
				$.cookie(
					'timeless-nav-text-toggle',
					true,
					{ 'expires': 30, 'path': '/' }
				);
			} else {
				$("#sidebar-collapsible-toggle").text("Expand All");
				$( '#site-navigation .mw-portlet > h3' ).each( function ( i ) {
					toggle( $(this), 'close');
				} );
				$.cookie(
					'timeless-nav-text-toggle',
					false,
					{ 'expires': 30, 'path': '/' }
				);
			}
		});
}() );