2 * JavaScript for Special:Preferences
6 var $preftoc
, $preferences
, $fieldsets
,
8 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
,
9 allowCloseWindow
, notif
;
11 labelFunc = function () {
12 return this.id
.replace( /^mw-prefsection/g, 'preftab' );
15 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
16 $preftoc
= $( '#preftoc' );
17 $preferences
= $( '#preferences' );
19 $fieldsets
= $preferences
.children( 'fieldset' )
22 'aria-labelledby': labelFunc
24 $fieldsets
.not( '#mw-prefsection-personal' )
26 .attr( 'aria-hidden', 'true' );
28 // T115692: The following is kept for backwards compatibility with older skins
29 $preferences
.addClass( 'jsprefs' );
30 $fieldsets
.addClass( 'prefsection' );
31 $fieldsets
.children( 'legend' ).addClass( 'mainLegend' );
33 // Make sure the accessibility tip is selectable so that screen reader users take notice,
34 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
35 // when selected. Similar to jquery.mw-jump
36 $( '<div>' ).addClass( 'mw-navigation-hint' )
37 .text( mw
.msg( 'prefs-tabs-navigation-hint' ) )
38 .attr( 'tabIndex', 0 )
39 .on( 'focus blur', function ( e
) {
40 if ( e
.type
=== 'blur' || e
.type
=== 'focusout' ) {
41 $( this ).css( 'height', '0' );
43 $( this ).css( 'height', 'auto' );
45 } ).insertBefore( $preftoc
);
48 * It uses document.getElementById for security reasons (HTML injections in $()).
51 * @param {string} name the name of a tab without the prefix ("mw-prefsection-")
52 * @param {string} [mode] A hash will be set according to the current
53 * open section. Set mode 'noHash' to surpress this.
55 function switchPrefTab( name
, mode
) {
57 // Handle hash manually to prevent jumping,
58 // therefore save and restore scrollTop to prevent jumping.
59 scrollTop
= $( window
).scrollTop();
60 if ( mode
!== 'noHash' ) {
61 location
.hash
= '#mw-prefsection-' + name
;
63 $( window
).scrollTop( scrollTop
);
65 $preftoc
.find( 'li' ).removeClass( 'selected' )
68 'aria-selected': 'false'
71 $tab
= $( document
.getElementById( 'preftab-' + name
) );
75 'aria-selected': 'true'
78 .parent().addClass( 'selected' );
80 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
81 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
85 // Check for messageboxes (.successbox, .warningbox, .errorbox) to replace with notifications
86 if ( $( '.mw-preferences-messagebox' ).length
) {
87 // If there is a #mw-preferences-success box and javascript is enabled, use a slick notification instead!
88 if ( $( '#mw-preferences-success' ).length
) {
89 notif
= mw
.notification
.notify( mw
.message( 'savedprefs' ), { autoHide
: false } );
90 // 'change' event not reliable!
91 $( '#preftoc, .prefsection' ).one( 'change keydown mousedown', function () {
98 // Remove now-unnecessary success=1 querystring to prevent reappearance of notification on reload
99 if ( history
.replaceState
) {
100 history
.replaceState( {}, document
.title
, location
.href
.replace( /&?success=1/, '' ) );
105 // Disable the button to save preferences unless preferences have changed
106 $( '#prefcontrol' ).prop( 'disabled', true );
107 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
108 $( '#prefcontrol' ).prop( 'disabled', false );
111 // Enable keyboard users to use left and right keys to switch tabs
112 $preftoc
.on( 'keydown', function ( event
) {
117 if ( event
.keyCode
=== keyLeft
) {
118 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
119 } else if ( event
.keyCode
=== keyRight
) {
120 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
124 if ( $el
.length
> 0 ) {
125 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
129 // If we've reloaded the page or followed an open-in-new-window,
130 // make the selected tab visible.
131 hash
= location
.hash
;
132 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
133 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
136 // In browsers that support the onhashchange event we will not bind click
137 // handlers and instead let the browser do the default behavior (clicking the
138 // <a href="#.."> will naturally set the hash, handled by onhashchange.
139 // But other things that change the hash will also be catched (e.g. using
140 // the Back and Forward browser navigation).
141 // Note the special check for IE "compatibility" mode.
142 if ( 'onhashchange' in window
&&
143 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
145 $( window
).on( 'hashchange', function () {
146 var hash
= location
.hash
;
147 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
148 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
149 } else if ( hash
=== '' ) {
150 switchPrefTab( 'personal', 'noHash' );
153 // In older browsers we'll bind a click handler as fallback.
154 // We must not have onhashchange *and* the click handlers, other wise
155 // the click handler calls switchPrefTab() which sets the hash value,
156 // which triggers onhashcange and calls switchPrefTab() again.
158 $preftoc
.on( 'click', 'li a', function ( e
) {
159 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
164 // Timezone functions.
165 // Guesses Timezone from browser and updates fields onchange.
167 $tzSelect
= $( '#mw-input-wptimecorrection' );
168 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
169 $localtimeHolder
= $( '#wpLocalTime' );
170 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
172 function minutesToHours( min
) {
173 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
174 tzMin
= Math
.abs( min
) % 60,
175 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
176 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
180 function hoursToMinutes( hour
) {
182 arr
= hour
.split( ':' );
184 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
186 if ( arr
.length
=== 1 ) {
187 // Specification is of the form [-]XX
188 minutes
= arr
[ 0 ] * 60;
190 // Specification is of the form [-]XX:XX
191 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
192 if ( arr
[ 0 ] < 0 ) {
196 // Gracefully handle non-numbers.
197 if ( isNaN( minutes
) ) {
204 function updateTimezoneSelection() {
205 var minuteDiff
, localTime
,
206 type
= $tzSelect
.val();
208 if ( type
=== 'guess' ) {
209 // Get browser timezone & fill it in
210 minuteDiff
= -( new Date().getTimezoneOffset() );
211 $tzTextbox
.val( minutesToHours( minuteDiff
) );
212 $tzSelect
.val( 'other' );
213 $tzTextbox
.prop( 'disabled', false );
214 } else if ( type
=== 'other' ) {
215 // Grab data from the textbox, parse it.
216 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
218 // Grab data from the $tzSelect value
219 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
220 $tzTextbox
.val( minutesToHours( minuteDiff
) );
223 // Determine local time from server time and minutes difference, for display.
224 localTime
= servertime
+ minuteDiff
;
226 // Bring time within the [0,1440) range.
227 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
229 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
232 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
233 $tzSelect
.change( updateTimezoneSelection
);
234 $tzTextbox
.blur( updateTimezoneSelection
);
235 updateTimezoneSelection();
238 // Preserve the tab after saving the preferences
239 // Not using cookies, because their deletion results are inconsistent.
240 // Not using jStorage due to its enormous size (for this feature)
241 if ( window
.sessionStorage
) {
242 if ( sessionStorage
.getItem( 'mediawikiPreferencesTab' ) !== null ) {
243 switchPrefTab( sessionStorage
.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
245 // Deleting the key, the tab states should be reset until we press Save
246 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
248 $( '#mw-prefs-form' ).submit( function () {
249 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
250 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);
254 // Set up a message to notify users if they try to leave the page without
256 $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
257 allowCloseWindow
= mw
.confirmCloseWindow( {
259 return $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' );
262 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
263 namespace: 'prefswarning'
265 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
266 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );
268 }( mediaWiki
, jQuery
) );