User:Bene*/statementfilter.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/**
 * Gadget which creates a quick filter to find statements by their labels on item pages.
 *
 * @author [[User:Bene*]]
 */
( function( $, mw ) {
	'use strict';

	if ( !mw.config.exists( 'wbEntity' ) ) {
		return;
	}

	var html = '<div class="statements-filter">\
  					<input type="text" placeholder="Filter by label" id="statements-filter-input" style="width: 100%; font-size: 1.2em; margin: 10px 0px;">\
				</div>';

	function matches( label, filters ) {
		label = $.trim( label.toLowerCase() );
		for ( var i in filters ) {
			if ( label.indexOf( filters[i] ) >= 0  ) {
				return true;
			}
		}
		return false;
	}

	function filter() {
		var filters = $( '#statements-filter-input' ).val().toLowerCase().split( /\s*,\s*/ );
		$( '.wikibase-statementgroupview' ).each( function() {
			var label = $( this ).find( '.wikibase-statementgroupview-property-label a' ).text();
			if ( matches( label, filters ) ) {
				$( this ).show();
			} else {
				$( this ).hide();
			}
		} );
	}

	$( function() {
		$( html ).insertAfter( '#claims' );
		$( '#statements-filter-input' ).on( 'input', filter );
	} );

} )( jQuery, mediaWiki );