Difference between revisions of "MediaWiki:Gadget-calculator-search.js"
From WikiAnesthesia
Chris Rishel (talk | contribs) |
Chris Rishel (talk | contribs) |
||
| Line 42: | Line 42: | ||
mw.calculators.searchCalculations = function( searchText ) { | mw.calculators.searchCalculations = function( searchText ) { | ||
var $calculators = $( '.calculator' ); | var $calculators = $( '.calculator' ); | ||
var reSearch = new RegExp( searchText, 'im' ); | |||
$calculators.each( function() { | $calculators.each( function() { | ||
// Get the calculations contained by the calculator | |||
var $calculations = $( this ).find( '.calculator-calculation' ); | var $calculations = $( this ).find( '.calculator-calculation' ); | ||
// If no search text is defined or if the search text matches one of the search terms, | |||
// show the calculator and all calculations. | |||
var showCalculator = !searchText || reSearch.test( $( this ).attr( 'data-search' ) ); | |||
var showCalculation = !searchText || reSearch.test( $( this ).attr( 'data-search' ) ); | if( showCalculator ) { | ||
// If the entire calculator should be shown, show all calculations | |||
$calculations.each( function() { | |||
$( this ).show(); | |||
} ); | |||
} else { | |||
// If we aren't certain we should show the entire calculator, see if the search matches | |||
// any of the contained calculations. | |||
$calculations.each( function() { | |||
var showCalculation = !searchText || reSearch.test( $( this ).attr( 'data-search' ) ); | |||
if( showCalculation ) { | |||
$( this ).show(); | |||
// Make sure the calculator heading and structure gets shown | |||
showCalculator = true; | |||
} else { | |||
} | $( this ).hide(); | ||
} | } | ||
} ); | |||
} | |||
// If either the calculator or any contained calculations matched, show the calculator container | |||
// (i.e. title and structure for the calculations) | |||
if( showCalculator ) { | if( showCalculator ) { | ||
$( this ).show(); | $( this ).show(); | ||
Revision as of 01:35, 28 August 2021
/**
* @author Chris Rishel
*/
( function() {
mw.calculators.initializeSearch = function() {
var $searchContainer = $( '#calculator-search' );
if( !$searchContainer.length ) {
return;
}
var searchLabel = 'Search';
searchLabel += $searchContainer.attr( 'data-title' ) ? ' ' + $searchContainer.attr( 'data-title' ) : '';
searchLabel += ': ';
var searchLabelAttributes = {
for: 'calculator-search-input'
};
var $searchLabel = $( '<label>', searchLabelAttributes ).html( searchLabel );
var searchInputAttributes = {
id: 'calculator-search-input',
class: 'form-control form-control-sm',
type: 'text',
autocomplete: 'off'
};
var $searchInput = $( '<input>', searchInputAttributes )
.on( 'input', function() {
mw.calculators.searchCalculations( $( this ).val() );
} );
$searchContainer
.append( $( '<div>', {
class: 'form-group row'
} )
.append(
$searchLabel, $searchInput ) );
};
mw.calculators.searchCalculations = function( searchText ) {
var $calculators = $( '.calculator' );
var reSearch = new RegExp( searchText, 'im' );
$calculators.each( function() {
// Get the calculations contained by the calculator
var $calculations = $( this ).find( '.calculator-calculation' );
// If no search text is defined or if the search text matches one of the search terms,
// show the calculator and all calculations.
var showCalculator = !searchText || reSearch.test( $( this ).attr( 'data-search' ) );
if( showCalculator ) {
// If the entire calculator should be shown, show all calculations
$calculations.each( function() {
$( this ).show();
} );
} else {
// If we aren't certain we should show the entire calculator, see if the search matches
// any of the contained calculations.
$calculations.each( function() {
var showCalculation = !searchText || reSearch.test( $( this ).attr( 'data-search' ) );
if( showCalculation ) {
$( this ).show();
// Make sure the calculator heading and structure gets shown
showCalculator = true;
} else {
$( this ).hide();
}
} );
}
// If either the calculator or any contained calculations matched, show the calculator container
// (i.e. title and structure for the calculations)
if( showCalculator ) {
$( this ).show();
} else {
$( this ).hide();
}
} );
};
mw.calculators.initializeSearch();
}() );