|
| 1 | +/** |
| 2 | + * Handles selection of categories. |
| 3 | + * |
| 4 | + * @author Olaf Braun |
| 5 | + * @copyright 2001-2024 WoltLab GmbH |
| 6 | + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 7 | + * @since 6.2 |
| 8 | + */ |
| 9 | +import { triggerEvent } from "../../Core"; |
| 10 | + |
| 11 | +export class FlexibleCategoryList { |
| 12 | + readonly #list: HTMLElement; |
| 13 | + readonly #categories = new Map<HTMLInputElement, HTMLInputElement[]>(); |
| 14 | + readonly #parentCategories = new Map<HTMLInputElement, HTMLInputElement>(); |
| 15 | + |
| 16 | + constructor(elementID: string) { |
| 17 | + this.#list = document.getElementById(elementID)!; |
| 18 | + |
| 19 | + // No nested categories |
| 20 | + if (!this.#list.querySelector("li li")) { |
| 21 | + this.#list.classList.add("flexibleCategoryListDisabled"); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + this.#buildStructure(); |
| 26 | + |
| 27 | + this.#list.querySelectorAll("input:checked").forEach((input: HTMLInputElement) => { |
| 28 | + triggerEvent(input, "change"); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + #buildStructure() { |
| 33 | + this.#list.querySelectorAll(".jsCategory").forEach((category: HTMLInputElement) => { |
| 34 | + category.addEventListener("change", () => { |
| 35 | + this.#updateSelection(category); |
| 36 | + }); |
| 37 | + this.#categories.set(category, []); |
| 38 | + |
| 39 | + category |
| 40 | + .closest("li")! |
| 41 | + .querySelectorAll<HTMLInputElement>(".jsChildCategory") |
| 42 | + .forEach((childCategory) => { |
| 43 | + this.#categories.get(category)!.push(childCategory); |
| 44 | + this.#categories.set(childCategory, []); |
| 45 | + this.#parentCategories.set(childCategory, category); |
| 46 | + |
| 47 | + childCategory.addEventListener("change", () => { |
| 48 | + this.#updateSelection(childCategory); |
| 49 | + }); |
| 50 | + |
| 51 | + childCategory |
| 52 | + .closest("li")! |
| 53 | + .querySelectorAll<HTMLInputElement>(".jsSubChildCategory") |
| 54 | + .forEach((subChildCategory) => { |
| 55 | + this.#categories.get(childCategory)!.push(subChildCategory); |
| 56 | + this.#parentCategories.set(subChildCategory, childCategory); |
| 57 | + |
| 58 | + subChildCategory.addEventListener("change", () => { |
| 59 | + this.#updateSelection(subChildCategory); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + #updateSelection(category: HTMLInputElement) { |
| 67 | + const parentCategory = this.#parentCategories.get(category); |
| 68 | + |
| 69 | + if (category.checked) { |
| 70 | + if (parentCategory) { |
| 71 | + parentCategory.checked = true; |
| 72 | + |
| 73 | + this.#updateSelection(parentCategory); |
| 74 | + } |
| 75 | + } else { |
| 76 | + // uncheck child categories |
| 77 | + this.#categories.get(category)?.forEach((childCategory) => { |
| 78 | + childCategory.checked = false; |
| 79 | + |
| 80 | + this.#updateSelection(childCategory); |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments