Merge "Use <div> wrappers instead of <p> in ProtectionForm"
[lhc/web/wiklou.git] / resources / src / mediawiki.special.preferences.ooui / timezone.js
1 /*!
2 * JavaScript for Special:Preferences: Timezone field enhancements.
3 */
4 ( function () {
5 mw.hook( 'htmlform.enhance' ).add( function ( $root ) {
6 var timezoneWidget, $localtimeHolder, servertime,
7 $target = $root.find( '#wpTimeCorrection' );
8
9 if (
10 !$target.length ||
11 $target.closest( '.mw-htmlform-autoinfuse-lazy' ).length
12 ) {
13 return;
14 }
15
16 // Timezone functions.
17 // Guesses Timezone from browser and updates fields onchange.
18
19 // This is identical to OO.ui.infuse( ... ), but it makes the class name of the result known.
20 try {
21 timezoneWidget = mw.widgets.SelectWithInputWidget.static.infuse( $target );
22 } catch ( err ) {
23 // This preference could theoretically be disabled ($wgHiddenPrefs)
24 timezoneWidget = null;
25 }
26
27 $localtimeHolder = $( '#wpLocalTime' );
28 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
29
30 function minutesToHours( min ) {
31 var tzHour = Math.floor( Math.abs( min ) / 60 ),
32 tzMin = Math.abs( min ) % 60,
33 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
34 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
35 return tzString;
36 }
37
38 function hoursToMinutes( hour ) {
39 var minutes,
40 arr = hour.split( ':' );
41
42 arr[ 0 ] = parseInt( arr[ 0 ], 10 );
43
44 if ( arr.length === 1 ) {
45 // Specification is of the form [-]XX
46 minutes = arr[ 0 ] * 60;
47 } else {
48 // Specification is of the form [-]XX:XX
49 minutes = Math.abs( arr[ 0 ] ) * 60 + parseInt( arr[ 1 ], 10 );
50 if ( arr[ 0 ] < 0 ) {
51 minutes *= -1;
52 }
53 }
54 // Gracefully handle non-numbers.
55 if ( isNaN( minutes ) ) {
56 return 0;
57 } else {
58 return minutes;
59 }
60 }
61
62 function updateTimezoneSelection() {
63 var minuteDiff, localTime,
64 type = timezoneWidget.dropdowninput.getValue();
65
66 if ( type === 'other' ) {
67 // User specified time zone manually in <input>
68 // Grab data from the textbox, parse it.
69 minuteDiff = hoursToMinutes( timezoneWidget.textinput.getValue() );
70 } else {
71 // Time zone not manually specified by user
72 if ( type === 'guess' ) {
73 // Get browser timezone & fill it in
74 minuteDiff = -( new Date().getTimezoneOffset() );
75 timezoneWidget.textinput.setValue( minutesToHours( minuteDiff ) );
76 timezoneWidget.dropdowninput.setValue( 'other' );
77 } else {
78 // Grab data from the dropdown value
79 minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0;
80 }
81 }
82
83 // Determine local time from server time and minutes difference, for display.
84 localTime = servertime + minuteDiff;
85
86 // Bring time within the [0,1440) range.
87 localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
88
89 $localtimeHolder.text( mw.language.convertNumber( minutesToHours( localTime ) ) );
90 }
91
92 if ( timezoneWidget ) {
93 timezoneWidget.dropdowninput.on( 'change', updateTimezoneSelection );
94 timezoneWidget.textinput.on( 'change', updateTimezoneSelection );
95 updateTimezoneSelection();
96 }
97
98 } );
99 }() );