Merge "Replace use of deprecated SpecialPageFactory::getGroup()"
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.preferences.js
1 /**
2 * JavaScript for Special:Preferences
3 */
4 jQuery( function ( $ ) {
5 var $preftoc, $preferences, $fieldsets, $legends,
6 hash, labelFunc,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime;
8
9 labelFunc = function () {
10 return this.id.replace( /^mw-prefsection/g, 'preftab' );
11 };
12
13 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
14 $preftoc = $('<ul id="preftoc"></ul>')
15 .attr( 'role', 'tablist' );
16 $preferences = $( '#preferences' )
17 .addClass( 'jsprefs' )
18 .before( $preftoc );
19 $fieldsets = $preferences.children( 'fieldset' )
20 .hide()
21 .attr( {
22 role: 'tabpanel',
23 'aria-hidden': 'true',
24 'aria-labelledby': labelFunc
25 } )
26 .addClass( 'prefsection' );
27 $legends = $fieldsets
28 .children( 'legend' )
29 .addClass( 'mainLegend' );
30
31 // Make sure the accessibility tip is selectable so that screen reader users take notice,
32 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
33 // when selected. Similar to jquery.mw-jump
34 $( '<div>' ).addClass( 'mw-navigation-hint' )
35 .text( mediaWiki.msg( 'prefs-tabs-navigation-hint' ) )
36 .attr( 'tabIndex', 0 )
37 .on( 'focus blur', function( e ) {
38 if ( e.type === 'blur' || e.type === 'focusout' ) {
39 $( this ).css( 'height', '0' );
40 } else {
41 $( this ).css( 'height', 'auto' );
42 }
43 } ).insertBefore( $preftoc );
44
45 /**
46 * It uses document.getElementById for security reasons (HTML injections in $()).
47 *
48 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
49 * @param String mode: [optional] A hash will be set according to the current
50 * open section. Set mode 'noHash' to surpress this.
51 */
52 function switchPrefTab( name, mode ) {
53 var $tab, scrollTop;
54 // Handle hash manually to prevent jumping,
55 // therefore save and restore scrollTop to prevent jumping.
56 scrollTop = $( window ).scrollTop();
57 if ( mode !== 'noHash' ) {
58 window.location.hash = '#mw-prefsection-' + name;
59 }
60 $( window ).scrollTop( scrollTop );
61
62 $preftoc.find( 'li' ).removeClass( 'selected' )
63 .find( 'a' ).attr( {
64 tabIndex: -1,
65 'aria-selected': 'false'
66 } );
67
68 $tab = $( document.getElementById( 'preftab-' + name ) );
69 if ( $tab.length ) {
70 $tab.attr( {
71 tabIndex: 0,
72 'aria-selected': 'true'
73 } )
74 .focus()
75 .parent().addClass( 'selected' );
76
77 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
78 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
79 }
80 }
81
82 // Populate the prefToc
83 $legends.each( function ( i, legend ) {
84 var $legend = $(legend),
85 ident, $li, $a;
86 if ( i === 0 ) {
87 $legend.parent().show();
88 }
89 ident = $legend.parent().attr( 'id' );
90
91 $li = $( '<li>' )
92 .attr( 'role', 'presentation' )
93 .addClass( i === 0 ? 'selected' : '' );
94 $a = $( '<a>' )
95 .attr( {
96 id: ident.replace( 'mw-prefsection', 'preftab' ),
97 href: '#' + ident,
98 role: 'tab',
99 tabIndex: i === 0 ? 0 : -1,
100 'aria-selected': i === 0 ? 'true' : 'false',
101 'aria-controls': ident
102 } )
103 .text( $legend.text() );
104 $li.append( $a );
105 $preftoc.append( $li );
106 } );
107
108 // Enable keyboard users to use left and right keys to switch tabs
109 $preftoc.on( 'keydown', function( event ) {
110 var keyLeft = 37,
111 keyRight = 39,
112 $el;
113
114 if( event.keyCode === keyLeft ) {
115 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
116 } else if ( event.keyCode === keyRight ) {
117 $el = $( '#preftoc li.selected' ).next().find( 'a' );
118 } else {
119 return;
120 }
121 if ( $el.length > 0 ) {
122 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
123 }
124 } );
125
126 // If we've reloaded the page or followed an open-in-new-window,
127 // make the selected tab visible.
128 hash = window.location.hash;
129 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
130 switchPrefTab( hash.replace( '#mw-prefsection-' , '' ) );
131 }
132
133 // In browsers that support the onhashchange event we will not bind click
134 // handlers and instead let the browser do the default behavior (clicking the
135 // <a href="#.."> will naturally set the hash, handled by onhashchange.
136 // But other things that change the hash will also be catched (e.g. using
137 // the Back and Forward browser navigation).
138 // Note the special check for IE "compatibility" mode.
139 if ( 'onhashchange' in window &&
140 ( document.documentMode === undefined || document.documentMode >= 8 )
141 ) {
142 $(window).on( 'hashchange' , function () {
143 var hash = window.location.hash;
144 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
145 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
146 } else if ( hash === '' ) {
147 switchPrefTab( 'personal', 'noHash' );
148 }
149 });
150 // In older browsers we'll bind a click handler as fallback.
151 // We must not have onhashchange *and* the click handlers, other wise
152 // the click handler calls switchPrefTab() which sets the hash value,
153 // which triggers onhashcange and calls switchPrefTab() again.
154 } else {
155 $preftoc.on( 'click', 'li a', function ( e ) {
156 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
157 e.preventDefault();
158 });
159 }
160
161 /**
162 * Timezone functions.
163 * Guesses Timezone from browser and updates fields onchange
164 */
165
166 $tzSelect = $( '#mw-input-wptimecorrection' );
167 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
168 $localtimeHolder = $( '#wpLocalTime' );
169 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
170
171 function minutesToHours( min ) {
172 var tzHour = Math.floor( Math.abs( min ) / 60 ),
173 tzMin = Math.abs( min ) % 60,
174 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
175 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
176 return tzString;
177 }
178
179 function hoursToMinutes( hour ) {
180 var minutes,
181 arr = hour.split( ':' );
182
183 arr[0] = parseInt( arr[0], 10 );
184
185 if ( arr.length === 1 ) {
186 // Specification is of the form [-]XX
187 minutes = arr[0] * 60;
188 } else {
189 // Specification is of the form [-]XX:XX
190 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
191 if ( arr[0] < 0 ) {
192 minutes *= -1;
193 }
194 }
195 // Gracefully handle non-numbers.
196 if ( isNaN( minutes ) ) {
197 return 0;
198 } else {
199 return minutes;
200 }
201 }
202
203 function updateTimezoneSelection () {
204 var minuteDiff, localTime,
205 type = $tzSelect.val();
206
207 if ( type === 'guess' ) {
208 // Get browser timezone & fill it in
209 minuteDiff = -( new Date().getTimezoneOffset() );
210 $tzTextbox.val( minutesToHours( minuteDiff ) );
211 $tzSelect.val( 'other' );
212 $tzTextbox.prop( 'disabled', false );
213 } else if ( type === 'other' ) {
214 // Grab data from the textbox, parse it.
215 minuteDiff = hoursToMinutes( $tzTextbox.val() );
216 } else {
217 // Grab data from the $tzSelect value
218 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
219 $tzTextbox.val( minutesToHours( minuteDiff ) );
220 }
221
222 // Determine local time from server time and minutes difference, for display.
223 localTime = servertime + minuteDiff;
224
225 // Bring time within the [0,1440) range.
226 while ( localTime < 0 ) {
227 localTime += 1440;
228 }
229 while ( localTime >= 1440 ) {
230 localTime -= 1440;
231 }
232 $localtimeHolder.text( minutesToHours( localTime ) );
233 }
234
235 if ( $tzSelect.length && $tzTextbox.length ) {
236 $tzSelect.change( updateTimezoneSelection );
237 $tzTextbox.blur( updateTimezoneSelection );
238 updateTimezoneSelection();
239 }
240
241 // Preserve the tab after saving the preferences
242 // Not using cookies, because their deletion results are inconsistent.
243 // Not using jStorage due to its enormous size (for this feature)
244 if ( window.sessionStorage ) {
245 if ( sessionStorage.getItem( 'mediawikiPreferencesTab' ) !== null ) {
246 switchPrefTab( sessionStorage.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
247 }
248 // Deleting the key, the tab states should be reset until we press Save
249 sessionStorage.removeItem( 'mediawikiPreferencesTab' );
250
251 $( '#mw-prefs-form' ).submit( function () {
252 var storageData = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
253 sessionStorage.setItem( 'mediawikiPreferencesTab', storageData );
254 } );
255 }
256 } );