Skip to content

Commit

Permalink
Block moving card to a section view or a strategy view (#20016)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Mar 7, 2024
1 parent 3b885dd commit 233c969
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
17 changes: 17 additions & 0 deletions src/panels/lovelace/components/hui-card-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import "../../../components/ha-icon-button";
import "../../../components/ha-list-item";
import { LovelaceCardConfig } from "../../../data/lovelace/config/card";
import { saveConfig } from "../../../data/lovelace/config/types";
import { isStrategyView } from "../../../data/lovelace/config/view";
import {
showAlertDialog,
showPromptDialog,
Expand All @@ -51,6 +52,7 @@ import {
} from "../editor/lovelace-path";
import { showSelectViewDialog } from "../editor/select-view/show-select-view-dialog";
import { Lovelace, LovelaceCard } from "../types";
import { SECTION_VIEW_LAYOUT } from "../views/const";

@customElement("hui-card-options")
export class HuiCardOptions extends LitElement {
Expand Down Expand Up @@ -351,6 +353,21 @@ export class HuiCardOptions extends LitElement {
allowDashboardChange: true,
header: this.hass!.localize("ui.panel.lovelace.editor.move_card.header"),
viewSelectedCallback: async (urlPath, selectedDashConfig, viewIndex) => {
const view = this.lovelace!.config.views[viewIndex];

if (!isStrategyView(view) && view.type === SECTION_VIEW_LAYOUT) {
showAlertDialog(this, {
title: this.hass!.localize(
"ui.panel.lovelace.editor.move_card.error_title"
),
text: this.hass!.localize(
"ui.panel.lovelace.editor.move_card.error_text_section"
),
warning: true,
});
return;
}

if (urlPath === this.lovelace!.urlPath) {
this.lovelace!.saveConfig(
moveCardToContainer(this.lovelace!.config, this.path!, [viewIndex])
Expand Down
20 changes: 15 additions & 5 deletions src/panels/lovelace/editor/select-view/hui-dialog-select-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
import { haStyleDialog } from "../../../../resources/styles";
import { HomeAssistant } from "../../../../types";
import type { SelectViewDialogParams } from "./show-select-view-dialog";
import { isStrategyView } from "../../../../data/lovelace/config/view";

declare global {
interface HASSDomEvents {
Expand Down Expand Up @@ -117,21 +118,30 @@ export class HuiDialogSelectView extends LitElement {
: this._config.views.length > 1
? html`
<mwc-list dialogInitialFocus>
${this._config.views.map(
(view, idx) => html`
${this._config.views.map((view, idx) => {
const isStrategy = isStrategyView(view);
return html`
<mwc-radio-list-item
.graphic=${this._config?.views.some(({ icon }) => icon)
? "icon"
: nothing}
@click=${this._viewChanged}
.value=${idx.toString()}
.selected=${this._selectedViewIdx === idx}
.disabled=${isStrategy &&
!this._params?.includeStrategyViews}
>
<span>${view.title}</span>
<span>
${view.title}${isStrategy
? ` (${this.hass.localize("ui.panel.lovelace.editor.select_view.strategy_type")})`
: nothing}
</span>
<ha-icon .icon=${view.icon} slot="graphic"></ha-icon>
</mwc-radio-list-item>
`
)}
`;
})}
</mwc-list>
`
: ""}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LovelaceDashboard } from "../../../../data/lovelace/dashboard";
export interface SelectViewDialogParams {
lovelaceConfig: LovelaceConfig;
allowDashboardChange: boolean;
includeStrategyViews?: boolean;
dashboards?: LovelaceDashboard[];
urlPath?: string | null;
header?: string;
Expand Down
7 changes: 5 additions & 2 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5184,7 +5184,9 @@
"search_cards": "Search cards"
},
"move_card": {
"header": "Choose a view to move the card to"
"header": "Choose a view to move the card to",
"error_title": "Impossible to move the card",
"error_text_section": "Moving a card to a section view is not supported yet. Use copy/cut/paste instead."
},
"change_position": {
"title": "Change card position",
Expand All @@ -5195,7 +5197,8 @@
"dashboard_label": "Dashboard",
"views_label": "View",
"no_config": "No config found.",
"no_views": "No views in this dashboard."
"no_views": "No views in this dashboard.",
"strategy_type": "strategy"
},
"section": {
"unnamed_section": "Unnamed section",
Expand Down

0 comments on commit 233c969

Please sign in to comment.