Debian Codegraph @

resources/src/mediawiki.special.preferences.ooui/timezone.js

raw symbols SCIP
1 /*!
2 * JavaScript for Special:Preferences: Timezone field enhancements.
3 */
4 ( function () {
5 mw.hook( 'htmlform.enhance' ).add( ( $root ) => {
6 const $target = $root.find( '#wpTimeCorrection' );
7
8 if (
9 // This preference could theoretically be disabled ($wgHiddenPrefs)
10 !$target.length ||
11 $target.closest( '.mw-htmlform-autoinfuse-lazy' ).length
12 ) {
13 return;
14 }
15
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 );
18
19 const $localtimeHolder = $( '#wpLocalTime' );
20 const servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
21
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;
27 return tzString;
28 }
29
30 function hoursToMinutes( hour ) {
31 const arr = hour.split( ':' );
32
33 arr[ 0 ] = parseInt( arr[ 0 ], 10 );
34
35 let minutes;
36 if ( arr.length === 1 ) {
37 // Specification is of the form [-]XX
38 minutes = arr[ 0 ] * 60;
39 } else {
40 // Specification is of the form [-]XX:XX
41 minutes = Math.abs( arr[ 0 ] ) * 60 + parseInt( arr[ 1 ], 10 );
42 if ( arr[ 0 ] < 0 ) {
43 minutes *= -1;
44 }
45 }
46 // Gracefully handle non-numbers.
47 if ( isNaN( minutes ) ) {
48 return 0;
49 } else {
50 return minutes;
51 }
52 }
53
54 function updateTimezoneSelection() {
55 const type = timezoneWidget.dropdowninput.getValue();
56
57 let minuteDiff;
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() );
62 } else {
63 // Time zone not manually specified by user
64 if ( type === 'guess' ) {
65 // Get the time offset
66 minuteDiff = -( new Date().getTimezoneOffset() );
67 } else {
68 // Grab data from the dropdown value
69 minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0;
70 }
71 }
72
73 // Determine local time from server time and minutes difference, for display.
74 let localTime = servertime + minuteDiff;
75
76 // Bring time within the [0,1440) range.
77 localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
78
79 $localtimeHolder.text( mw.language.convertNumber( minutesToHours( localTime ) ) );
80 }
81
82 timezoneWidget.dropdowninput.on( 'change', updateTimezoneSelection );
83 timezoneWidget.textinput.on( 'change', updateTimezoneSelection );
84 updateTimezoneSelection();
85
86 } );
87 }() );