2 * JavaScript for Special:Preferences: Timezone field enhancements.
5 mw
.hook( 'htmlform.enhance' ).add( function ( $root
) {
6 var $tzSelect
, $tzTextbox
, timezoneWidget
, $localtimeHolder
, servertime
,
7 $target
= $root
.find( '#wpTimeCorrection' ),
8 oouiEnabled
= $( '#mw-prefs-form' ).hasClass( 'mw-htmlform-ooui' );
12 $target
.closest( '.mw-htmlform-autoinfuse-lazy' ).length
17 // Timezone functions.
18 // Guesses Timezone from browser and updates fields onchange.
21 // This is identical to OO.ui.infuse( ... ), but it makes the class name of the result known.
23 timezoneWidget
= mw
.widgets
.SelectWithInputWidget
.static.infuse( $target
);
25 // This preference could theoretically be disabled ($wgHiddenPrefs)
26 timezoneWidget
= null;
30 $tzTextbox
= $( '#wpTimeCorrection-other' );
33 $localtimeHolder
= $( '#wpLocalTime' );
34 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
36 function minutesToHours( min
) {
37 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
38 tzMin
= Math
.abs( min
) % 60,
39 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
40 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
44 function hoursToMinutes( hour
) {
46 arr
= hour
.split( ':' );
48 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
50 if ( arr
.length
=== 1 ) {
51 // Specification is of the form [-]XX
52 minutes
= arr
[ 0 ] * 60;
54 // Specification is of the form [-]XX:XX
55 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
60 // Gracefully handle non-numbers.
61 if ( isNaN( minutes
) ) {
68 function updateTimezoneSelection() {
69 var minuteDiff
, localTime
,
70 type
= oouiEnabled
? timezoneWidget
.dropdowninput
.getValue() : $tzSelect
.val(),
71 val
= oouiEnabled
? timezoneWidget
.textinput
.getValue() : $tzTextbox
.val();
73 if ( type
=== 'other' ) {
74 // User specified time zone manually in <input>
75 // Grab data from the textbox, parse it.
76 minuteDiff
= hoursToMinutes( val
);
78 // Time zone not manually specified by user
79 if ( type
=== 'guess' ) {
80 // Get browser timezone & fill it in
81 minuteDiff
= -( new Date().getTimezoneOffset() );
83 timezoneWidget
.textinput
.setValue( minutesToHours( minuteDiff
) );
84 timezoneWidget
.dropdowninput
.setValue( 'other' );
86 $tzTextbox
.val( minutesToHours( minuteDiff
) );
87 $tzSelect
.val( 'other' );
90 // Grab data from the dropdown value
91 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
95 // Determine local time from server time and minutes difference, for display.
96 localTime
= servertime
+ minuteDiff
;
98 // Bring time within the [0,1440) range.
99 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
101 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
105 if ( timezoneWidget
) {
106 timezoneWidget
.dropdowninput
.on( 'change', updateTimezoneSelection
);
107 timezoneWidget
.textinput
.on( 'change', updateTimezoneSelection
);
108 updateTimezoneSelection();
111 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
112 $tzSelect
.change( updateTimezoneSelection
);
113 $tzTextbox
.blur( updateTimezoneSelection
);
114 updateTimezoneSelection();