Follow-up r90369: Add third parameter to filetype-unwanted-type (for PLURAL)
[mediawiki.git] / resources / mediawiki.special / mediawiki.special.preferences.js
blob1b2cefe05bc3652cbae27c67989fecc47243c02e
1 /*
2  * JavaScript for Special:Preferences
3  */
4 ( function( $, mw ) {
6 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
7 $( '#preferences' )
8         .addClass( 'jsprefs' )
9         .before( $( '<ul id="preftoc"></ul>' ) )
10         .children( 'fieldset' )
11                 .hide()
12                 .addClass( 'prefsection' )
13                 .children( 'legend' )
14                         .addClass( 'mainLegend' )
15                         .each( function( i, legend ) {
16                                         $(legend).parent().attr( 'id', 'prefsection-' + i );
17                                         if ( i === 0 ) {
18                                                 $(legend).parent().show();
19                                         }
20                                         $( '#preftoc' ).append(
21                                                 $( '<li></li>' )
22                                                         .addClass( i === 0 ? 'selected' : null )
23                                                         .append(
24                                                                 $( '<a></a>')
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
28                                                                         .click( function() {
29                                                                                 $(this).parent().parent().find( 'li' ).removeClass( 'selected' );
30                                                                                 $(this).parent().addClass( 'selected' );
31                                                                                 $( '#preferences > fieldset' ).hide();
32                                                                                 $( '#prefsection-' + i ).show();
33                                                                         } )
34                                                         )
35                                         );
36                                 }
37                         );
39 // If we've reloaded the page or followed an open-in-new-window,
40 // make the selected tab visible.
41 // On document ready:
42 $( function() {
43         var hash = window.location.hash;
44         if( hash.match( /^#preftab-[\d]+$/ ) ) {
45                 var $tab = $( hash + '-tab' );
46                 $tab.click();
47         }
48 } );
50 /**
51  * Given an email validity status (true, false, null) update the label CSS class
52  */
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' );
61         // Valid
62         } else if ( isValid ) {
63                 $label.text( mw.msg( 'email-address-validity-valid' ) ).addClass( 'valid' ).removeClass( 'invalid' );
65         // Not valid
66         } else {
67                 $label.text( mw.msg( 'email-address-validity-invalid' ) ).addClass( 'invalid' ).removeClass( 'valid' );
68         }
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>' );
77         }
78         updateMailValidityLabel( $(this).val() );
79         $(this).keyup( function() {
80                 updateMailValidityLabel( $(this).val() );
81         } );
82 } );
86 /**
87 * Timezone functions.
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 );
96 var minuteDiff = 0;
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;
103         return tzString;
106 var hoursToMinutes = function( hour ) {
107         var arr = hour.split( ':' );
108         arr[0] = parseInt( arr[0], 10 );
110         var minutes;
111         if ( arr.length == 1 ) {
112                 // Specification is of the form [-]XX
113                 minutes = arr[0] * 60;
114         } else {
115                 // Specification is of the form [-]XX:XX
116                 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
117                 if ( arr[0] < 0 ) {
118                         minutes *= -1;
119                 }
120         }
121         // Gracefully handle non-numbers.
122         if ( isNaN( minutes ) ) {
123                 return 0;
124         } else {
125                 return minutes;
126         }
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() );
140         } else {
141                 // Grab data from the $tzSelect value
142                 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
143                 $tzTextbox.val( minutesToHours( minuteDiff ) );
144         }
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 ) {
151                 localTime += 1440;
152         }
153         while ( localTime >= 1440 ) {
154                 localTime -= 1440;
155         }
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 );