2 * JavaScript for Special:Preferences
4 jQuery( function ( $ ) {
5 var $preftoc
, $preferences
, $fieldsets
, $legends
,
7 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
,
8 $checkBoxes
, allowCloseWindow
;
10 labelFunc = function () {
11 return this.id
.replace( /^mw-prefsection/g, 'preftab' );
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc
= $( '<ul>' )
20 $preferences
= $( '#preferences' )
21 .addClass( 'jsprefs' )
23 $fieldsets
= $preferences
.children( 'fieldset' )
27 'aria-hidden': 'true',
28 'aria-labelledby': labelFunc
30 .addClass( 'prefsection' );
33 .addClass( 'mainLegend' );
35 // Make sure the accessibility tip is selectable so that screen reader users take notice,
36 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
37 // when selected. Similar to jquery.mw-jump
38 $( '<div>' ).addClass( 'mw-navigation-hint' )
39 .text( mediaWiki
.msg( 'prefs-tabs-navigation-hint' ) )
40 .attr( 'tabIndex', 0 )
41 .on( 'focus blur', function ( e
) {
42 if ( e
.type
=== 'blur' || e
.type
=== 'focusout' ) {
43 $( this ).css( 'height', '0' );
45 $( this ).css( 'height', 'auto' );
47 } ).insertBefore( $preftoc
);
50 * It uses document.getElementById for security reasons (HTML injections in $()).
53 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
54 * @param String mode: [optional] A hash will be set according to the current
55 * open section. Set mode 'noHash' to surpress this.
57 function switchPrefTab( name
, mode
) {
59 // Handle hash manually to prevent jumping,
60 // therefore save and restore scrollTop to prevent jumping.
61 scrollTop
= $( window
).scrollTop();
62 if ( mode
!== 'noHash' ) {
63 location
.hash
= '#mw-prefsection-' + name
;
65 $( window
).scrollTop( scrollTop
);
67 $preftoc
.find( 'li' ).removeClass( 'selected' )
70 'aria-selected': 'false'
73 $tab
= $( document
.getElementById( 'preftab-' + name
) );
77 'aria-selected': 'true'
80 .parent().addClass( 'selected' );
82 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
83 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
87 // Populate the prefToc
88 $legends
.each( function ( i
, legend
) {
89 var $legend
= $( legend
),
92 $legend
.parent().show();
94 ident
= $legend
.parent().attr( 'id' );
97 .attr( 'role', 'presentation' )
98 .addClass( i
=== 0 ? 'selected' : '' );
101 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
104 tabIndex
: i
=== 0 ? 0 : -1,
105 'aria-selected': i
=== 0 ? 'true' : 'false',
106 'aria-controls': ident
108 .text( $legend
.text() );
110 $preftoc
.append( $li
);
113 // Disable the button to save preferences unless preferences have changed
114 $( '#prefcontrol' ).prop( 'disabled', true );
115 $( '.prefsection' ).one( 'change keydown mousedown', function () {
116 $( '#prefcontrol' ).prop( 'disabled', false);
119 // Enable keyboard users to use left and right keys to switch tabs
120 $preftoc
.on( 'keydown', function ( event
) {
125 if ( event
.keyCode
=== keyLeft
) {
126 $el
= $( '#preftoc li.selected' ).prev().find( 'a' );
127 } else if ( event
.keyCode
=== keyRight
) {
128 $el
= $( '#preftoc li.selected' ).next().find( 'a' );
132 if ( $el
.length
> 0 ) {
133 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
137 // If we've reloaded the page or followed an open-in-new-window,
138 // make the selected tab visible.
139 hash
= location
.hash
;
140 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
141 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
144 // In browsers that support the onhashchange event we will not bind click
145 // handlers and instead let the browser do the default behavior (clicking the
146 // <a href="#.."> will naturally set the hash, handled by onhashchange.
147 // But other things that change the hash will also be catched (e.g. using
148 // the Back and Forward browser navigation).
149 // Note the special check for IE "compatibility" mode.
150 if ( 'onhashchange' in window
&&
151 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
153 $( window
).on( 'hashchange', function () {
154 var hash
= location
.hash
;
155 if ( hash
.match( /^#mw-prefsection-[\w\-]+/ ) ) {
156 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
157 } else if ( hash
=== '' ) {
158 switchPrefTab( 'personal', 'noHash' );
161 // In older browsers we'll bind a click handler as fallback.
162 // We must not have onhashchange *and* the click handlers, other wise
163 // the click handler calls switchPrefTab() which sets the hash value,
164 // which triggers onhashcange and calls switchPrefTab() again.
166 $preftoc
.on( 'click', 'li a', function ( e
) {
167 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
172 // Timezone functions.
173 // Guesses Timezone from browser and updates fields onchange.
175 $tzSelect
= $( '#mw-input-wptimecorrection' );
176 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
177 $localtimeHolder
= $( '#wpLocalTime' );
178 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
180 function minutesToHours( min
) {
181 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
182 tzMin
= Math
.abs( min
) % 60,
183 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
184 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
188 function hoursToMinutes( hour
) {
190 arr
= hour
.split( ':' );
192 arr
[0] = parseInt( arr
[0], 10 );
194 if ( arr
.length
=== 1 ) {
195 // Specification is of the form [-]XX
196 minutes
= arr
[0] * 60;
198 // Specification is of the form [-]XX:XX
199 minutes
= Math
.abs( arr
[0] ) * 60 + parseInt( arr
[1], 10 );
204 // Gracefully handle non-numbers.
205 if ( isNaN( minutes
) ) {
212 function updateTimezoneSelection() {
213 var minuteDiff
, localTime
,
214 type
= $tzSelect
.val();
216 if ( type
=== 'guess' ) {
217 // Get browser timezone & fill it in
218 minuteDiff
= -( new Date().getTimezoneOffset() );
219 $tzTextbox
.val( minutesToHours( minuteDiff
) );
220 $tzSelect
.val( 'other' );
221 $tzTextbox
.prop( 'disabled', false );
222 } else if ( type
=== 'other' ) {
223 // Grab data from the textbox, parse it.
224 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
226 // Grab data from the $tzSelect value
227 minuteDiff
= parseInt( type
.split( '|' )[1], 10 ) || 0;
228 $tzTextbox
.val( minutesToHours( minuteDiff
) );
231 // Determine local time from server time and minutes difference, for display.
232 localTime
= servertime
+ minuteDiff
;
234 // Bring time within the [0,1440) range.
235 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
237 $localtimeHolder
.text( mediaWiki
.language
.convertNumber( minutesToHours( localTime
) ) );
240 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
241 $tzSelect
.change( updateTimezoneSelection
);
242 $tzTextbox
.blur( updateTimezoneSelection
);
243 updateTimezoneSelection();
246 // Preserve the tab after saving the preferences
247 // Not using cookies, because their deletion results are inconsistent.
248 // Not using jStorage due to its enormous size (for this feature)
249 if ( window
.sessionStorage
) {
250 if ( sessionStorage
.getItem( 'mediawikiPreferencesTab' ) !== null ) {
251 switchPrefTab( sessionStorage
.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
253 // Deleting the key, the tab states should be reset until we press Save
254 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
256 $( '#mw-prefs-form' ).submit( function () {
257 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
258 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);
262 // To disable all 'namespace' checkboxes in Search preferences
263 // when 'Search in all namespaces' checkbox is ticked.
264 $checkBoxes
= $( '#mw-htmlform-advancedsearchoptions input[id^=mw-input-wpsearchnamespaces]' );
265 if ( $( '#mw-input-wpsearcheverything' ).prop( 'checked' ) ) {
266 $checkBoxes
.prop( 'disabled', true );
268 $( '#mw-input-wpsearcheverything' ).change( function () {
269 $checkBoxes
.prop( 'disabled', $( this ).prop( 'checked' ) );
272 // Set up a message to notify users if they try to leave the page without
274 $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
275 allowCloseWindow
= mediaWiki
.confirmCloseWindow( {
277 return $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' );
280 message
: mediaWiki
.msg( 'prefswarning-warning', mediaWiki
.msg( 'saveprefs' ) ),
281 namespace: 'prefswarning'
283 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
284 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );