http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95053
Revision: 95053
Author: jeroendedauw
Date: 2011-08-19 23:13:01 +0000 (Fri, 19 Aug 2011)
Log Message:
-----------
rewrote most of the table QP and fixed numeric sorting JS
Modified Paths:
--------------
trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php
trunk/extensions/SemanticMediaWiki/skins/SMW_sorttable.js
Modified:
trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php
===================================================================
--- trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php
2011-08-19 23:12:46 UTC (rev 95052)
+++ trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Table.php
2011-08-19 23:13:01 UTC (rev 95053)
@@ -13,6 +13,8 @@
*/
class SMWTableResultPrinter extends SMWResultPrinter {
+ protected $columnsWithSortKey = array();
+
public function getName() {
smwfLoadExtensionMessages( 'SemanticMediaWiki' );
return wfMsg( 'smw_printername_' . $this->mFormat );
@@ -22,58 +24,39 @@
global $smwgIQRunningNumber;
SMWOutputs::requireHeadItem( SMW_HEADER_SORTTABLE );
+ $tableRows = array();
+
+ while ( $record = $res->getNext() ) {
+ $tableRows[] = $this->getRowForRecord( $record,
$outputmode );
+ }
+
// print header
$result = '<table class="smwtable"' .
( $this->mFormat == 'broadtable' ? ' width="100%"' :
'' ) .
" id=\"querytable$smwgIQRunningNumber\">\n";
if ( $this->mShowHeaders != SMW_HEADERS_HIDE ) { // building
headers
- $result .= "\t<tr>\n";
+ $headers = array();
foreach ( $res->getPrintRequests() as $pr ) {
- $result .= "\t\t<th>" . $pr->getText(
$outputmode, ( $this->mShowHeaders == SMW_HEADERS_PLAIN ? null:$this->mLinker )
) . "</th>\n";
+ $attribs = array();
+
+ if ( array_key_exists( $pr->getHash(),
$this->columnsWithSortKey ) ) {
+ $attribs['class'] = 'numericsort';
+ }
+
+ $headers[] = Html::rawElement(
+ 'th',
+ $attribs,
+ $pr->getText( $outputmode, (
$this->mShowHeaders == SMW_HEADERS_PLAIN ? null:$this->mLinker ) )
+ );
}
- $result .= "\t</tr>\n";
+ array_unshift( $tableRows, '<tr>' . implode( "\n",
$headers ) . '</tr>' );
}
- // print all result rows
- while ( $row = $res->getNext() ) {
- $result .= "\t<tr>\n";
- $firstcol = true;
- $fieldcount = - 1;
- foreach ( $row as $field ) {
- $fieldcount = $fieldcount + 1;
-
- $result .= "\t\t<td";
- $alignment = trim(
$field->getPrintRequest()->getParameter( 'align' ) );
- if ( ( $alignment == 'right' ) || ( $alignment
== 'left' ) || ( $alignment == 'center' ) ) {
- $result .= ' style="text-align:' .
$alignment . ';"';
- }
- $result .= ">";
-
- $first = true;
- while ( ( $dv = $field->getNextDataValue() )
!== false ) {
- if ( $first ) {
- $sortkey =
$dv->getDataItem()->getSortKey();
- if ( is_numeric( $sortkey ) ) {
// additional hidden sortkey for numeric entries
- $result .= '<span
class="smwsortkey">' . $sortkey . '</span>';
- }
- $first = false;
- } else {
- $result .= '<br />';
- }
- // use shorter "LongText" for wikipage
- $result .= ( ( $dv->getTypeID() ==
'_wpg' ) || ( $dv->getTypeID() == '__sin' ) ) ?
- $dv->getLongText(
$outputmode, $this->getLinker( $firstcol ) ) :
- $dv->getShortText(
$outputmode, $this->getLinker( $firstcol ) );
- }
- $result .= "</td>\n";
- $firstcol = false;
- }
- $result .= "\t</tr>\n";
- }
-
+ $result .= implode( "\n", $tableRows );
+
// print further results footer
if ( $this->linkFurtherResults( $res ) ) {
$link = $res->getQueryLink();
@@ -82,11 +65,67 @@
}
$result .= "\t<tr class=\"smwfooter\"><td
class=\"sortbottom\" colspan=\"" . $res->getColumnCount() . '"> ' .
$link->getText( $outputmode, $this->mLinker ) . "</td></tr>\n";
}
+
$result .= "</table>\n"; // print footer
$this->isHTML = ( $outputmode == SMW_OUTPUT_HTML ); // yes, our
code can be viewed as HTML if requested, no more parsing needed
+
return $result;
}
+ protected function getRowForRecord( array /* of SMWResultArray */
$record, $outputmode ) {
+ $cells = array();
+
+ foreach ( $record as $field ) {
+ $cells[] = $this->getCellForPropVals( $field,
$outputmode );
+ }
+
+ return "<tr>\n\t" . implode( "\n\t", $cells ) . "\n</tr>";
+ }
+
+ protected function getCellForPropVals( SMWResultArray $resultArray,
$outputmode ) {
+ $attribs = array();
+
+ $alignment = trim(
$resultArray->getPrintRequest()->getParameter( 'align' ) );
+
+ if ( in_array( $alignment, array( 'right', 'left', 'center' ) )
) {
+ $attribs['style'] = "text-align:' . $alignment . ';";
+ }
+
+ return Html::rawElement(
+ 'td',
+ $attribs,
+ $this->getCellContent( $resultArray, $outputmode )
+ );
+ }
+
+ protected function getCellContent( SMWResultArray $resultArray,
$outputmode ) {
+ $values = array();
+ $isFirst = true;
+
+ while ( ( $dv = $resultArray->getNextDataValue() ) !== false ) {
+ $sortKey = '';
+
+ if ( $isFirst ) {
+ $isFirst = false;
+ $sortkey = $dv->getDataItem()->getSortKey();
+
+ if ( is_numeric( $sortkey ) ) { // additional
hidden sortkey for numeric entries
+
$this->columnsWithSortKey[$resultArray->getPrintRequest()->getHash()] = true;
+ $sortKey .= '<span class="smwsortkey">'
. $sortkey . '</span>';
+ }
+ }
+
+ $isSubject = $resultArray->getPrintRequest()->getMode()
== SMWPrintRequest::PRINT_THIS;
+ $value = ( ( $dv->getTypeID() == '_wpg' ) || (
$dv->getTypeID() == '__sin' ) ) ?
+ $dv->getLongText( $outputmode,
$this->getLinker( $isSubject ) ) :
+ $dv->getShortText( $outputmode,
$this->getLinker( $isSubject ) );
+
+ $values[] = $sortKey . $value;
+ }
+
+ return implode( '<br />', $values );
+ }
+
public function getParameters() {
return array_merge( parent::getParameters(),
parent::textDisplayParameters() );
}
Modified: trunk/extensions/SemanticMediaWiki/skins/SMW_sorttable.js
===================================================================
--- trunk/extensions/SemanticMediaWiki/skins/SMW_sorttable.js 2011-08-19
23:12:46 UTC (rev 95052)
+++ trunk/extensions/SemanticMediaWiki/skins/SMW_sorttable.js 2011-08-19
23:13:01 UTC (rev 95053)
@@ -126,17 +126,12 @@
return;
}
- sortfn = smw_sort_caseinsensitive; // sorting w/o keys
- // check for sorting keys and change sorting function
- var itm = table.rows[1].cells[column];
- var spans = itm.getElementsByTagName( 'span' );
- if( spans.length > 0 ) {
- for ( var i = 0; i < spans.length; i++ ) {
- if( spans[i].className == 'smwsortkey' ) {
- sortfn = smw_sort_numeric; // sorting with keys
- }
- }
+ if ( table.rows[0].cells[column].className == 'numericsort' ) {
+ sortfn = smw_sort_numeric; // sorting with keys
}
+ else {
+ sortfn = smw_sort_caseinsensitive;
+ }
SORT_COLUMN_INDEX = column;
var firstRow = new Array();
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs