2 * JavaScript for Special:Preferences: Timezone field enhancements.
6 var $tzSelect
, $tzTextbox
, timezoneWidget
, $localtimeHolder
, servertime
,
7 oouiEnabled
= $( '#mw-prefs-form' ).hasClass( 'mw-htmlform-ooui' );
10 // Guesses Timezone from browser and updates fields onchange.
13 // This is identical to OO.ui.infuse( ... ), but it makes the class name of the result known.
15 timezoneWidget
= mw
.widgets
.SelectWithInputWidget
.static.infuse( $( '#wpTimeCorrection' ) );
17 // This preference could theoretically be disabled ($wgHiddenPrefs)
18 timezoneWidget
= null;
21 $tzSelect
= $( '#mw-input-wptimecorrection' );
22 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
25 $localtimeHolder
= $( '#wpLocalTime' );
26 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
28 function minutesToHours( min
) {
29 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
30 tzMin
= Math
.abs( min
) % 60,
31 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
32 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
36 function hoursToMinutes( hour
) {
38 arr
= hour
.split( ':' );
40 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
42 if ( arr
.length
=== 1 ) {
43 // Specification is of the form [-]XX
44 minutes
= arr
[ 0 ] * 60;
46 // Specification is of the form [-]XX:XX
47 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
52 // Gracefully handle non-numbers.
53 if ( isNaN( minutes
) ) {
60 function updateTimezoneSelection() {
61 var minuteDiff
, localTime
,
62 type
= oouiEnabled
? timezoneWidget
.dropdowninput
.getValue() : $tzSelect
.val(),
63 val
= oouiEnabled
? timezoneWidget
.textinput
.getValue() : $tzTextbox
.val();
65 if ( type
=== 'other' ) {
66 // User specified time zone manually in <input>
67 // Grab data from the textbox, parse it.
68 minuteDiff
= hoursToMinutes( val
);
70 // Time zone not manually specified by user
71 if ( type
=== 'guess' ) {
72 // Get browser timezone & fill it in
73 minuteDiff
= -( new Date().getTimezoneOffset() );
75 timezoneWidget
.textinput
.setValue( minutesToHours( minuteDiff
) );
76 timezoneWidget
.dropdowninput
.setValue( 'other' );
78 $tzTextbox
.val( minutesToHours( minuteDiff
) );
79 $tzSelect
.val( 'other' );
82 // Grab data from the dropdown value
83 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
87 // Determine local time from server time and minutes difference, for display.
88 localTime
= servertime
+ minuteDiff
;
90 // Bring time within the [0,1440) range.
91 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
93 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
97 if ( timezoneWidget
) {
98 timezoneWidget
.dropdowninput
.on( 'change', updateTimezoneSelection
);
99 timezoneWidget
.textinput
.on( 'change', updateTimezoneSelection
);
100 updateTimezoneSelection();
103 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
104 $tzSelect
.change( updateTimezoneSelection
);
105 $tzTextbox
.blur( updateTimezoneSelection
);
106 updateTimezoneSelection();
111 }( mediaWiki
, jQuery
) );