Merge "jquery.tablesorter: Silence an expected "sort-rowspan-error" warning"
[mediawiki.git] / resources / src / mediawiki.special.preferences.ooui / nav.js
blob94bff163d121cc9f11da6c690543ec4ebedc8efd
1 /*!
2 * JavaScript for Special:Preferences: section navigation.
3 */
4 ( function () {
5 let switchingNoHash;
6 const session = require( 'mediawiki.storage' ).session;
7 /**
8 * @ignore
9 */
10 module.exports = {
11 switchingNoHash: switchingNoHash,
12 /**
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.
17 * @ignore
18 * @param {string} hintMsg the layout-specific navigation hint message
19 * @return {jQuery}
21 insertHints: function ( hintMsg ) {
22 return $( '<div>' ).addClass( 'mw-navigation-hint' )
23 .text( hintMsg )
24 .attr( {
25 tabIndex: 0
26 } )
27 .insertBefore( '.mw-htmlform-ooui-wrapper' );
30 /**
31 * Call layout-specific function for jumping to the correct section and manage hash state.
33 * @ignore
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 ) {
41 if ( noHash ) {
42 this.switchingNoHash = true;
44 setSection( sectionName, fieldset );
45 if ( noHash ) {
46 this.switchingNoHash = false;
50 /**
51 * Determine the correct section indicated by the hash.
52 * This function is called onload and onhashchange.
54 * @ignore
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 );
74 /**
75 * Determine if there is a valid hash or default section.
77 * @ignore
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 );
90 /**
91 * Trigger onHashChange onload to select the proper tab on startup.
93 * @ignore
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
105 * @ignore
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' );
111 if ( sectionName ) {
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 );
120 }() );