1+ // app/assets/javascript/expanded-state-tracker.js
2+
3+ // Generic expanded state tracker
4+ // Tracks open/closed state of elements with .js-track-expanded class
5+
6+ ( function ( ) {
7+ 'use strict'
8+
9+ const STORAGE_KEY_PREFIX = 'expanded-sections-'
10+ const PENDING_EXPAND_KEY_PREFIX = 'pending-expand-'
11+
12+ function getStorageKey ( ) {
13+ return STORAGE_KEY_PREFIX + window . location . pathname
14+ }
15+
16+ function getPendingExpandKey ( ) {
17+ return PENDING_EXPAND_KEY_PREFIX + window . location . pathname
18+ }
19+
20+ function saveExpandedState ( ) {
21+ const trackableElements = document . querySelectorAll ( '.js-track-expanded' )
22+ const expandedSections = [ ]
23+
24+ trackableElements . forEach ( element => {
25+ if ( element . hasAttribute ( 'open' ) ) {
26+ expandedSections . push ( element . id )
27+ }
28+ } )
29+
30+ sessionStorage . setItem ( getStorageKey ( ) , JSON . stringify ( expandedSections ) )
31+ }
32+
33+ function restoreExpandedState ( ) {
34+ const storedState = sessionStorage . getItem ( getStorageKey ( ) )
35+ if ( storedState ) {
36+ try {
37+ const expandedSections = JSON . parse ( storedState )
38+ expandedSections . forEach ( sectionId => {
39+ const element = document . getElementById ( sectionId )
40+ if ( element && element . classList . contains ( 'js-track-expanded' ) ) {
41+ element . setAttribute ( 'open' , 'open' )
42+ }
43+ } )
44+ } catch ( e ) {
45+ console . warn ( 'Failed to restore expanded state:' , e )
46+ }
47+ }
48+
49+ // Also check for pending expand sections
50+ const pendingExpandState = sessionStorage . getItem ( getPendingExpandKey ( ) )
51+ if ( pendingExpandState ) {
52+ try {
53+ const sectionsToExpand = JSON . parse ( pendingExpandState )
54+ sectionsToExpand . forEach ( sectionId => {
55+ const element = document . getElementById ( sectionId )
56+ if ( element && element . classList . contains ( 'js-track-expanded' ) ) {
57+ element . setAttribute ( 'open' , 'open' )
58+ }
59+ } )
60+ // Clear pending expand state after using it
61+ sessionStorage . removeItem ( getPendingExpandKey ( ) )
62+ } catch ( e ) {
63+ console . warn ( 'Failed to restore pending expand state:' , e )
64+ }
65+ }
66+ }
67+
68+ function clearExpandedStateIfNeeded ( ) {
69+ const path = window . location . pathname
70+
71+ // Clear if on main event page (not sub-pages)
72+ if ( path . match ( / ^ \/ c l i n i c s \/ [ ^ \/ ] + \/ e v e n t s \/ [ ^ \/ ] + \/ ? $ / ) ) {
73+ clearAllExpandedStates ( )
74+ }
75+ }
76+
77+ function clearAllExpandedStates ( ) {
78+ Object . keys ( sessionStorage ) . forEach ( key => {
79+ if ( key . startsWith ( STORAGE_KEY_PREFIX ) || key . startsWith ( PENDING_EXPAND_KEY_PREFIX ) ) {
80+ sessionStorage . removeItem ( key )
81+ }
82+ } )
83+ }
84+
85+ function setupEventListeners ( ) {
86+ const trackableElements = document . querySelectorAll ( '.js-track-expanded' )
87+
88+ trackableElements . forEach ( element => {
89+ element . addEventListener ( 'toggle' , saveExpandedState )
90+ } )
91+
92+ // Handle links that should expand their parent section
93+ const expandParentLinks = document . querySelectorAll ( '.js-expand-parent-section' )
94+ expandParentLinks . forEach ( link => {
95+ link . addEventListener ( 'click' , function ( ) {
96+ // Find the parent section (closest .js-track-expanded element)
97+ const parentSection = this . closest ( '.js-track-expanded' )
98+ if ( parentSection && parentSection . id ) {
99+ // Store this section ID as pending expand for the return URL
100+ const returnUrl = this . getAttribute ( 'href' )
101+ if ( returnUrl ) {
102+ // We need to figure out what the return URL will be
103+ // For now, assume it returns to the current page
104+ const currentPath = window . location . pathname
105+ const pendingExpandSections = [ parentSection . id ]
106+ sessionStorage . setItem ( PENDING_EXPAND_KEY_PREFIX + currentPath , JSON . stringify ( pendingExpandSections ) )
107+ console . log ( 'Marked section for expansion on return:' , parentSection . id )
108+ }
109+ }
110+ } )
111+ } )
112+ }
113+
114+ // Initialize on page load
115+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
116+ clearExpandedStateIfNeeded ( )
117+ restoreExpandedState ( )
118+ setupEventListeners ( )
119+ } )
120+
121+ // Expose clear function globally for clear data link
122+ window . clearAllExpandedStates = clearAllExpandedStates
123+
124+ } ) ( )
0 commit comments