Skip to content

Commit

Permalink
Use imported container for individual card move
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Oct 30, 2024
1 parent 375a021 commit 2f513a9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 62 deletions.
90 changes: 34 additions & 56 deletions src/panels/lovelace/components/hui-card-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,17 @@ 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,
type LovelaceViewConfig,
} from "../../../data/lovelace/config/view";
import { isStrategyView } from "../../../data/lovelace/config/view";
import {
showAlertDialog,
showPromptDialog,
} from "../../../dialogs/generic/show-dialog-box";
import { haStyle } from "../../../resources/styles";
import { HomeAssistant } from "../../../types";
import { showSaveSuccessToast } from "../../../util/toast-saved-success";
import { computeCardSize } from "../common/compute-card-size";
import { showEditCardDialog } from "../editor/card-editor/show-edit-card-dialog";
import {
addCard,
addSection,
deleteCard,
moveCardToContainer,
moveCardToIndex,
Expand All @@ -57,8 +52,6 @@ import {
} from "../editor/lovelace-path";
import { showSelectViewDialog } from "../editor/select-view/show-select-view-dialog";
import { Lovelace, LovelaceCard } from "../types";
import { SECTIONS_VIEW_LAYOUT } from "../views/const";
import type { LovelaceSectionConfig } from "../../../data/lovelace/config/section";

@customElement("hui-card-options")
export class HuiCardOptions extends LitElement {
Expand Down Expand Up @@ -359,9 +352,13 @@ export class HuiCardOptions extends LitElement {
allowDashboardChange: true,
header: this.hass!.localize("ui.panel.lovelace.editor.move_card.header"),
viewSelectedCallback: async (urlPath, selectedDashConfig, viewIndex) => {
const fromView = selectedDashConfig.views[this.path![0]];
let toView = selectedDashConfig.views[viewIndex];
let newConfig = selectedDashConfig;
if (!this.lovelace) return;
const toView = selectedDashConfig.views[viewIndex];
const newConfig = selectedDashConfig;

const undoAction = async () => {
this.lovelace!.saveConfig(selectedDashConfig);
};

if (isStrategyView(toView)) {
showAlertDialog(this, {
Expand All @@ -376,53 +373,22 @@ export class HuiCardOptions extends LitElement {
return;
}

const isSectionsView = toView.type === SECTIONS_VIEW_LAYOUT;

let toPath: LovelaceContainerPath = [viewIndex];

// If the view is a section view and has no "imported cards" section, adds a default section.
if (isSectionsView) {
const importedCardHeading = fromView.title
? this.hass!.localize(
"ui.panel.lovelace.editor.section.imported_card_section_title_view",
{ view_title: fromView.title }
)
: this.hass!.localize(
"ui.panel.lovelace.editor.section.imported_card_section_title_default"
);

let sectionIndex = toView.sections
? toView.sections.findIndex(
(s) =>
"cards" in s &&
s.cards?.some(
(c) =>
c.type === "heading" && c.heading === importedCardHeading
)
)
: -1;
if (sectionIndex === -1) {
const newSection: LovelaceSectionConfig = {
type: "grid",
cards: [
{
type: "heading",
heading: importedCardHeading,
},
],
};
newConfig = addSection(selectedDashConfig, viewIndex, newSection);
toView = newConfig.views[viewIndex] as LovelaceViewConfig;
sectionIndex = toView.sections!.length - 1;
}
toPath = [viewIndex, sectionIndex];
}
const toPath: LovelaceContainerPath = [viewIndex];

if (urlPath === this.lovelace!.urlPath) {
this.lovelace!.saveConfig(
moveCardToContainer(newConfig, this.path!, toPath)
);
showSaveSuccessToast(this, this.hass!);
this.lovelace.showToast({
message: this.hass!.localize(
"ui.panel.lovelace.editor.move_card.success"
),
duration: 4000,
action: {
action: undoAction,
text: this.hass!.localize("ui.common.undo"),
},
});
return;
}
try {
Expand All @@ -436,10 +402,22 @@ export class HuiCardOptions extends LitElement {
this.lovelace!.saveConfig(
deleteCard(this.lovelace!.config, this.path!)
);
showSaveSuccessToast(this, this.hass!);

this.lovelace.showToast({
message: this.hass!.localize(
"ui.panel.lovelace.editor.move_card.success"
),
duration: 4000,
action: {
action: undoAction,
text: this.hass!.localize("ui.common.undo"),
},
});
} catch (err: any) {
showAlertDialog(this, {
text: `Moving failed: ${err.message}`,
this.lovelace.showToast({
message: this.hass!.localize(
"ui.panel.lovelace.editor.move_card.error"
),
});
}
},
Expand Down
10 changes: 4 additions & 6 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5708,7 +5708,9 @@
"move_card": {
"header": "Choose a view to move the card to",
"strategy_error_title": "Impossible to move the card",
"strategy_error_text_strategy": "Moving a card to an auto generated view is not supported."
"strategy_error_text_strategy": "Moving a card to an auto generated view is not supported.",
"success": "Card moved successfully",
"error": "Error while moving card"
},
"change_position": {
"title": "Change card position",
Expand All @@ -5726,11 +5728,7 @@
"add_badge": "Add badge",
"add_card": "[%key:ui::panel::lovelace::editor::edit_card::add%]",
"create_section": "Create section",
"default_section_title": "New section",
"imported_card_section_title_view": "Imported cards from ''{view_title}'' view",
"imported_card_section_title_default": "Imported cards from another view",
"imported_cards_title": "Imported cards",
"imported_cards_description": "These cards are imported from another view. They will only be displayed in edit mode. Move them into sections to display them in your view."
"default_section_title": "New section"
},
"delete_section": {
"title": "Delete section",
Expand Down

0 comments on commit 2f513a9

Please sign in to comment.