bitburner-src/utils/uiHelpers/createAccordionElement.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

import { createElement } from "./createElement";
2018-07-08 07:11:34 +02:00
/**
* Possible configuration parameters when creating the accordion element.
*/
interface IAccordionConfigurationParameters {
/**
* The HTML to appear in the accordion header.
*/
hdrText?: string;
/**
* A (hopefully) unique identifier for the accordion.
*/
id?: string;
/**
* The HTML to appear in the expanded accordion.
*/
panelText?: string;
}
2018-07-08 07:11:34 +02:00
/**
* Creates both the header and panel element of an accordion and sets the click handler
* @param params The creation parameters.
*/
2021-05-01 09:17:31 +02:00
export function createAccordionElement(params: IAccordionConfigurationParameters): any[] {
const liElem: HTMLLIElement = createElement("li") as HTMLLIElement;
const header: HTMLButtonElement = createElement("button", {
2018-07-18 18:49:46 +02:00
class: "accordion-header",
clickListener() {
this.classList.toggle("active");
const pnl: CSSStyleDeclaration = (this.nextElementSibling as HTMLDivElement).style;
pnl.display = pnl.display === "block" ? "none" : "block";
},
2018-07-08 07:11:34 +02:00
id: params.id !== undefined ? `${params.id}-hdr` : undefined,
innerHTML: params.hdrText,
}) as HTMLButtonElement;
const panel: HTMLDivElement = createElement("div", {
2018-07-18 18:49:46 +02:00
class: "accordion-panel",
2018-07-08 07:11:34 +02:00
id: params.id !== undefined ? `${params.id}-panel` : undefined,
innerHTML: params.panelText,
}) as HTMLDivElement;
liElem.appendChild(header);
liElem.appendChild(panel);
return [
liElem,
header,
panel,
];
}