Automatically add section to empty section view and improve delete wording (#19892)

* Automatically add section to empty section view and improve delete wording

* Update delete dialog

* Update delete dialog
This commit is contained in:
Paul Bottein 2024-02-28 11:35:19 +01:00 committed by GitHub
parent 0892ed18e5
commit f4c932ef9c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 70 deletions

View File

@ -4,16 +4,16 @@ import { mdiCheck, mdiClose, mdiDotsVertical } from "@mdi/js";
import "@polymer/paper-tabs/paper-tab"; import "@polymer/paper-tabs/paper-tab";
import "@polymer/paper-tabs/paper-tabs"; import "@polymer/paper-tabs/paper-tabs";
import { import {
css,
CSSResultGroup, CSSResultGroup,
html,
LitElement, LitElement,
nothing,
PropertyValues, PropertyValues,
css,
html,
nothing,
} from "lit"; } from "lit";
import { customElement, property, query, state } from "lit/decorators"; import { customElement, property, query, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map"; import { classMap } from "lit/directives/class-map";
import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event"; import { HASSDomEvent, fireEvent } from "../../../../common/dom/fire_event";
import { stopPropagation } from "../../../../common/dom/stop_propagation"; import { stopPropagation } from "../../../../common/dom/stop_propagation";
import { navigate } from "../../../../common/navigate"; import { navigate } from "../../../../common/navigate";
import { deepEqual } from "../../../../common/util/deep-equal"; import { deepEqual } from "../../../../common/util/deep-equal";
@ -23,6 +23,11 @@ import "../../../../components/ha-dialog";
import "../../../../components/ha-dialog-header"; import "../../../../components/ha-dialog-header";
import "../../../../components/ha-yaml-editor"; import "../../../../components/ha-yaml-editor";
import type { HaYamlEditor } from "../../../../components/ha-yaml-editor"; import type { HaYamlEditor } from "../../../../components/ha-yaml-editor";
import { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge";
import {
LovelaceViewConfig,
isStrategyView,
} from "../../../../data/lovelace/config/view";
import { import {
showAlertDialog, showAlertDialog,
showConfirmationDialog, showConfirmationDialog,
@ -33,6 +38,7 @@ import "../../components/hui-entity-editor";
import { import {
DEFAULT_VIEW_LAYOUT, DEFAULT_VIEW_LAYOUT,
PANEL_VIEW_LAYOUT, PANEL_VIEW_LAYOUT,
SECTION_VIEW_LAYOUT,
VIEWS_NO_BADGE_SUPPORT, VIEWS_NO_BADGE_SUPPORT,
} from "../../views/const"; } from "../../views/const";
import { addView, deleteView, replaceView } from "../config-util"; import { addView, deleteView, replaceView } from "../config-util";
@ -46,12 +52,6 @@ import {
import "./hui-view-editor"; import "./hui-view-editor";
import "./hui-view-visibility-editor"; import "./hui-view-visibility-editor";
import { EditViewDialogParams } from "./show-edit-view-dialog"; import { EditViewDialogParams } from "./show-edit-view-dialog";
import {
LovelaceViewConfig,
isStrategyView,
} from "../../../../data/lovelace/config/view";
import { LovelaceBadgeConfig } from "../../../../data/lovelace/config/badge";
import { LovelaceCardConfig } from "../../../../data/lovelace/config/card";
@customElement("hui-dialog-edit-view") @customElement("hui-dialog-edit-view")
export class HuiDialogEditView extends LitElement { export class HuiDialogEditView extends LitElement {
@ -63,8 +63,6 @@ export class HuiDialogEditView extends LitElement {
@state() private _badges?: LovelaceBadgeConfig[]; @state() private _badges?: LovelaceBadgeConfig[];
@state() private _cards?: LovelaceCardConfig[];
@state() private _saving = false; @state() private _saving = false;
@state() private _curTab?: string; @state() private _curTab?: string;
@ -102,7 +100,6 @@ export class HuiDialogEditView extends LitElement {
if (this._params.viewIndex === undefined) { if (this._params.viewIndex === undefined) {
this._config = {}; this._config = {};
this._badges = []; this._badges = [];
this._cards = [];
this._dirty = false; this._dirty = false;
return; return;
} }
@ -112,13 +109,11 @@ export class HuiDialogEditView extends LitElement {
const { strategy, ...viewConfig } = view; const { strategy, ...viewConfig } = view;
this._config = viewConfig; this._config = viewConfig;
this._badges = []; this._badges = [];
this._cards = [];
return; return;
} }
const { cards, badges, ...viewConfig } = view; const { badges, ...viewConfig } = view;
this._config = viewConfig; this._config = viewConfig;
this._badges = badges ? processEditorEntities(badges) : []; this._badges = badges ? processEditorEntities(badges) : [];
this._cards = cards;
} }
public closeDialog(): void { public closeDialog(): void {
@ -181,7 +176,7 @@ export class HuiDialogEditView extends LitElement {
)} )}
</ha-alert> </ha-alert>
` `
: ""} : nothing}
<div class="preview-badges"> <div class="preview-badges">
${this._badges.map( ${this._badges.map(
(badgeConfig) => html` (badgeConfig) => html`
@ -193,7 +188,7 @@ export class HuiDialogEditView extends LitElement {
)} )}
</div> </div>
` `
: ""} : nothing}
<hui-entity-editor <hui-entity-editor
.hass=${this.hass} .hass=${this.hass}
.entities=${this._badges} .entities=${this._badges}
@ -297,7 +292,7 @@ export class HuiDialogEditView extends LitElement {
)}</paper-tab )}</paper-tab
> >
</paper-tabs>` </paper-tabs>`
: ""} : nothing}
</ha-dialog-header> </ha-dialog-header>
${content} ${content}
${this._params.viewIndex !== undefined ${this._params.viewIndex !== undefined
@ -312,7 +307,7 @@ export class HuiDialogEditView extends LitElement {
)} )}
</mwc-button> </mwc-button>
` `
: ""} : nothing}
<mwc-button <mwc-button
slot="primaryAction" slot="primaryAction"
?disabled=${!this._config || this._saving || !this._dirty} ?disabled=${!this._config || this._saving || !this._dirty}
@ -324,7 +319,7 @@ export class HuiDialogEditView extends LitElement {
size="small" size="small"
aria-label="Saving" aria-label="Saving"
></ha-circular-progress>` ></ha-circular-progress>`
: ""} : nothing}
${this.hass!.localize("ui.common.save")}</mwc-button ${this.hass!.localize("ui.common.save")}</mwc-button
> >
</ha-dialog> </ha-dialog>
@ -361,24 +356,28 @@ export class HuiDialogEditView extends LitElement {
} }
} }
private _deleteConfirm(): void { private async _deleteConfirm() {
showConfirmationDialog(this, { const type = this._config?.sections?.length
title: this.hass!.localize( ? "sections"
`ui.panel.lovelace.views.confirm_delete${ : this._config?.cards?.length
this._cards?.length ? `_existing_cards` : "" ? "cards"
}` : "only";
),
const named = this._config?.title ? "named" : "unnamed";
const confirm = await showConfirmationDialog(this, {
title: this.hass!.localize("ui.panel.lovelace.views.delete_title"),
text: this.hass!.localize( text: this.hass!.localize(
`ui.panel.lovelace.views.confirm_delete${ `ui.panel.lovelace.views.delete_${named}_view_${type}`,
this._cards?.length ? "_existing_cards" : "" { name: this._config?.title }
}_text`,
{
name: this._config?.title || "Unnamed view",
number: this._cards?.length || 0,
}
), ),
confirm: () => this._delete(), confirmText: this.hass!.localize("ui.common.delete"),
destructive: true,
}); });
if (!confirm) return;
this._delete();
} }
private _handleTabSelected(ev: CustomEvent): void { private _handleTabSelected(ev: CustomEvent): void {
@ -402,9 +401,18 @@ export class HuiDialogEditView extends LitElement {
const viewConf: LovelaceViewConfig = { const viewConf: LovelaceViewConfig = {
...this._config, ...this._config,
badges: this._badges, badges: this._badges,
cards: this._cards,
}; };
if (viewConf.type === SECTION_VIEW_LAYOUT && !viewConf.sections?.length) {
viewConf.sections = [{ cards: [] }];
} else if (!viewConf.cards?.length) {
viewConf.cards = [];
}
if (!viewConf.badges?.length) {
delete viewConf.badges;
}
const lovelace = this._params.lovelace!; const lovelace = this._params.lovelace!;
try { try {
@ -469,7 +477,7 @@ export class HuiDialogEditView extends LitElement {
if (!ev.detail.isValid) { if (!ev.detail.isValid) {
return; return;
} }
const { badges = [], ...config } = ev.detail.value; const { badges, ...config } = ev.detail.value;
this._config = config; this._config = config;
this._badges = badges; this._badges = badges;
this._dirty = true; this._dirty = true;

View File

@ -180,38 +180,21 @@ export class SectionsView extends LitElement implements LovelaceViewElement {
path path
) as LovelaceRawSectionConfig; ) as LovelaceRawSectionConfig;
const title = section.title; const title = section.title?.trim();
const cardCount = section.cards?.length; const cardCount = section.cards?.length;
if (title || cardCount) { if (title || cardCount) {
const sectionName = title?.trim() const named = title ? "named" : "unnamed";
? this.hass.localize( const type = cardCount ? "cards" : "only";
"ui.panel.lovelace.editor.delete_section.named_section",
{ name: title }
)
: this.hass.localize(
"ui.panel.lovelace.editor.delete_section.unnamed_section"
);
const content = cardCount
? this.hass.localize(
"ui.panel.lovelace.editor.delete_section.text_section_and_cards",
{
section: sectionName,
}
)
: this.hass.localize(
"ui.panel.lovelace.editor.delete_section.text_section_only",
{
section: sectionName,
}
);
const confirm = await showConfirmationDialog(this, { const confirm = await showConfirmationDialog(this, {
title: this.hass.localize( title: this.hass.localize(
"ui.panel.lovelace.editor.delete_section.title" "ui.panel.lovelace.editor.delete_section.title"
), ),
text: content, text: this.hass.localize(
`ui.panel.lovelace.editor.delete_section.text_${named}_section_${type}`,
{ name: title }
),
confirmText: this.hass.localize("ui.common.delete"), confirmText: this.hass.localize("ui.common.delete"),
destructive: true, destructive: true,
}); });

View File

@ -5026,10 +5026,13 @@
"saving_failed": "Saving dashboard configuration failed." "saving_failed": "Saving dashboard configuration failed."
}, },
"views": { "views": {
"confirm_delete": "Delete view?", "delete_title": "Delete view",
"confirm_delete_text": "Are you sure you want to delete your ''{name}'' view?", "delete_named_view_only": "''{name}'' view will be deleted.",
"confirm_delete_existing_cards": "Deleting this view will also remove the cards", "delete_unnamed_view_only": "This view will be deleted.",
"confirm_delete_existing_cards_text": "Are you sure you want to delete your ''{name}'' view? The view contains {number} cards that will be deleted. This action cannot be undone." "delete_named_view_cards": "''{name}'' view and all its cards will be deleted.",
"delete_unnamed_view_cards": "This view and all its cards will be deleted.",
"delete_named_view_sections": "''{name}'' view and all its sections will be deleted.",
"delete_unnamed_view_sections": "This view and all its sections will be deleted."
}, },
"menu": { "menu": {
"configure_ui": "Edit dashboard", "configure_ui": "Edit dashboard",
@ -5162,10 +5165,10 @@
}, },
"delete_section": { "delete_section": {
"title": "Delete section", "title": "Delete section",
"named_section": "\"{name}\" section", "text_named_section_only": "''{name}'' section will be deleted.",
"unnamed_section": "This section", "text_unnamed_section_only": "This section will be deleted.",
"text_section_only": "{section} will be deleted.", "text_named_section_cards": "''{name}'' section and all its cards will be deleted.",
"text_section_and_cards": "{section} and all its cards will be deleted." "text_unnamed_section_cards": "This section and all its cards will be deleted."
}, },
"edit_section_title": { "edit_section_title": {
"title": "Edit name", "title": "Edit name",