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