Skip to content

Commit

Permalink
Move condition editor into its own file (#18340)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Oct 23, 2023
1 parent aeaf091 commit 4354ad3
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 154 deletions.
27 changes: 17 additions & 10 deletions src/panels/lovelace/editor/conditions/ha-card-condition-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { customElement, property, state } from "lit/decorators";
import { dynamicElement } from "../../../../common/dom/dynamic-element-directive";
import { fireEvent } from "../../../../common/dom/fire_event";
import { stopPropagation } from "../../../../common/dom/stop_propagation";
import { handleStructError } from "../../../../common/structs/handle-errors";
import "../../../../components/ha-button-menu";
import "../../../../components/ha-icon-button";
import "../../../../components/ha-list-item";
Expand All @@ -14,36 +15,40 @@ import "../../../../components/ha-yaml-editor";
import { haStyle } from "../../../../resources/styles";
import type { HomeAssistant } from "../../../../types";
import { ICON_CONDITION } from "../../common/icon-condition";
import { Condition } from "../../common/validate-condition";
import { Condition, LegacyCondition } from "../../common/validate-condition";
import type { LovelaceConditionEditorConstructor } from "./types";
import { handleStructError } from "../../../../common/structs/handle-errors";

@customElement("ha-card-condition-editor")
export default class HaCardConditionEditor extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) condition!: Condition;
@property({ attribute: false }) condition!: Condition | LegacyCondition;

@state() public _yamlMode = false;

@state() public _uiAvailable = false;

@state() public _uiWarnings: string[] = [];

@state() _condition?: Condition;

