Merge "API: Force indexes for prop=linkshere|transcludedin|fileusage"
[lhc/web/wiklou.git] / resources / src / mediawiki.special / mediawiki.special.preferences.js
1 /*!
2 * JavaScript for Special:Preferences
3 */
4 ( function ( mw, $ ) {
5 $( function () {
6 var $preftoc, $preferences, $fieldsets, labelFunc,
7 $tzSelect, $tzTextbox, $localtimeHolder, servertime, allowCloseWindow,
8 convertmessagebox = require( 'mediawiki.notification.convertmessagebox' );
9
10 labelFunc = function () {
11 return this.id.replace( /^mw-prefsection/g, 'preftab' );
12 };
13
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc = $( '#preftoc' );
16 $preferences = $( '#preferences' );
17
18 $fieldsets = $preferences.children( 'fieldset' )
19 .attr( {
20 role: 'tabpanel',
21 'aria-labelledby': labelFunc
22 } );
23 $fieldsets.not( '#mw-prefsection-personal' )
24 .hide()
25 .attr( 'aria-hidden', 'true' );
26
27 // T115692: The following is kept for backwards compatibility with older skins
28 $preferences.addClass( 'jsprefs' );
29 $fieldsets.addClass( 'prefsection' );
30 $fieldsets.children( 'legend' ).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( mw.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 * @ignore
50 * @param {string} name the name of a tab without the prefix ("mw-prefsection-")
51 * @param {string} [mode] A hash will be set according to the current
52 * open section. Set mode 'noHash' to surpress this.
53 */
54 function switchPrefTab( name, mode ) {
55 var $tab, scrollTop;
56 // Handle hash manually to prevent jumping,
57 // therefore save and restore scrollTop to prevent jumping.
58 scrollTop = $( window ).scrollTop();
59 if ( mode !== 'noHash' ) {
60 location.hash = '#mw-prefsection-' + name;
61 }
62 $( window ).scrollTop( scrollTop );
63
64 $preftoc.find( 'li' ).removeClass( 'selected' )
65 .find( 'a' ).attr( {
66 tabIndex: -1,
67 'aria-selected': 'false'
68 } );
69
70 $tab = $( document.getElementById( 'preftab-' + name ) );
71 if ( $tab.length ) {
72 $tab.attr( {
73 tabIndex: 0,
74 'aria-selected': 'true'
75 } )
76 .focus()
77 .parent().addClass( 'selected' );
78
79 $preferences.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
80 $( document.getElementById( 'mw-prefsection-' + name ) ).show().attr( 'aria-hidden', 'false' );
81 }
82 }
83
84 // Check for successbox to replace with notifications
85 convertmessagebox();
86
87 // Enable keyboard users to use left and right keys to switch tabs
88 $preftoc.on( 'keydown', function ( event ) {
89 var keyLeft = 37,
90 keyRight = 39,
91 $el;
92
93 if ( event.keyCode === keyLeft ) {
94 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
95 } else if ( event.keyCode === keyRight ) {
96 $el = $( '#preftoc li.selected' ).next().find( 'a' );
97 } else {
98 return;
99 }
100 if ( $el.length > 0 ) {
101 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
102 }
103 } );
104
105 // Jump to correct section as indicated by the hash.
106 // This function is called onload and onhashchange.
107 function detectHash() {
108 var hash = location.hash,
109 matchedElement, parentSection;
110 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
111 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
112 } else if ( hash.match( /^#mw-[\w\-]+/ ) ) {
113 matchedElement = document.getElementById( hash.slice( 1 ) );
114 parentSection = $( matchedElement ).closest( '.prefsection' );
115 if ( parentSection.length ) {
116 // Switch to proper tab and scroll to selected item.
117 switchPrefTab( parentSection.attr( 'id' ).replace( 'mw-prefsection-', '' ), 'noHash' );
118 matchedElement.scrollIntoView();
119 }
120 }
121 }
122
123 // In browsers that support the onhashchange event we will not bind click
124 // handlers and instead let the browser do the default behavior (clicking the
125 // <a href="#.."> will naturally set the hash, handled by onhashchange.
126 // But other things that change the hash will also be caught (e.g. using
127 // the Back and Forward browser navigation).
128 // Note the special check for IE "compatibility" mode.
129 if ( 'onhashchange' in window &&
130 ( document.documentMode === undefined || document.documentMode >= 8 )
131 ) {
132 $( window ).on( 'hashchange', function () {
133 var hash = location.hash;
134 if ( hash.match( /^#mw-[\w\-]+/ ) ) {
135 detectHash();
136 } else if ( hash === '' ) {
137 switchPrefTab( 'personal', 'noHash' );
138 }
139 } )
140 // Run the function immediately to select the proper tab on startup.
141 .trigger( 'hashchange' );
142 // In older browsers we'll bind a click handler as fallback.
143 // We must not have onhashchange *and* the click handlers, otherwise
144 // the click handler calls switchPrefTab() which sets the hash value,
145 // which triggers onhashchange and calls switchPrefTab() again.
146 } else {
147 $preftoc.on( 'click', 'li a', function ( e ) {
148 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
149 e.preventDefault();
150 } );
151 // If we've reloaded the page or followed an open-in-new-window,
152 // make the selected tab visible.
153 detectHash();
154 }
155
156 // Timezone functions.
157 // Guesses Timezone from browser and updates fields onchange.
158
159 $tzSelect = $( '#mw-input-wptimecorrection' );
160 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
161 $localtimeHolder = $( '#wpLocalTime' );
162 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
163
164 function minutesToHours( min ) {
165 var tzHour = Math.floor( Math.abs( min ) / 60 ),
166 tzMin = Math.abs( min ) % 60,
167 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
168 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
169 return tzString;
170 }
171
172 function hoursToMinutes( hour ) {
173 var minutes,
174 arr = hour.split( ':' );
175
176 arr[ 0 ] = parseInt( arr[ 0 ], 10 );
177
178 if ( arr.length === 1 ) {
179 // Specification is of the form [-]XX
180 minutes = arr[ 0 ] * 60;
181 } else {
182 // Specification is of the form [-]XX:XX
183 minutes = Math.abs( arr[ 0 ] ) * 60 + parseInt( arr[ 1 ], 10 );
184 if ( arr[ 0 ] < 0 ) {
185 minutes *= -1;
186 }
187 }
188 // Gracefully handle non-numbers.
189 if ( isNaN( minutes ) ) {
190 return 0;
191 } else {
192 return minutes;
193 }
194 }
195
196 function updateTimezoneSelection() {
197 var minuteDiff, localTime,
198 type = $tzSelect.val();
199
200 if ( type === 'other' ) {
201 // User specified time zone manually in <input>
202 // Grab data from the textbox, parse it.
203 minuteDiff = hoursToMinutes( $tzTextbox.val() );
204 } else {
205 // Time zone not manually specified by user
206 if ( type === 'guess' ) {
207 // Get browser timezone & fill it in
208 minuteDiff = -( new Date().getTimezoneOffset() );
209 $tzTextbox.val( minutesToHours( minuteDiff ) );
210 $tzSelect.val( 'other' );
211 $tzTextbox.prop( 'disabled', false );
212 } else {
213 // Grab data from the $tzSelect value
214 minuteDiff = parseInt( type.split( '|' )[ 1 ], 10 ) || 0;
215 $tzTextbox.val( minutesToHours( minuteDiff ) );
216 }
217
218 // Set defaultValue prop on the generated box so we don't trigger the
219 // unsaved preferences check
220 $tzTextbox.prop( 'defaultValue', $tzTextbox.val() );
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 localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
228
229 $localtimeHolder.text( mw.language.convertNumber( minutesToHours( localTime ) ) );
230 }
231
232 if ( $tzSelect.length && $tzTextbox.length ) {
233 $tzSelect.change( updateTimezoneSelection );
234 $tzTextbox.blur( updateTimezoneSelection );
235 updateTimezoneSelection();
236 }
237
238 // Preserve the tab after saving the preferences
239 // Not using cookies, because their deletion results are inconsistent.
240 // Not using jStorage due to its enormous size (for this feature)
241 if ( window.sessionStorage ) {
242 if ( sessionStorage.getItem( 'mediawikiPreferencesTab' ) !== null ) {
243 switchPrefTab( sessionStorage.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
244 }
245 // Deleting the key, the tab states should be reset until we press Save
246 sessionStorage.removeItem( 'mediawikiPreferencesTab' );
247
248 $( '#mw-prefs-form' ).submit( function () {
249 var storageData = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
250 sessionStorage.setItem( 'mediawikiPreferencesTab', storageData );
251 } );
252 }
253
254 // Check if all of the form values are unchanged
255 function isPrefsChanged() {
256 var inputs = $( '#mw-prefs-form :input[name]' ),
257 input, $input, inputType,
258 index, optIndex,
259 opt;
260
261 for ( index = 0; index < inputs.length; index++ ) {
262 input = inputs[ index ];
263 $input = $( input );
264
265 // Different types of inputs have different methods for accessing defaults
266 if ( $input.is( 'select' ) ) {
267 // <select> has the property defaultSelected for each option
268 for ( optIndex = 0; optIndex < input.options.length; optIndex++ ) {
269 opt = input.options[ optIndex ];
270 if ( opt.selected !== opt.defaultSelected ) {
271 return true;
272 }
273 }
274 } else if ( $input.is( 'input' ) ) { // <input> has defaultValue or defaultChecked
275 inputType = input.type;
276 if ( inputType === 'radio' || inputType === 'checkbox' ) {
277 if ( input.checked !== input.defaultChecked ) {
278 return true;
279 }
280 } else if ( input.value !== input.defaultValue ) {
281 return true;
282 }
283 }
284 }
285
286 return false;
287 }
288
289 // Disable the button to save preferences unless preferences have changed
290 // Check if preferences have been changed before JS has finished loading
291 if ( !isPrefsChanged() ) {
292 $( '#prefcontrol' ).prop( 'disabled', true );
293 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
294 $( '#prefcontrol' ).prop( 'disabled', false );
295 } );
296 }
297
298 // Set up a message to notify users if they try to leave the page without
299 // saving.
300 allowCloseWindow = mw.confirmCloseWindow( {
301 test: isPrefsChanged,
302 message: mw.msg( 'prefswarning-warning', mw.msg( 'saveprefs' ) ),
303 namespace: 'prefswarning'
304 } );
305 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
306 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
307 } );
308 }( mediaWiki, jQuery ) );