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

From WikiAnesthesia
Line 47: Line 47:
         } ) );
         } ) );


         mw.trackSubscribe( 'mw.calculators.CalculatorRendered', function() {
         var categorySelectAttributes = {
            var categorySelectAttributes = {
            id: 'calculator-category-input',
                id: 'calculator-category-input',
            class: 'custom-select custom-select-sm'
                class: 'custom-select custom-select-sm'
        };
             };
 
        var $categorySelectInput = $( '<select>', categorySelectAttributes )
            .on( 'change', function() {
                mw.calculators.selectCategory( $( this ).val() );
             } );


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


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


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


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


     mw.calculators.searchCalculations = function( searchText ) {
     mw.calculators.searchCalculations = function( searchText ) {
         var $calculators = $( '.calculator' );
         var $calculationCategories = $( '.calculator-calculationcategory' );


         var reSearch = new RegExp( searchText, 'im' );
         var reSearch = new RegExp( searchText, 'im' );


         $calculators.each( function() {
         $calculationCategories.each( function() {
             // Get the calculations contained by the calculator
             // Get the calculations contained by the calculator
             var $calculations = $( this ).find( '.calculator-calculation' );
             var $calculations = $( this ).find( '.calculator-calculation' );
Line 91: Line 89:
             // If no search text is defined or if the search text matches one of the search terms,
             // If no search text is defined or if the search text matches one of the search terms,
             // show the calculator and all calculations.
             // show the calculator and all calculations.
             var showCalculator = !searchText || reSearch.test( $( this ).data( 'search' ) );
             var showCalculationCategory = !searchText || reSearch.test( $( this ).data( 'search' ) );


             if( showCalculator ) {
             if( showCalculationCategory ) {
                 // If the entire calculator should be shown, show all calculations
                 // If the entire calculator should be shown, show all calculations
                 $calculations.each( function() {
                 $calculations.each( function() {
Line 108: Line 106:


                         // Make sure the calculator heading and structure gets shown
                         // Make sure the calculator heading and structure gets shown
                         showCalculator = true;
                         showCalculationCategory = true;
                     } else {
                     } else {
                         $( this ).hide();
                         $( this ).hide();
Line 117: Line 115:
             // If either the calculator or any contained calculations matched, show the calculator container
             // If either the calculator or any contained calculations matched, show the calculator container
             // (i.e. title and structure for the calculations)
             // (i.e. title and structure for the calculations)
             if( showCalculator ) {
             if( showCalculationCategory ) {
                 $( this ).show();
                 $( this ).show();
             } else {
             } else {
Line 125: Line 123:
     };
     };


     mw.calculators.selectCategory = function( calculatorId ) {
     mw.calculators.selectCategory = function( calculationCategoryId ) {
         $( '.calculator' ).each( function() {
         $( '.calculator-calculationcategory' ).each( function() {
             if( !calculatorId || $( this ).data( 'id' ) === calculatorId ) {
             if( !calculationCategoryId || $( this ).data( 'id' ) === calculationCategoryId ) {
                 $( this ).show();
                 $( this ).show();
             } else {
             } else {

Revision as of 15:28, 19 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 += $searchContainer.data( 'title' ) ? $searchContainer.data( 'title' ) : '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'
        } ) );

        var categorySelectAttributes = {
            id: 'calculator-category-input',
            class: 'custom-select custom-select-sm'
        };

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


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

        $( '.calculator-calculationcategory' ).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 $calculationCategories = $( '.calculator-calculationcategory' );

        var reSearch = new RegExp( searchText, 'im' );

        $calculationCategories.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 showCalculationCategory = !searchText || reSearch.test( $( this ).data( 'search' ) );

            if( showCalculationCategory ) {
                // 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
                        showCalculationCategory = 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( showCalculationCategory ) {
                $( this ).show();
            } else {
                $( this ).hide();
            }
        } );
    };

    mw.calculators.selectCategory = function( calculationCategoryId ) {
        $( '.calculator-calculationcategory' ).each( function() {
            if( !calculationCategoryId || $( this ).data( 'id' ) === calculationCategoryId ) {
                $( this ).show();
            } else {
                $( this ).hide();
            }
        } );
    };

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