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