Merge "Skin: Make skins aware of their registered skin name"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.preferences.timezone.js
1 /*!
2 * JavaScript for Special:Preferences: Timezone field enhancements.
3 */
4 ( function ( mw, $ ) {
5 $( function () {
6 var
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime;
8
9 // Timezone functions.
10 // Guesses Timezone from browser and updates fields onchange.
11
12 $tzSelect = $( '#mw-input-wptimecorrection' );
13 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
14 $localtimeHolder = $( '#wpLocalTime' );
15 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
16
17 function minutesToHours( min ) {
18 var tzHour = Math.floor( Math.abs( min ) / 60 ),
19 tzMin = Math.abs( min ) % 60,
20 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
21 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
22 return tzString;
23 }
24
25 function hoursToMinutes( hour ) {
26 var minutes,
27 arr = hour.split( ':' );
28
29 arr[ 0 ] = parseInt( arr[ 0 ], 10 );
30
31 if ( arr.length === 1 ) {
32 // Specification is of the form [-]XX
33 minutes = arr[ 0 ] * 60;
34 } else {
35 // Specification is of the form [-]XX:XX
36 minutes = Math.abs( arr[ 0 ] ) * 60 + parseInt( arr[ 1 ], 10 );
37 if ( arr[ 0 ] < 0 ) {
38 minutes *= -1;
39 }
40 }
41 // Gracefully handle non-numbers.
42 if ( isNaN( minutes ) ) {
43 return 0;
44 } else {
45 return minutes;
46 }
47 }
48
49 function updateTimezoneSelection() {
50 var minuteDiff, localTime,
51 type = $tzSelect.val();
52
53 if ( type === 'other' ) {
54 // User specified time zone manually in <input>
55 // Grab data from the textbox, parse it.
56 minuteDiff = hoursToMinutes( $tzTextbox.val() );
57 } else {
58 // Time zone not manually specified by user
59 if ( type === 'guess' ) {
60 // Get browser timezone & fill it in
61 minuteDiff = -( new Date().getTimezoneOffset() );
62 $tzTextbox.val( minutesToHours( minuteDiff ) );
63 $tzSelect.val( 'other' );
64 } else {
65 // Grab data from the $tzSelect value
66 minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0;
67 }
68 }
69
70 // Determine local time from server time and minutes difference, for display.
71 localTime = servertime + minuteDiff;
72
73 // Bring time within the [0,1440) range.
74 localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
75
76 $localtimeHolder.text( mw.language.convertNumber( minutesToHours( localTime ) ) );
77 }
78
79 if ( $tzSelect.length && $tzTextbox.length ) {
80 $tzSelect.change( updateTimezoneSelection );
81 $tzTextbox.blur( updateTimezoneSelection );
82 updateTimezoneSelection();
83 }
84
85 } );
86 }( mediaWiki, jQuery ) );