2 * JavaScript for Special:Preferences
6 var $preftoc
, $preferences
, $fieldsets
, labelFunc
,
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 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();
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 )
132 $( window
).on( 'hashchange', function () {
133 var hash
= location
.hash
;
134 if ( hash
.match( /^#mw-[\w\-]+/ ) ) {
136 } else if ( hash
=== '' ) {
137 switchPrefTab( 'personal', 'noHash' );
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.
147 $preftoc
.on( 'click', 'li a', function ( e
) {
148 switchPrefTab( $( this ).attr( 'href' ).replace( '#mw-prefsection-', '' ) );
151 // If we've reloaded the page or followed an open-in-new-window,
152 // make the selected tab visible.
156 // Timezone functions.
157 // Guesses Timezone from browser and updates fields onchange.
159 $tzSelect
= $( '#mw-input-wptimecorrection' );
160 $tzTextbox
= $( '#mw-input-wptimecorrection-other' );
161 $localtimeHolder
= $( '#wpLocalTime' );
162 servertime
= parseInt( $( 'input[name="wpServerTime"]' ).val(), 10 );
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
;
172 function hoursToMinutes( hour
) {
174 arr
= hour
.split( ':' );
176 arr
[ 0 ] = parseInt( arr
[ 0 ], 10 );
178 if ( arr
.length
=== 1 ) {
179 // Specification is of the form [-]XX
180 minutes
= arr
[ 0 ] * 60;
182 // Specification is of the form [-]XX:XX
183 minutes
= Math
.abs( arr
[ 0 ] ) * 60 + parseInt( arr
[ 1 ], 10 );
184 if ( arr
[ 0 ] < 0 ) {
188 // Gracefully handle non-numbers.
189 if ( isNaN( minutes
) ) {
196 function updateTimezoneSelection() {
197 var minuteDiff
, localTime
,
198 type
= $tzSelect
.val();
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() );
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 );
213 // Grab data from the $tzSelect value
214 minuteDiff
= parseInt( type
.split( '|' )[ 1 ], 10 ) || 0;
215 $tzTextbox
.val( minutesToHours( minuteDiff
) );
218 // Set defaultValue prop on the generated box so we don't trigger the
219 // unsaved preferences check
220 $tzTextbox
.prop( 'defaultValue', $tzTextbox
.val() );
223 // Determine local time from server time and minutes difference, for display.
224 localTime
= servertime
+ minuteDiff
;
226 // Bring time within the [0,1440) range.
227 localTime
= ( ( localTime
% 1440 ) + 1440 ) % 1440;
229 $localtimeHolder
.text( mw
.language
.convertNumber( minutesToHours( localTime
) ) );
232 if ( $tzSelect
.length
&& $tzTextbox
.length
) {
233 $tzSelect
.change( updateTimezoneSelection
);
234 $tzTextbox
.blur( updateTimezoneSelection
);
235 updateTimezoneSelection();
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' );
245 // Deleting the key, the tab states should be reset until we press Save
246 sessionStorage
.removeItem( 'mediawikiPreferencesTab' );
248 $( '#mw-prefs-form' ).submit( function () {
249 var storageData
= $( $preftoc
).find( 'li.selected a' ).attr( 'id' ).replace( 'preftab-', '' );
250 sessionStorage
.setItem( 'mediawikiPreferencesTab', storageData
);
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
,
261 for ( index
= 0; index
< inputs
.length
; index
++ ) {
262 input
= inputs
[ index
];
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
) {
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
) {
280 } else if ( input
.value
!== input
.defaultValue
) {
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 );
298 // Set up a message to notify users if they try to leave the page without
300 allowCloseWindow
= mw
.confirmCloseWindow( {
301 test
: isPrefsChanged
,
302 message
: mw
.msg( 'prefswarning-warning', mw
.msg( 'saveprefs' ) ),
303 namespace: 'prefswarning'
305 $( '#mw-prefs-form' ).submit( $.proxy( allowCloseWindow
, 'release' ) );
306 $( '#mw-prefs-restoreprefs' ).click( $.proxy( allowCloseWindow
, 'release' ) );
308 }( mediaWiki
, jQuery
) );