MediaWiki:Common.js

THDeb​(留言 | 贡献)2026年8月11日 (二) 15:25的版本 (Add no-JavaScript-safe collapsible groups to Minerva sidebar)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
( function () {
	'use strict';

	var topicLabelList = [
		'校园总览',
		'区域与地点',
		'课程与教学',
		'组织与社团',
		'人物',
		'活动与赛事',
		'作品与资料',
		'待完善内容',
		'参与编写',
		'工具与浏览',
		'语言'
	];
	var topicLabels = new Set( topicLabelList );

	function buildMinervaTopicGroups() {
		var navigation = document.getElementById( 'p-navigation' );
		if ( !navigation || navigation.dataset.tyzxMobileGroupsBuilt === '1' ) {
			return;
		}
		var insertionPoint = navigation;
		topicLabelList.forEach( function ( label, index ) {
			var items = navigation.querySelectorAll( '.tyzx-mobile-group-' + index );
			if ( !items.length ) {
				return;
			}
			var portlet = document.createElement( 'nav' );
			var heading = document.createElement( 'h3' );
			var content = document.createElement( 'div' );
			var list = document.createElement( 'ul' );
			portlet.id = 'p-tyzx-mobile-' + index;
			portlet.className = 'mw-portlet tyzx-minerva-sidebar-portlet';
			heading.textContent = label;
			content.className = 'mw-portlet-body';
			items.forEach( function ( item ) {
				list.appendChild( item );
			} );
			content.appendChild( list );
			portlet.appendChild( heading );
			portlet.appendChild( content );
			insertionPoint.insertAdjacentElement( 'afterend', portlet );
			insertionPoint = portlet;
		} );
		navigation.dataset.tyzxMobileGroupsBuilt = '1';
	}

	function storageKey( portlet ) {
		return 'tyzx.sidebar.' + ( portlet.id || 'topic' );
	}

	function readOpenPreference( portlet ) {
		try {
			return window.localStorage.getItem( storageKey( portlet ) ) === 'open';
		} catch ( error ) {
			return false;
		}
	}

	function writeOpenPreference( portlet, open ) {
		try {
			window.localStorage.setItem( storageKey( portlet ), open ? 'open' : 'closed' );
		} catch ( error ) {
			// Storage can be unavailable in private or restricted browser contexts.
		}
	}

	function uniqueContentId( portlet, content, index ) {
		if ( content.id ) {
			return content.id;
		}
		var base = ( portlet.id || 'tyzx-sidebar-topic-' + index ) + '-content';
		var candidate = base;
		var suffix = 2;
		while ( document.getElementById( candidate ) ) {
			candidate = base + '-' + suffix;
			suffix++;
		}
		content.id = candidate;
		return candidate;
	}

	function setExpanded( portlet, button, content, expanded, persist ) {
		button.setAttribute( 'aria-expanded', expanded ? 'true' : 'false' );
		content.setAttribute( 'aria-hidden', expanded ? 'false' : 'true' );
		portlet.dataset.tyzxCollapsed = expanded ? 'false' : 'true';
		if ( expanded ) {
			content.removeAttribute( 'inert' );
		} else {
			content.setAttribute( 'inert', '' );
		}
		if ( persist ) {
			writeOpenPreference( portlet, expanded );
		}
	}

	function initializePortlet( portlet, index ) {
		if ( portlet.dataset.tyzxSidebarInitialized === '1' ) {
			return;
		}
		var heading = portlet.querySelector( ':scope > .vector-menu-heading, :scope > h3' );
		var content = portlet.querySelector(
			':scope > .vector-menu-content, :scope > .pBody, :scope > .mw-portlet-body'
		);
		if ( !heading || !content ) {
			return;
		}

		var label = heading.textContent.trim();
		if ( portlet.id === 'p-navigation' ) {
			portlet.classList.add( 'tyzx-sidebar-fixed' );
			portlet.dataset.tyzxSidebarInitialized = '1';
			return;
		}
		if ( !topicLabels.has( label ) ) {
			return;
		}
		var contentId = uniqueContentId( portlet, content, index );
		var button = document.createElement( 'button' );
		button.type = 'button';
		button.className = 'tyzx-sidebar-toggle';
		button.setAttribute( 'aria-controls', contentId );
		button.appendChild( document.createTextNode( label ) );
		heading.replaceChildren( button );
		content.classList.add( 'tyzx-sidebar-topic-content' );
		portlet.classList.add( 'tyzx-sidebar-topic' );
		if ( label === '语言' ) {
			portlet.classList.add( 'tyzx-sidebar-language' );
			var currentLanguage = content.querySelector( '.tyzx-sidebar-language-current a, a.tyzx-sidebar-language-current' );
			if ( currentLanguage ) {
				currentLanguage.setAttribute( 'aria-current', 'true' );
			}
		}
		portlet.dataset.tyzxSidebarInitialized = '1';
		setExpanded( portlet, button, content, readOpenPreference( portlet ), false );

		button.addEventListener( 'click', function () {
			setExpanded(
				portlet,
				button,
				content,
				button.getAttribute( 'aria-expanded' ) !== 'true',
				true
			);
		} );
	}

	function initializeSidebar() {
		buildMinervaTopicGroups();
		document.querySelectorAll( '.mw-portlet, .portlet.mw-portlet' )
			.forEach( initializePortlet );
	}

	function scheduleInitialization() {
		window.requestAnimationFrame( initializeSidebar );
	}

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', initializeSidebar, { once: true } );
	} else {
		initializeSidebar();
	}

	if ( window.mw && mw.hook ) {
		mw.hook( 'wikipage.content' ).add( scheduleInitialization );
	}
}() );