mediawiki.special.preferences/tabs.legacy.js: Drop old `jsprefs` class, unused
[lhc/web/wiklou.git] / resources / src / mediawiki.special.preferences / tabs.legacy.js
1 /*!
2 * JavaScript for Special:Preferences: Tab navigation.
3 */
4 ( function () {
5 $( function () {
6 var $preftoc, $preferences, $fieldsets, labelFunc, previousTab;
7
8 labelFunc = function () {
9 return this.id.replace( /^mw-prefsection/g, 'preftab' );
10 };
11
12 $preftoc = $( '#preftoc' );
13 $preferences = $( '#preferences' );
14
15 $fieldsets = $preferences.children( 'fieldset' )
16 .attr( {
17 role: 'tabpanel',
18 'aria-labelledby': labelFunc
19 } );
20 $fieldsets.not( '#mw-prefsection-personal' )
21 .hide()
22 .attr( 'aria-hidden', 'true' );
23
24 // T115692: The following is kept for backwards compatibility with older skins
25 $fieldsets.addClass( 'prefsection' );
26 $fieldsets.children( 'legend' ).addClass( 'mainLegend' );
27
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' );
37 } else {
38 $( this ).css( 'height', 'auto' );
39 }
40 } ).insertBefore( $preftoc );
41
42 /**
43 * It uses document.getElementById for security reasons (HTML injections in $()).
44 *
45 * @ignore
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.
49 */
50 function switchPrefTab( name, mode ) {
51 var $tab, scrollTop;
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;
57 }
58 $( window ).scrollTop( scrollTop );
59
60 $preftoc.find( 'li' ).removeClass( 'selected' )
61 .find( 'a' ).attr( {
62 tabIndex: -1,
63 'aria-selected': 'false'
64 } );
65
66 $tab = $( document.getElementById( 'preftab-' + name ) );
67 if ( $tab.length ) {
68 $tab.attr( {
69 tabIndex: 0,
70 'aria-selected': 'true'
71 } ).focus()
72 .parent().addClass( 'selected' );
73
74 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
75 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
76 }
77 }
78
79 // Enable keyboard users to use left and right keys to switch tabs
80 $preftoc.on( 'keydown', function ( event ) {
81 var keyLeft = 37,
82 keyRight = 39,
83 $el;
84
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' );
89 } else {
90 return;
91 }
92 if ( $el.length > 0 ) {
93 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
94 }
95 } );
96
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();
113 }
114 }
115 }
116
117 $( window ).on( 'hashchange', function () {
118 var hash = location.hash;
119 if ( hash.match( /^#mw-[\w-]+/ ) ) {
120 detectHash();
121 } else if ( hash === '' ) {
122 switchPrefTab( 'personal', 'noHash' );
123 }
124 } )
125 // Run the function immediately to select the proper tab on startup.
126 .trigger( 'hashchange' );
127
128 // Restore the active tab after saving the preferences
129 previousTab = mw.storage.session.get( 'mwpreferences-prevTab' );
130 if ( previousTab ) {
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' );
134 }
135
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 );
139 } );
140
141 } );
142 }() );