Merge "Omit 'external' changes from ChangesFeed"
[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 // Enable keyboard users to use left and right keys to switch tabs
114 $preftoc.on( 'keydown', function ( event ) {
115 var keyLeft = 37,
116 keyRight = 39,
117 $el;
118
119 if ( event.keyCode === keyLeft ) {
120 $el = $( '#preftoc li.selected' ).prev().find( 'a' );
121 } else if ( event.keyCode === keyRight ) {
122 $el = $( '#preftoc li.selected' ).next().find( 'a' );
123 } else {
124 return;
125 }
126 if ( $el.length > 0 ) {
127 switchPrefTab( $el.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
128 }
129 } );
130
131 // If we've reloaded the page or followed an open-in-new-window,
132 // make the selected tab visible.
133 hash = location.hash;
134 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
135 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
136 }
137
138 // In browsers that support the onhashchange event we will not bind click
139 // handlers and instead let the browser do the default behavior (clicking the
140 // <a href="#.."> will naturally set the hash, handled by onhashchange.
141 // But other things that change the hash will also be catched (e.g. using
142 // the Back and Forward browser navigation).
143 // Note the special check for IE "compatibility" mode.
144 if ( 'onhashchange' in window &&
145 ( document.documentMode === undefined || document.documentMode >= 8 )
146 ) {
147 $( window ).on( 'hashchange', function () {
148 var hash = location.hash;
149 if ( hash.match( /^#mw-prefsection-[\w\-]+/ ) ) {
150 switchPrefTab( hash.replace( '#mw-prefsection-', '' ) );
151 } else if ( hash === '' ) {
152 switchPrefTab( 'personal', 'noHash' );
153 }
154 } );
155 // In older browsers we'll bind a click handler as fallback.
156 // We must not have onhashchange *and* the click handlers, other wise
157 // the click handler calls switchPrefTab() which sets the hash value,
158 // which triggers onhashcange and calls switchPrefTab() again.
159 } else {
160 $preftoc.on( 'click', 'li a', function ( e ) {
161 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
162 e.preventDefault();
163 } );
164 }
165
166 // Timezone functions.
167 // Guesses Timezone from browser and updates fields onchange.
168
169 $tzSelect = $( '#mw-input-wptimecorrection' );
170 $tzTextbox = $( '#mw-input-wptimecorrection-other' );
171 $localtimeHolder = $( '#wpLocalTime' );
172 servertime = parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
173
174 function minutesToHours( min ) {
175 var tzHour = Math.floor( Math.abs( min ) / 60 ),
176 tzMin = Math.abs( min ) % 60,
177 tzString = ( ( min >= 0 ) ? '' : '-' ) + ( ( tzHour < 10 ) ? '0' : '' ) + tzHour +
178 ':' + ( ( tzMin < 10 ) ? '0' : '' ) + tzMin;
179 return tzString;
180 }
181
182 function hoursToMinutes( hour ) {
183 var minutes,
184 arr = hour.split( ':' );
185
186 arr[0] = parseInt( arr[0], 10 );
187
188 if ( arr.length === 1 ) {
189 // Specification is of the form [-]XX
190 minutes = arr[0] * 60;
191 } else {
192 // Specification is of the form [-]XX:XX
193 minutes = Math.abs( arr[0] ) * 60 + parseInt( arr[1], 10 );
194 if ( arr[0] < 0 ) {
195 minutes *= -1;
196 }
197 }
198 // Gracefully handle non-numbers.
199 if ( isNaN( minutes ) ) {
200 return 0;
201 } else {
202 return minutes;
203 }
204 }
205
206 function updateTimezoneSelection() {
207 var minuteDiff, localTime,
208 type = $tzSelect.val();
209
210 if ( type === 'guess' ) {
211 // Get browser timezone & fill it in
212 minuteDiff = -( new Date().getTimezoneOffset() );
213 $tzTextbox.val( minutesToHours( minuteDiff ) );
214 $tzSelect.val( 'other' );
215 $tzTextbox.prop( 'disabled', false );
216 } else if ( type === 'other' ) {
217 // Grab data from the textbox, parse it.
218 minuteDiff = hoursToMinutes( $tzTextbox.val() );
219 } else {
220 // Grab data from the $tzSelect value
221 minuteDiff = parseInt( type.split( '|' )[1], 10 ) || 0;
222 $tzTextbox.val( minutesToHours( minuteDiff ) );
223 }
224
225 // Determine local time from server time and minutes difference, for display.
226 localTime = servertime + minuteDiff;
227
228 // Bring time within the [0,1440) range.
229 localTime = ( ( localTime % 1440 ) + 1440 ) % 1440;
230
231 $localtimeHolder.text( mediaWiki.language.convertNumber( minutesToHours( localTime ) ) );
232 }
233
234 if ( $tzSelect.length && $tzTextbox.length ) {
235 $tzSelect.change( updateTimezoneSelection );
236 $tzTextbox.blur( updateTimezoneSelection );
237 updateTimezoneSelection();
238 }
239
240 // Preserve the tab after saving the preferences
241 // Not using cookies, because their deletion results are inconsistent.
242 // Not using jStorage due to its enormous size (for this feature)
243 if ( window.sessionStorage ) {
244 if ( sessionStorage.getItem( 'mediawikiPreferencesTab' ) !== null ) {
245 switchPrefTab( sessionStorage.getItem( 'mediawikiPreferencesTab' ), 'noHash' );
246 }
247 // Deleting the key, the tab states should be reset until we press Save
248 sessionStorage.removeItem( 'mediawikiPreferencesTab' );
249
250 $( '#mw-prefs-form' ).submit( function () {
251 var storageData = $( $preftoc ).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
252 sessionStorage.setItem( 'mediawikiPreferencesTab', storageData );
253 } );
254 }
255
256 // To disable all 'namespace' checkboxes in Search preferences
257 // when 'Search in all namespaces' checkbox is ticked.
258 $checkBoxes = $( '#mw-htmlform-advancedsearchoptions input[id^=mw-input-wpsearchnamespaces]' );
259 if ( $( '#mw-input-wpsearcheverything' ).prop( 'checked' ) ) {
260 $checkBoxes.prop( 'disabled', true );
261 }
262 $( '#mw-input-wpsearcheverything' ).change( function () {
263 $checkBoxes.prop( 'disabled', $( this ).prop( 'checked' ) );
264 } );
265
266 // Set up a message to notify users if they try to leave the page without
267 // saving.
268 $( '#mw-prefs-form' ).data( 'origdata', $( '#mw-prefs-form' ).serialize() );
269 allowCloseWindow = mediaWiki.confirmCloseWindow( {
270 test: function () {
271 return $( '#mw-prefs-form' ).serialize() !== $( '#mw-prefs-form' ).data( 'origdata' );
272 },
273
274 message: mediaWiki.msg( 'prefswarning-warning', mediaWiki.msg( 'saveprefs' ) ),
275 namespace: 'prefswarning'
276 } );
277 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow, 'release' ) );
278 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow, 'release' ) );
279 } );