2 * JavaScript for Special:Preferences
5 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
6 var $preftoc
= $('<ul id="preftoc"></ul>');
7 var $preferences
= $( '#preferences' )
11 var $fieldsets
= $preferences
.children( 'fieldset' )
13 .addClass( 'prefsection' );
15 var $legends
= $fieldsets
.children( 'legend' )
16 .addClass( 'mainLegend' );
18 // Populate the prefToc
19 $legends
.each( function( i
, legend
) {
20 var $legend
= $(legend
);
22 $legend
.parent().show();
24 var ident
= $legend
.parent().attr( 'id' );
26 var $li
= $( '<li/>', {
27 'class' : ( i
=== 0 ) ? 'selected' : null
30 text
: $legend
.text(),
31 id
: ident
.replace( 'mw-prefsection', 'preftab' ),
33 }).click( function( e
) {
35 // Handle hash manually to prevent jumping
36 // Therefore save and restore scrollTop to prevent jumping
37 var scrollTop
= $(window
).scrollTop();
38 window
.location
.hash
= $(this).attr('href');
39 $(window
).scrollTop(scrollTop
);
41 $preftoc
.find( 'li' ).removeClass( 'selected' );
42 $(this).parent().addClass( 'selected' );
43 $( '#preferences > fieldset' ).hide();
44 $( '#' + ident
).show();
47 $preftoc
.append( $li
);
50 // If we've reloaded the page or followed an open-in-new-window,
51 // make the selected tab visible.
54 var hash
= window
.location
.hash
;
55 if( hash
.match( /^#mw-prefsection-[\w-]+/ ) ) {
56 var $tab
= $( hash
.replace( 'mw-prefsection', 'preftab' ) );
62 * Given an email validity status (true, false, null) update the label CSS class
64 var updateMailValidityLabel = function( mail
) {
65 var isValid
= mw
.util
.validateEmail( mail
),
66 $label
= $( '#mw-emailaddress-validity' );
68 // We allow empty address
69 if( isValid
=== null ) {
70 $label
.text( '' ).removeClass( 'valid invalid' );
73 } else if ( isValid
) {
74 $label
.text( mw
.msg( 'email-address-validity-valid' ) ).addClass( 'valid' ).removeClass( 'invalid' );
78 $label
.text( mw
.msg( 'email-address-validity-invalid' ) ).addClass( 'invalid' ).removeClass( 'valid' );
82 // Lame tip to let user know if its email is valid. See bug 22449
83 // Only bind once for 'blur' so that the user can fill it in without errors
84 // After that look at every keypress for direct feedback if it was invalid onblur
85 $( '#mw-input-wpemailaddress' ).one( 'blur', function() {
86 if ( $( '#mw-emailaddress-validity' ).length
=== 0 ) {
87 $(this).after( '<label for="mw-input-wpemailaddress" id="mw-emailaddress-validity"></label>' );
89 updateMailValidityLabel( $(this).val() );
90 $(this).keyup( function() {
91 updateMailValidityLabel( $(this).val() );
99 * Guesses Timezone from browser and updates fields onchange
102 var $tzSelect
= $( '#mw-input-wptimecorrection' );
103 var $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
105 var $localtimeHolder
= $( '#wpLocalTime' );
106 var servertime
= parseInt( $( 'input[name=wpServerTime]' ).val(), 10 );
109 var minutesToHours = function( min
) {
110 var tzHour
= Math
.floor( Math
.abs( min
) / 60 );
111 var tzMin
= Math
.abs( min
) % 60;
112 var tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
113 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
117 var hoursToMinutes = function( hour
) {
118 var arr
= hour
.split( ':' );
119 arr
[0] = parseInt( arr
[0], 10 );
122 if ( arr
.length
== 1 ) {
123 // Specification is of the form [-]XX
124 minutes
= arr
[0] * 60;
126 // Specification is of the form [-]XX:XX
127 minutes
= Math
.abs( arr
[0] ) * 60 + parseInt( arr
[1], 10 );
132 // Gracefully handle non-numbers.
133 if ( isNaN( minutes
) ) {
140 var updateTimezoneSelection = function() {
141 var type
= $tzSelect
.val();
142 if ( type
== 'guess' ) {
143 // Get browser timezone & fill it in
144 minuteDiff
= -new Date().getTimezoneOffset();
145 $tzTextbox
.val( minutesToHours( minuteDiff
) );
146 $tzSelect
.val( 'other' );
147 $tzTextbox
.get( 0 ).disabled
= false;
148 } else if ( type
== 'other' ) {
149 // Grab data from the textbox, parse it.
150 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
152 // Grab data from the $tzSelect value
153 minuteDiff
= parseInt( type
.split( '|' )[1], 10 ) || 0;
154 $tzTextbox
.val( minutesToHours( minuteDiff
) );
157 // Determine local time from server time and minutes difference, for display.
158 var localTime
= servertime
+ minuteDiff
;
160 // Bring time within the [0,1440) range.
161 while ( localTime
< 0 ) {
164 while ( localTime
>= 1440 ) {
167 $localtimeHolder
.text( minutesToHours( localTime
) );
170 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
171 $tzSelect
.change( function() { updateTimezoneSelection(); } );
172 $tzTextbox
.blur( function() { updateTimezoneSelection(); } );
173 updateTimezoneSelection();
175 } )( jQuery
, mediaWiki
);