2 * JavaScript for Special:Preferences: Timezone field enhancements.
5 mw
.hook( 'htmlform.enhance' ).add( ( $root
) => {
6 const $target
= $root
.find( '#wpTimeCorrection' );
9 // This preference could theoretically be disabled ($wgHiddenPrefs)
11 $target
.closest( '.mw-htmlform-autoinfuse-lazy' ).length
16 // This is identical to OO.ui.infuse( ... ), but it makes the class name of the result known.
17 const timezoneWidget
= mw
.widgets
.SelectWithInputWidget
.static.infuse( $target
);
19 const $localtimeHolder
= $( '#wpLocalTime' );
20 const servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
22 function minutesToHours( min
) {
23 const tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
24 tzMin
= Math
.abs( min
) % 60,
25 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
26 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
30 function hoursToMinutes( hour
) {
31 const arr
= hour
.split( ':' );
33 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
36 if ( arr
.length
=== 1 ) {
37 // Specification is of the form [-]XX
38 minutes
= arr
[ 0 ] * 60;
40 // Specification is of the form [-]XX:XX
41 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
46 // Gracefully handle non-numbers.
47 if ( isNaN( minutes
) ) {
54 function updateTimezoneSelection() {
55 const type
= timezoneWidget
.dropdowninput
.getValue();
58 if ( type
=== 'other' ) {
59 // User specified time zone manually in <input>
60 // Grab data from the textbox, parse it.
61 minuteDiff
= hoursToMinutes( timezoneWidget
.textinput
.getValue() );
63 // Time zone not manually specified by user
64 if ( type
=== 'guess' ) {
65 // Get the time offset
66 minuteDiff
= -( new Date().getTimezoneOffset() );
68 // Grab data from the dropdown value
69 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
73 // Determine local time from server time and minutes difference, for display.
74 let localTime
= servertime
+ minuteDiff
;
76 // Bring time within the [0,1440) range.
77 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
79 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
82 timezoneWidget
.dropdowninput
.on( 'change', updateTimezoneSelection
);
83 timezoneWidget
.textinput
.on( 'change', updateTimezoneSelection
);
84 updateTimezoneSelection();