2 * JavaScript for Special:Preferences
6 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
9 .before( $( '<ul id="preftoc"></ul>' ) )
10 .children( 'fieldset' )
12 .addClass( 'prefsection' )
14 .addClass( 'mainLegend' )
15 .each( function( i, legend ) {
16 $(legend).parent().attr( 'id', 'prefsection-' + i );
18 $(legend).parent().show();
20 $( '#preftoc' ).append(
22 .addClass( i === 0 ? 'selected' : null )
25 .text( $(legend).text() )
26 .attr( 'id', 'preftab-' + i + '-tab' )
27 .attr( 'href', '#preftab-' + i ) // Use #preftab-N instead of #prefsection-N to avoid jumping on click
29 $(this).parent().parent().find( 'li' ).removeClass( 'selected' );
30 $(this).parent().addClass( 'selected' );
31 $( '#preferences > fieldset' ).hide();
32 $( '#prefsection-' + i ).show();
39 // If we've reloaded the page or followed an open-in-new-window,
40 // make the selected tab visible.
43 var hash = window.location.hash;
44 if( hash.match( /^#preftab-[\d]+$/ ) ) {
45 var $tab = $( hash + '-tab' );
51 * Given an email validity status (true, false, null) update the label CSS class
53 var updateMailValidityLabel = function( mail ) {
54 var isValid = mw.util.validateEmail( mail ),
55 $label = $( '#mw-emailaddress-validity' );
57 // We allow empty address
58 if( isValid === null ) {
59 $label.text( '' ).removeClass( 'valid invalid' );
62 } else if ( isValid ) {
63 $label.text( mw.msg( 'email-address-validity-valid' ) ).addClass( 'valid' ).removeClass( 'invalid' );
67 $label.text( mw.msg( 'email-address-validity-invalid' ) ).addClass( 'invalid' ).removeClass( 'valid' );
71 // Lame tip to let user know if its email is valid. See bug 22449
72 // Only bind once for 'blur' so that the user can fill it in without errors
73 // After that look at every keypress for direct feedback if it was invalid onblur
74 $( '#mw-input-wpemailaddress' ).one( 'blur', function() {
75 if ( $( '#mw-emailaddress-validity' ).length === 0 ) {
76 $(this).after( '<label for="mw-input-wpemailaddress" id="mw-emailaddress-validity"></label>' );
78 updateMailValidityLabel( $(this).val() );
79 $(this).keyup( function() {
80 updateMailValidityLabel( $(this).val() );
88 * Guesses Timezone from browser and updates fields onchange
91 var $tzSelect = $( '#mw-input-wptimecorrection' );
92 var $tzTextbox = $( '#mw-input-wptimecorrection-other' );
94 var $localtimeHolder = $( '#wpLocalTime' );
95 var servertime = parseInt( $( 'input[name=wpServerTime]' ).val(), 10 );
98 var minutesToHours = function( min ) {
99 var tzHour = Math.floor( Math.abs( min ) / 60 );
100 var tzMin = Math.abs( min ) % 60;
101 var tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
102 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
106 var hoursToMinutes = function( hour ) {
107 var arr = hour.split( ':' );
108 arr[0] = parseInt( arr[0], 10 );
111 if ( arr.length == 1 ) {
112 // Specification is of the form [-]XX
113 minutes = arr[0] * 60;
115 // Specification is of the form [-]XX:XX
116 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
121 // Gracefully handle non-numbers.
122 if ( isNaN( minutes ) ) {
129 var updateTimezoneSelection = function() {
130 var type = $tzSelect.val();
131 if ( type == 'guess' ) {
132 // Get browser timezone & fill it in
133 minuteDiff = -new Date().getTimezoneOffset();
134 $tzTextbox.val( minutesToHours( minuteDiff ) );
135 $tzSelect.val( 'other' );
136 $tzTextbox.get( 0 ).disabled = false;
137 } else if ( type == 'other' ) {
138 // Grab data from the textbox, parse it.
139 minuteDiff = hoursToMinutes( $tzTextbox.val() );
141 // Grab data from the $tzSelect value
142 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
143 $tzTextbox.val( minutesToHours( minuteDiff ) );
146 // Determine local time from server time and minutes difference, for display.
147 var localTime = servertime + minuteDiff;
149 // Bring time within the [0,1440) range.
150 while ( localTime < 0 ) {
153 while ( localTime >= 1440 ) {
156 $localtimeHolder.text( minutesToHours( localTime ) );
159 if ( $tzSelect.length && $tzTextbox.length ) {
160 $tzSelect.change( function() { updateTimezoneSelection(); } );
161 $tzTextbox.blur( function() { updateTimezoneSelection(); } );
162 updateTimezoneSelection();
164 } )( jQuery, mediaWiki );