From 7fc73f4c38e5b8a54ae30c4a563a99a9cb442509 Mon Sep 17 00:00:00 2001 From: nathan Date: Wed, 28 Feb 2024 14:59:39 -0700 Subject: [PATCH] Et2TreeDropdownCategory: Get category colors working --- api/js/etemplate/Et2Tree/Et2Tree.ts | 14 ++++++- api/js/etemplate/Et2Tree/Et2TreeDropdown.ts | 9 +++- .../Et2Tree/Et2TreeDropdownCategory.ts | 42 ++++++++++++++++++- api/src/Etemplate/Widget/Tree.php | 3 +- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/api/js/etemplate/Et2Tree/Et2Tree.ts b/api/js/etemplate/Et2Tree/Et2Tree.ts index 783a7542b8..b758e4e7c2 100644 --- a/api/js/etemplate/Et2Tree/Et2Tree.ts +++ b/api/js/etemplate/Et2Tree/Et2Tree.ts @@ -577,6 +577,15 @@ export class Et2Tree extends Et2WidgetWithSelectMixin(LitElement) return this.getNode(_nodeId)?.userdata?.find(elem => elem.name === _name)?.content } + /** + * Overridable, add style + * @returns {TemplateResult<1>} + */ + styleTemplate() + { + return html``; + } + //this.selectOptions = find_select_options(this)[1]; _optionTemplate(selectOption: TreeItemData): TemplateResult<1> { @@ -606,9 +615,10 @@ export class Et2Tree extends Et2WidgetWithSelectMixin(LitElement) return html` { diff --git a/api/js/etemplate/Et2Tree/Et2TreeDropdown.ts b/api/js/etemplate/Et2Tree/Et2TreeDropdown.ts index add8ab8816..e324f84555 100644 --- a/api/js/etemplate/Et2Tree/Et2TreeDropdown.ts +++ b/api/js/etemplate/Et2Tree/Et2TreeDropdown.ts @@ -1,4 +1,4 @@ -import {LitElement, nothing} from "lit"; +import {LitElement, nothing, TemplateResult} from "lit"; import {html, literal, StaticValue} from "lit/static-html.js"; import {Et2Tree, TreeItemData, TreeSearchResult} from "./Et2Tree"; import {Et2WidgetWithSelectMixin} from "../Et2Select/Et2WidgetWithSelectMixin"; @@ -493,6 +493,11 @@ export class Et2TreeDropdown extends SearchMixin & Et2InputWidg `; } + styleTemplate() : TemplateResult + { + return html``; + } + /** * Tag used for rendering tags when multiple=true * Used for creating, finding & filtering options. @@ -624,11 +629,13 @@ export class Et2TreeDropdown extends SearchMixin & Et2InputWidg .id=${this.id + "_tree"} ._parent=${this} class="tree-dropdown__tree" + exportparts="" ?readonly=${this.readonly} ?disabled=${this.disabled} value=${this.multiple ? nothing : this.value} ._selectOptions=${this.select_options} .actions=${this.actions} + .styleTemplate=${() => this.styleTemplate()} @sl-selection-change=${this.handleTreeChange} > diff --git a/api/js/etemplate/Et2Tree/Et2TreeDropdownCategory.ts b/api/js/etemplate/Et2Tree/Et2TreeDropdownCategory.ts index d00c2646d6..d35d9f1c28 100644 --- a/api/js/etemplate/Et2Tree/Et2TreeDropdownCategory.ts +++ b/api/js/etemplate/Et2Tree/Et2TreeDropdownCategory.ts @@ -3,15 +3,31 @@ * * @returns {string} */ -import {literal, StaticValue} from "lit/static-html.js"; +import {html, literal, StaticValue} from "lit/static-html.js"; import {property} from "lit/decorators/property.js"; -import {PropertyValues} from "lit"; +import {css, PropertyValues, unsafeCSS} from "lit"; import {Et2TreeDropdown} from "./Et2TreeDropdown"; import {Et2CategoryTag} from "../Et2Select/Tag/Et2CategoryTag"; export class Et2TreeDropdownCategory extends Et2TreeDropdown { + static get styles() + { + return [ + super.styles, + css` + :host { + --category-color: transparent; + } + + ::part(item-item) { + border-inline-start: 4px solid transparent; + border-inline-start-color: var(--category-color, transparent); + } + ` + ]; + } /** * Application to get categories from */ @@ -57,6 +73,28 @@ export class Et2TreeDropdownCategory extends Et2TreeDropdown { return literal`et2-category-tag`; } + + /** + * Set CSS category colors + * @returns {TemplateResult} + * @protected + */ + protected styleTemplate() + { + let css = ""; + const catColor = (option) => + { + css += ".cat_" + option.value + " {--category-color: " + (option.data?.color || "transparent") + ";}\n"; + + option.children?.forEach((option) => catColor(option)) + } + this.select_options.forEach((option => catColor(option))); + // @formatter:off + return html` + + `; + // @formatter:on + } } // @ts-ignore Type problems because of Et2WidgetWithSelectMixin in parent diff --git a/api/src/Etemplate/Widget/Tree.php b/api/src/Etemplate/Widget/Tree.php index 8de5a736b4..3d677b8608 100644 --- a/api/src/Etemplate/Widget/Tree.php +++ b/api/src/Etemplate/Widget/Tree.php @@ -552,7 +552,8 @@ class Tree extends Etemplate\Widget 'value' => $cat['id'], 'label' => $s, 'icon' => $cat['data']['icon'] ?? '', - 'title' => $cat['description'] + 'title' => $cat['description'], + 'class' => "cat_${cat['id']}" ); if(!empty($cat['children'])) {