|
1 | 1 | var initToggleItems = () => { |
2 | 2 | var itemsToToggle = document.querySelectorAll(togglebuttonSelector); |
3 | | - console.log(itemsToToggle, togglebuttonSelector) |
| 3 | + console.log(`[togglebutton]: Adding toggle buttons to ${itemsToToggle.length} items`) |
4 | 4 | // Add the button to each admonition and hook up a callback to toggle visibility |
5 | 5 | itemsToToggle.forEach((item, index) => { |
| 6 | + // Generate unique IDs for this item |
6 | 7 | var toggleID = `toggle-${index}`; |
7 | 8 | var buttonID = `button-${toggleID}`; |
8 | | - var collapseButton = ` |
9 | | - <button id="${buttonID}" class="toggle-button" data-target="${toggleID}" data-button="${buttonID}"> |
10 | | - <div class="bar horizontal" data-button="${buttonID}"></div> |
11 | | - <div class="bar vertical" data-button="${buttonID}"></div> |
12 | | - </button>`; |
13 | 9 |
|
14 | 10 | item.setAttribute('id', toggleID); |
15 | | - |
16 | 11 | if (!item.classList.contains("toggle")){ |
17 | 12 | item.classList.add("toggle"); |
18 | 13 | } |
19 | 14 |
|
20 | | - // If it's an admonition block, then we'll add the button inside |
| 15 | + // This is the button that will be added to each item to trigger the toggle |
| 16 | + var collapseButton = ` |
| 17 | + <button id="${buttonID}" class="toggle-button" data-target="${toggleID}" data-button="${buttonID}"> |
| 18 | + <div class="bar horizontal" data-button="${buttonID}"></div> |
| 19 | + <div class="bar vertical" data-button="${buttonID}"></div> |
| 20 | + </button>`; |
| 21 | + |
| 22 | + // Add the button HTML to this element and assign it as a variable to use later |
21 | 23 | if (item.classList.contains("admonition")) { |
| 24 | + // If it's an admonition block, then we'll add the button inside |
22 | 25 | item.insertAdjacentHTML("afterbegin", collapseButton); |
23 | 26 | } else { |
24 | 27 | item.insertAdjacentHTML('beforebegin', collapseButton); |
25 | 28 | } |
26 | | - |
27 | 29 | thisButton = document.getElementById(buttonID); |
28 | | - thisButton.on('click', toggleClickHandler); |
| 30 | + |
| 31 | + // Add click handlers for the button + admonition title (if admonition) |
| 32 | + thisButton.addEventListener('click', toggleClickHandler); |
| 33 | + |
29 | 34 | // If admonition has a single direct-child title make it clickable. |
30 | 35 | admonitionTitle = document.querySelector(`#${toggleID} > .admonition-title`) |
31 | 36 | if (admonitionTitle) { |
32 | | - admonitionTitle.on('click', toggleClickHandler); |
| 37 | + admonitionTitle.addEventListener('click', toggleClickHandler); |
33 | 38 | admonitionTitle.dataset.target = toggleID |
34 | 39 | admonitionTitle.dataset.button = buttonID |
35 | 40 | } |
| 41 | + |
| 42 | + // Now hide the item for this toggle button unless explicitly noted to show |
36 | 43 | if (!item.classList.contains("toggle-shown")) { |
37 | | - toggleHidden(thisButton[0]); |
| 44 | + toggleHidden(thisButton); |
38 | 45 | } |
39 | 46 | }) |
40 | 47 | }; |
|
0 commit comments