|
|
|
|
|
|
|
|
|
let toggleChevron = ` |
|
<svg xmlns="http://www.w3.org/2000/svg" class="tb-icon toggle-chevron-right" width="44" height="44" viewBox="0 0 24 24" stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round"> |
|
<path stroke="none" d="M0 0h24v24H0z" fill="none"/> |
|
<polyline points="9 6 15 12 9 18" /> |
|
</svg>`; |
|
|
|
var initToggleItems = () => { |
|
var itemsToToggle = document.querySelectorAll(togglebuttonSelector); |
|
console.log(`[togglebutton]: Adding toggle buttons to ${itemsToToggle.length} items`) |
|
|
|
itemsToToggle.forEach((item, index) => { |
|
if (item.classList.contains("admonition")) { |
|
|
|
|
|
var toggleID = `toggle-${index}`; |
|
var buttonID = `button-${toggleID}`; |
|
|
|
item.setAttribute('id', toggleID); |
|
if (!item.classList.contains("toggle")){ |
|
item.classList.add("toggle"); |
|
} |
|
|
|
var collapseButton = ` |
|
<button type="button" id="${buttonID}" class="toggle-button" data-target="${toggleID}" data-button="${buttonID}" data-toggle-hint="${toggleHintShow}" aria-label="Toggle hidden content"> |
|
${toggleChevron} |
|
</button>`; |
|
|
|
title = item.querySelector(".admonition-title") |
|
title.insertAdjacentHTML("beforeend", collapseButton); |
|
thisButton = document.getElementById(buttonID); |
|
|
|
|
|
admonitionTitle = document.querySelector(`#${toggleID} > .admonition-title`) |
|
if (admonitionTitle) { |
|
|
|
admonitionTitle.addEventListener('click', toggleClickHandler); |
|
admonitionTitle.dataset.target = toggleID |
|
admonitionTitle.dataset.button = buttonID |
|
} else { |
|
|
|
thisButton.addEventListener('click', toggleClickHandler); |
|
} |
|
|
|
|
|
if (!item.classList.contains("toggle-shown")) { |
|
toggleHidden(thisButton); |
|
} |
|
} else { |
|
|
|
|
|
var detailsBlock = ` |
|
<details class="toggle-details"> |
|
<summary class="toggle-details__summary"> |
|
${toggleChevron} |
|
<span class="toggle-details__summary-text">${toggleHintShow}</span> |
|
</summary> |
|
</details>`; |
|
item.insertAdjacentHTML("beforebegin", detailsBlock); |
|
|
|
|
|
details = item.previousElementSibling |
|
details.appendChild(item) |
|
item.classList.add("toggle-details__container") |
|
|
|
|
|
details.addEventListener('click', (click) => { |
|
let parent = click.target.parentElement; |
|
if (parent.tagName.toLowerCase() == "details") { |
|
summary = parent.querySelector("summary"); |
|
details = parent; |
|
} else { |
|
summary = parent; |
|
details = parent.parentElement; |
|
} |
|
|
|
if (details.open) { |
|
summary.querySelector("span.toggle-details__summary-text").innerText = toggleHintShow; |
|
} else { |
|
summary.querySelector("span.toggle-details__summary-text").innerText = toggleHintHide; |
|
} |
|
|
|
}); |
|
|
|
|
|
if (item.classList.contains("toggle-shown")) { |
|
details.click(); |
|
} |
|
} |
|
}) |
|
}; |
|
|
|
|
|
var toggleHidden = (button) => { |
|
target = button.dataset['target'] |
|
var itemToToggle = document.getElementById(target); |
|
if (itemToToggle.classList.contains("toggle-hidden")) { |
|
itemToToggle.classList.remove("toggle-hidden"); |
|
button.classList.remove("toggle-button-hidden"); |
|
} else { |
|
itemToToggle.classList.add("toggle-hidden"); |
|
button.classList.add("toggle-button-hidden"); |
|
} |
|
} |
|
|
|
var toggleClickHandler = (click) => { |
|
|
|
|
|
|
|
if (click.target.classList.contains("admonition-title")) { |
|
button = click.target.querySelector(".toggle-button"); |
|
} else if (click.target.classList.contains("tb-icon")) { |
|
|
|
button = click.target.parentElement; |
|
} else if (click.target.tagName == "polyline") { |
|
|
|
button = click.target.parentElement.parentElement; |
|
} else if (click.target.classList.contains("toggle-button")) { |
|
|
|
button = click.target; |
|
} else { |
|
console.log(`[togglebutton]: Couldn't find button for ${click.target}`) |
|
} |
|
target = document.getElementById(button.dataset['button']); |
|
toggleHidden(target); |
|
} |
|
|
|
|
|
var addToggleToSelector = () => { |
|
const selector = ""; |
|
if (selector.length > 0) { |
|
document.querySelectorAll(selector).forEach((item) => { |
|
item.classList.add("toggle"); |
|
}) |
|
} |
|
} |
|
|
|
|
|
const sphinxToggleRunWhenDOMLoaded = cb => { |
|
if (document.readyState != 'loading') { |
|
cb() |
|
} else if (document.addEventListener) { |
|
document.addEventListener('DOMContentLoaded', cb) |
|
} else { |
|
document.attachEvent('onreadystatechange', function() { |
|
if (document.readyState == 'complete') cb() |
|
}) |
|
} |
|
} |
|
sphinxToggleRunWhenDOMLoaded(addToggleToSelector) |
|
sphinxToggleRunWhenDOMLoaded(initToggleItems) |
|
|
|
|
|
if (toggleOpenOnPrint == "true") { |
|
window.addEventListener("beforeprint", () => { |
|
|
|
document.querySelectorAll("details.toggle-details").forEach((el) => { |
|
el.dataset["togglestatus"] = el.open; |
|
el.open = true; |
|
}); |
|
|
|
|
|
document.querySelectorAll(".admonition.toggle.toggle-hidden").forEach((el) => { |
|
console.log(el); |
|
el.querySelector("button.toggle-button").click(); |
|
el.dataset["toggle_after_print"] = "true"; |
|
}); |
|
}); |
|
window.addEventListener("afterprint", () => { |
|
|
|
document.querySelectorAll("details.toggle-details").forEach((el) => { |
|
el.open = el.dataset["togglestatus"] == "true"; |
|
delete el.dataset["togglestatus"]; |
|
}); |
|
|
|
|
|
document.querySelectorAll(".admonition.toggle").forEach((el) => { |
|
if (el.dataset["toggle_after_print"] == "true") { |
|
el.querySelector("button.toggle-button").click(); |
|
delete el.dataset["toggle_after_print"]; |
|
} |
|
}); |
|
}); |
|
} |
|
|