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 $fieldsets
.addClass( 'prefsection' );
26 $fieldsets
.children( 'legend' ).addClass( 'mainLegend' );
28 // Make sure the accessibility tip is selectable so that screen reader users take notice,
29 // but hide it by default to reduce visual clutter.
30 // Make sure it becomes visible when focused.
31 $( '<div>' ).addClass( 'mw-navigation-hint' )
32 .text( mw
.msg( 'prefs-tabs-navigation-hint' ) )
33 .attr( 'tabIndex', 0 )
34 .on( 'focus blur', function ( e
) {
35 if ( e
.type
=== 'blur' || e
.type
=== 'focusout' ) {
36 $( this ).css( 'height', '0' );
38 $( this ).css( 'height', 'auto' );
40 } ).insertBefore( $preftoc
);
43 * It uses document.getElementById for security reasons (HTML injections in $()).
46 * @param {string} name the name of a tab without the prefix ("mw-prefsection-")
47 * @param {string} [mode] A hash will be set according to the current
48 * open section. Set mode 'noHash' to surpress this.
50 function switchPrefTab( name
, mode
) {
52 // Handle hash manually to prevent jumping,
53 // therefore save and restore scrollTop to prevent jumping.
54 scrollTop
= $( window
).scrollTop();
55 if ( mode
!== 'noHash' ) {
56 location
.hash
= '#mw-prefsection-' + name
;
58 $( window
).scrollTop( scrollTop
);
60 $preftoc
.find( 'li' ).removeClass( 'selected' )
63 'aria-selected': 'false'
66 $tab
= $( document
.getElementById( 'preftab-' + name
) );
70 'aria-selected': 'true'
72 .parent().addClass( 'selected' );
74 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
75 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
79 // Enable keyboard users to use left and right keys to switch tabs
80 $preftoc
.on( 'keydown', function ( event
) {
85 if ( event
.keyCode
=== keyLeft
) {
86 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
87 } else if ( event
.keyCode
=== keyRight
) {
88 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
92 if ( $el
.length
> 0 ) {
93 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
97 // Jump to correct section as indicated by the hash.
98 // This function is called onload and onhashchange.
99 function detectHash() {
100 var hash
= location
.hash
,
101 matchedElement
, parentSection
;
102 if ( hash
.match( /^#mw-prefsection-[\w]+$/ ) ) {
103 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
104 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
105 } else if ( hash
.match( /^#mw-[\w-]+$/ ) ) {
106 matchedElement
= document
.getElementById( hash
.slice( 1 ) );
107 parentSection
= $( matchedElement
).parent().closest( '[id^="mw-prefsection-"]' );
108 if ( parentSection
.length
) {
109 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
110 // Switch to proper tab and scroll to selected item.
111 switchPrefTab( parentSection
.attr( 'id' ).replace( 'mw-prefsection-', '' ), 'noHash' );
112 matchedElement
.scrollIntoView();
117 $( window
).on( 'hashchange', function () {
118 var hash
= location
.hash
;
119 if ( hash
.match( /^#mw-[\w-]+/ ) ) {
121 } else if ( hash
=== '' ) {
122 switchPrefTab( 'personal', 'noHash' );
125 // Run the function immediately to select the proper tab on startup.
126 .trigger( 'hashchange' );
128 // Restore the active tab after saving the preferences
129 previousTab
= mw
.storage
.session
.get( 'mwpreferences-prevTab' );
131 switchPrefTab( previousTab
, 'noHash' );
132 // Deleting the key, the tab states should be reset until we press Save
133 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
136 $( '#mw-prefs-form' ).on( 'submit', function () {
137 var value
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
138 mw
.storage
.session
.set( 'mwpreferences-prevTab', value
);