2 * JavaScript for Special:Preferences
6 var $preftoc
, $preferences
, $fieldsets
, labelFunc
, previousTab
,
7 $tzSelect
, $tzTextbox
, $localtimeHolder
, servertime
, allowCloseWindow
,
8 convertmessagebox
= require( 'mediawiki.notification.convertmessagebox' );
10 labelFunc = function () {
11 return this.id
.replace( /^mw-prefsection/g, 'preftab' );
14 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
15 $preftoc
= $( '#preftoc' );
16 $preferences
= $( '#preferences' );
18 $fieldsets
= $preferences
.children( 'fieldset' )
21 'aria-labelledby': labelFunc
23 $fieldsets
.not( '#mw-prefsection-personal' )
25 .attr( 'aria-hidden', 'true' );
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' );
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' );
42 $( this ).css( 'height', 'auto' );
44 } ).insertBefore( $preftoc
);
47 * It uses document.getElementById for security reasons (HTML injections in $()).
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.
54 function switchPrefTab( name
, mode
) {
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
;
62 $( window
).scrollTop( scrollTop
);
64 $preftoc
.find( 'li' ).removeClass( 'selected' )
67 'aria-selected': 'false'
70 $tab
= $( document
.getElementById( 'preftab-' + name
) );
74 'aria-selected': 'true'
77 .parent().addClass( 'selected' );
79 $preferences
.children( 'fieldset' ).hide().attr( 'aria-hidden', 'true' );
80 $( document
.getElementById( 'mw-prefsection-' + name
) ).show().attr( 'aria-hidden', 'false' );
84 // Check for successbox to replace with notifications
87 // Enable keyboard users to use left and right keys to switch tabs
88 $preftoc
.on( 'keydown', function ( event
) {
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' );
100 if ( $el
.length
> 0 ) {
101 switchPrefTab( $el
.attr( 'href' ).replace( '#mw-prefsection-', '' ) );
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 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
112 switchPrefTab( hash
.replace( '#mw-prefsection-', '' ) );
113 } else if ( hash
.match( /^#mw-[\w\-]+/ ) ) {
114 matchedElement
= document
.getElementById( hash
.slice( 1 ) );
115 parentSection
= $( matchedElement
).closest( '.prefsection' );
116 if ( parentSection
.length
) {
117 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
118 // Switch to proper tab and scroll to selected item.
119 switchPrefTab( parentSection
.attr( 'id' ).replace( 'mw-prefsection-', '' ), 'noHash' );
120 matchedElement
.scrollIntoView();
125 // In browsers that support the onhashchange event we will not bind click
126 // handlers and instead let the browser do the default behavior (clicking the
127 // <a href="#.."> will naturally set the hash, handled by onhashchange.
128 // But other things that change the hash will also be caught (e.g. using
129 // the Back and Forward browser navigation).
130 // Note the special check for IE "compatibility" mode.
131 if ( 'onhashchange' in window
&&
132 ( document
.documentMode
=== undefined || document
.documentMode
>= 8 )
134 $( window
).on( 'hashchange', function () {
135 var hash
= location
.hash
;
136 if ( hash
.match( /^#mw-[\w\-]+/ ) ) {
138 } else if ( hash
=== '' ) {
139 switchPrefTab( 'personal', 'noHash' );
142 // Run the function immediately to select the proper tab on startup.
143 .trigger( 'hashchange' );
144 // In older browsers we'll bind a click handler as fallback.
145 // We must not have onhashchange *and* the click handlers, otherwise
146 // the click handler calls switchPrefTab() which sets the hash value,
147 // which triggers onhashchange and calls switchPrefTab() again.
149 $preftoc
.on( 'click', 'li a', function ( e
) {
150 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
153 // If we've reloaded the page or followed an open-in-new-window,
154 // make the selected tab visible.
158 // Timezone functions.
159 // Guesses Timezone from browser and updates fields onchange.
161 $tzSelect
= $( '#mw-input-wptimecorrection' );
162 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
163 $localtimeHolder
= $( '#wpLocalTime' );
164 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
166 function minutesToHours( min
) {
167 var tzHour
= Math
.floor( Math
.abs( min
) / 60 ),
168 tzMin
= Math
.abs( min
) % 60,
169 tzString
= ( ( min
>= 0 ) ? '' : '-' ) + ( ( tzHour
< 10 ) ? '0' : '' ) + tzHour
+
170 ':' + ( ( tzMin
< 10 ) ? '0' : '' ) + tzMin
;
174 function hoursToMinutes( hour
) {
176 arr
= hour
.split( ':' );
178 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
180 if ( arr
.length
=== 1 ) {
181 // Specification is of the form [-]XX
182 minutes
= arr
[ 0 ] * 60;
184 // Specification is of the form [-]XX:XX
185 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
186 if ( arr
[ 0 ] < 0 ) {
190 // Gracefully handle non-numbers.
191 if ( isNaN( minutes
) ) {
198 function updateTimezoneSelection() {
199 var minuteDiff
, localTime
,
200 type
= $tzSelect
.val();
202 if ( type
=== 'other' ) {
203 // User specified time zone manually in <input>
204 // Grab data from the textbox, parse it.
205 minuteDiff
= hoursToMinutes( $tzTextbox
.val() );
207 // Time zone not manually specified by user
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 );
215 // Grab data from the $tzSelect value
216 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
217 $tzTextbox
.val( minutesToHours( minuteDiff
) );
220 // Set defaultValue prop on the generated box so we don't trigger the
221 // unsaved preferences check
222 $tzTextbox
.prop( 'defaultValue', $tzTextbox
.val() );
225 // Determine local time from server time and minutes difference, for display.
226 localTime
= servertime
+ minuteDiff
;
228 // Bring time within the [0,1440) range.
229 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
231 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
234 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
235 $tzSelect
.change( updateTimezoneSelection
);
236 $tzTextbox
.blur( updateTimezoneSelection
);
237 updateTimezoneSelection();
240 // Restore the active tab after saving the preferences
241 previousTab
= mw
.storage
.session
.get( 'mwpreferences-prevTab' );
243 switchPrefTab( previousTab
, 'noHash' );
244 // Deleting the key, the tab states should be reset until we press Save
245 mw
.storage
.session
.remove( 'mwpreferences-prevTab' );
248 $( '#mw-prefs-form' ).on( 'submit', function () {
249 var value
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
250 mw
.storage
.session
.set( 'mwpreferences-prevTab', value
);
253 // Check if all of the form values are unchanged
254 function isPrefsChanged() {
255 var inputs
= $( '#mw-prefs-form :input[name]' ),
256 input
, $input
, inputType
,
260 for ( index
= 0; index
< inputs
.length
; index
++ ) {
261 input
= inputs
[ index
];
264 // Different types of inputs have different methods for accessing defaults
265 if ( $input
.is( 'select' ) ) {
266 // <select> has the property defaultSelected for each option
267 for ( optIndex
= 0; optIndex
< input
.options
.length
; optIndex
++ ) {
268 opt
= input
.options
[ optIndex
];
269 if ( opt
.selected
!== opt
.defaultSelected
) {
273 } else if ( $input
.is( 'input' ) ) { // <input> has defaultValue or defaultChecked
274 inputType
= input
.type
;
275 if ( inputType
=== 'radio' || inputType
=== 'checkbox' ) {
276 if ( input
.checked
!== input
.defaultChecked
) {
279 } else if ( input
.value
!== input
.defaultValue
) {
288 // Disable the button to save preferences unless preferences have changed
289 // Check if preferences have been changed before JS has finished loading
290 if ( !isPrefsChanged() ) {
291 $( '#prefcontrol' ).prop( 'disabled', true );
292 $( '#preferences > fieldset' ).one( 'change keydown mousedown', function () {
293 $( '#prefcontrol' ).prop( 'disabled', false );
297 // Set up a message to notify users if they try to leave the page without
299 allowCloseWindow
= mw
.confirmCloseWindow( {
300 test
: isPrefsChanged
,
301 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
302 namespace: 'prefswarning'
304 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
305 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );
307 }( mediaWiki
, jQuery
) );