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