2 * JavaScript for Special:Preferences
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( mw
.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] 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 // Check for messageboxes (.successbox, .warningbox, .errorbox) to replace with notifications
90 if ( $( '.mw-preferences-messagebox' ).length
) {
91 // If there is a #mw-preferences-success box and javascript is enabled, use a slick notification instead!
92 if ( $( '#mw-preferences-success' ).length
) {
93 notif
= mw
.notification
.notify( mw
.message( 'savedprefs' ), { autoHide
: false } );
94 // 'change' event not reliable!
95 $( '#preftoc, .prefsection' ).one( 'change keydown mousedown', function () {
102 // Remove now-unnecessary success=1 querystring to prevent reappearance of notification on reload
103 if ( history
.replaceState
) {
104 history
.replaceState( {}, document
.title
, location
.href
.replace( /&?success=1/, '' ) );
109 // Populate the prefToc
110 $legends
.each( function ( i
, legend
) {
111 var $legend
= $( legend
),
114 $legend
.parent().show();
116 ident
= $legend
.parent().attr( 'id' );
119 .attr( 'role', 'presentation' )
120 .addClass( i
=== 0 ? 'selected' : '' );
123 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
126 tabIndex
: i
=== 0 ? 0 : -1,
127 'aria-selected': i
=== 0 ? 'true' : 'false',
128 'aria-controls': ident
130 .text( $legend
.text() );
132 $preftoc
.append( $li
);
135 // Disable the button to save preferences unless preferences have changed
136 $( '#prefcontrol' ).prop( 'disabled', true );
137 $( '.prefsection' ).one( 'change keydown mousedown', function () {
138 $( '#prefcontrol' ).prop( 'disabled', false );
141 // Enable keyboard users to use left and right keys to switch tabs
142 $preftoc
.on( 'keydown', function ( event
) {
147 if ( event
.keyCode
=== keyLeft
) {
148 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
149 } else if ( event
.keyCode
=== keyRight
) {
150 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
154 if ( $el
.length
> 0 ) {
155 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
159 // If we've reloaded the page or followed an open-in-new-window,
160 // make the selected tab visible.
161 hash
= location
.hash
;
162 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
163 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
166 // In browsers that support the onhashchange event we will not bind click
167 // handlers and instead let the browser do the default behavior (clicking the
168 // <a href="#.."> will naturally set the hash, handled by onhashchange.
169 // But other things that change the hash will also be catched (e.g. using
170 // the Back and Forward browser navigation).
171 // Note the special check for IE "compatibility" mode.
172 if ( 'onhashchange' in window
&&
173 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
175 $( window
).on( 'hashchange', function () {
176 var hash
= location
.hash
;
177 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
178 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
179 } else if ( hash
=== '' ) {
180 switchPrefTab( 'personal', 'noHash' );
183 // In older browsers we'll bind a click handler as fallback.
184 // We must not have onhashchange *and* the click handlers, other wise
185 // the click handler calls switchPrefTab() which sets the hash value,
186 // which triggers onhashcange and calls switchPrefTab() again.
188 $preftoc
.on( 'click', 'li a', function ( e
) {
189 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
194 // Timezone functions.
195 // Guesses Timezone from browser and updates fields onchange.
197 $tzSelect
= $( '#mw-input-wptimecorrection' );
198 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
199 $localtimeHolder
= $( '#wpLocalTime' );
200 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
202 function minutesToHours( min
) {
203 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
204 tzMin
= Math
.abs( min
) % 60,
205 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
206 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
210 function hoursToMinutes( hour
) {
212 arr
= hour
.split( ':' );
214 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
216 if ( arr
.length
=== 1 ) {
217 // Specification is of the form [-]XX
218 minutes
= arr
[ 0 ] * 60;
220 // Specification is of the form [-]XX:XX
221 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
222 if ( arr
[ 0 ] < 0 ) {
226 // Gracefully handle non-numbers.
227 if ( isNaN( minutes
) ) {
234 function updateTimezoneSelection() {
235 var minuteDiff
, localTime
,
236 type
= $tzSelect
.val();
238 if ( type
=== 'guess' ) {
239 // Get browser timezone & fill it in
240 minuteDiff
= -( new Date().getTimezoneOffset() );
241 $tzTextbox
.val( minutesToHours( minuteDiff
) );
242 $tzSelect
.val( 'other' );
243 $tzTextbox
.prop( 'disabled', false );
244 } else if ( type
=== 'other' ) {
245 // Grab data from the textbox, parse it.
246 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
248 // Grab data from the $tzSelect value
249 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
250 $tzTextbox
.val( minutesToHours( minuteDiff
) );
253 // Determine local time from server time and minutes difference, for display.
254 localTime
= servertime
+ minuteDiff
;
256 // Bring time within the [0,1440) range.
257 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
259 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
262 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
263 $tzSelect
.change( updateTimezoneSelection
);
264 $tzTextbox
.blur( updateTimezoneSelection
);
265 updateTimezoneSelection();
268 // Preserve the tab after saving the preferences
269 // Not using cookies, because their deletion results are inconsistent.
270 // Not using jStorage due to its enormous size (for this feature)
271 if ( window
.sessionStorage
) {
272 if ( sessionStorage
.getItem( 'mediawikiPreferencesTab' ) !== null ) {
273 switchPrefTab( sessionStorage
.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
275 // Deleting the key, the tab states should be reset until we press Save
276 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
278 $( '#mw-prefs-form' ).submit( function () {
279 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
280 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);
284 // To disable all 'namespace' checkboxes in Search preferences
285 // when 'Search in all namespaces' checkbox is ticked.
286 $checkBoxes
= $( '#mw-htmlform-advancedsearchoptions input[id^=mw-input-wpsearchnamespaces]' );
287 if ( $( '#mw-input-wpsearcheverything' ).prop( 'checked' ) ) {
288 $checkBoxes
.prop( 'disabled', true );
290 $( '#mw-input-wpsearcheverything' ).change( function () {
291 $checkBoxes
.prop( 'disabled', $( this ).prop( 'checked' ) );
294 // Set up a message to notify users if they try to leave the page without
296 $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
297 allowCloseWindow
= mw
.confirmCloseWindow( {
299 return $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' );
302 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
303 namespace: 'prefswarning'
305 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
306 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );
308 }( mediaWiki
, jQuery
) );