Merge "Fix E_STRICT in ForeignAPIRepo"
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.preferences.js
1 /*
2 * JavaScript for Special:Preferences
3 */
4 jQuery( document ).ready( function ( $ ) {
5 var $preftoc, $preferences, $fieldsets, $legends,
6 hash,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime;
8
9 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
10
11 $preftoc = $('<ul id="preftoc"></ul>'),
12 $preferences = $( '#preferences' )
13 .addClass( 'jsprefs' )
14 .before( $preftoc ),
15 $fieldsets = $preferences.children( 'fieldset' )
16 .hide()
17 .addClass( 'prefsection' ),
18 $legends = $fieldsets
19 .children( 'legend' )
20 .addClass( 'mainLegend' );
21
22 /**
23 * It uses document.getElementById for security reasons (html injections in
24 * jQuery()).
25 *
26 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
27 * @param String mode: [optional] A hash will be set according to the current
28 * open section. Set mode 'noHash' to surpress this.
29 */
30 function switchPrefTab( name, mode ) {
31 var $tab, scrollTop;
32 // Handle hash manually to prevent jumping,
33 // therefore save and restore scrollTop to prevent jumping.
34 scrollTop = $( window ).scrollTop();
35 if ( mode !== 'noHash' ) {
36 window.location.hash = '#mw-prefsection-' + name;
37 }
38 $( window ).scrollTop( scrollTop );
39
40 $preftoc.find( 'li' ).removeClass( 'selected' );
41 $tab = $( document.getElementById( 'preftab-' + name ) );
42 if ( $tab.length ) {
43 $tab.parent().addClass( 'selected' );
44 $preferences.children( 'fieldset' ).hide();
45 $( document.getElementById( 'mw-prefsection-' + name ) ).show();
46 }
47 }
48
49 // Populate the prefToc
50 $legends.each( function ( i, legend ) {
51 var $legend = $(legend),
52 ident, $li, $a;
53 if ( i === 0 ) {
54 $legend.parent().show();
55 }
56 ident = $legend.parent().attr( 'id' );
57
58 $li = $( '<li>' )
59 .addClass( i === 0 ? 'selected' : '' );
60 $a = $( '<a>' )
61 .attr( {
62 id: ident.replace( 'mw-prefsection', 'preftab' ),
63 href: '#' + ident
64 } )
65 .text( $legend.text() );
66 $li.append( $a );
67 $preftoc.append( $li );
68 } );
69
70 // If we've reloaded the page or followed an open-in-new-window,
71 // make the selected tab visible.
72 hash = window.location.hash;
73 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
74 switchPrefTab( hash.replace( '#mw-prefsection-' , '' ) );
75 }
76
77 // In browsers that support the onhashchange event we will not bind click
78 // handlers and instead let the browser do the default behavior (clicking the
79 // <a href="#.."> will naturally set the hash, handled by onhashchange.
80 // But other things that change the hash will also be catched (e.g. using
81 // the Back and Forward browser navigation).
82 if ( 'onhashchange' in window ) {
83 $(window).on( 'hashchange' , function () {
84 var hash = window.location.hash;
85 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
86 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
87 } else if ( hash === '' ) {
88 switchPrefTab( 'personal', 'noHash' );
89 }
90 });
91 // In older browsers we'll bind a click handler as fallback.
92 // We must not have onhashchange *and* the click handlers, other wise
93 // the click handler calls switchPrefTab() which sets the hash value,
94 // which triggers onhashcange and calls switchPrefTab() again.
95 } else {
96 $preftoc.on( 'click', 'li a', function ( e ) {
97 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
98 e.preventDefault();
99 });
100 }
101
102 /**
103 * Timezone functions.
104 * Guesses Timezone from browser and updates fields onchange
105 */
106
107 $tzSelect = $( '#mw-input-wptimecorrection' );
108 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
109 $localtimeHolder = $( '#wpLocalTime' );
110 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
111
112 function minutesToHours( min ) {
113 var tzHour = Math.floor( Math.abs( min ) / 60 ),
114 tzMin = Math.abs( min ) % 60,
115 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
116 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
117 return tzString;
118 }
119
120 function hoursToMinutes( hour ) {
121 var minutes,
122 arr = hour.split( ':' );
123
124 arr[0] = parseInt( arr[0], 10 );
125
126 if ( arr.length === 1 ) {
127 // Specification is of the form [-]XX
128 minutes = arr[0] * 60;
129 } else {
130 // Specification is of the form [-]XX:XX
131 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
132 if ( arr[0] < 0 ) {
133 minutes *= -1;
134 }
135 }
136 // Gracefully handle non-numbers.
137 if ( isNaN( minutes ) ) {
138 return 0;
139 } else {
140 return minutes;
141 }
142 }
143
144 function updateTimezoneSelection () {
145 var minuteDiff, localTime,
146 type = $tzSelect.val();
147
148 if ( type === 'guess' ) {
149 // Get browser timezone & fill it in
150 minuteDiff = -( new Date().getTimezoneOffset() );
151 $tzTextbox.val( minutesToHours( minuteDiff ) );
152 $tzSelect.val( 'other' );
153 $tzTextbox.prop( 'disabled', false );
154 } else if ( type === 'other' ) {
155 // Grab data from the textbox, parse it.
156 minuteDiff = hoursToMinutes( $tzTextbox.val() );
157 } else {
158 // Grab data from the $tzSelect value
159 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
160 $tzTextbox.val( minutesToHours( minuteDiff ) );
161 }
162
163 // Determine local time from server time and minutes difference, for display.
164 localTime = servertime + minuteDiff;
165
166 // Bring time within the [0,1440) range.
167 while ( localTime < 0 ) {
168 localTime += 1440;
169 }
170 while ( localTime >= 1440 ) {
171 localTime -= 1440;
172 }
173 $localtimeHolder.text( minutesToHours( localTime ) );
174 }
175
176 if ( $tzSelect.length && $tzTextbox.length ) {
177 $tzSelect.change( updateTimezoneSelection );
178 $tzTextbox.blur( updateTimezoneSelection );
179 updateTimezoneSelection();
180 }
181 } );