2 * HTMLForm enhancements:
3 * Enable the "Fill in from browser" option for the timezone selector
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;
13 mw.hook( 'htmlform.enhance' ).add( ( $root ) => {
14 mw.loader.using( 'mediawiki.widgets.SelectWithInputWidget', () => {
15 $root.find( '.mw-htmlform-timezone-field' ).each( function () {
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( $( this ) );
19 function maybeGuessTimezone() {
20 if ( timezoneWidget.dropdowninput.getValue() !== 'guess' ) {
23 // If available, get the named time zone from the browser.
24 // (We also support older browsers where this API is not available.)
27 // This may return undefined
28 timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
33 // Get the time offset
34 const minuteDiff = -( new Date().getTimezoneOffset() );
38 // Try to save both time zone and offset
39 newValue = 'ZoneInfo|' + minuteDiff + '|' + timeZone;
40 timezoneWidget.dropdowninput.setValue( newValue );
42 if ( !timeZone || timezoneWidget.dropdowninput.getValue() !== newValue ) {
43 // No time zone, or it's unknown to MediaWiki. Save only offset
44 timezoneWidget.dropdowninput.setValue( 'other' );
45 timezoneWidget.textinput.setValue( minutesToHours( minuteDiff ) );
49 timezoneWidget.dropdowninput.on( 'change', maybeGuessTimezone );