private get _editor() {
const element = customElements.get(
`ha-card-condition-${this.condition.condition}`
if (!this._condition) return undefined;
return customElements.get(
`ha-card-condition-${this._condition.condition}`
) as LovelaceConditionEditorConstructor | undefined;

return element;
}

protected willUpdate(changedProperties: PropertyValues): void {
if (changedProperties.has("condition")) {
this._condition = {
condition: "state",
...this.condition,
};
const validator = this._editor?.validateUIConfig;
if (validator) {
try {
validator(this.condition, this.hass);
validator(this._condition, this.hass);
this._uiAvailable = true;
this._uiWarnings = [];
} catch (err) {
Expand All @@ -65,7 +70,9 @@ export default class HaCardConditionEditor extends LitElement {
}

protected render() {
const condition = this.condition;
const condition = this._condition;

if (!condition) return nothing;

return html`
<div class="header">
Expand All @@ -75,7 +82,7 @@ export default class HaCardConditionEditor extends LitElement {
></ha-svg-icon>
<span class="title">
${this.hass.localize(
`ui.panel.lovelace.editor.card.conditional.condition.${condition.condition}.label`
`ui.panel.lovelace.editor.condition-editor.condition.${condition.condition}.label`
) || condition.condition}
</span>
<ha-button-menu
Expand Down
144 changes: 144 additions & 0 deletions src/panels/lovelace/editor/conditions/ha-card-conditions-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { mdiPlus } from "@mdi/js";
import { CSSResultGroup, LitElement, css, html } from "lit";
import { customElement, property } from "lit/decorators";
import { fireEvent } from "../../../../common/dom/fire_event";
import { stopPropagation } from "../../../../common/dom/stop_propagation";
import "../../../../components/ha-button";
import "../../../../components/ha-list-item";
import type { HaSelect } from "../../../../components/ha-select";
import "../../../../components/ha-svg-icon";
import type { HomeAssistant } from "../../../../types";
import { ICON_CONDITION } from "../../common/icon-condition";
import { Condition, LegacyCondition } from "../../common/validate-condition";
import "./ha-card-condition-editor";
import { LovelaceConditionEditorConstructor } from "./types";
import "./types/ha-card-condition-screen";
import "./types/ha-card-condition-state";

const UI_CONDITION = [
"state",
"screen",
] as const satisfies readonly Condition["condition"][];

@customElement("ha-card-conditions-editor")
export class HaCardConditionsEditor extends LitElement {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public conditions!: (
| Condition
| LegacyCondition
)[];

protected render() {
return html`
<div class="conditions">
${this.hass!.localize(
"ui.panel.lovelace.editor.condition-editor.explanation"
)}
${this.conditions.map(
(cond, idx) => html`
<div class="condition">
<ha-card-condition-editor
.index=${idx}
@value-changed=${this._conditionChanged}
.hass=${this.hass}
.condition=${cond}
></ha-card-condition-editor>
</div>
`
)}
<div>
<ha-button-menu
@action=${this._addCondition}
fixed
@closed=${stopPropagation}
>
<ha-button
slot="trigger"
outlined
.label=${this.hass.localize(
"ui.panel.lovelace.editor.condition-editor.add"
)}
>
<ha-svg-icon .path=${mdiPlus} slot="icon"></ha-svg-icon>
</ha-button>
${UI_CONDITION.map(
(condition) => html`
<ha-list-item .value=${condition} graphic="icon">
${this.hass!.localize(
`ui.panel.lovelace.editor.condition-editor.condition.${condition}.label`
) || condition}
<ha-svg-icon
slot="graphic"
.path=${ICON_CONDITION[condition]}
></ha-svg-icon>
</ha-list-item>
`
)}
</ha-button-menu>
</div>
</div>
`;
}

private _addCondition(ev: CustomEvent): void {
const condition = (ev.currentTarget as HaSelect).items[ev.detail.index]
.value as Condition["condition"];
const conditions = [...this.conditions];

const elClass = customElements.get(`ha-card-condition-${condition}`) as
| LovelaceConditionEditorConstructor
| undefined;

conditions.push(
elClass?.defaultConfig
? { ...elClass.defaultConfig }
: { condition: condition }
);
fireEvent(this, "value-changed", { value: conditions });
}

private _conditionChanged(ev: CustomEvent) {
ev.stopPropagation();
const conditions = [...this.conditions];
const newValue = ev.detail.value;
const index = (ev.target as any).index;

if (newValue === null) {
conditions.splice(index, 1);
} else {
conditions[index] = newValue;
}

fireEvent(this, "value-changed", { value: conditions });
}

static get styles(): CSSResultGroup {
return [
css`
mwc-tab-bar {
border-bottom: 1px solid var(--divider-color);
}
.conditions {
margin-top: 8px;
}
.condition {
margin-top: 8px;
border: 1px solid var(--divider-color);
}
.condition .content {
padding: 12px;
}
ha-button-menu {
margin-top: 12px;
}
`,
];
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-card-conditions-editor": HaCardConditionsEditor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ export class HaCardConditionScreen extends LitElement {
return {
value: b,
label: `${localize(
`ui.panel.lovelace.editor.card.conditional.condition.screen.breakpoints_list.${b}`
`ui.panel.lovelace.editor.condition-editor.condition.screen.breakpoints_list.${b}`
)}${
value
? ` (${localize(
`ui.panel.lovelace.editor.card.conditional.condition.screen.min`,
`ui.panel.lovelace.editor.condition-editor.condition.screen.min`,
{ size: value }
)})`
: ""
Expand Down Expand Up @@ -188,7 +188,7 @@ export class HaCardConditionScreen extends LitElement {
switch (schema.name) {
case "breakpoints":
return this.hass.localize(
`ui.panel.lovelace.editor.card.conditional.condition.screen.${schema.name}`
`ui.panel.lovelace.editor.condition-editor.condition.screen.${schema.name}`
);
default:
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,20 @@ export class HaCardConditionState extends LitElement {
schema: [
{
name: "invert",
required: true,
selector: {
select: {
mode: "dropdown",
options: [
{
label: localize(
"ui.panel.lovelace.editor.card.conditional.state_equal"
"ui.panel.lovelace.editor.condition-editor.condition.state.state_equal"
),
value: "false",
},
{
label: localize(
"ui.panel.lovelace.editor.card.conditional.state_not_equal"
"ui.panel.lovelace.editor.condition-editor.condition.state.state_not_equal"
),
value: "true",
},
Expand Down Expand Up @@ -148,7 +149,7 @@ export class HaCardConditionState extends LitElement {
return `${this.hass.localize(
"ui.components.entity.entity-state-picker.state"
)} (${this.hass.localize(
"ui.panel.lovelace.editor.card.conditional.current_state"
"ui.panel.lovelace.editor.condition-editor.condition.state.current_state"
)}: ${this.hass.formatEntityState(entity)})`;
}
return `${this.hass.localize(
Expand Down
Loading

0 comments on commit 4354ad3

Please sign in to comment.