r95884 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95883‎ | r95884 | r95885 >
Date:16:31, 31 August 2011
Author:yaron
Status:deferred
Tags:
Comment:
Added more CSS classes, to enable customization of table appearance - new 'class' parameter sets additional class(es) for main table, while all columns and rows now have their own CSS class (row classes differentiate between even- and odd-numbered rows)
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php
@@ -11,30 +11,50 @@
1212 */
1313 class SMWTableResultPrinter extends SMWResultPrinter {
1414
 15+ protected $mHTMLClass = '';
 16+
 17+ public function __construct( $format, $inline, $useValidator = true ) {
 18+ parent::__construct( $format, $inline );
 19+ $this->useValidator = $useValidator;
 20+ }
 21+
1522 public function getName() {
1623 smwfLoadExtensionMessages( 'SemanticMediaWiki' );
1724 return wfMsg( 'smw_printername_' . $this->mFormat );
1825 }
1926
 27+ /**
 28+ * @see SMWResultPrinter::handleParameters
 29+ *
 30+ * @since 1.6
 31+ *
 32+ * @param array $params
 33+ * @param $outputmode
 34+ */
 35+ protected function handleParameters( array $params, $outputmode ) {
 36+ parent::handleParameters( $params, $outputmode );
 37+ if ( array_key_exists( 'class', $params ) ) {
 38+ $this->mHTMLClass = $params['class'];
 39+ }
 40+ }
 41+
2042 protected function getResultText( SMWQueryResult $res, $outputmode ) {
2143 global $wgVersion;
2244
2345 $tableRows = array();
2446
 47+ $rowNum = 1;
2548 while ( $subject = $res->getNext() ) {
26 - $tableRows[] = $this->getRowForSubject( $subject, $outputmode );
 49+ $tableRows[] = $this->getRowForSubject( $subject, $outputmode, $rowNum++ );
2750 }
2851
29 - // print header
30 - $result = '<table class="sortable wikitable"' .
31 - ( $this->mFormat == 'broadtable' ? ' width="100%"' : '' ) .
32 - ">\n";
33 -
3452 if ( $this->mShowHeaders != SMW_HEADERS_HIDE ) { // building headers
3553 $headers = array();
3654
3755 foreach ( $res->getPrintRequests() as $pr ) {
3856 $attribs = array();
 57+ $columnClass = str_replace( array( ' ', '_' ), '-', $pr->getText( SMW_OUTPUT_WIKI ) );
 58+ $attribs['class'] = $columnClass;
3959
4060 $headers[] = Html::rawElement(
4161 'th',
@@ -71,7 +91,18 @@
7292 $result .= "\t<tr class=\"smwfooter\"><td class=\"sortbottom\" colspan=\"" . $res->getColumnCount() . '"> ' . $link->getText( $outputmode, $this->mLinker ) . "</td></tr>\n";
7393 }
7494
75 - $result .= "</table>\n"; // print footer
 95+ // Put the <table> tag around the whole thing
 96+ // print header
 97+ $tableClass = "sortable wikitable";
 98+ if ( !empty( $this->mHTMLClass ) ) {
 99+ $tableClass .= ' ' . $this->mHTMLClass;
 100+ }
 101+ $tableAttrs = array( 'class' => $tableClass );
 102+ if ( $this->mFormat == 'broadtable' ) {
 103+ $tableAttrs['width'] = '100%';
 104+ }
 105+ $result = Xml::tags( 'table', $tableAttrs, $result );
 106+
76107 $this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our code can be viewed as HTML if requested, no more parsing needed
77108
78109 return $result;
@@ -87,14 +118,15 @@
88119 *
89120 * @return string
90121 */
91 - protected function getRowForSubject( array /* of SMWResultArray */ $subject, $outputmode ) {
 122+ protected function getRowForSubject( array /* of SMWResultArray */ $subject, $outputmode, $rowNum ) {
92123 $cells = array();
93124
94125 foreach ( $subject as $field ) {
95126 $cells[] = $this->getCellForPropVals( $field, $outputmode );
96127 }
97128
98 - return "<tr>\n\t" . implode( "\n\t", $cells ) . "\n</tr>";
 129+ $rowClass = ( $rowNum % 2 == 1 ) ? 'row-odd' : 'row-even';
 130+ return "<tr class=\"$rowClass\">\n\t" . implode( "\n\t", $cells ) . "\n</tr>";
99131 }
100132
101133 /**
@@ -170,7 +202,10 @@
171203 }
172204
173205 public function getParameters() {
174 - return array_merge( parent::getParameters(), parent::textDisplayParameters() );
 206+ $params = array_merge( parent::getParameters(), parent::textDisplayParameters() );
 207+ $params['class'] = new Parameter( 'class', Parameter::TYPE_STRING );
 208+ $params['class']->setMessage( 'smw-paramdesc-table-class' );
 209+ return $params;
175210 }
176211
177212 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r95885followup to r95884jeroendedauw17:01, 31 August 2011