search-widget.js
48 lines
| 1 | var jetpackSearchModule = function () { |
| 2 | var i, |
| 3 | j, |
| 4 | checkboxes, |
| 5 | filter_list = document.querySelectorAll( '.jetpack-search-filters-widget__filter-list' ); |
| 6 | |
| 7 | for ( i = 0; i < filter_list.length; i++ ) { |
| 8 | filter_list[ i ].addEventListener( 'click', function ( event ) { |
| 9 | var target = event.target; |
| 10 | var precedingCheckbox; |
| 11 | var nextAnchor; |
| 12 | |
| 13 | // If the target is an anchor, we want to toggle the checkbox. |
| 14 | if ( target.nodeName && 'a' === target.nodeName.toLowerCase() ) { |
| 15 | precedingCheckbox = target.previousElementSibling; |
| 16 | if ( |
| 17 | precedingCheckbox && |
| 18 | precedingCheckbox.type && |
| 19 | 'checkbox' === precedingCheckbox.type |
| 20 | ) { |
| 21 | precedingCheckbox.checked = ! precedingCheckbox.checked; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // If the target is a checkbox, we want to navigate. |
| 26 | if ( target.type && 'checkbox' === target.type ) { |
| 27 | nextAnchor = target.nextElementSibling; |
| 28 | if ( nextAnchor && 'a' === nextAnchor.nodeName.toLowerCase() ) { |
| 29 | window.location.href = nextAnchor.getAttribute( 'href' ); |
| 30 | } |
| 31 | } |
| 32 | } ); |
| 33 | |
| 34 | // Enable checkboxes now that we're setup. |
| 35 | checkboxes = filter_list[ i ].querySelectorAll( 'input[type="checkbox"]' ); |
| 36 | for ( j = 0; j < checkboxes.length; j++ ) { |
| 37 | checkboxes[ j ].disabled = false; |
| 38 | checkboxes[ j ].style.cursor = 'inherit'; |
| 39 | } |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | if ( document.readyState === 'interactive' || document.readyState === 'complete' ) { |
| 44 | jetpackSearchModule(); |
| 45 | } else { |
| 46 | document.addEventListener( 'DOMContentLoaded', jetpackSearchModule ); |
| 47 | } |
| 48 |