Use native Vector 2022 navigation and language selection 标签:替换 |
小 Add native collapsible sidebar groups |
||
| 第1行: | 第1行: | ||
/* Navigation and language selection use the native skin components. */ | /* Navigation and language selection use the native skin components. */ | ||
/* Add accessible disclosure behaviour to the native Vector sidebar groups. */ | |||
( function () { | |||
'use strict'; | |||
var collapsiblePortlets = [ | |||
'p-校园总览', | |||
'p-区域与地点', | |||
'p-课程与教学', | |||
'p-组织与社团', | |||
'p-人物', | |||
'p-活动与赛事', | |||
'p-作品与资料', | |||
'p-待完善内容', | |||
'p-参与编写', | |||
'p-工具与浏览' | |||
]; | |||
function isCurrentPortlet( content ) { | |||
var current = new URL( window.location.href ); | |||
if ( content.querySelector( '.mw-list-item.selected, .mw-list-item.active, a[aria-current="page"]' ) ) { | |||
return true; | |||
} | |||
return Array.prototype.some.call( content.querySelectorAll( 'a[href]' ), function ( link ) { | |||
var target = new URL( link.href, window.location.href ); | |||
return target.origin === current.origin && | |||
target.pathname === current.pathname && | |||
target.search === current.search; | |||
} ); | |||
} | |||
function enhancePortlet( portlet ) { | |||
var heading; | |||
var content; | |||
var button; | |||
var icon; | |||
var label; | |||
var contentId; | |||
if ( collapsiblePortlets.indexOf( portlet.id ) === -1 || portlet.dataset.tyzxCollapsible === '1' ) { | |||
return; | |||
} | |||
heading = portlet.querySelector( ':scope > .vector-menu-heading' ); | |||
content = portlet.querySelector( ':scope > .vector-menu-content' ); | |||
if ( !heading || !content ) { | |||
return; | |||
} | |||
label = heading.textContent.trim(); | |||
contentId = content.id || portlet.id + '-content'; | |||
content.id = contentId; | |||
button = document.createElement( 'button' ); | |||
button.type = 'button'; | |||
button.className = 'tyzx-sidebar-toggle'; | |||
button.setAttribute( 'aria-controls', contentId ); | |||
icon = document.createElement( 'span' ); | |||
icon.className = 'tyzx-sidebar-toggle__icon'; | |||
icon.setAttribute( 'aria-hidden', 'true' ); | |||
button.appendChild( icon ); | |||
button.appendChild( document.createTextNode( label ) ); | |||
heading.textContent = ''; | |||
heading.appendChild( button ); | |||
portlet.classList.add( 'tyzx-sidebar-collapsible' ); | |||
portlet.dataset.tyzxCollapsible = '1'; | |||
function setOpen( open ) { | |||
button.setAttribute( 'aria-expanded', open ? 'true' : 'false' ); | |||
content.hidden = !open; | |||
portlet.classList.toggle( 'is-open', open ); | |||
} | |||
setOpen( isCurrentPortlet( content ) ); | |||
button.addEventListener( 'click', function () { | |||
setOpen( button.getAttribute( 'aria-expanded' ) !== 'true' ); | |||
} ); | |||
button.addEventListener( 'keydown', function ( event ) { | |||
if ( event.key === 'Escape' ) { | |||
setOpen( false ); | |||
button.focus(); | |||
} | |||
} ); | |||
} | |||
function initSidebar() { | |||
Array.prototype.forEach.call( | |||
document.querySelectorAll( '#vector-main-menu .mw-portlet' ), | |||
enhancePortlet | |||
); | |||
} | |||
if ( document.readyState === 'loading' ) { | |||
document.addEventListener( 'DOMContentLoaded', initSidebar ); | |||
} else { | |||
initSidebar(); | |||
} | |||
}() ); | |||