|
| 1 | +// Import Third-party Dependencies |
| 2 | +import { LitElement, html } from "lit"; |
| 3 | +import { when } from "lit/directives/when.js"; |
| 4 | + |
1 | 5 | // Import Internal Dependencies |
2 | | -import { createDOMElement, currentLang } from "../../common/utils"; |
3 | | - |
4 | | -export function createExpandableSpan( |
5 | | - hideItemsLength, |
6 | | - onclick = () => void 0 |
7 | | -) { |
8 | | - const lang = currentLang(); |
9 | | - const span = createDOMElement("span", { |
10 | | - classList: ["expandable"], |
11 | | - attributes: { "data-value": "closed" }, |
12 | | - childs: [ |
13 | | - createDOMElement("i", { className: "icon-plus-squared-alt" }), |
14 | | - createDOMElement("p", { text: window.i18n[lang].home.showMore }) |
15 | | - ] |
16 | | - }); |
17 | | - span.addEventListener("click", function itemListClickAction() { |
18 | | - const isClosed = this.getAttribute("data-value") === "closed"; |
19 | | - { |
20 | | - const innerI = this.querySelector("i"); |
21 | | - innerI.classList.remove(isClosed ? "icon-plus-squared-alt" : "icon-minus-squared-alt"); |
22 | | - innerI.classList.add(isClosed ? "icon-minus-squared-alt" : "icon-plus-squared-alt"); |
23 | | - } |
24 | | - |
25 | | - this.querySelector("p").textContent = isClosed ? |
26 | | - window.i18n[lang].home.showLess : window.i18n[lang].home.showMore; |
27 | | - this.setAttribute("data-value", isClosed ? "opened" : "closed"); |
28 | | - |
29 | | - for (let id = 0; id < this.parentNode.childNodes.length; id++) { |
30 | | - const node = this.parentNode.childNodes[id]; |
31 | | - if (node !== this) { |
32 | | - if (isClosed) { |
33 | | - node.classList.remove("hidden"); |
34 | | - } |
35 | | - else if (id >= hideItemsLength) { |
36 | | - node.classList.add("hidden"); |
37 | | - } |
38 | | - } |
39 | | - } |
40 | | - onclick(this); |
41 | | - }); |
42 | | - |
43 | | - return span; |
| 6 | +import { currentLang } from "../../common/utils"; |
| 7 | + |
| 8 | +class Expandable extends LitElement { |
| 9 | + static properties = { |
| 10 | + onToggle: { type: Function }, |
| 11 | + isClosed: { type: Boolean } |
| 12 | + }; |
| 13 | + |
| 14 | + constructor() { |
| 15 | + super(); |
| 16 | + this.isClosed = false; |
| 17 | + this.onToggle = () => void 0; |
| 18 | + } |
| 19 | + |
| 20 | + // FIXME: must opt out from the shadow DOM for now because of to be able to apply CSS from fontello |
| 21 | + createRenderRoot() { |
| 22 | + return this; |
| 23 | + } |
| 24 | + |
| 25 | + render() { |
| 26 | + const lang = currentLang(); |
| 27 | + |
| 28 | + return html` |
| 29 | + <span data-value=${this.isClosed ? "opened" : "closed"} @click=${this.#handleClick} class="expandable"> |
| 30 | + ${when(this.isClosed, |
| 31 | + () => html`<i class="icon-minus-squared-alt"></i> |
| 32 | + <p>${window.i18n[lang].home.showLess}</p>`, |
| 33 | + () => html`<i class="icon-plus-squared-alt"></i> |
| 34 | + <p>${window.i18n[lang].home.showMore}</p>` |
| 35 | + )} |
| 36 | + </span> |
| 37 | +`; |
| 38 | + } |
| 39 | + |
| 40 | + #handleClick() { |
| 41 | + this.onToggle(this); |
| 42 | + } |
44 | 43 | } |
| 44 | + |
| 45 | +customElements.define("expandable-span", Expandable); |
0 commit comments