Merge "Add 'dimensions' and 'thumbmine' to resultproperties in prop=imageinfo"
[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 // Note the special check for IE "compatibility" mode.
83 if ( 'onhashchange' in window &&
84 ( document.documentMode === undefined || document.documentMode >= 8 )
85 ) {
86 $(window).on( 'hashchange' , function () {
87 var hash = window.location.hash;
88 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
89 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
90 } else if ( hash === '' ) {
91 switchPrefTab( 'personal', 'noHash' );
92 }
93 });
94 // In older browsers we'll bind a click handler as fallback.
95 // We must not have onhashchange *and* the click handlers, other wise
96 // the click handler calls switchPrefTab() which sets the hash value,
97 // which triggers onhashcange and calls switchPrefTab() again.
98 } else {
99 $preftoc.on( 'click', 'li a', function ( e ) {
100 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
101 e.preventDefault();
102 });
103 }
104
105 /**
106 * Timezone functions.
107 * Guesses Timezone from browser and updates fields onchange
108 */
109
110 $tzSelect = $( '#mw-input-wptimecorrection' );
111 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
112 $localtimeHolder = $( '#wpLocalTime' );
113 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
114
115 function minutesToHours( min ) {
116 var tzHour = Math.floor( Math.abs( min ) / 60 ),
117 tzMin = Math.abs( min ) % 60,
118 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
119 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
120 return tzString;
121 }
122
123 function hoursToMinutes( hour ) {
124 var minutes,
125 arr = hour.split( ':' );
126
127 arr[0] = parseInt( arr[0], 10 );
128
129 if ( arr.length === 1 ) {
130 // Specification is of the form [-]XX
131 minutes = arr[0] * 60;
132 } else {
133 // Specification is of the form [-]XX:XX
134 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
135 if ( arr[0] < 0 ) {
136 minutes *= -1;
137 }
138 }
139 // Gracefully handle non-numbers.
140 if ( isNaN( minutes ) ) {
141 return 0;
142 } else {
143 return minutes;
144 }
145 }
146
147 function updateTimezoneSelection () {
148 var minuteDiff, localTime,
149 type = $tzSelect.val();
150
151 if ( type === 'guess' ) {
152 // Get browser timezone & fill it in
153 minuteDiff = -( new Date().getTimezoneOffset() );
154 $tzTextbox.val( minutesToHours( minuteDiff ) );
155 $tzSelect.val( 'other' );
156 $tzTextbox.prop( 'disabled', false );
157 } else if ( type === 'other' ) {
158 // Grab data from the textbox, parse it.
159 minuteDiff = hoursToMinutes( $tzTextbox.val() );
160 } else {
161 // Grab data from the $tzSelect value
162 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
163 $tzTextbox.val( minutesToHours( minuteDiff ) );
164 }
165
166 // Determine local time from server time and minutes difference, for display.
167 localTime = servertime + minuteDiff;
168
169 // Bring time within the [0,1440) range.
170 while ( localTime < 0 ) {
171 localTime += 1440;
172 }
173 while ( localTime >= 1440 ) {
174 localTime -= 1440;
175 }
176 $localtimeHolder.text( minutesToHours( localTime ) );
177 }
178
179 if ( $tzSelect.length && $tzTextbox.length ) {
180 $tzSelect.change( updateTimezoneSelection );
181 $tzTextbox.blur( updateTimezoneSelection );
182 updateTimezoneSelection();
183 }
184 } );