11// Client-side filtering for case studies with page reload
22
3+ // No persistence needed for current requirement ("open if tag is selected").
4+
5+ const hasSelectedTagsInCategory = function ( filterType ) {
6+ const selectedTags = document . querySelectorAll (
7+ `#filter-content-${ filterType } .tag-item.active` ,
8+ ) ;
9+ return selectedTags . length > 0 ;
10+ } ;
11+
12+ const isDesktop = function ( ) {
13+ return window . innerWidth > 1024 ;
14+ } ;
15+
16+ const updateCategoriesVisibility = function ( ) {
17+ const checkboxes = document . querySelectorAll ( '.filter-category__toggle' ) ;
18+ checkboxes . forEach ( function ( checkbox ) {
19+ const filterType = checkbox . id . replace ( 'filter-toggle-' , '' ) ;
20+ const hasSelectedTags = hasSelectedTagsInCategory ( filterType ) ;
21+ const isIndustryCategory = filterType === 'industry' ;
22+
23+ const shouldBeOpen = hasSelectedTags || ( isIndustryCategory && isDesktop ( ) ) ;
24+ if ( shouldBeOpen && ! checkbox . checked ) {
25+ checkbox . checked = true ;
26+ const button = document . querySelector ( `label[for="${ checkbox . id } "]` ) ;
27+ if ( button ) {
28+ button . setAttribute ( 'aria-expanded' , 'true' ) ;
29+ }
30+ } else if ( ! shouldBeOpen && checkbox . checked ) {
31+ checkbox . checked = false ;
32+ const button = document . querySelector ( `label[for="${ checkbox . id } "]` ) ;
33+ if ( button ) {
34+ button . setAttribute ( 'aria-expanded' , 'false' ) ;
35+ }
36+ }
37+ } ) ;
38+ } ;
39+
40+ const initializeCategoriesVisibility = function ( ) {
41+ updateCategoriesVisibility ( ) ;
42+ } ;
43+
344const shouldInterceptClick = function ( link ) {
445 const href = link . getAttribute ( 'href' ) ;
546 return (
@@ -32,7 +73,6 @@ const handleFilterClick = function (event) {
3273 history . pushState ( { clientSideFilter : true , url : newUrl } , '' , newUrl ) ;
3374 window . location . reload ( ) ;
3475 } catch {
35- // Fallback to default navigation if URL construction fails
3676 window . location . href = href ;
3777 }
3878} ;
@@ -43,11 +83,34 @@ const handlePopState = function (event) {
4383 }
4484} ;
4585
86+ const handleResize = function ( ) {
87+ updateCategoriesVisibility ( ) ;
88+ } ;
89+
4690export const initClientSideFiltering = function ( ) {
4791 if ( ! document . querySelector ( '.cs_list' ) ) {
4892 return ;
4993 }
5094
95+ initializeCategoriesVisibility ( ) ;
96+
5197 window . addEventListener ( 'popstate' , handlePopState ) ;
98+ window . addEventListener ( 'resize' , handleResize ) ;
5299 document . addEventListener ( 'click' , handleFilterClick ) ;
100+
101+ // Keep aria-expanded in sync with checkbox state
102+ document . addEventListener ( 'change' , function ( event ) {
103+ const checkbox = event . target . closest ( '.filter-category__toggle' ) ;
104+ if ( ! checkbox ) {
105+ return ;
106+ }
107+ const button = document . querySelector ( `label[for="${ checkbox . id } "]` ) ;
108+ if ( button ) {
109+ if ( checkbox . checked ) {
110+ button . setAttribute ( 'aria-expanded' , 'true' ) ;
111+ } else {
112+ button . setAttribute ( 'aria-expanded' , 'false' ) ;
113+ }
114+ }
115+ } ) ;
53116} ;
0 commit comments