diff --git a/page/accordion.js b/page/accordion.js index eb8c5a3..9562957 100644 --- a/page/accordion.js +++ b/page/accordion.js @@ -16,9 +16,16 @@ window.addEventListener("load", function(event){ // collect this view let accordion_view = accordion_collect_view(one_accordion_root); accordion_view_list.push(accordion_view); - - // initialized this view - accordion_init(accordion_view); + } + + + // HANDLE STATE CHANGE BY URL // + accordion_open_from_url_hash(); + + + // INITIALIZE VIEWS // + for(let one_accordion of accordion_view_list){ + accordion_init(one_accordion); } }); @@ -206,3 +213,49 @@ function accordion_state_set(item, state){ function accordion_body_resize(bodyContainer){ bodyContainer.style.maxHeight = bodyContainer.scrollHeight + "px"; } + + + +/** +* HELPER: Maybe open a accordion item that is referenced in url. +*/ +function accordion_open_from_url_hash(){ + // FIND REFERENCED ITEM ELEMENT // + // get its id + let hash = window.location.hash; + if(hash.length < 2 || !hash.startsWith("#")) return; + let openId = hash.substring(1); + + // find item element + let itemElement = document.getElementById(openId); + if(itemElement === null) return; + + // maybe unwrap + if(itemElement.classList.contains("wrapper")) itemElement = itemElement.children[0] ?? null; + if(itemElement === null) return; + + + // SET TO OPEN // + // find item in list of accordion views + let foundAccordion = null; + let foundItem = null; + findLoop: + for(let one_accordion of accordion_view_list){ + for(let one_item of one_accordion.item_list){ + if(one_item.element === itemElement){ + foundAccordion = one_accordion; + foundItem = one_item; + break findLoop; + } + } + } + if(foundAccordion === null || foundItem === null) return; + + // close all items in this accordion + for(let one_item of foundAccordion.item_list){ + accordion_state_set(one_item, false); + } + + // set state to open + accordion_state_set(foundItem, true); +}