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