| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
( function () { |
| 5 |
mw.hook( 'htmlform.enhance' ).add( ( $root ) => { |
| 6 |
const $target = $root.find( '#wpTimeCorrection' ); |
| 7 |
|
| 8 |
if ( |
| 9 |
|
| 10 |
!$target.length || |
| 11 |
$target.closest( '.mw-htmlform-autoinfuse-lazy' ).length |
| 12 |
) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
|
| 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 |
|
| 38 |
minutes = arr[ 0 ] * 60; |
| 39 |
} else { |
| 40 |
|
| 41 |
minutes = Math.abs( arr[ 0 ] ) * 60 + parseInt( arr[ 1 ], 10 ); |
| 42 |
if ( arr[ 0 ] < 0 ) { |
| 43 |
minutes *= -1; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 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 |
|
| 60 |
|
| 61 |
minuteDiff = hoursToMinutes( timezoneWidget.textinput.getValue() ); |
| 62 |
} else { |
| 63 |
|
| 64 |
if ( type === 'guess' ) { |
| 65 |
|
| 66 |
minuteDiff = -( new Date().getTimezoneOffset() ); |
| 67 |
} else { |
| 68 |
|
| 69 |
minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
let localTime = servertime + minuteDiff; |
| 75 |
|
| 76 |
|
| 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 |
}() ); |