跳转到内容

天一学子免费网站托管计划: 面向全体天一在校学生及校友,提供免费网站托管、二级域名分发及付费制作服务。详见 公告页面。

立即参与贡献🎉 天一中学维基现在严重缺少词条,希望大家踊跃参与编辑。

警告⚠️: 严禁公开他人的私人信息,违者永久封杀

成为管理员: 若你是图灵社社员或是对天一维基作出重大贡献,便有机会成为管理员

MediaWiki:Common.js

来自江苏省天一中学非官方维基
THDeb​(留言 | 贡献)2026年8月11日 (二) 15:28的版本 (Render Minerva sidebar groups from server-generated navigation data)

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

  • 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 groups = window.mw && mw.config ? mw.config.get( 'wgTyzxSidebarGroups' ) : null;
		if ( !Array.isArray( groups ) || !groups.length ) {
			return;
		}
		var insertionPoint = navigation;
		groups.forEach( function ( group, index ) {
			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 = group.label;
			content.className = 'mw-portlet-body';
			group.links.forEach( function ( link, linkIndex ) {
				var item = document.createElement( 'li' );
				var anchor = document.createElement( 'a' );
				item.id = 'n-tyzx-mobile-' + index + '-' + linkIndex;
				item.className = 'mw-list-item ' + ( link.class || '' );
				anchor.href = link.href;
				anchor.textContent = link.text;
				if ( link.lang ) {
					anchor.lang = link.lang;
				}
				if ( link.hreflang ) {
					anchor.hreflang = link.hreflang;
				}
				if ( item.classList.contains( 'tyzx-sidebar-language-current' ) ) {
					anchor.setAttribute( 'aria-current', 'true' );
				}
				item.appendChild( anchor );
				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 );
	}
}() );