mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2025-04-27 20:50:01 +02:00
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
//General helper functions
|
|
import {isString} from "./helpers/isString";
|
|
|
|
function removeChildrenFromElement(el) {
|
|
if (isString(el)) {
|
|
el = document.getElementById(el);
|
|
}
|
|
if (el == null) {return;}
|
|
if (el instanceof Element) {
|
|
while(el.firstChild) {
|
|
el.removeChild(el.firstChild);
|
|
}
|
|
}
|
|
}
|
|
|
|
//Creates both the header and panel element of an accordion and sets the click handler
|
|
function createAccordionElement(params) {
|
|
var li = document.createElement("li"),
|
|
hdr = document.createElement("button"),
|
|
panel = document.createElement("div");
|
|
hdr.classList.add("accordion-header");
|
|
panel.classList.add("accordion-panel");
|
|
|
|
if (params.id) {
|
|
hdr.id = params.id + "-hdr";
|
|
panel.id = params.id + "-panel";
|
|
}
|
|
if (params.hdrText) {hdr.innerHTML = params.hdrText;}
|
|
if (params.panelText) {panel.innerHTML = params.panelText;}
|
|
li.appendChild(hdr);
|
|
li.appendChild(panel);
|
|
//Click handler
|
|
hdr.onclick = function() {
|
|
this.classList.toggle("active");
|
|
var tmpPanel = this.nextElementSibling;
|
|
if (tmpPanel.style.display === "block") {
|
|
tmpPanel.style.display = "none";
|
|
} else {
|
|
tmpPanel.style.display = "block";
|
|
}
|
|
}
|
|
return [li, hdr, panel];
|
|
}
|
|
|
|
function clearSelector(selector) {
|
|
for (var i = selector.options.length - 1; i >= 0; --i) {
|
|
selector.remove(i);
|
|
}
|
|
}
|
|
|
|
export {createAccordionElement,
|
|
removeChildrenFromElement,
|
|
clearSelector};
|