Benutzer:MatthiasDD/ts diagnosis.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
// Diagnose sortierbarer Tabellen
//
// Nach rechts-Klick auf den Tabellenkopf wird die Sortierfunktion und alle Sortierschlüssel angezeigt,
// wenn angegeben erscheint oben das Attribut 'data-sort-type' mit Wert.
// Danach wird als Tooltip für jede Zelle einzeln die Sortierfunktion und der daraus folgende Sortierschlüssel angezeigt.

// This script uses code from MediaWiki 1.34.0-wmf.13
// https://phabricator.wikimedia.org/source/mediawiki/browse/master/resources/src/jquery.tablesorter/jquery.tablesorter.js
// last change: 2019-07-05  Update eslint-config-wikimedia to 0.13.0
//
( function () {
	// Nachdem der tablesorter geladen ist, müssen alle sortierbaren Tabellen noch initialisiert werden (mediawiki.page.ready.js)
	// deshalb startet diese Initialisierung hier verzögert.
	mw.loader.using( "jquery.tablesorter", function(){ setTimeout( ts_diagnosis_init, 500 ); } );

	function ts_diagnosis_init() {
	  if( $('table.sortable').length ){
		//tablesorter.construct() muss vorher ausgeführt sein!
		if( $('table.jquery-tablesorter').length ){
		  //jede sortierbare Tabelle bearbeiten
		  construct( $('table.sortable') );
		}else{
		  console.log("ts_diagnosis.js not initialised.");
		}
	  }
	}

	function getElementSortKey( node ) {
		// Get data-sort-value attribute. Uses jQuery to allow live value
		// changes from other code paths via data(), which reside only in jQuery.
		// Must use $().data() instead of $.data(), as the latter *only*
		// accesses the live values, without reading HTML5 attribs first (T40152).
		var data = $( node ).data( 'sortValue' );

		if ( data !== null && data !== undefined ) {
			// Cast any numbers or other stuff to a string, methods
			// like charAt, toLowerCase and split are expected.
			return String( data );
		}
		if ( node.tagName.toLowerCase() === 'img' ) {
			return node.alt;
		}
		// Iterate the NodeList (not an array).
		// Also uses null-return as filter in the same pass.
		// eslint-disable-next-line no-jquery/no-map-util
		return $.map( node.childNodes, function ( elem ) {
			if ( elem.nodeType === Node.ELEMENT_NODE ) {
				// eslint-disable-next-line no-jquery/no-class-state
				if ( $( elem ).hasClass( 'reference' ) ) {
					return null;
				}
				return getElementSortKey( elem );
			}
			if ( elem.nodeType === Node.TEXT_NODE ) {
				return elem.textContent;
			}
			// Ignore other node types, such as HTML comments.
			return null;
		} ).join( '' );
	}


//neu: ähnlich wie detectParserForColumn()
function detectParserForCell(table, rows, rowIndex, cellIndex){
	var parsers = $.tablesorter.getParsers(),
		l = parsers.length,
		nodeValue,
		i = 1;
		
	if ( rows[ rowIndex ] && rows[ rowIndex ].cells[ cellIndex ] ) {
		nodeValue = $.trim( getElementSortKey( rows[ rowIndex ].cells[ cellIndex ] ) );
	} else {
		nodeValue = '';
	}
	
	while ( i < l ){
		if ( nodeValue !== '' ){
			if ( parsers[i].is( nodeValue, table ) ){
				return parsers[i]; // Confirmed the parser, let's return it
			} else { 
				i++; // Check next parser
			}
		}
		else { return parsers[0]; } // Empty cell
	}
	// 0 is always the generic parser (text)
	return parsers[0];
}

//neu: ähnlich wie buildCache() 
function addTitleTags( table ) {
	var i, j, $row,
		totalRows = ( table.tBodies[0] && table.tBodies[0].rows.length) || 0,
		cellsInRow,
		config = $( table ).data( 'tablesorter' ).config,
		$node, out, data, cellParser; // extra Variable

	for ( i = 0; i < totalRows; ++i ) {

		$row = $( table.tBodies[0].rows[i] ) ;

		// if this is a child row, add it to the last row's children and
		// continue to the next row
		if ( $row.hasClass( config.cssChildRow ) ) {
			continue;
		}

		cellsInRow = ( $row.cells.length ) || 0;  //all cells in this row
		for ( j = 0; j < cellsInRow; ++j ) {
			$node = $row.cells[j];
			// wenn vorhanden, für jede Zelle das Attribut data-sort-value anzeigen 
			data = $node.attr( 'data-sort-value' );
			if ( data !== undefined ) {
				out = "data-sort-value = \"" +data +"\"\n";
			} else {
				out = "";
			}
			// für jede Zelle den Parser ermitteln
			cellParser = detectParserForCell( table, table.tBodies[0].rows, i, j );
			// die Parser.id und den vom Parser ermittelten Sortiertext anzeigen
			out += cellParser.id+": "+cellParser.format( getElementSortKey( row.cells[j] ) );
			//von explodeRowspans() angelegter Index
			//if ( $node.data( 'tablesorter' ) ) {
			//  out += ", realCellIndex: " +$node.data( 'tablesorter' ).realCellIndex;
			//}
			// dem Attribut title zuordnen
			$node.attr( 'title', out );
		}
	}
}
//end neu geschrieben

	/* Other utility functions */

	function buildCache( table ) {
		var i, j, $row, cols,
			totalRows = ( table.tBodies[ 0 ] && table.tBodies[ 0 ].rows.length ) || 0,
			config = $( table ).data( 'tablesorter' ).config,
			parsers = config.parsers,
			len = parsers.length,
			cellIndex,
			cache = {
				row: [],
				normalized: []
			};

		for ( i = 0; i < totalRows; i++ ) {

		// Add the table data to main data array
			$row = $( table.tBodies[ 0 ].rows[ i ] );
			cols = [];

			// if this is a child row, add it to the last row's children and
			// continue to the next row
			// eslint-disable-next-line no-jquery/no-class-state
			if ( $row.hasClass( config.cssChildRow ) ) {
				cache.row[ cache.row.length - 1 ] = cache.row[ cache.row.length - 1 ].add( $row );
				// go to the next for loop
				continue;
			}

			cache.row.push( $row );

			for ( j = 0; j < len; j++ ) {
				cellIndex = $row.data( 'columnToCell' )[ j ];
				cols.push( parsers[ j ].format( getElementSortKey( $row[ 0 ].cells[ cellIndex ] ) ) );
			}

			cols.push( cache.normalized.length ); // add position for rowCache
			cache.normalized.push( cols );
			cols = null;
		}

		return cache;
	}
//
//...
//

//neu: Initialisierung der Diagnose
	/**
	 * @param {jQuery} $tables
	 */
	function construct( $tables ) {
		return $tables.each( function ( i, table ) {
			// Declare and cache.
			var cache, config,
				$table = $( table ),
				firstTime = true;

			// Quit if no tbody
			if ( !table.tBodies ) {
				return
			}

			// Read the settings from tablesorter
			config = $.data( table, 'tablesorter').config;

			//Erweitere Event 'mousedown' für sortierbare header
			$table.find( '.' + config.cssHeader ).on( 'mousedown', function (e) {
				// Rechtsklick:
				if ( e.button==2 ) {
					//Beim ersten Klick muß 'config.parsers' ermittelt werden
					if ( firstTime ) {
						firstTime = false;
						if ( !config.parsers.length ) {
							//Diagnose is started before sorting.
							//sort() without sortList does noting, but call tablesorter's
							//setupForFirstSort() and config.parsers = buildParserCache() 
							$table.data( 'tablesorter' ).sort();
						}
						addTitleTags(table);
					}
					//Für diese Spalte die Parser.id und die Sortierwerte im alert()-Fenster anzeigen 
					var cell = this,
						$cell = $( cell ),
						j = config.headerToColumns[ $cell.data( 'headerIndex' ) ],
						//j = $cell.data( 'headerIndex' ),
						// Index of first column belonging to this header
						//j = columns[0],
						out = "", k,
					//aus BuildParserCache() (hier j)
						parser = false,
						sortType = $cell.data('sortType');
					if ( sortType !== undefined ) {
						out += "data-sort-type = \"" +sortType +"\"\n";
					}
					//erst wenn TH.'click' setupForFirstSort() aufgerufen hat ist parsers gefüllt
					if ( config.parsers.length ) {
						out += "Parser: " + config.parsers[ j[0] ].id;
						for ( k = 1; k < j.length; k++ ) {
							out += " | "  + config.parsers[ j[k] ].id;
						}
						out += "\n\n";
						cache = buildCache( table, config.parsers );
						var totalRows = cache.normalized.length;
						for ( i = 0; i < totalRows; i++ ) {
							out += cache.normalized[ i ][ j[0] ];
							for ( k = 1; k < j.length; k++ ) {
								out += "\t"  + cache.normalized[ i ][ j[k] ];
							}
							out += "\n"
						}
					} else {
						out += 'setupForFirstSort() has config.parsers not set.'; 
					}
					alert(out);
				}
			}); //.on
		});
	} //end construct

//
//...
//

}() );