2 * JavaScript for Special:Preferences: section navigation.
6 const session
= require( 'mediawiki.storage' ).session
;
11 switchingNoHash
: switchingNoHash
,
13 * Make sure the accessibility tip is focussable so that keyboard users take notice,
14 * but hide it by default to reduce visual clutter.
15 * Make sure it becomes visible when focused.
18 * @param {string} hintMsg the layout-specific navigation hint message
21 insertHints: function ( hintMsg
) {
22 return $( '<div>' ).addClass( 'mw-navigation-hint' )
27 .insertBefore( '.mw-htmlform-ooui-wrapper' );
31 * Call layout-specific function for jumping to the correct section and manage hash state.
34 * @param {Function} setSection callback for opening the section
35 * @param {string} sectionName The name of a section
36 * @param {string} [fieldset] A fieldset containing a subsection
37 * @param {boolean} [noHash] A hash will be set according to the current
38 * open section. Use this flag to suppress this.
40 switchPrefSection: function ( setSection
, sectionName
, fieldset
, noHash
) {
42 this.switchingNoHash
= true;
44 setSection( sectionName
, fieldset
);
46 this.switchingNoHash
= false;
51 * Determine the correct section indicated by the hash.
52 * This function is called onload and onhashchange.
55 * @param {Function} setSection callback for opening the section
57 detectHash: function ( setSection
) {
58 const hash
= location
.hash
;
59 if ( /^#mw-prefsection-[\w]+$/.test( hash
) ) {
60 session
.remove( 'mwpreferences-prevTab' );
61 // Open proper section.
62 this.switchPrefSection( setSection
, hash
.slice( 1 ) );
63 } else if ( /^#mw-[\w-]+$/.test( hash
) ) {
64 const subsection
= document
.getElementById( hash
.slice( 1 ) );
65 const $section
= $( subsection
).closest( '.mw-prefs-section-fieldset' );
66 if ( $section
.length
) {
67 session
.remove( 'mwpreferences-prevTab' );
68 // Open proper section and scroll to selected fieldset.
69 this.switchPrefSection( setSection
, $section
.attr( 'id' ), subsection
, true );
75 * Determine if there is a valid hash or default section.
78 * @param {Function} setSection callback for opening the section
79 * @param {string} defaultSectionName The name of a section to load by default
81 onHashChange: function ( setSection
, defaultSectionName
) {
82 const hash
= location
.hash
;
83 if ( /^#mw-[\w-]+/.test( hash
) ) {
84 this.detectHash( setSection
);
85 } else if ( hash
=== '' && defaultSectionName
) {
86 this.switchPrefSection( setSection
, defaultSectionName
, undefined, true );
91 * Trigger onHashChange onload to select the proper tab on startup.
94 * @param {Function} setSection callback for opening the section
95 * @param {string} defaultSection The name of a section to load by default
97 onLoad: function ( setSection
, defaultSection
) {
98 $( window
).on( 'hashchange', this.onHashChange
.bind( this, setSection
, defaultSection
)
99 ).trigger( 'hashchange' );
103 * Restore the active tab after saving the preferences
106 * @param {Function} setSection callback for opening the section
107 * @param {Function} onSubmit callback for saving the active section name
109 restorePrevSection: function ( setSection
, onSubmit
) {
110 const sectionName
= session
.get( 'mwpreferences-prevTab' );
112 this.switchPrefSection( setSection
, sectionName
, undefined, true );
113 // Deleting the key, the section states should be reset until we press Save
114 session
.remove( 'mwpreferences-prevTab' );
116 $( '#mw-prefs-form' ).on( 'submit', onSubmit
);