r44170 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r44169‎ | r44170 | r44171 >
Date:06:27, 3 December 2008
Author:werdna
Status:ok
Tags:
Comment:
Configure extension - summarise hidden settings with JS! :-)
Modified paths:
  • /trunk/extensions/Configure/Configure.i18n.php (modified) (history)
  • /trunk/extensions/Configure/Configure.js (modified) (history)
  • /trunk/extensions/Configure/Configure.page.php (modified) (history)
  • /trunk/phase3/skins/common/wikibits.js (modified) (history)

Diff [purge]

Index: trunk/phase3/skins/common/wikibits.js
@@ -289,7 +289,29 @@
290290 return item;
291291 }
292292
 293+function getInnerText(el) {
 294+ if (typeof el == "string") return el;
 295+ if (typeof el == "undefined") { return el };
 296+ if (el.textContent) return el.textContent; // not needed but it is faster
 297+ if (el.innerText) return el.innerText; // IE doesn't have textContent
 298+ var str = "";
293299
 300+ var cs = el.childNodes;
 301+ var l = cs.length;
 302+ for (var i = 0; i < l; i++) {
 303+ switch (cs[i].nodeType) {
 304+ case 1: //ELEMENT_NODE
 305+ str += ts_getInnerText(cs[i]);
 306+ break;
 307+ case 3: //TEXT_NODE
 308+ str += cs[i].nodeValue;
 309+ break;
 310+ }
 311+ }
 312+ return str;
 313+}
 314+
 315+
294316 /**
295317 * Set up accesskeys/tooltips from the deprecated ta array. If doId
296318 * is specified, only set up for that id. Note that this function is
@@ -539,25 +561,7 @@
540562 }
541563
542564 function ts_getInnerText(el) {
543 - if (typeof el == "string") return el;
544 - if (typeof el == "undefined") { return el };
545 - if (el.textContent) return el.textContent; // not needed but it is faster
546 - if (el.innerText) return el.innerText; // IE doesn't have textContent
547 - var str = "";
548 -
549 - var cs = el.childNodes;
550 - var l = cs.length;
551 - for (var i = 0; i < l; i++) {
552 - switch (cs[i].nodeType) {
553 - case 1: //ELEMENT_NODE
554 - str += ts_getInnerText(cs[i]);
555 - break;
556 - case 3: //TEXT_NODE
557 - str += cs[i].nodeValue;
558 - break;
559 - }
560 - }
561 - return str;
 565+ return getInnerText( el );
562566 }
563567
564568 function ts_resortTable(lnk) {
Index: trunk/extensions/Configure/Configure.page.php
@@ -756,6 +756,7 @@
757757 $biglist_hidden = Xml::encodeJsVar( wfMsg( 'configure-js-biglist-hidden' ) );
758758 $biglist_show = Xml::encodeJsVar( wfMsg( 'configure-js-biglist-show' ) );
759759 $biglist_hide = Xml::encodeJsVar( wfMsg( 'configure-js-biglist-hide' ) );
 760+ $summary_none = Xml::encodeJsVar( wfMsg( 'configure-js-summary-none' ) );
760761 $ajax = isset( $wgUseAjax ) && $wgUseAjax ? 'true' : 'false';
761762 $script = array(
762763 "<script type=\"$wgJsMimeType\">/*<![CDATA[*/",
@@ -771,6 +772,7 @@
772773 "var wgConfigureBiglistHidden = {$biglist_hidden};",
773774 "var wgConfigureBiglistShow = {$biglist_show};",
774775 "var wgConfigureBiglistHide = {$biglist_hide};",
 776+ "var wgConfigureSummaryNone = {$summary_none};",
775777 "/*]]>*/</script>",
776778 "<script type=\"{$wgJsMimeType}\" src=\"{$wgScriptPath}/extensions/Configure/Configure.js?{$wgConfigureStyleVersion}\"></script>",
777779 );
@@ -989,7 +991,7 @@
990992 $attr
991993 ) . "</span>\n";
992994 }
993 - $text = Xml::tags( 'div', array( 'class' => 'configure-biglist' ), $text );
 995+ $text = Xml::tags( 'div', array( 'class' => 'configure-biglist '.$type ), $text );
