Difference between revisions of "MediaWiki:Gadget-calculator-search.js"

From WikiAnesthesia
Line 14: Line 14:


         var $searchHeading = $( '<h4>' ).append( searchHeading );
         var $searchHeading = $( '<h4>' ).append( searchHeading );
        var searchPlaceholderText = 'Search ';
        if( $searchContainer.data( 'search-placeholder' ) ){
            searchPlaceholderText += $searchContainer.data( 'search-placeholder' );
        } else {
            searchPlaceholderText += 'calculations';
        }


         var searchInputAttributes = {
         var searchInputAttributes = {
Line 19: Line 27:
             class: 'form-control form-control-sm',
             class: 'form-control form-control-sm',
             type: 'text',
             type: 'text',
             placeholder: 'Enter search terms',
             placeholder: searchPlaceholderText,
             autocomplete: 'off'
             autocomplete: 'off'
         };
         };
Line 50: Line 58:
                 } );
                 } );


             var categorySelectPlaceholderText = '';
             var categorySelectPlaceholderText;


             if( $searchContainer.data( 'title' ) ){
             if( $searchContainer.data( 'title' ) ){
Line 57: Line 65:
                 categorySelectPlaceholderText = '(All calculations)';
                 categorySelectPlaceholderText = '(All calculations)';
             }
             }
           
 
             $categorySelectInput.append( $( '<option>', {
             $categorySelectInput.append( $( '<option>', {
                 text: categorySelectPlaceholderText,
                 text: categorySelectPlaceholderText,

Revision as of 23:26, 7 September 2021

/**
 * @author Chris Rishel
 */
( function() {
    mw.calculators.initializeSearch = function() {
        var $searchContainer = $( '#calculator-search' );

        if( !$searchContainer.length ) {
            return;
        }

        var searchHeading = 'Search';
        searchHeading += $searchContainer.data( 'title' ) ? ' ' + $searchContainer.data( 'title' ) : '';

        var $searchHeading = $( '<h4>' ).append( searchHeading );

        var searchPlaceholderText = 'Search ';

        if( $searchContainer.data( 'search-placeholder' ) ){
            searchPlaceholderText += $searchContainer.data( 'search-placeholder' );
        } else {
            searchPlaceholderText += 'calculations';
        }

        var searchInputAttributes = {
            id: 'calculator-search-input',
            class: 'form-control form-control-sm',
            type: 'text',
            placeholder: searchPlaceholderText,
            autocomplete: 'off'
        };

        var $searchInput = $( '<input>', searchInputAttributes )
            .on( 'input', function() {
                mw.calculators.searchCalculations( $( this ).val() );
            } );

        $searchContainer.append( $searchHeading );

        $searchContainer
            .append( $( '<form>' )
                .append(
                    $searchInput ) );

        $searchContainer.append( $( '<form>', {
            id: 'calculator-search-category-form'
        } ) );

        mw.trackSubscribe( 'mw.calculators.CalculatorRendered', function() {
            var categorySelectAttributes = {
                id: 'calculator-category-input',
                class: 'custom-select custom-select-sm'
            };

            var $categorySelectInput = $( '<select>', categorySelectAttributes )
                .on( 'change', function() {
                    mw.calculators.selectCategory( $( this ).val() );
                } );

            var categorySelectPlaceholderText;

            if( $searchContainer.data( 'title' ) ){
                categorySelectPlaceholderText = '(All ' + $searchContainer.data( 'title' ) + ')';
            } else {
                categorySelectPlaceholderText = '(All calculations)';
            }

            $categorySelectInput.append( $( '<option>', {
                text: categorySelectPlaceholderText,
                value: ''
            } ) );

            $( '.calculator' ).each( function() {
                $categorySelectInput.append( $( '<option>', {
                    value: $( this ).data( 'id' ),
                    html: $( this ).data( 'title' )
                } ) );
            } );

            $( '#calculator-search-category-form' )
                .replaceWith( $( '<form>', {
                    id: 'calculator-search-category-form'
                } )
                    .append(
                        $categorySelectInput ) );
        } );
    };

    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 ).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 ).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.selectCategory = function( calculatorId ) {
        $( '.calculator' ).each( function() {
            if( !calculatorId || $( this ).data( 'id' ) === calculatorId ) {
                $( this ).show();
            } else {
                $( this ).hide();
            }
        } );
    };

    mw.calculators.initializeSearch();
}() );