mediawiki.searchSuggest: Show full article title as a tooltip for each suggestion
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.preferences.js
1 /**
2 * JavaScript for Special:Preferences
3 */
4 jQuery( function ( $ ) {
5 var $preftoc, $preferences, $fieldsets, $legends,
6 hash, labelFunc,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime,
8 $checkBoxes;
9
10 labelFunc = function () {
11 return this.id.replace( /^mw-prefsection/g, 'preftab' );
12 };
13
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc = $( '<ul id="preftoc"></ul>' )
16 .attr( 'role', 'tablist' );
17 $preferences = $( '#preferences' )
18 .addClass( 'jsprefs' )
19 .before( $preftoc );
20 $fieldsets = $preferences.children( 'fieldset' )
21 .hide()
22 .attr( {
23 role: 'tabpanel',
24 'aria-hidden': 'true',
25 'aria-labelledby': labelFunc
26 } )
27 .addClass( 'prefsection' );
28 $legends = $fieldsets
29 .children( 'legend' )
30 .addClass( 'mainLegend' );
31
32 // Make sure the accessibility tip is selectable so that screen reader users take notice,
33 // but hide it per default to reduce interface clutter. Also make sure it becomes visible
34 // when selected. Similar to jquery.mw-jump
35 $( '<div>' ).addClass( 'mw-navigation-hint' )
36 .text( mediaWiki.msg( 'prefs-tabs-navigation-hint' ) )
37 .attr( 'tabIndex', 0 )
38 .on( 'focus blur', function ( e ) {
39 if ( e.type === 'blur' || e.type === 'focusout' ) {
40 $( this ).css( 'height', '0' );
41 } else {
42 $( this ).css( 'height', 'auto' );
43 }
44 } ).insertBefore( $preftoc );
45
46 /**
47 * It uses document.getElementById for security reasons (HTML injections in $()).
48 *
49 * @param String name: the name of a tab without the prefix ("mw-prefsection-")
50 * @param String mode: [optional] A hash will be set according to the current
51 * open section. Set mode 'noHash' to surpress this.
52 */
53 function switchPrefTab( name, mode ) {
54 var $tab, scrollTop;
55 // Handle hash manually to prevent jumping,
56 // therefore save and restore scrollTop to prevent jumping.
57 scrollTop = $( window ).scrollTop();
58 if ( mode !== 'noHash' ) {
59 window.location.hash = '#mw-prefsection-' + name;
60 }
61 $( window ).scrollTop( scrollTop );
62
63 $preftoc.find( 'li' ).removeClass( 'selected' )
64 .find( 'a' ).attr( {
65 tabIndex: -1,
66 'aria-selected': 'false'
67 } );
68
69 $tab = $( document.getElementById( 'preftab-' + name ) );
70 if ( $tab.length ) {
71 $tab.attr( {
72 tabIndex: 0,
73 'aria-selected': 'true'
74 } )
75 .focus()
76 .parent().addClass( 'selected' );
77
78 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
79 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
80 }
81 }
82
83 // Populate the prefToc
84 $legends.each( function ( i, legend ) {
85 var $legend = $( legend ),
86 ident, $li, $a;
87 if ( i === 0 ) {
88 $legend.parent().show();
89 }
90 ident = $legend.parent().attr( 'id' );
91
92 $li = $( '<li>' )
93 .attr( 'role', 'presentation' )
94 .addClass( i === 0 ? 'selected' : '' );
95 $a = $( '<a>' )
96 .attr( {
97 id: ident.replace( 'mw-prefsection', 'preftab' ),
98 href: '#' + ident,
99 role: 'tab',
100 tabIndex: i === 0 ? 0 : -1,
101 'aria-selected': i === 0 ? 'true' : 'false',
102 'aria-controls': ident
103 } )
104 .text( $legend.text() );
105 $li.append( $a );
106 $preftoc.append( $li );
107 } );
108
109 // Enable keyboard users to use left and right keys to switch tabs
110 $preftoc.on( 'keydown', function ( event ) {
111 var keyLeft = 37,
112 keyRight = 39,
113 $el;
114
115 if ( event.keyCode === keyLeft ) {
116 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
117 } else if ( event.keyCode === keyRight ) {
118 $el = $( '#preftoc li.selected' ).next().find( 'a' );
119 } else {
120 return;
121 }
122 if ( $el.length > 0 ) {
123 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
124 }
125 } );
126
127 // If we've reloaded the page or followed an open-in-new-window,
128 // make the selected tab visible.
129 hash = window.location.hash;
130 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
131 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
132 }
133
134 // In browsers that support the onhashchange event we will not bind click
135 // handlers and instead let the browser do the default behavior (clicking the
136 // <a href="#.."> will naturally set the hash, handled by onhashchange.
137 // But other things that change the hash will also be catched (e.g. using
138 // the Back and Forward browser navigation).
139 // Note the special check for IE "compatibility" mode.
140 if ( 'onhashchange' in window &&
141 ( document.documentMode === undefined || document.documentMode >= 8 )
142 ) {
143 $( window ).on( 'hashchange', function () {
144 var hash = window.location.hash;
145 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
146 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
147 } else if ( hash === '' ) {
148 switchPrefTab( 'personal', 'noHash' );
149 }
150 } );
151 // In older browsers we'll bind a click handler as fallback.
152 // We must not have onhashchange *and* the click handlers, other wise
153 // the click handler calls switchPrefTab() which sets the hash value,
154 // which triggers onhashcange and calls switchPrefTab() again.
155 } else {
156 $preftoc.on( 'click', 'li a', function ( e ) {
157 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
158 e.preventDefault();
159 } );
160 }
161
162 /**
163 * Timezone functions.
164 * Guesses Timezone from browser and updates fields onchange
165 */
166
167 $tzSelect = $( '#mw-input-wptimecorrection' );
168 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
169 $localtimeHolder = $( '#wpLocalTime' );
170 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
171
172 function minutesToHours( min ) {
173 var tzHour = Math.floor( Math.abs( min ) / 60 ),
174 tzMin = Math.abs( min ) % 60,
175 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
176 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
177 return tzString;
178 }
179
180 function hoursToMinutes( hour ) {
181 var minutes,
182 arr = hour.split( ':' );
183
184 arr[0] = parseInt( arr[0], 10 );
185
186 if ( arr.length === 1 ) {
187 // Specification is of the form [-]XX
188 minutes = arr[0] * 60;
189 } else {
190 // Specification is of the form [-]XX:XX
191 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
192 if ( arr[0] < 0 ) {
193 minutes *= -1;
194 }
195 }
196 // Gracefully handle non-numbers.
197 if ( isNaN( minutes ) ) {
198 return 0;
199 } else {
200 return minutes;
201 }
202 }
203
204 function updateTimezoneSelection () {
205 var minuteDiff, localTime,
206 type = $tzSelect.val();
207
208 if ( type === 'guess' ) {
209 // Get browser timezone & fill it in
210 minuteDiff = -( new Date().getTimezoneOffset() );
211 $tzTextbox.val( minutesToHours( minuteDiff ) );
212 $tzSelect.val( 'other' );
213 $tzTextbox.prop( 'disabled', false );
214 } else if ( type === 'other' ) {
215 // Grab data from the textbox, parse it.
216 minuteDiff = hoursToMinutes( $tzTextbox.val() );
217 } else {
218 // Grab data from the $tzSelect value
219 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
220 $tzTextbox.val( minutesToHours( minuteDiff ) );
221 }
222
223 // Determine local time from server time and minutes difference, for display.
224 localTime = servertime + minuteDiff;
225
226 // Bring time within the [0,1440) range.
227 while ( localTime < 0 ) {
228 localTime += 1440;
229 }
230 while ( localTime >= 1440 ) {
231 localTime -= 1440;
232 }
233 $localtimeHolder.text( mediaWiki.language.convertNumber( minutesToHours( localTime ) ) );
234 }
235
236 if ( $tzSelect.length && $tzTextbox.length ) {
237 $tzSelect.change( updateTimezoneSelection );
238 $tzTextbox.blur( updateTimezoneSelection );
239 updateTimezoneSelection();
240 }
241
242 // Preserve the tab after saving the preferences
243 // Not using cookies, because their deletion results are inconsistent.
244 // Not using jStorage due to its enormous size (for this feature)
245 if ( window.sessionStorage ) {
246 if ( sessionStorage.getItem( 'mediawikiPreferencesTab' ) !== null ) {
247 switchPrefTab( sessionStorage.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
248 }
249 // Deleting the key, the tab states should be reset until we press Save
250 sessionStorage.removeItem( 'mediawikiPreferencesTab' );
251
252 $( '#mw-prefs-form' ).submit( function () {
253 var storageData = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
254 sessionStorage.setItem( 'mediawikiPreferencesTab', storageData );
255 } );
256 }
257
258 // To disable all 'namespace' checkboxes in Search preferences
259 // when 'Search in all namespaces' checkbox is ticked.
260 $checkBoxes = $( '#mw-htmlform-advancedsearchoptions input[id^=mw-input-wpsearchnamespaces]' );
261 if ( $( '#mw-input-wpsearcheverything' ).prop( 'checked' ) ) {
262 $checkBoxes.prop( 'disabled', true );
263 }
264 $( '#mw-input-wpsearcheverything' ).change( function () {
265 $checkBoxes.prop( 'disabled', $( this ).prop( 'checked' ) );
266 } );
267 } );