2 * JavaScript for Special:Preferences: Timezone field enhancements.
7 timezoneWidget
, $localtimeHolder
, servertime
;
10 // Guesses Timezone from browser and updates fields onchange.
12 // This is identical to OO.ui.infuse( ... ), but it makes the class name of the result known.
14 timezoneWidget
= mw
.widgets
.SelectWithInputWidget
.static.infuse( $( '#wpTimeCorrection' ) );
16 // This preference could theoretically be disabled ($wgHiddenPrefs)
17 timezoneWidget
= null;
20 $localtimeHolder
= $( '#wpLocalTime' );
21 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
23 function minutesToHours( min
) {
24 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
25 tzMin
= Math
.abs( min
) % 60,
26 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
27 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
31 function hoursToMinutes( hour
) {
33 arr
= hour
.split( ':' );
35 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
37 if ( arr
.length
=== 1 ) {
38 // Specification is of the form [-]XX
39 minutes
= arr
[ 0 ] * 60;
41 // Specification is of the form [-]XX:XX
42 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
47 // Gracefully handle non-numbers.
48 if ( isNaN( minutes
) ) {
55 function updateTimezoneSelection() {
56 var minuteDiff
, localTime
,
57 type
= timezoneWidget
.dropdowninput
.getValue();
59 if ( type
=== 'other' ) {
60 // User specified time zone manually in <input>
61 // Grab data from the textbox, parse it.
62 minuteDiff
= hoursToMinutes( timezoneWidget
.textinput
.getValue() );
64 // Time zone not manually specified by user
65 if ( type
=== 'guess' ) {
66 // Get browser timezone & fill it in
67 minuteDiff
= -( new Date().getTimezoneOffset() );
68 timezoneWidget
.textinput
.setValue( minutesToHours( minuteDiff
) );
69 timezoneWidget
.dropdowninput
.setValue( 'other' );
71 // Grab data from the dropdown value
72 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
76 // Determine local time from server time and minutes difference, for display.
77 localTime
= servertime
+ minuteDiff
;
79 // Bring time within the [0,1440) range.
80 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
82 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
85 if ( timezoneWidget
) {
86 timezoneWidget
.dropdowninput
.on( 'change', updateTimezoneSelection
);
87 timezoneWidget
.textinput
.on( 'change', updateTimezoneSelection
);
88 updateTimezoneSelection();
92 }( mediaWiki
, jQuery
) );