MediaWiki:Gadget-CollapsibleNav.js

From Festipedia, hosted by the FR Heritage Group
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Wikipedia:Bypass your cache.
/**
 * Collapsible navigation for Vector
 * @maintainer Nardog
 */
// CC0

'use strict';

function onToggle( e ) {
	var collapsedIds = mw.storage.getObject( 'vector-nav-collapsed' ) || [],
		index = collapsedIds.indexOf( this.id );

	if ( e.type === 'afterExpand' ) {
		// Remove ID from array
		if ( index !== -1 ) {
			collapsedIds.splice( index, 1 );
		}

		if ( collapsedIds.length ) {
			mw.storage.setObject( 'vector-nav-collapsed', collapsedIds );
		} else {
			// Remove the data altogether if empty
			mw.storage.remove( 'vector-nav-collapsed' );
		}
	} else {
		if ( index === -1 ) {
			collapsedIds.push( this.id );
			mw.storage.setObject( 'vector-nav-collapsed', collapsedIds );
		}
	}
}

$( function () {
	var collapsedIds = mw.storage.getObject( 'vector-nav-collapsed' ) || [];

	$( '#mw-panel .vector-menu' )
		.addClass( 'collapsible-nav mw-collapsible' )
		.addClass( function () {
			if ( collapsedIds.indexOf( this.id ) !== -1 ) {
				return 'mw-collapsed';
			}
		} )
		.children( '.vector-menu-heading' )
			.addClass( 'mw-collapsible-toggle' )
			.end()
		.children( '.vector-menu-content' )
			.addClass( 'mw-collapsible-content' )
			.end()
		.makeCollapsible()
		.on( 'afterCollapse.mw-collapsible afterExpand.mw-collapsible', onToggle );
} );