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