3 * JavaScript for Special:Preferences
5 jQuery( function ( $ ) {
6 var $preftoc
, $preferences
, $fieldsets
, $legends
,
8 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
,
9 $checkBoxes
, allowCloseWindow
,
12 labelFunc = function () {
13 return this.id
.replace( /^mw-prefsection/g, 'preftab' );
16 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
17 $preftoc
= $( '<ul>' )
22 $preferences
= $( '#preferences' )
23 .addClass( 'jsprefs' )
25 $fieldsets
= $preferences
.children( 'fieldset' )
29 'aria-hidden': 'true',
30 'aria-labelledby': labelFunc
32 .addClass( 'prefsection' );
35 .addClass( 'mainLegend' );
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' );
47 $( this ).css( 'height', 'auto' );
49 } ).insertBefore( $preftoc
);
52 * It uses document.getElementById for security reasons (HTML injections in $()).
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.
59 function switchPrefTab( name
, mode
) {
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
;
67 $( window
).scrollTop( scrollTop
);
69 $preftoc
.find( 'li' ).removeClass( 'selected' )
72 'aria-selected': 'false'
75 $tab
= $( document
.getElementById( 'preftab-' + name
) );
79 'aria-selected': 'true'
82 .parent().addClass( 'selected' );
84 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
85 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
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!
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/, '' ) );
104 // Populate the prefToc
105 $legends
.each( function ( i
, legend
) {
106 var $legend
= $( legend
),
109 $legend
.parent().show();
111 ident
= $legend
.parent().attr( 'id' );
114 .attr( 'role', 'presentation' )
115 .addClass( i
=== 0 ? 'selected' : '' );
118 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
121 tabIndex
: i
=== 0 ? 0 : -1,
122 'aria-selected': i
=== 0 ? 'true' : 'false',
123 'aria-controls': ident
125 .text( $legend
.text() );
127 $preftoc
.append( $li
);
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);
136 // Enable keyboard users to use left and right keys to switch tabs
137 $preftoc
.on( 'keydown', function ( event
) {
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' );
149 if ( $el
.length
> 0 ) {
150 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
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-', '' ) );
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 )
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' );
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.
183 $preftoc
.on( 'click', 'li a', function ( e
) {
184 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
189 // Timezone functions.
190 // Guesses Timezone from browser and updates fields onchange.
192 $tzSelect
= $( '#mw-input-wptimecorrection' );
193 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
194 $localtimeHolder
= $( '#wpLocalTime' );
195 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
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
;
205 function hoursToMinutes( hour
) {
207 arr
= hour
.split( ':' );
209 arr
[0] = parseInt( arr
[0], 10 );
211 if ( arr
.length
=== 1 ) {
212 // Specification is of the form [-]XX
213 minutes
= arr
[0] * 60;
215 // Specification is of the form [-]XX:XX
216 minutes
= Math
.abs( arr
[0] ) * 60 + parseInt( arr
[1], 10 );
221 // Gracefully handle non-numbers.
222 if ( isNaN( minutes
) ) {
229 function updateTimezoneSelection() {
230 var minuteDiff
, localTime
,
231 type
= $tzSelect
.val();
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() );
243 // Grab data from the $tzSelect value
244 minuteDiff
= parseInt( type
.split( '|' )[1], 10 ) || 0;
245 $tzTextbox
.val( minutesToHours( minuteDiff
) );
248 // Determine local time from server time and minutes difference, for display.
249 localTime
= servertime
+ minuteDiff
;
251 // Bring time within the [0,1440) range.
252 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
254 $localtimeHolder
.text( mediaWiki
.language
.convertNumber( minutesToHours( localTime
) ) );
257 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
258 $tzSelect
.change( updateTimezoneSelection
);
259 $tzTextbox
.blur( updateTimezoneSelection
);
260 updateTimezoneSelection();
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' );
270 // Deleting the key, the tab states should be reset until we press Save
271 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
273 $( '#mw-prefs-form' ).submit( function () {
274 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
275 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);
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 );
285 $( '#mw-input-wpsearcheverything' ).change( function () {
286 $checkBoxes
.prop( 'disabled', $( this ).prop( 'checked' ) );
289 // Set up a message to notify users if they try to leave the page without
291 $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
292 allowCloseWindow
= mediaWiki
.confirmCloseWindow( {
294 return $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' );
297 message
: mediaWiki
.msg( 'prefswarning-warning', mediaWiki
.msg( 'saveprefs' ) ),
298 namespace: 'prefswarning'
300 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
301 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );