mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-04 23:17:46 +00:00
Improve deletion functions
This commit is contained in:
parent
97c69e620c
commit
d24b9b6ced
@ -199,14 +199,14 @@ export class HuiBadgeEditMode extends LitElement {
|
|||||||
this._cutBadge();
|
this._cutBadge();
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
this._deleteBadge(true);
|
this._deleteBadge({ silent: false });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _cutBadge(): void {
|
private _cutBadge(): void {
|
||||||
this._copyBadge();
|
this._copyBadge();
|
||||||
this._deleteBadge(false);
|
this._deleteBadge({ silent: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
private _copyBadge(): void {
|
private _copyBadge(): void {
|
||||||
@ -231,8 +231,8 @@ export class HuiBadgeEditMode extends LitElement {
|
|||||||
fireEvent(this, "ll-edit-badge", { path: this.path! });
|
fireEvent(this, "ll-edit-badge", { path: this.path! });
|
||||||
}
|
}
|
||||||
|
|
||||||
private _deleteBadge(confirm: boolean): void {
|
private _deleteBadge({ silent }: { silent: boolean }): void {
|
||||||
fireEvent(this, "ll-delete-badge", { path: this.path!, confirm });
|
fireEvent(this, "ll-delete-badge", { path: this.path!, silent });
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
|
@ -199,7 +199,7 @@ export class HuiCardEditMode extends LitElement {
|
|||||||
this._cutCard();
|
this._cutCard();
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
this._deleteCard(true);
|
this._deleteCard({ silent: false });
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -222,7 +222,7 @@ export class HuiCardEditMode extends LitElement {
|
|||||||
|
|
||||||
private _cutCard(): void {
|
private _cutCard(): void {
|
||||||
this._copyCard();
|
this._copyCard();
|
||||||
this._deleteCard(false);
|
this._deleteCard({ silent: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
private _copyCard(): void {
|
private _copyCard(): void {
|
||||||
@ -231,8 +231,8 @@ export class HuiCardEditMode extends LitElement {
|
|||||||
this._clipboard = deepClone(cardConfig);
|
this._clipboard = deepClone(cardConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
private _deleteCard(confirm: boolean): void {
|
private _deleteCard({ silent }: { silent: boolean }): void {
|
||||||
fireEvent(this, "ll-delete-card", { path: this.path!, confirm });
|
fireEvent(this, "ll-delete-card", { path: this.path!, silent });
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
|
@ -1,30 +1,37 @@
|
|||||||
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
|
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { Lovelace } from "../types";
|
import { Lovelace } from "../types";
|
||||||
import { deleteBadge } from "./config-util";
|
import { deleteBadge } from "./config-util";
|
||||||
import { LovelaceCardPath } from "./lovelace-path";
|
import { LovelaceCardPath } from "./lovelace-path";
|
||||||
|
|
||||||
export async function deleteBadgeWithUndo(
|
export type DeleteBadgeParams = { path: LovelaceCardPath; silent: boolean };
|
||||||
element: HTMLElement,
|
|
||||||
|
export async function performDeleteBadge(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
lovelace: Lovelace,
|
lovelace: Lovelace,
|
||||||
path: LovelaceCardPath
|
params: DeleteBadgeParams
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
const { path, silent } = params;
|
||||||
const oldConfig = lovelace.config;
|
const oldConfig = lovelace.config;
|
||||||
const newConfig = deleteBadge(oldConfig, path);
|
const newConfig = deleteBadge(oldConfig, path);
|
||||||
|
await lovelace.saveConfig(newConfig);
|
||||||
|
|
||||||
|
if (silent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const action = async () => {
|
const action = async () => {
|
||||||
lovelace.saveConfig(oldConfig);
|
lovelace.saveConfig(oldConfig);
|
||||||
};
|
};
|
||||||
await lovelace.saveConfig(newConfig);
|
|
||||||
lovelace.showToast({
|
lovelace.showToast({
|
||||||
message: hass.localize("ui.common.successfully_deleted"),
|
message: hass.localize("ui.common.successfully_deleted"),
|
||||||
duration: 8000,
|
duration: 8000,
|
||||||
action: { action, text: hass.localize("ui.common.undo") },
|
action: { action, text: hass.localize("ui.common.undo") },
|
||||||
});
|
});
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
showAlertDialog(element, {
|
lovelace.showToast({
|
||||||
text: `Deleting failed: ${err.message}`,
|
message: `Deleting failed: ${err.message}`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,37 @@
|
|||||||
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
|
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { Lovelace } from "../types";
|
import { Lovelace } from "../types";
|
||||||
import { deleteCard } from "./config-util";
|
import { deleteCard } from "./config-util";
|
||||||
import { LovelaceCardPath } from "./lovelace-path";
|
import { LovelaceCardPath } from "./lovelace-path";
|
||||||
|
|
||||||
export async function deleteCardWithUndo(
|
export type DeleteCardParams = { path: LovelaceCardPath; silent: boolean };
|
||||||
element: HTMLElement,
|
|
||||||
|
export async function performDeleteCard(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
lovelace: Lovelace,
|
lovelace: Lovelace,
|
||||||
path: LovelaceCardPath
|
params: DeleteCardParams
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
|
const { path, silent } = params;
|
||||||
const oldConfig = lovelace.config;
|
const oldConfig = lovelace.config;
|
||||||
const newConfig = deleteCard(oldConfig, path);
|
const newConfig = deleteCard(oldConfig, path);
|
||||||
const action = async () => {
|
|
||||||
await lovelace.saveConfig(oldConfig);
|
|
||||||
};
|
|
||||||
await lovelace.saveConfig(newConfig);
|
await lovelace.saveConfig(newConfig);
|
||||||
|
|
||||||
|
if (silent) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const action = async () => {
|
||||||
|
lovelace.saveConfig(oldConfig);
|
||||||
|
};
|
||||||
|
|
||||||
lovelace.showToast({
|
lovelace.showToast({
|
||||||
message: hass.localize("ui.common.successfully_deleted"),
|
message: hass.localize("ui.common.successfully_deleted"),
|
||||||
duration: 8000,
|
duration: 8000,
|
||||||
action: { action, text: hass.localize("ui.common.undo") },
|
action: { action, text: hass.localize("ui.common.undo") },
|
||||||
});
|
});
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
showAlertDialog(element, {
|
lovelace.showToast({
|
||||||
text: `Deleting failed: ${err.message}`,
|
message: `Deleting failed: ${err.message}`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,7 @@ import {
|
|||||||
import { createSectionElement } from "../create-element/create-section-element";
|
import { createSectionElement } from "../create-element/create-section-element";
|
||||||
import { showCreateCardDialog } from "../editor/card-editor/show-create-card-dialog";
|
import { showCreateCardDialog } from "../editor/card-editor/show-create-card-dialog";
|
||||||
import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog";
|
import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog";
|
||||||
import { deleteCard } from "../editor/config-util";
|
import { performDeleteCard } from "../editor/delete-card";
|
||||||
import { deleteCardWithUndo } from "../editor/delete-card";
|
|
||||||
import { parseLovelaceCardPath } from "../editor/lovelace-path";
|
import { parseLovelaceCardPath } from "../editor/lovelace-path";
|
||||||
import { generateLovelaceSectionStrategy } from "../strategies/get-strategy";
|
import { generateLovelaceSectionStrategy } from "../strategies/get-strategy";
|
||||||
import type { Lovelace } from "../types";
|
import type { Lovelace } from "../types";
|
||||||
@ -257,17 +256,7 @@ export class HuiSection extends ReactiveElement {
|
|||||||
this._layoutElement.addEventListener("ll-delete-card", (ev) => {
|
this._layoutElement.addEventListener("ll-delete-card", (ev) => {
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
if (!this.lovelace) return;
|
if (!this.lovelace) return;
|
||||||
if (ev.detail.confirm) {
|
performDeleteCard(this.hass, this.lovelace, ev.detail);
|
||||||
deleteCardWithUndo(
|
|
||||||
this._layoutElement!,
|
|
||||||
this.hass!,
|
|
||||||
this.lovelace,
|
|
||||||
ev.detail.path
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
const newLovelace = deleteCard(this.lovelace!.config, ev.detail.path);
|
|
||||||
this.lovelace.saveConfig(newLovelace);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,9 +25,8 @@ import { showCreateBadgeDialog } from "../editor/badge-editor/show-create-badge-
|
|||||||
import { showEditBadgeDialog } from "../editor/badge-editor/show-edit-badge-dialog";
|
import { showEditBadgeDialog } from "../editor/badge-editor/show-edit-badge-dialog";
|
||||||
import { showCreateCardDialog } from "../editor/card-editor/show-create-card-dialog";
|
import { showCreateCardDialog } from "../editor/card-editor/show-create-card-dialog";
|
||||||
import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog";
|
import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog";
|
||||||
import { deleteBadge, deleteCard } from "../editor/config-util";
|
import { DeleteBadgeParams, performDeleteBadge } from "../editor/delete-badge";
|
||||||
import { deleteBadgeWithUndo } from "../editor/delete-badge";
|
import { DeleteCardParams, performDeleteCard } from "../editor/delete-card";
|
||||||
import { deleteCardWithUndo } from "../editor/delete-card";
|
|
||||||
import {
|
import {
|
||||||
LovelaceCardPath,
|
LovelaceCardPath,
|
||||||
parseLovelaceCardPath,
|
parseLovelaceCardPath,
|
||||||
@ -44,10 +43,10 @@ declare global {
|
|||||||
interface HASSDomEvents {
|
interface HASSDomEvents {
|
||||||
"ll-create-card": { suggested?: string[] } | undefined;
|
"ll-create-card": { suggested?: string[] } | undefined;
|
||||||
"ll-edit-card": { path: LovelaceCardPath };
|
"ll-edit-card": { path: LovelaceCardPath };
|
||||||
"ll-delete-card": { path: LovelaceCardPath; confirm: boolean };
|
"ll-delete-card": DeleteCardParams;
|
||||||
"ll-create-badge": undefined;
|
"ll-create-badge": undefined;
|
||||||
"ll-edit-badge": { path: LovelaceCardPath };
|
"ll-edit-badge": { path: LovelaceCardPath };
|
||||||
"ll-delete-badge": { path: LovelaceCardPath; confirm: boolean };
|
"ll-delete-badge": DeleteBadgeParams;
|
||||||
}
|
}
|
||||||
interface HTMLElementEventMap {
|
interface HTMLElementEventMap {
|
||||||
"ll-create-card": HASSDomEvent<HASSDomEvents["ll-create-card"]>;
|
"ll-create-card": HASSDomEvent<HASSDomEvents["ll-create-card"]>;
|
||||||
@ -324,17 +323,7 @@ export class HUIView extends ReactiveElement {
|
|||||||
});
|
});
|
||||||
this._layoutElement.addEventListener("ll-delete-card", (ev) => {
|
this._layoutElement.addEventListener("ll-delete-card", (ev) => {
|
||||||
if (!this.lovelace) return;
|
if (!this.lovelace) return;
|
||||||
if (ev.detail.confirm) {
|
performDeleteCard(this.hass, this.lovelace, ev.detail);
|
||||||
deleteCardWithUndo(
|
|
||||||
this._layoutElement!,
|
|
||||||
this.hass!,
|
|
||||||
this.lovelace!,
|
|
||||||
ev.detail.path
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
const newLovelace = deleteCard(this.lovelace!.config, ev.detail.path);
|
|
||||||
this.lovelace.saveConfig(newLovelace);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
this._layoutElement.addEventListener("ll-create-badge", async () => {
|
this._layoutElement.addEventListener("ll-create-badge", async () => {
|
||||||
showCreateBadgeDialog(this, {
|
showCreateBadgeDialog(this, {
|
||||||
@ -352,19 +341,9 @@ export class HUIView extends ReactiveElement {
|
|||||||
badgeIndex: cardIndex,
|
badgeIndex: cardIndex,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
this._layoutElement.addEventListener("ll-delete-badge", (ev) => {
|
this._layoutElement.addEventListener("ll-delete-badge", async (ev) => {
|
||||||
if (!this.lovelace) return;
|
if (!this.lovelace) return;
|
||||||
if (ev.detail.confirm) {
|
performDeleteBadge(this.hass, this.lovelace, ev.detail);
|
||||||
deleteBadgeWithUndo(
|
|
||||||
this._layoutElement!,
|
|
||||||
this.hass!,
|
|
||||||
this.lovelace!,
|
|
||||||
ev.detail.path
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
const newLovelace = deleteBadge(this.lovelace!.config, ev.detail.path);
|
|
||||||
this.lovelace.saveConfig(newLovelace);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user