]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - resources/src/mediawiki.special/mediawiki.special.preferences.timezone.js
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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                                         $tzTextbox.prop( 'disabled', false );
65                                 } else {
66                                         // Grab data from the $tzSelect value
67                                         minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0;
68                                         $tzTextbox.val( minutesToHours( minuteDiff ) );
69                                 }
70
71                                 // Set defaultValue prop on the generated box so we don't trigger the
72                                 // unsaved preferences check
73                                 $tzTextbox.prop( 'defaultValue', $tzTextbox.val() );
74                         }
75
76                         // Determine local time from server time and minutes difference, for display.
77                         localTime = servertime + minuteDiff;
78
79                         // Bring time within the [0,1440) range.
80                         localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
81
82                         $localtimeHolder.text( mw.language.convertNumber( minutesToHours( localTime ) ) );
83                 }
84
85                 if ( $tzSelect.length && $tzTextbox.length ) {
86                         $tzSelect.change( updateTimezoneSelection );
87                         $tzTextbox.blur( updateTimezoneSelection );
88                         updateTimezoneSelection();
89                 }
90
91         } );
92 }( mediaWiki, jQuery ) );