From 0cfb2628dae68e8add4a2e0377747995df706971 Mon Sep 17 00:00:00 2001 From: Leo Koppelkamm Date: Thu, 14 Jul 2011 12:49:39 +0000 Subject: [PATCH] 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. --- includes/HTMLForm.php | 2 +- .../mediawiki.special.preferences.js | 78 +++++++++++-------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/includes/HTMLForm.php b/includes/HTMLForm.php index aeec390baa..8b589c0f17 100644 --- a/includes/HTMLForm.php +++ b/includes/HTMLForm.php @@ -701,7 +701,7 @@ class HTMLForm { } $attributes = array(); if ( $displayTitle ) { - $attributes["title"] = Sanitizer::escapeId( $key ); + $attributes["id"] = 'prefsection-' . Sanitizer::escapeId( $key, 'noninitial' ); } $subsectionHtml .= Xml::fieldset( $legend, $section, $attributes ) . "\n"; } diff --git a/resources/mediawiki.special/mediawiki.special.preferences.js b/resources/mediawiki.special/mediawiki.special.preferences.js index 2d2713844e..c04fbfa0ce 100644 --- a/resources/mediawiki.special/mediawiki.special.preferences.js +++ b/resources/mediawiki.special/mediawiki.special.preferences.js @@ -2,48 +2,58 @@ * JavaScript for Special:Preferences */ ( function( $, mw ) { - $( '#prefsubmit' ).attr( 'id', 'prefcontrol' ); -$( '#preferences' ) +var $preftoc = $(''); +var $preferences = $( '#preferences' ) .addClass( 'jsprefs' ) - .before( $( '' ) ) - .children( 'fieldset' ) - .hide() - .addClass( 'prefsection' ) - .children( 'legend' ) - .addClass( 'mainLegend' ) - .each( function( i, legend ) { - $(legend).parent().attr( 'id', 'prefsection-' + i ); - if ( i === 0 ) { - $(legend).parent().show(); - } - var ident = $(legend).parent().attr( 'title' ); - $( '#preftoc' ).append( - $( '
  • ' ) - .addClass( i === 0 ? 'selected' : null ) - .append( - $( '') - .text( $(legend).text() ) - .attr( 'id', 'preftab-' + ident + '-tab' ) - .attr( 'href', '#preftab-' + ident ) // Use #preftab-N instead of #prefsection-N to avoid jumping on click - .click( function() { - $(this).parent().parent().find( 'li' ).removeClass( 'selected' ); - $(this).parent().addClass( 'selected' ); - $( '#preferences > fieldset' ).hide(); - $( '#prefsection-' + i ).show(); - } ) - ) - ); - } - ); + .before( $preftoc ); + +var $fieldsets = $preferences.children( 'fieldset' ) + .hide() + .addClass( 'prefsection' ); + +var $legends = $fieldsets.children( 'legend' ) + .addClass( 'mainLegend' ); + +// Populate the prefToc +$legends.each( function( i, legend ) { + var $legend = $(legend); + if ( i === 0 ) { + $legend.parent().show(); + } + var ident = $legend.parent().attr( 'id' ); + + var $li = $( '
  • ', { + 'class' : ( i === 0 ) ? 'selected' : null + }); + var $a = $( '', { + text : $legend.text(), + id : ident.replace('prefsection', 'preftab'), + href : '#' + ident + }).click( function( e ) { + e.preventDefault(); + // Handle hash manually to prevent jumping + // Therefore save and restore scrollTop to prevent jumping + var scrollTop = $(window).scrollTop(); + window.location.hash = $(this).attr('href'); + $(window).scrollTop(scrollTop); + + $preftoc.find( 'li' ).removeClass( 'selected' ); + $(this).parent().addClass( 'selected' ); + $( '#preferences > fieldset' ).hide(); + $( '#' + ident ).show(); + }); + $li.append( $a ); + $preftoc.append( $li ); +} ); // If we've reloaded the page or followed an open-in-new-window, // make the selected tab visible. // On document ready: $( function() { var hash = window.location.hash; - if( hash.match( /^#preftab-[\w-]+/ ) ) { - var $tab = $( hash + '-tab' ); + if( hash.match( /^#prefsection-[\w-]+/ ) ) { + var $tab = $( hash.replace('prefsection', 'preftab') ); $tab.click(); } } ); -- 2.20.1