fix for bug 34466 - making sure mediawiki module isnt executed before the DOM is...
[mediawiki.git] / resources / mediawiki.special / mediawiki.special.preferences.js
blobd82714422c962cefdafd74fb24a468c43bdc1677
1 /*
2  * JavaScript for Special:Preferences
3  */
4 jQuery( document ).ready( function( $ ) {
5 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
6 var $preftoc = $('<ul id="preftoc"></ul>');
7 var $preferences = $( '#preferences' )
8         .addClass( 'jsprefs' )
9         .before( $preftoc );
11 var $fieldsets = $preferences.children( 'fieldset' )
12         .hide()
13         .addClass( 'prefsection' );
15 var $legends = $fieldsets.children( 'legend' )
16         .addClass( 'mainLegend' );
18 // Populate the prefToc
19 $legends.each( function( i, legend ) {
20         var $legend = $(legend);
21         if ( i === 0 ) {
22                 $legend.parent().show();
23         }
24         var ident = $legend.parent().attr( 'id' );
26         var $li = $( '<li/>', {
27                 'class' : ( i === 0 ) ? 'selected' : null
28         });
29         var $a = $( '<a/>', {
30                 text : $legend.text(),
31                 id : ident.replace( 'mw-prefsection', 'preftab' ),
32                 href : '#' + ident
33         }).click( function( e ) {
34                 e.preventDefault();
35                 // Handle hash manually to prevent jumping
36                 // Therefore save and restore scrollTop to prevent jumping
37                 var scrollTop = $(window).scrollTop();
38                 window.location.hash = $(this).attr('href');
39                 $(window).scrollTop(scrollTop);
41                 $preftoc.find( 'li' ).removeClass( 'selected' );
42                 $(this).parent().addClass( 'selected' );
43                 $( '#preferences > fieldset' ).hide();
44                 $( '#' + ident ).show();
45         });
46         $li.append( $a );
47         $preftoc.append( $li );
48 } );
50 // If we've reloaded the page or followed an open-in-new-window,
51 // make the selected tab visible.
52 // On document ready:
53 $( function() {
54         var hash = window.location.hash;
55         if( hash.match( /^#mw-prefsection-[\w-]+/ ) ) {
56                 var $tab = $( hash.replace( 'mw-prefsection', 'preftab' ) );
57                 $tab.click();
58         }
59 } );
62 /**
63 * Timezone functions.
64 * Guesses Timezone from browser and updates fields onchange
67 var $tzSelect = $( '#mw-input-wptimecorrection' );
68 var $tzTextbox = $( '#mw-input-wptimecorrection-other' );
70 var $localtimeHolder = $( '#wpLocalTime' );
71 var servertime = parseInt( $( 'input[name=wpServerTime]' ).val(), 10 );
72 var minuteDiff = 0;
74 var minutesToHours = function( min ) {
75         var tzHour = Math.floor( Math.abs( min ) / 60 );
76         var tzMin = Math.abs( min ) % 60;
77         var tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
78                 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
79         return tzString;
82 var hoursToMinutes = function( hour ) {
83         var arr = hour.split( ':' );
84         arr[0] = parseInt( arr[0], 10 );
86         var minutes;
87         if ( arr.length == 1 ) {
88                 // Specification is of the form [-]XX
89                 minutes = arr[0] * 60;
90         } else {
91                 // Specification is of the form [-]XX:XX
92                 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
93                 if ( arr[0] < 0 ) {
94                         minutes *= -1;
95                 }
96         }
97         // Gracefully handle non-numbers.
98         if ( isNaN( minutes ) ) {
99                 return 0;
100         } else {
101                 return minutes;
102         }
105 var updateTimezoneSelection = function() {
106         var type = $tzSelect.val();
107         if ( type == 'guess' ) {
108                 // Get browser timezone & fill it in
109                 minuteDiff = -new Date().getTimezoneOffset();
110                 $tzTextbox.val( minutesToHours( minuteDiff ) );
111                 $tzSelect.val( 'other' );
112                 $tzTextbox.get( 0 ).disabled = false;
113         } else if ( type == 'other' ) {
114                 // Grab data from the textbox, parse it.
115                 minuteDiff = hoursToMinutes( $tzTextbox.val() );
116         } else {
117                 // Grab data from the $tzSelect value
118                 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
119                 $tzTextbox.val( minutesToHours( minuteDiff ) );
120         }
122         // Determine local time from server time and minutes difference, for display.
123         var localTime = servertime + minuteDiff;
125         // Bring time within the [0,1440) range.
126         while ( localTime < 0 ) {
127                 localTime += 1440;
128         }
129         while ( localTime >= 1440 ) {
130                 localTime -= 1440;
131         }
132         $localtimeHolder.text( minutesToHours( localTime ) );
135 if ( $tzSelect.length && $tzTextbox.length ) {
136         $tzSelect.change( function() { updateTimezoneSelection(); } );
137         $tzTextbox.blur( function() { updateTimezoneSelection(); } );
138         updateTimezoneSelection();
140 } );