Merge "Generalizing LinksUpdate to allow extensions to add arbitrary update handlers."
[mediawiki.git] / resources / mediawiki.special / mediawiki.special.preferences.js
blobe9df83c760987dbd93d142b3243235056aea84e7
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 var hash = window.location.hash;
53 if( hash.match( /^#mw-prefsection-[\w-]+/ ) ) {
54         var $tab = $( hash.replace( 'mw-prefsection', 'preftab' ) );
55         $tab.click();
59 /**
60 * Timezone functions.
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 );
69 var minuteDiff = 0;
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;
76         return tzString;
79 var hoursToMinutes = function( hour ) {
80         var arr = hour.split( ':' );
81         arr[0] = parseInt( arr[0], 10 );
83         var minutes;
84         if ( arr.length == 1 ) {
85                 // Specification is of the form [-]XX
86                 minutes = arr[0] * 60;
87         } else {
88                 // Specification is of the form [-]XX:XX
89                 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
90                 if ( arr[0] < 0 ) {
91                         minutes *= -1;
92                 }
93         }
94         // Gracefully handle non-numbers.
95         if ( isNaN( minutes ) ) {
96                 return 0;
97         } else {
98                 return minutes;
99         }
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() );
113         } else {
114                 // Grab data from the $tzSelect value
115                 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
116                 $tzTextbox.val( minutesToHours( minuteDiff ) );
117         }
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 ) {
124                 localTime += 1440;
125         }
126         while ( localTime >= 1440 ) {
127                 localTime -= 1440;
128         }
129         $localtimeHolder.text( minutesToHours( localTime ) );
132 if ( $tzSelect.length && $tzTextbox.length ) {
133         $tzSelect.change( function() { updateTimezoneSelection(); } );
134         $tzTextbox.blur( function() { updateTimezoneSelection(); } );
135         updateTimezoneSelection();
137 } );