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

From WikiAnesthesia
 
(34 intermediate revisions by the same user not shown)
Line 10: Line 10:
         }
         }


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


         var searchLabelAttributes = {
         var $searchHeading = $( '<h4>' ).append( searchHeading );
            class: 'calculator-search-label',
 
            for: 'calculator-search-input'
         var searchPlaceholderText;
         };


         var $searchLabel = $( '<label>', searchLabelAttributes ).html( searchLabel );
         if( $searchContainer.data( 'search-placeholder' ) ){
            searchPlaceholderText = $searchContainer.data( 'search-placeholder' );
        } else {
            searchPlaceholderText = 'Search ';
            searchPlaceholderText += $searchContainer.data( 'title' ) ? $searchContainer.data( 'title' ) : 'calculations';
        }


         var searchInputAttributes = {
         var searchInputAttributes = {
Line 25: Line 27:
             class: 'form-control form-control-sm',
             class: 'form-control form-control-sm',
             type: 'text',
             type: 'text',
            placeholder: searchPlaceholderText,
             autocomplete: 'off'
             autocomplete: 'off'
         };
         };
Line 31: Line 34:
             .on( 'input', function() {
             .on( 'input', function() {
                 mw.calculators.searchCalculations( $( this ).val() );
                 mw.calculators.searchCalculations( $( this ).val() );
            } )
            .on( 'keypress', function( e ) {
                if( event.keyCode === 13 ) {
                    e.preventDefault();
                    return false;
                }
             } );
             } );
        $searchContainer.append( $searchHeading );


         $searchContainer
         $searchContainer
             .append( $( '<form>', {
             .append( $( '<form>' )
                class: 'form-inline'
            } )
                 .append(
                 .append(
                     $searchLabel, $searchInput ) );
                     $searchInput ) );
 
        if( $( mw.calculators.selectors.calculationCategories ).length ) {
            $searchContainer.append( $( '<form>', {
                id: 'calculator-search-category-form'
            } ) );
 
 
            var categoryPlaceholderText;
 
            if( $searchContainer.data( 'category-placeholder' ) ){
                categoryPlaceholderText = $searchContainer.data( 'category-placeholder' );
            } else {
                categoryPlaceholderText = '(Show all categories)';
            }
 
            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: categoryPlaceholderText,
                value: ''
            } ) );
 
            $( mw.calculators.selectors.calculationCategories ).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 ) {
     mw.calculators.searchCalculations = function( searchText ) {
        var $calculators = $( '.calculator' );
         var reSearch = new RegExp( searchText, 'im' );
         var reSearch = new RegExp( searchText, 'im' );


         $calculators.each( function() {
        // Search categories and calculations contained within categories
             // Get the calculations contained by the calculator
         $( mw.calculators.selectors.calculationCategories ).each( function() {
             var $calculations = $( this ).find( '.calculator-calculation' );
             // Get the calculations contained by the category
             var $calculations = $( this ).find( mw.calculators.selectors.calculations );


             // 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 category and all calculations.
             var showCalculator = !searchText || reSearch.test( $( this ).attr( '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 category should be shown, show all calculations
                 $calculations.each( function() {
                 $calculations.each( function() {
                     $( this ).show();
                     $( this ).show();
                 } );
                 } );
             } else {
             } else {
                 // If we aren't certain we should show the entire calculator, see if the search matches
                 // If we aren't certain we should show the entire category, see if the search matches
                 // any of the contained calculations.
                 // any of the contained calculations.
                 $calculations.each( function() {
                 $calculations.each( function() {
                     var showCalculation = !searchText || reSearch.test( $( this ).attr( 'data-search' ) );
                     var showCalculation = !searchText || reSearch.test( $( this ).data( 'search' ) );


                     if( showCalculation ) {
                     if( showCalculation ) {
Line 69: Line 122:


                         // 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 78: Line 131:
             // 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();
            } else {
                $( this ).hide();
            }
        } );
 
        // Search calculations not contained by a category.
        $( mw.calculators.selectors.calculations ).not( mw.calculators.selectors.calculationCategories  + ' ' + mw.calculators.selectors.calculations ).each(
            function() {
                if( !searchText || reSearch.test( $( this ).data( 'search' ) ) ) {
                    $( this ).show();
                } else {
                    $( this ).hide();
                }
            }
        );
    };
 
    mw.calculators.selectCategory = function( calculationCategoryId ) {
        $( mw.calculators.selectors.calculationCategories ).each( function() {
            if( !calculationCategoryId || $( this ).data( 'id' ) === calculationCategoryId ) {
                 $( this ).show();
                 $( this ).show();
             } else {
             } else {

Latest revision as of 16:40, 5 April 2022

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

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

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

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

        var searchPlaceholderText;

        if( $searchContainer.data( 'search-placeholder' ) ){
            searchPlaceholderText = $searchContainer.data( 'search-placeholder' );
        } else {
            searchPlaceholderText = 'Search ';
            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() );
            } )
            .on( 'keypress', function( e ) {
                if( event.keyCode === 13 ) {
                    e.preventDefault();
                    return false;
                }
            } );

        $searchContainer.append( $searchHeading );

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

        if( $( mw.calculators.selectors.calculationCategories ).length ) {
            $searchContainer.append( $( '<form>', {
                id: 'calculator-search-category-form'
            } ) );


            var categoryPlaceholderText;

            if( $searchContainer.data( 'category-placeholder' ) ){
                categoryPlaceholderText = $searchContainer.data( 'category-placeholder' );
            } else {
                categoryPlaceholderText = '(Show all categories)';
            }

            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: categoryPlaceholderText,
                value: ''
            } ) );

            $( mw.calculators.selectors.calculationCategories ).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 reSearch = new RegExp( searchText, 'im' );

        // Search categories and calculations contained within categories
        $( mw.calculators.selectors.calculationCategories ).each( function() {
            // Get the calculations contained by the category
            var $calculations = $( this ).find( mw.calculators.selectors.calculations );

            // If no search text is defined or if the search text matches one of the search terms,
            // show the category and all calculations.
            var showCalculationCategory = !searchText || reSearch.test( $( this ).data( 'search' ) );

            if( showCalculationCategory ) {
                // If the entire category should be shown, show all calculations
                $calculations.each( function() {
                    $( this ).show();
                } );
            } else {
                // If we aren't certain we should show the entire category, 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();
            }
        } );

        // Search calculations not contained by a category.
        $( mw.calculators.selectors.calculations ).not( mw.calculators.selectors.calculationCategories  + ' ' + mw.calculators.selectors.calculations ).each(
            function() {
                if( !searchText || reSearch.test( $( this ).data( 'search' ) ) ) {
                    $( this ).show();
                } else {
                    $( this ).hide();
                }
            }
        );
    };

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

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