summaryrefslogtreecommitdiff
blob: 0f47e8e8f1fcd8a4e82939270676d8c208237bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var jetpackSearchModule = function () {
	var i,
		j,
		checkboxes,
		filter_list = document.querySelectorAll( '.jetpack-search-filters-widget__filter-list' );

	for ( i = 0; i < filter_list.length; i++ ) {
		filter_list[ i ].addEventListener( 'click', function ( event ) {
			var target = event.target;
			var precedingCheckbox;
			var nextAnchor;

			// If the target is an anchor, we want to toggle the checkbox.
			if ( target.nodeName && 'a' === target.nodeName.toLowerCase() ) {
				precedingCheckbox = target.previousElementSibling;
				if (
					precedingCheckbox &&
					precedingCheckbox.type &&
					'checkbox' === precedingCheckbox.type
				) {
					precedingCheckbox.checked = ! precedingCheckbox.checked;
				}
			}

			// If the target is a checkbox, we want to navigate.
			if ( target.type && 'checkbox' === target.type ) {
				nextAnchor = target.nextElementSibling;
				if ( nextAnchor && 'a' === nextAnchor.nodeName.toLowerCase() ) {
					window.location.href = nextAnchor.getAttribute( 'href' );
				}
			}
		} );

		// Enable checkboxes now that we're setup.
		checkboxes = filter_list[ i ].querySelectorAll( 'input[type="checkbox"]' );
		for ( j = 0; j < checkboxes.length; j++ ) {
			checkboxes[ j ].disabled = false;
			checkboxes[ j ].style.cursor = 'inherit';
		}
	}
};

if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
	jetpackSearchModule();
} else {
	document.addEventListener( 'DOMContentLoaded', jetpackSearchModule );
}