994996 return $text;
995997 }
996998 if ( $type == 'ns-text' ) {
@@ -1034,7 +1036,7 @@
10351037 if ( '' == $name ) {
10361038 $name = wfMsgExt( 'blanknamespace', array( 'parseinline' ) );
10371039 }
1038 - $text .= '<tr><td>' . $name . '</td><td>';
 1040+ $text .= '<tr><td>' . Xml::label( $name, "wp{$conf}-ns{$ns}" ) . '</td><td>';
10391041 if ( $allowed ) {
10401042 $text .= Xml::openElement( 'textarea', array(
10411043 'name' => "wp{$conf}-ns{$ns}",
@@ -1075,7 +1077,7 @@
10761078 $encConf = htmlspecialchars( $conf );
10771079 $text = "<table id= '{$encConf}' class='{$type} configure-array-table'>\n<tr><th>{$groupdesc}</th><th>{$valdesc}</th></tr>\n";
10781080 foreach ( $iter as $group => $levs ) {
1079 - $row = '<div class="configure-biglist"><ul>';
 1081+ $row = '<div class="configure-biglist '.$type.'-element"><ul>';
10801082 foreach ( $all as $right ) {
10811083 if ( $type == 'group-bool' )
10821084 $checked = ( isset( $levs[$right] ) && $levs[$right] );
Index: trunk/extensions/Configure/Configure.i18n.php
@@ -49,6 +49,7 @@
5050 'configure-js-biglist-hidden' => 'Setting details hidden',
5151 'configure-js-biglist-show' => '[show details]',
5252 'configure-js-biglist-hide' => '[hide details]',
 53+ 'configure-js-summary-none' => 'No settings',
5354 'configure-no-diff' => 'There are no changes between selected versions.',
5455 'configure-no-directory' => 'The directory used to store the settings, <tt>$1</tt>, does not exist.
5556 Please create it or change it to use this extension.',
Index: trunk/extensions/Configure/Configure.js
@@ -254,8 +254,104 @@
255255 div.appendChild( tn );
256256 div.insertBefore( toggleLink, div.childNodes[0] );
257257 list.parentNode.insertBefore( div, list );
 258+
 259+ // Summaries
 260+ var summary = document.createElement( 'div' );
 261+ summary.id = 'configure-biglist-summary-'+l;
 262+ summary.className = 'configure-biglist-summary';
 263+ summariseSetting( list, summary );
 264+ list.parentNode.insertBefore( summary, list );
258265 }
259266 }
 267+// Summarise the setting contained in 'div' to the summary field 'summary'.
 268+function summariseSetting( div, summary ) {
 269+ // Empty the existing summary
 270+ while(summary.firstChild) {
 271+ summary.removeChild(summary.firstChild);
 272+ }
 273+
 274+ // Based on class, do something.
 275+ var elementType = ' '+div.className+' ';
 276+
 277+ var isType = function(type) { return elementType.indexOf( ' '+type+' ' ) !== -1; }
 278+
 279+ if (isType('assoc') ) {
 280+ // If it's too big to display as an associative array, it's too big to display as a summary.
 281+ } else if ( isType( 'ns-bool' ) || isType( 'ns-simple' ) || isType( 'group-bool-element' ) || isType( 'group-array-element' ) ) {
 282+ var labels = div.getElementsByTagName( 'label' );
 283+ var matches = [];
 284+ for( var i=0; i<labels.length; ++i ) {
 285+ var label = labels[i];
 286+ var checkbox = document.getElementById( label.htmlFor );
 287+
 288+ if (checkbox.checked) {
 289+ matches.push( label.innerHTML ); // Yuck
 290+ }
 291+ }
 292+
 293+ summary.innerHTML = matches.join( ', ' ); // Be aware of velociraptors.
 294+ } else if ( isType( 'ns-array' ) || isType( 'ns-text' ) ) {
 295+ // Get the headers
 296+ var header_key = undefined;
 297+ var header_value = undefined;
 298+
 299+ var headers = div.getElementsByTagName( 'th' );
 300+ header_key = getInnerText( headers[0] );
 301+ header_value = getInnerText( headers[1] );
 302+
 303+ var table = document.createElement( 'table' );
 304+ table.className = 'ns-array';
 305+ table.appendChild( document.createElement( 'tbody' ) );
 306+ table = table.firstChild;
 307+
 308+ var tr = document.createElement( 'tr' );
 309+ var key_th = document.createElement( 'th' );
 310+ var value_th = document.createElement( 'th' );
 311+ key_th.appendChild( document.createTextNode( header_key ) );
 312+ value_th.appendChild( document.createTextNode( header_value ) );
 313+
 314+ tr.appendChild( key_th );
 315+ tr.appendChild( value_th );
 316+ table.appendChild( tr );
 317+
 318+ var rows = false;
 319+
 320+ var labels = div.getElementsByTagName( 'label' );
 321+ for( var i=0; i<labels.length; ++i ) {
 322+ var label = labels[i];
 323+ var arrayfield = document.getElementById( label.htmlFor );
 324+
 325+ if (arrayfield.value) {
 326+ rows = true;
 327+
 328+ tr = document.createElement( 'tr' );
 329+ var key_td = document.createElement( 'td' );
 330+ var value_td = document.createElement( 'td' );
 331+
 332+ key_td.appendChild( document.createTextNode( getInnerText( label ) ) );
 333+ value_td.appendChild( document.createTextNode( arrayfield.value ) );
 334+
 335+ tr.appendChild( key_td );
 336+ tr.appendChild( value_td );
 337+
 338+ table.appendChild( tr );
 339+ }
 340+ }
 341+
 342+ if (!rows) {
 343+ tr = document.createElement( 'tr' );
 344+ var td = document.createElement( 'td' );
 345+ td.setAttribute( 'colspan', 2 );
 346+ td.appendChild( document.createTextNode( wgConfigureSummaryNone ) );
 347+ tr.appendChild( td );
 348+ table.appendChild( tr );
 349+ }
 350+
 351+ summary.appendChild( table );
 352+ } else {
 353+ summary.appendChild( document.createTextNode( 'Useless type:'+elementType ) );
 354+ }
 355+}
260356
261357 // Collapsible stuff
262358 function createToggleCallback( id ){
@@ -263,6 +359,7 @@
264360 var content = document.getElementById( 'configure-biglist-content-'+id );
265361 var toggleLink = document.getElementById( 'configure-biglist-link-'+id );
266362 var div = document.getElementById( 'configure-biglist-placeholder-'+id );
 363+ var summary = document.getElementById( 'configure-biglist-summary-'+id );
267364 var act;
268365 var newLinkText;
269366 var newPlaceholderText;
@@ -271,11 +368,14 @@
272369 act = 'show';
273370 newLinkText = wgConfigureBiglistHide;
274371 content.style.display = 'block';
 372+ summary.style.display = 'none';
275373 newPlaceholderText = wgConfigureBiglistShown;
276374 } else {
277375 act = 'hide';
278376 newLinkText = wgConfigureBiglistShow;
279377 content.style.display = 'none';
 378+ summary.style.display = 'block';
 379+ summariseSetting( content, summary );
280380 newPlaceholderText = wgConfigureBiglistHidden
281381 }
282382

Status & tagging log