Skip to content

Commit

Permalink
Extract toggleText() function from collapse
Browse files Browse the repository at this point in the history
- Extracted the toggleText() function from the collapse() function
  • Loading branch information
almic committed Apr 12, 2024
1 parent 1e1e5cc commit c1cc768
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions docs/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,8 @@ async function loadSourceCode(name, path) {
* given element
*/
function collapse(self, toggleText, name) {
toggleText(self, toggleText);
self.classList.toggle('collapse-active');
if (!self.getAttribute('data-toggle-text')) {
self.setAttribute('data-toggle-text', toggleText);
}
const text = self.getAttribute('data-toggle-text');
self.setAttribute('data-toggle-text', self.innerHTML);
self.innerHTML = text;
const elements = document.getElementsByName(name);
if (!elements.length) {
console.warn(`Tried to toggle collapsible elements named ${name}, but it wasn't on the page!`);
Expand All @@ -61,6 +56,18 @@ function collapse(self, toggleText, name) {
});
}

/**
* Toggles the text on the given element
*/
export function toggleText(element, toggleText) {
if (!element.getAttribute('data-toggle-text')) {
element.setAttribute('data-toggle-text', toggleText);
}
const text = element.getAttribute('data-toggle-text');
element.setAttribute('data-toggle-text', element.innerHTML);
element.innerHTML = text;
}

/**
* Hides the incoming button, then shows all following buttons
* under the same parent.
Expand All @@ -78,3 +85,5 @@ window.collapse = collapse;
window.hideButtons = hideButtons;
//@ts-ignore
window.loadSourceCode = loadSourceCode;
//@ts-ignore
window.toggleText = toggleText;

0 comments on commit c1cc768

Please sign in to comment.