Gists

6 months ago
<?php
add_action( 'facetwp_scripts', function() {
    ?>
        <script>
        (function($) {
            document.addEventListener('facetwp-loaded', function() {
                $('.facetwp-facet').each(function() {
                    var facet = $(this);
                    var facet_name = facet.attr('data-name');
                    var facet_type = facet.attr('data-type');
                    var facet_label = FWP.settings.labels[facet_name];
                    if (facet_type !== 'pager' && facet_type !== 'sort'  && facet_type !== 'reset') {
                        if (facet.closest('.facet-wrap').len() < 1 && facet.closest('.facetwp-flyout').len() < 1) {
                            facet.prepend('<h3 class="facet-label">' + facet_label + '</h3>');
                        }
                    }
                });
            });
        })(fUtil);
        </script>
<?php
}, 100 );
3 weeks ago
<?php
// Temporary fix to remove accessibility attributes from the Pager facets "dots" element.
add_action( 'facetwp_scripts', function() {
  ?>
  <script>
    (function($) {
      if ('undefined' !== typeof FWP.hooks) {
        FWP.hooks.addAction('facetwp/loaded', function() {
          $('.facetwp-page').each(function() {
            let el = $(this);
            if (el.hasClass('dots')) {
              el.nodes[0].removeAttribute('role');
              el.nodes[0].removeAttribute('aria-label');
              el.nodes[0].removeAttribute('tabindex');
            }
          });
        }, 1000);
      }
    })(fUtil);
  </script>
  <?php
}, 100 );
3 weeks ago
<?php
add_action('facetwp_scripts', function () {
  ?>
  <script>
    FWP.hooks.addAction('facetwp/loaded', function() {

      /** 1. add an aria-label attribute to input */
      
        fUtil('.facetwp-per-page-select').each(function() {
        fUtil(this).attr('aria-label', 'Set the number of results per page');
      });

      /** 2. prepend a <label for="id"> element to the <select> element and add an 'id' to the <select>  */

      let select = document.querySelector('.facetwp-per-page-select');
        
      // set 'id' attribute for select
      select.setAttribute('id', 'set-results-per-page');

      // create label element
      let label = document.createElement('label');

      // set 'for' attribute for label: must me same as 'id' of the select element.
      label.setAttribute('for', 'set-results-per-page');

      // insert label before select in the DOM tree
      select.parentNode.insertBefore(label, select);

      // add label text
      label.textContent = "Set the number of results per page";

    }, 1000);
  </script>
  <?php
}, 100);
1 month ago
<?php
// Changes 'mi' to 'miles' in a Proximity facet's radius dropdown
add_action('facetwp_scripts', function () {
  ?>
  <script>
    (function($) {
      document.addEventListener('facetwp-loaded', function() {
        let radiusdropdown = document.querySelector('.facetwp-radius-dropdown');
        radiusdropdown.querySelectorAll('option').forEach(option => {
          if (option.textContent.trim().endsWith('mi')) {
            const newOptionText = option.textContent.replace(/mi\b/g, 'miles'); // Replace 'mi' with 'miles' if it is at the end of the option string
            option.textContent = newOptionText;
          }
        });
      });
    })(fUtil);
  </script>
  <?php
}, 100);
1 month ago
<?php
add_action('facetwp_scripts', function () {
  ?>
  <script>
    (function($) {
      document.addEventListener('facetwp-refresh', function() {
        if ( 'undefined' != typeof FWP_MAP && true === FWP_MAP.is_filtering) {
          $('.facetwp-location').val(''); // reset the value of the input field
          $('.facetwp-lat').val(''); // reset hidden field
          $('.facetwp-lng').val(''); // reset hidden field
          $('.facetwp-radius option').each(function () {
            if (this.defaultSelected) {
              this.selected = true;
              return false;
            } // reset default radius if it is a dropdown
          });
          FWP.facets['my_proximity_facet'] = []; // change 'my_proximity_facet' to name of your Proximity facet
        }
      });
    })(fUtil);
  </script>
  <?php
}, 100);
1 month ago
<?php
add_action('facetwp_scripts', function () {
?>
    <script>
        FWP.hooks.addAction('facetwp/loaded', function() {

            /** adds aria-label to input */
            fUtil('.facetwp-type-fselect .fs-search input').each(function() {
                fUtil(this).attr('aria-label', 'this is the label');
            });

            /** prepends <label> and text to input  */

            // element that will be wrapped
            var el = document.querySelector('.facetwp-type-fselect .fs-search input');

            // create wrapper container
            var wrapper = document.createElement('label');

            // insert wrapper before el in the DOM tree
            el.parentNode.insertBefore(wrapper, el);

            // add label text
            wrapper.textContent = "This is the label";

        }, 1000);
    </script>
<?php
}, 100);
2 months ago
<?php
/** turn off map filtering when resetting the map facet **/
add_action('facetwp_scripts', function () {
?>
    <script>        
        (function($) {
            FWP.hooks.addAction('facetwp/reset', function() {
                $.each(FWP.facet_type, function(type, name) {
                    if ('map' === type) {
                        var $button = $('.facetwp-map-filtering');
                        $button.text(FWP_JSON['map']['filterText']);
                        FWP_MAP.is_filtering = false;
                        $button.toggleClass('enabled');
                    }
                });
            });
        })(fUtil);	
    </script>
<?php
}, 100);
2 months ago
<?php
// Hides a facet if there is only one option left
// Replace 'my_facet_name' with the name of your facet (4x)
add_action( 'facetwp_scripts', function() { ?>
  <script>
    (function($) {
      document.addEventListener('facetwp-loaded', function() {
        if ( FWP.settings.num_choices.my_facet_name !== 'undefined' && FWP.settings.num_choices.my_facet_name < 2 ) {
          $('.facetwp-facet-my_facet_name').addClass( 'facetwp-hidden' );
        } else {
          $('.facetwp-facet-my_facet_name').removeClass( 'facetwp-hidden' );
        }
      });
    })(fUtil);
  </script>
<?php } );
3 months ago
<?php
/**
 ** change facet1 to the name of your first facet (the facet to check selection)
 ** facet2 to the name of your second facet (to be disabled)
 **/

add_action( 'facetwp_scripts', function() {
?>
<script>
    (function($) {
        document.addEventListener('facetwp-loaded', function() {
            if ( 'undefined' !== typeof FWP.facets['facet1'] && FWP.facets['facet1'].length > 0 ) {
                $( '.facetwp-facet-facet2 select' ).attr('disabled', 'disabled');
            }
        });
    })(fUtil);
</script>
<?php
}, 100 );
4 months ago
<?php
/** captures pagination for use with facet
 ** pager must be within facetwp-template class
 ** change 'a.page-numbers' to match element of the links ln 12
 ** ln 14 matches on page link format of /page/x/, change if needed
 **/

add_action( 'facetwp_scripts', function() {
    ?>
    <script>
        (function($) {
            $().on('click', 'a.page-numbers', function(e) {
                e.preventDefault();
                var matches = $(this).attr('href').match(/\/page\/(\d+)/);
                if (null !== matches) {
                    FWP.paged = parseInt(matches[1]);
                    FWP.soft_refresh = true;
                    FWP.refresh();
                }
            });
        })(fUtil);
    </script>
    <?php
}, 100 );