2 * JavaScript for Special:Preferences: Tab navigation.
6 var $preftoc
, $preferences
, $fieldsets
, labelFunc
, previousTab
;
8 labelFunc = function () {
9 return this.id
.replace( /^mw-prefsection/g, 'preftab' );
12 $preftoc
= $( '#preftoc' );
13 $preferences
= $( '#preferences' );
15 $fieldsets
= $preferences
.children( 'fieldset' )
18 'aria-labelledby': labelFunc
20 $fieldsets
.not( '#mw-prefsection-personal' )
22 .attr( 'aria-hidden', 'true' );
24 // T115692: The following is kept for backwards compatibility with older skins
25 $preferences
.addClass( 'jsprefs' );
26 $fieldsets
.addClass( 'prefsection' );
27 $fieldsets
.children( 'legend' ).addClass( 'mainLegend' );
29 // Make sure the accessibility tip is selectable so that screen reader users take notice,
30 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
31 // when selected. Similar to jquery.mw-jump
32 $( '<div>' ).addClass( 'mw-navigation-hint' )
33 .text( mw
.msg( 'prefs-tabs-navigation-hint' ) )
34 .attr( 'tabIndex', 0 )
35 .on( 'focus blur', function ( e
) {
36 if ( e
.type
=== 'blur' || e
.type
=== 'focusout' ) {
37 $( this ).css( 'height', '0' );
39 $( this ).css( 'height', 'auto' );
41 } ).insertBefore( $preftoc
);
44 * It uses document.getElementById for security reasons (HTML injections in $()).
47 * @param {string} name the name of a tab without the prefix ("mw-prefsection-")
48 * @param {string} [mode] A hash will be set according to the current
49 * open section. Set mode 'noHash' to surpress this.
51 function switchPrefTab( name
, mode
) {
53 // Handle hash manually to prevent jumping,
54 // therefore save and restore scrollTop to prevent jumping.
55 scrollTop
= $( window
).scrollTop();
56 if ( mode
!== 'noHash' ) {
57 location
.hash
= '#mw-prefsection-' + name
;
59 $( window
).scrollTop( scrollTop
);
61 $preftoc
.find( 'li' ).removeClass( 'selected' )
64 'aria-selected': 'false'
67 $tab
= $( document
.getElementById( 'preftab-' + name
) );
71 'aria-selected': 'true'
73 .parent().addClass( 'selected' );
75 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
76 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
80 // Enable keyboard users to use left and right keys to switch tabs
81 $preftoc
.on( 'keydown', function ( event
) {
86 if ( event
.keyCode
=== keyLeft
) {
87 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
88 } else if ( event
.keyCode
=== keyRight
) {
89 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
93 if ( $el
.length
> 0 ) {
94 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
98 // Jump to correct section as indicated by the hash.
99 // This function is called onload and onhashchange.
100 function detectHash() {
101 var hash
= location
.hash
,
102 matchedElement
, parentSection
;
103 if ( hash
.match( /^#mw-prefsection-[\w]+$/ ) ) {
104 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
105 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
106 } else if ( hash
.match( /^#mw-[\w-]+$/ ) ) {
107 matchedElement
= document
.getElementById( hash
.slice( 1 ) );
108 parentSection
= $( matchedElement
).parent().closest( '[id^="mw-prefsection-"]' );
109 if ( parentSection
.length
) {
110 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
111 // Switch to proper tab and scroll to selected item.
112 switchPrefTab( parentSection
.attr( 'id' ).replace( 'mw-prefsection-', '' ), 'noHash' );
113 matchedElement
.scrollIntoView();
118 $( window
).on( 'hashchange', function () {
119 var hash
= location
.hash
;
120 if ( hash
.match( /^#mw-[\w-]+/ ) ) {
122 } else if ( hash
=== '' ) {
123 switchPrefTab( 'personal', 'noHash' );
126 // Run the function immediately to select the proper tab on startup.
127 .trigger( 'hashchange' );
129 // Restore the active tab after saving the preferences
130 previousTab
= mw
.storage
.session
.get( 'mwpreferences-prevTab' );
132 switchPrefTab( previousTab
, 'noHash' );
133 // Deleting the key, the tab states should be reset until we press Save
134 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
137 $( '#mw-prefs-form' ).on( 'submit', function () {
138 var value
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
139 mw
.storage
.session
.set( 'mwpreferences-prevTab', value
);
143 }( mediaWiki
, jQuery
) );