mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Use callback instead of changing nested config with sub editor (#22081)
Use callback instead of changing nested config
This commit is contained in:
parent
291c026da0
commit
4e51c7cf96
@ -1,17 +0,0 @@
|
|||||||
export function updateNestedObject<T = any>(
|
|
||||||
obj: T,
|
|
||||||
path: (number | string)[],
|
|
||||||
value: any
|
|
||||||
): T {
|
|
||||||
if (path.length === 0) return value;
|
|
||||||
const newObj = (Array.isArray(obj) ? [...obj] : { ...obj }) as T;
|
|
||||||
const key = path[0];
|
|
||||||
newObj[key] = updateNestedObject(obj[key], path.slice(1), value);
|
|
||||||
return newObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function findNestedObject(obj: any, path: (number | string)[]) {
|
|
||||||
if (path.length === 0) return obj;
|
|
||||||
const key = path[0];
|
|
||||||
return findNestedObject(obj[key], path.slice(1));
|
|
||||||
}
|
|
@ -30,6 +30,7 @@ import { actionConfigStruct } from "../structs/action-struct";
|
|||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { configElementStyle } from "./config-elements-style";
|
import { configElementStyle } from "./config-elements-style";
|
||||||
import "./hui-entities-editor";
|
import "./hui-entities-editor";
|
||||||
|
import { EditSubElementEvent } from "../types";
|
||||||
|
|
||||||
const actions: UiAction[] = ["navigate", "url", "perform-action", "none"];
|
const actions: UiAction[] = ["navigate", "url", "perform-action", "none"];
|
||||||
|
|
||||||
@ -176,9 +177,22 @@ export class HuiHeadingCardEditor
|
|||||||
|
|
||||||
private _editEntity(ev: HASSDomEvent<{ index: number }>): void {
|
private _editEntity(ev: HASSDomEvent<{ index: number }>): void {
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
|
const index = ev.detail.index;
|
||||||
|
const config = this._config!.entities![index!];
|
||||||
|
|
||||||
fireEvent(this, "edit-sub-element", {
|
fireEvent(this, "edit-sub-element", {
|
||||||
path: ["entities", ev.detail.index],
|
config: config,
|
||||||
|
saveConfig: (newConfig) => this._updateEntity(index, newConfig),
|
||||||
type: "heading-entity",
|
type: "heading-entity",
|
||||||
|
} as EditSubElementEvent<HeadingEntityConfig>);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updateEntity(index: number, entity: HeadingEntityConfig) {
|
||||||
|
const entities = this._config!.entities!.concat();
|
||||||
|
entities[index] = entity;
|
||||||
|
const config = { ...this._config!, entities };
|
||||||
|
fireEvent(this, "config-changed", {
|
||||||
|
config: config,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ import {
|
|||||||
import type { HumidifierCardConfig } from "../../cards/types";
|
import type { HumidifierCardConfig } from "../../cards/types";
|
||||||
import type { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { EditDetailElementEvent } from "../types";
|
import { EditDetailElementEvent, EditSubElementEvent } from "../types";
|
||||||
import { configElementStyle } from "./config-elements-style";
|
import { configElementStyle } from "./config-elements-style";
|
||||||
import "./hui-card-features-editor";
|
import "./hui-card-features-editor";
|
||||||
import type { FeatureType } from "./hui-card-features-editor";
|
import type { FeatureType } from "./hui-card-features-editor";
|
||||||
@ -145,12 +145,28 @@ export class HuiHumidifierCardEditor
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _editDetailElement(ev: HASSDomEvent<EditDetailElementEvent>): void {
|
private _editDetailElement(ev: HASSDomEvent<EditDetailElementEvent>): void {
|
||||||
|
const index = ev.detail.subElementConfig.index;
|
||||||
|
const config = this._config!.features![index!];
|
||||||
|
|
||||||
fireEvent(this, "edit-sub-element", {
|
fireEvent(this, "edit-sub-element", {
|
||||||
path: ["features", ev.detail.subElementConfig.index!],
|
config: config,
|
||||||
|
saveConfig: (newConfig) => this._updateFeature(index!, newConfig),
|
||||||
context: {
|
context: {
|
||||||
entity_id: this._config!.entity,
|
entity_id: this._config!.entity,
|
||||||
} as LovelaceCardFeatureContext,
|
},
|
||||||
type: "feature",
|
type: "feature",
|
||||||
|
} as EditSubElementEvent<
|
||||||
|
LovelaceCardFeatureConfig,
|
||||||
|
LovelaceCardFeatureContext
|
||||||
|
>);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updateFeature(index: number, feature: LovelaceCardFeatureConfig) {
|
||||||
|
const features = this._config!.features!.concat();
|
||||||
|
features[index] = feature;
|
||||||
|
const config = { ...this._config!, features };
|
||||||
|
fireEvent(this, "config-changed", {
|
||||||
|
config: config,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ import {
|
|||||||
import type { ThermostatCardConfig } from "../../cards/types";
|
import type { ThermostatCardConfig } from "../../cards/types";
|
||||||
import type { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { EditDetailElementEvent } from "../types";
|
import { EditDetailElementEvent, EditSubElementEvent } from "../types";
|
||||||
import { configElementStyle } from "./config-elements-style";
|
import { configElementStyle } from "./config-elements-style";
|
||||||
import "./hui-card-features-editor";
|
import "./hui-card-features-editor";
|
||||||
import type { FeatureType } from "./hui-card-features-editor";
|
import type { FeatureType } from "./hui-card-features-editor";
|
||||||
@ -143,12 +143,28 @@ export class HuiThermostatCardEditor
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _editDetailElement(ev: HASSDomEvent<EditDetailElementEvent>): void {
|
private _editDetailElement(ev: HASSDomEvent<EditDetailElementEvent>): void {
|
||||||
|
const index = ev.detail.subElementConfig.index;
|
||||||
|
const config = this._config!.features![index!];
|
||||||
|
|
||||||
fireEvent(this, "edit-sub-element", {
|
fireEvent(this, "edit-sub-element", {
|
||||||
path: ["features", ev.detail.subElementConfig.index!],
|
config: config,
|
||||||
|
saveConfig: (newConfig) => this._updateFeature(index!, newConfig),
|
||||||
context: {
|
context: {
|
||||||
entity_id: this._config!.entity,
|
entity_id: this._config!.entity,
|
||||||
} as LovelaceCardFeatureContext,
|
},
|
||||||
type: "feature",
|
type: "feature",
|
||||||
|
} as EditSubElementEvent<
|
||||||
|
LovelaceCardFeatureConfig,
|
||||||
|
LovelaceCardFeatureContext
|
||||||
|
>);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updateFeature(index: number, feature: LovelaceCardFeatureConfig) {
|
||||||
|
const features = this._config!.features!.concat();
|
||||||
|
features[index] = feature;
|
||||||
|
const config = { ...this._config!, features };
|
||||||
|
fireEvent(this, "config-changed", {
|
||||||
|
config: config,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ import type { TileCardConfig } from "../../cards/types";
|
|||||||
import type { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { actionConfigStruct } from "../structs/action-struct";
|
import { actionConfigStruct } from "../structs/action-struct";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import { EditDetailElementEvent } from "../types";
|
import { EditDetailElementEvent, EditSubElementEvent } from "../types";
|
||||||
import { configElementStyle } from "./config-elements-style";
|
import { configElementStyle } from "./config-elements-style";
|
||||||
import "./hui-card-features-editor";
|
import "./hui-card-features-editor";
|
||||||
|
|
||||||
@ -246,12 +246,28 @@ export class HuiTileCardEditor
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _editDetailElement(ev: HASSDomEvent<EditDetailElementEvent>): void {
|
private _editDetailElement(ev: HASSDomEvent<EditDetailElementEvent>): void {
|
||||||
|
const index = ev.detail.subElementConfig.index;
|
||||||
|
const config = this._config!.features![index!];
|
||||||
|
|
||||||
fireEvent(this, "edit-sub-element", {
|
fireEvent(this, "edit-sub-element", {
|
||||||
path: ["features", ev.detail.subElementConfig.index!],
|
config: config,
|
||||||
|
saveConfig: (newConfig) => this._updateFeature(index!, newConfig),
|
||||||
context: {
|
context: {
|
||||||
entity_id: this._config!.entity,
|
entity_id: this._config!.entity,
|
||||||
} as LovelaceCardFeatureContext,
|
},
|
||||||
type: "feature",
|
type: "feature",
|
||||||
|
} as EditSubElementEvent<
|
||||||
|
LovelaceCardFeatureConfig,
|
||||||
|
LovelaceCardFeatureContext
|
||||||
|
>);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updateFeature(index: number, feature: LovelaceCardFeatureConfig) {
|
||||||
|
const features = this._config!.features!.concat();
|
||||||
|
features[index] = feature;
|
||||||
|
const config = { ...this._config!, features };
|
||||||
|
fireEvent(this, "config-changed", {
|
||||||
|
config: config,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,10 +12,6 @@ import { cache } from "lit/directives/cache";
|
|||||||
import { fireEvent, HASSDomEvent } from "../../../common/dom/fire_event";
|
import { fireEvent, HASSDomEvent } from "../../../common/dom/fire_event";
|
||||||
import { handleStructError } from "../../../common/structs/handle-errors";
|
import { handleStructError } from "../../../common/structs/handle-errors";
|
||||||
import { deepEqual } from "../../../common/util/deep-equal";
|
import { deepEqual } from "../../../common/util/deep-equal";
|
||||||
import {
|
|
||||||
findNestedObject,
|
|
||||||
updateNestedObject,
|
|
||||||
} from "../../../common/util/nested-object";
|
|
||||||
import "../../../components/ha-alert";
|
import "../../../components/ha-alert";
|
||||||
import "../../../components/ha-circular-progress";
|
import "../../../components/ha-circular-progress";
|
||||||
import "../../../components/ha-yaml-editor";
|
import "../../../components/ha-yaml-editor";
|
||||||
@ -197,14 +193,7 @@ export abstract class HuiElementEditor<
|
|||||||
elementConfig: value,
|
elementConfig: value,
|
||||||
};
|
};
|
||||||
|
|
||||||
const config = updateNestedObject(
|
this._subElementEditorConfig.saveElementConfig?.(value);
|
||||||
this._config,
|
|
||||||
this._subElementEditorConfig.path!,
|
|
||||||
value
|
|
||||||
);
|
|
||||||
|
|
||||||
this._config = config;
|
|
||||||
this._setConfig();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _goBack(ev): void {
|
private _goBack(ev): void {
|
||||||
@ -215,22 +204,15 @@ export abstract class HuiElementEditor<
|
|||||||
private async _editSubElement(
|
private async _editSubElement(
|
||||||
ev: HASSDomEvent<EditSubElementEvent>
|
ev: HASSDomEvent<EditSubElementEvent>
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (!ev.detail.path) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
const config = findNestedObject(this._config, ev.detail.path);
|
|
||||||
|
|
||||||
if (!config) {
|
|
||||||
throw new Error("Failed to edit config");
|
|
||||||
}
|
|
||||||
|
|
||||||
await import("./hui-sub-element-editor");
|
await import("./hui-sub-element-editor");
|
||||||
|
|
||||||
this._subElementEditorConfig = {
|
this._subElementEditorConfig = {
|
||||||
type: ev.detail.type,
|
type: ev.detail.type,
|
||||||
path: ev.detail.path,
|
elementConfig: ev.detail.config,
|
||||||
elementConfig: config,
|
context: ev.detail.context,
|
||||||
|
saveElementConfig: ev.detail.saveConfig,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,21 +92,22 @@ export interface BadgePickTarget extends EventTarget {
|
|||||||
|
|
||||||
export interface SubElementEditorConfig {
|
export interface SubElementEditorConfig {
|
||||||
index?: number;
|
index?: number;
|
||||||
path?: (string | number)[];
|
|
||||||
elementConfig?:
|
elementConfig?:
|
||||||
| LovelaceRowConfig
|
| LovelaceRowConfig
|
||||||
| LovelaceHeaderFooterConfig
|
| LovelaceHeaderFooterConfig
|
||||||
| LovelaceCardFeatureConfig
|
| LovelaceCardFeatureConfig
|
||||||
| LovelaceElementConfig
|
| LovelaceElementConfig
|
||||||
| HeadingEntityConfig;
|
| HeadingEntityConfig;
|
||||||
|
saveElementConfig?: (elementConfig: any) => void;
|
||||||
context?: any;
|
context?: any;
|
||||||
type: "header" | "footer" | "row" | "feature" | "element" | "heading-entity";
|
type: "header" | "footer" | "row" | "feature" | "element" | "heading-entity";
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EditSubElementEvent {
|
export interface EditSubElementEvent<T = any, C = any> {
|
||||||
path: (string | number)[];
|
|
||||||
type: SubElementEditorConfig["type"];
|
type: SubElementEditorConfig["type"];
|
||||||
context?: any;
|
context?: C;
|
||||||
|
config: T;
|
||||||
|
saveConfig: (config: T) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EditDetailElementEvent {
|
export interface EditDetailElementEvent {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user