2 * JavaScript for Special:Preferences
4 jQuery( document ).ready( function( $ ) {
5 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
6 var $preftoc = $('<ul id="preftoc"></ul>');
7 var $preferences = $( '#preferences' )
11 var $fieldsets = $preferences.children( 'fieldset' )
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);
22 $legend.parent().show();
24 var ident = $legend.parent().attr( 'id' );
26 var $li = $( '<li/>', {
27 'class' : ( i === 0 ) ? 'selected' : null
30 text : $legend.text(),
31 id : ident.replace( 'mw-prefsection', 'preftab' ),
33 }).click( function( e ) {
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();
47 $preftoc.append( $li );
50 // If we've reloaded the page or followed an open-in-new-window,
51 // make the selected tab visible.
52 var hash = window.location.hash;
53 if( hash.match( /^#mw-prefsection-[\w-]+/ ) ) {
54 var $tab = $( hash.replace( 'mw-prefsection', 'preftab' ) );
61 * Guesses Timezone from browser and updates fields onchange
64 var $tzSelect = $( '#mw-input-wptimecorrection' );
65 var $tzTextbox = $( '#mw-input-wptimecorrection-other' );
67 var $localtimeHolder = $( '#wpLocalTime' );
68 var servertime = parseInt( $( 'input[name=wpServerTime]' ).val(), 10 );
71 var minutesToHours = function( min ) {
72 var tzHour = Math.floor( Math.abs( min ) / 60 );
73 var tzMin = Math.abs( min ) % 60;
74 var tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
75 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
79 var hoursToMinutes = function( hour ) {
80 var arr = hour.split( ':' );
81 arr[0] = parseInt( arr[0], 10 );
84 if ( arr.length == 1 ) {
85 // Specification is of the form [-]XX
86 minutes = arr[0] * 60;
88 // Specification is of the form [-]XX:XX
89 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
94 // Gracefully handle non-numbers.
95 if ( isNaN( minutes ) ) {
102 var updateTimezoneSelection = function() {
103 var type = $tzSelect.val();
104 if ( type == 'guess' ) {
105 // Get browser timezone & fill it in
106 minuteDiff = -new Date().getTimezoneOffset();
107 $tzTextbox.val( minutesToHours( minuteDiff ) );
108 $tzSelect.val( 'other' );
109 $tzTextbox.get( 0 ).disabled = false;
110 } else if ( type == 'other' ) {
111 // Grab data from the textbox, parse it.
112 minuteDiff = hoursToMinutes( $tzTextbox.val() );
114 // Grab data from the $tzSelect value
115 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
116 $tzTextbox.val( minutesToHours( minuteDiff ) );
119 // Determine local time from server time and minutes difference, for display.
120 var localTime = servertime + minuteDiff;
122 // Bring time within the [0,1440) range.
123 while ( localTime < 0 ) {
126 while ( localTime >= 1440 ) {
129 $localtimeHolder.text( minutesToHours( localTime ) );
132 if ( $tzSelect.length && $tzTextbox.length ) {
133 $tzSelect.change( function() { updateTimezoneSelection(); } );
134 $tzTextbox.blur( function() { updateTimezoneSelection(); } );
135 updateTimezoneSelection();