r92155 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92154‎ | r92155 | r92156 >
Date:12:49, 14 July 2011
Author:diebuche
Status:resolved (Comments)
Tags:
Comment:
Restructure tab code of mediawiki.special.preferences.js . * 10 levels of indent, really? * Follow up r91869: Instead of setting the title, and later parsing it out and setting id from it, just set the id directly. * Hashes are now in the form #prefsection-personal. This means that they now work in browser with deactivated JS as well.
Modified paths:
  • /trunk/phase3/includes/HTMLForm.php (modified) (history)
  • /trunk/phase3/resources/mediawiki.special/mediawiki.special.preferences.js (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/HTMLForm.php
@@ -701,7 +701,7 @@
702702 }
703703 $attributes = array();
704704 if ( $displayTitle ) {
705 - $attributes["title"] = Sanitizer::escapeId( $key );
 705+ $attributes["id"] = 'prefsection-' . Sanitizer::escapeId( $key, 'noninitial' );
706706 }
707707 $subsectionHtml .= Xml::fieldset( $legend, $section, $attributes ) . "\n";
708708 }
Index: trunk/phase3/resources/mediawiki.special/mediawiki.special.preferences.js
@@ -2,48 +2,58 @@
33 * JavaScript for Special:Preferences
44 */
55 ( function( $, mw ) {
6 -
76 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
8 -$( '#preferences' )
 7+var $preftoc = $('<ul id="preftoc"></ul>');
 8+var $preferences = $( '#preferences' )
99 .addClass( 'jsprefs' )
10 - .before( $( '<ul id="preftoc"></ul>' ) )
11 - .children( 'fieldset' )
12 - .hide()
13 - .addClass( 'prefsection' )
14 - .children( 'legend' )
15 - .addClass( 'mainLegend' )
16 - .each( function( i, legend ) {
17 - $(legend).parent().attr( 'id', 'prefsection-' + i );
18 - if ( i === 0 ) {
19 - $(legend).parent().show();
20 - }
21 - var ident = $(legend).parent().attr( 'title' );
22 - $( '#preftoc' ).append(
23 - $( '<li></li>' )
24 - .addClass( i === 0 ? 'selected' : null )
25 - .append(
26 - $( '<a></a>')
27 - .text( $(legend).text() )
28 - .attr( 'id', 'preftab-' + ident + '-tab' )
29 - .attr( 'href', '#preftab-' + ident ) // Use #preftab-N instead of #prefsection-N to avoid jumping on click
30 - .click( function() {
31 - $(this).parent().parent().find( 'li' ).removeClass( 'selected' );
32 - $(this).parent().addClass( 'selected' );
33 - $( '#preferences > fieldset' ).hide();
34 - $( '#prefsection-' + i ).show();
35 - } )
36 - )
37 - );
38 - }
39 - );
 10+ .before( $preftoc );
4011
 12+var $fieldsets = $preferences.children( 'fieldset' )
 13+ .hide()
 14+ .addClass( 'prefsection' );
 15+
 16+var $legends = $fieldsets.children( 'legend' )
 17+ .addClass( 'mainLegend' );
 18+
 19+// Populate the prefToc
 20+$legends.each( function( i, legend ) {
 21+ var $legend = $(legend);
 22+ if ( i === 0 ) {
 23+ $legend.parent().show();
 24+ }
 25+ var ident = $legend.parent().attr( 'id' );
 26+
 27+ var $li = $( '<li/>', {
 28+ 'class' : ( i === 0 ) ? 'selected' : null
 29+ });
 30+ var $a = $( '<a/>', {
 31+ text : $legend.text(),
 32+ id : ident.replace('prefsection', 'preftab'),
 33+ href : '#' + ident
 34+ }).click( function( e ) {
 35+ e.preventDefault();
 36+ // Handle hash manually to prevent jumping
 37+ // Therefore save and restore scrollTop to prevent jumping
 38+ var scrollTop = $(window).scrollTop();
 39+ window.location.hash = $(this).attr('href');
 40+ $(window).scrollTop(scrollTop);
 41+
 42+ $preftoc.find( 'li' ).removeClass( 'selected' );
 43+ $(this).parent().addClass( 'selected' );
 44+ $( '#preferences > fieldset' ).hide();
 45+ $( '#' + ident ).show();
 46+ });
 47+ $li.append( $a );
 48+ $preftoc.append( $li );
 49+} );
 50+
4151 // If we've reloaded the page or followed an open-in-new-window,
4252 // make the selected tab visible.
4353 // On document ready:
4454 $( function() {
4555 var hash = window.location.hash;
46 - if( hash.match( /^#preftab-[\w-]+/ ) ) {
47 - var $tab = $( hash + '-tab' );
 56+ if( hash.match( /^#prefsection-[\w-]+/ ) ) {
 57+ var $tab = $( hash.replace('prefsection', 'preftab') );
4858 $tab.click();
4959 }
5060 } );

Follow-up revisions

RevisionCommit summaryAuthorDate
r95408Revert changes to HTMLForm from r92155reedy18:11, 24 August 2011
r95467Followup r92155, move preferences-specific code introduced in HTMLForm to Pre...catrope09:44, 25 August 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r91869use tab name, not tab index for anchors on Special:Preferences. Bug 29672 . P...diebuche09:54, 11 July 2011

Comments

#Comment by Catrope (talk | contribs)   19:01, 14 August 2011
+					$attributes["id"] = 'prefsection-' . Sanitizer::escapeId( $key, 'noninitial' );

I'm pretty sure that an identifier as specific as prefsection- does not belong in a generic class like HTMLForm.

OK otherwise.

#Comment by Reedy (talk | contribs)   19:49, 24 August 2011

r95408 broke preferences tabs

Could someone more JS familiar look at fixing this up? Thanks!

#Comment by Catrope (talk | contribs)   22:29, 24 August 2011

I'll fix this one

#Comment by Catrope (talk | contribs)   09:45, 25 August 2011

Done in r95467 by making the HTMLForm bit (which was originally added specifically to support the preferences form anyway) a bit more generic and moving the preferences-specific ID prefix to Preferences.php. Marking this one as resolved; if there's something wrong with the way I fixed it, comment on r95467.

Status & tagging log