Merge "resourceloader: Simplify StringSet fallback"
[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 $preferences.addClass( 'jsprefs' );
26 $fieldsets.addClass( 'prefsection' );
27 $fieldsets.children( 'legend' ).addClass( 'mainLegend' );
28
29 // Make sure the accessibility tip is selectable so that screen reader users take notice,
30 // but hide it by default to reduce visual clutter.
31 // Make sure it becomes visible when focused.
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' );
38 } else {
39 $( this ).css( 'height', 'auto' );
40 }
41 } ).insertBefore( $preftoc );
42
43 /**
44 * It uses document.getElementById for security reasons (HTML injections in $()).
45 *
46 * @ignore
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.
50 */
51 function switchPrefTab( name, mode ) {
52 var $tab, scrollTop;
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;
58 }
59 $( window ).scrollTop( scrollTop );
60
61 $preftoc.find( 'li' ).removeClass( 'selected' )
62 .find( 'a' ).attr( {
63 tabIndex: -1,
64 'aria-selected': 'false'
65 } );
66
67 $tab = $( document.getElementById( 'preftab-' + name ) );
68 if ( $tab.length ) {
69 $tab.attr( {
70 tabIndex: 0,
71 'aria-selected': 'true'
72 } ).focus()
73 .parent().addClass( 'selected' );
74
75 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
76 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
77 }
78 }
79
80 // Enable keyboard users to use left and right keys to switch tabs
81 $preftoc.on( 'keydown', function ( event ) {
82 var keyLeft = 37,
83 keyRight = 39,
84 $el;
85
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' );
90 } else {
91 return;
92 }
93 if ( $el.length > 0 ) {
94 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
95 }
96 } );
97
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();
114 }
115 }
116 }
117
118 $( window ).on( 'hashchange', function () {
119 var hash = location.hash;
120 if ( hash.match( /^#mw-[\w-]+/ ) ) {
121 detectHash();
122 } else if ( hash === '' ) {
123 switchPrefTab( 'personal', 'noHash' );
124 }
125 } )
126 // Run the function immediately to select the proper tab on startup.
127 .trigger( 'hashchange' );
128
129 // Restore the active tab after saving the preferences
130 previousTab = mw.storage.session.get( 'mwpreferences-prevTab' );
131 if ( previousTab ) {
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' );
135 }
136
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 );
140 } );
141
142 } );
143 }() );