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