| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
function minutesToHours( min ) { |
| 6 |
const tzHour = Math.floor( Math.abs( min ) / 60 ), |
| 7 |
tzMin = Math.abs( min ) % 60, |
| 8 |
tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour + |
| 9 |
':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin; |
| 10 |
return tzString; |
| 11 |
} |
| 12 |
|
| 13 |
mw.hook( 'htmlform.enhance' ).add( ( $root ) => { |
| 14 |
mw.loader.using( 'mediawiki.widgets.SelectWithInputWidget', () => { |
| 15 |
$root.find( '.mw-htmlform-timezone-field' ).each( function () { |
| 16 |
|
| 17 |
const timezoneWidget = mw.widgets.SelectWithInputWidget.static.infuse( $( this ) ); |
| 18 |
|
| 19 |
function maybeGuessTimezone() { |
| 20 |
if ( timezoneWidget.dropdowninput.getValue() !== 'guess' ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
let timeZone; |
| 26 |
try { |
| 27 |
|
| 28 |
timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 29 |
} catch ( err ) { |
| 30 |
timeZone = null; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
const minuteDiff = -( new Date().getTimezoneOffset() ); |
| 35 |
|
| 36 |
let newValue; |
| 37 |
if ( timeZone ) { |
| 38 |
|
| 39 |
newValue = 'ZoneInfo|' + minuteDiff + '|' + timeZone; |
| 40 |
timezoneWidget.dropdowninput.setValue( newValue ); |
| 41 |
} |
| 42 |
if ( !timeZone || timezoneWidget.dropdowninput.getValue() !== newValue ) { |
| 43 |
|
| 44 |
timezoneWidget.dropdowninput.setValue( 'other' ); |
| 45 |
timezoneWidget.textinput.setValue( minutesToHours( minuteDiff ) ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
timezoneWidget.dropdowninput.on( 'change', maybeGuessTimezone ); |
| 50 |
maybeGuessTimezone(); |
| 51 |
} ); |
| 52 |
} ); |
| 53 |
} ); |