Improve lovelace card events (#19785)

This commit is contained in:
Paul Bottein 2024-02-14 10:07:50 +01:00 committed by GitHub
parent a3a099126e
commit 33cdd51f00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 23 additions and 18 deletions

View File

@ -275,9 +275,9 @@ export class HuiCardOptions extends LitElement {
const cardConfig = this._currentView.cards![path[1]]; const cardConfig = this._currentView.cards![path[1]];
showEditCardDialog(this, { showEditCardDialog(this, {
lovelaceConfig: this.lovelace!.config, lovelaceConfig: this.lovelace!.config,
cardConfig,
saveConfig: this.lovelace!.saveConfig, saveConfig: this.lovelace!.saveConfig,
path: [path[0]], path: [path[0], null],
newCardConfig: cardConfig,
}); });
} }

View File

@ -214,8 +214,8 @@ export class HuiCreateDialogCard
showEditCardDialog(this, { showEditCardDialog(this, {
lovelaceConfig: this._params!.lovelaceConfig, lovelaceConfig: this._params!.lovelaceConfig,
saveConfig: this._params!.saveConfig, saveConfig: this._params!.saveConfig,
path: this._params!.path, path: [this._params!.path[0], null],
cardConfig: config, newCardConfig: config,
}); });
this.closeDialog(); this.closeDialog();

View File

@ -87,12 +87,13 @@ export class HuiDialogEditCard
const [view, card] = params.path; const [view, card] = params.path;
this._viewConfig = params.lovelaceConfig.views[view]; this._viewConfig = params.lovelaceConfig.views[view];
this._cardConfig = this._cardConfig =
card !== undefined ? this._viewConfig.cards![card] : params.cardConfig; params.newCardConfig ??
(card !== null ? this._viewConfig.cards![card] : undefined);
this.large = false; this.large = false;
if (this._cardConfig && !Object.isFrozen(this._cardConfig)) { if (this._cardConfig && !Object.isFrozen(this._cardConfig)) {
this._cardConfig = deepFreeze(this._cardConfig); this._cardConfig = deepFreeze(this._cardConfig);
} }
if (params.cardConfig) { if (params.newCardConfig) {
this._dirty = true; this._dirty = true;
} }
} }
@ -368,16 +369,13 @@ export class HuiDialogEditCard
return; return;
} }
this._saving = true; this._saving = true;
const [view, card] = this._params!.path;
await this._params!.saveConfig( await this._params!.saveConfig(
this._params!.path.length === 1 card === null
? addCard( ? addCard(this._params!.lovelaceConfig, [view], this._cardConfig!)
this._params!.lovelaceConfig,
this._params!.path as [number],
this._cardConfig!
)
: replaceCard( : replaceCard(
this._params!.lovelaceConfig, this._params!.lovelaceConfig,
this._params!.path as [number, number], [view, card],
this._cardConfig! this._cardConfig!
) )
); );

View File

@ -4,7 +4,7 @@ import type { LovelaceConfig } from "../../../../data/lovelace/config/types";
export interface CreateCardDialogParams { export interface CreateCardDialogParams {
lovelaceConfig: LovelaceConfig; lovelaceConfig: LovelaceConfig;
saveConfig: (config: LovelaceConfig) => void; saveConfig: (config: LovelaceConfig) => void;
path: [number] | [number, number]; path: [number];
entities?: string[]; // We can pass entity id's that will be added to the config when a card is picked entities?: string[]; // We can pass entity id's that will be added to the config when a card is picked
} }

View File

@ -5,8 +5,9 @@ import type { LovelaceConfig } from "../../../../data/lovelace/config/types";
export interface EditCardDialogParams { export interface EditCardDialogParams {
lovelaceConfig: LovelaceConfig; lovelaceConfig: LovelaceConfig;
saveConfig: (config: LovelaceConfig) => void; saveConfig: (config: LovelaceConfig) => void;
path: [number] | [number, number]; path: [number, number | null];
cardConfig?: LovelaceCardConfig; // If specified, the card will be replaced with the new card.
newCardConfig?: LovelaceCardConfig;
} }
export const importEditCardDialog = () => import("./hui-dialog-edit-card"); export const importEditCardDialog = () => import("./hui-dialog-edit-card");

View File

@ -31,13 +31,19 @@ import {
LovelaceViewConfig, LovelaceViewConfig,
isStrategyView, isStrategyView,
} from "../../../data/lovelace/config/view"; } from "../../../data/lovelace/config/view";
import { HASSDomEvent } from "../../../common/dom/fire_event";
declare global { declare global {
// for fire event // for fire event
interface HASSDomEvents { interface HASSDomEvents {
"ll-create-card": undefined; "ll-create-card": undefined;
"ll-edit-card": { path: [number] | [number, number] }; "ll-edit-card": { path: [number, number] };
"ll-delete-card": { path: [number] | [number, number]; confirm: boolean }; "ll-delete-card": { path: [number, number]; confirm: boolean };
}
interface HTMLElementEventMap {
"ll-create-card": HASSDomEvent<HASSDomEvents["ll-create-card"]>;
"ll-edit-card": HASSDomEvent<HASSDomEvents["ll-edit-card"]>;
"ll-delete-card": HASSDomEvent<HASSDomEvents["ll-delete-card"]>;
} }
} }