Skip to content

Commit

Permalink
Move to dedicated section
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya committed Oct 22, 2024
1 parent 169a5f1 commit 52fccba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
56 changes: 43 additions & 13 deletions src/panels/lovelace/components/hui-card-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
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 @@ -358,10 +359,11 @@ export class HuiCardOptions extends LitElement {
allowDashboardChange: true,
header: this.hass!.localize("ui.panel.lovelace.editor.move_card.header"),
viewSelectedCallback: async (urlPath, selectedDashConfig, viewIndex) => {
let view = selectedDashConfig.views[viewIndex];
const fromView = selectedDashConfig.views[this.path![0]];
let toView = selectedDashConfig.views[viewIndex];
let newConfig = selectedDashConfig;

if (isStrategyView(view)) {
if (isStrategyView(toView)) {
showAlertDialog(this, {
title: this.hass!.localize(
"ui.panel.lovelace.editor.move_card.error_title"
Expand All @@ -374,19 +376,47 @@ export class HuiCardOptions extends LitElement {
return;
}

const isSectionsView = view.type === SECTIONS_VIEW_LAYOUT;

// If the view is a section view and has no sections, add a default section.
if (isSectionsView && !view.sections?.length) {
const newSection = { type: "grid", cards: [] };
newConfig = addSection(selectedDashConfig, viewIndex, newSection);
view = newConfig.views[viewIndex] as LovelaceViewConfig;
const isSectionsView = toView.type === SECTIONS_VIEW_LAYOUT;

let toPath: LovelaceContainerPath = [viewIndex];

// If the view is a section view and has no i"mported 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?.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 = isSectionsView
? [viewIndex, view.sections!.length - 1]
: [viewIndex];

if (urlPath === this.lovelace!.urlPath) {
this.lovelace!.saveConfig(
moveCardToContainer(newConfig, this.path!, toPath)
Expand Down
4 changes: 3 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5710,7 +5710,9 @@
"add_badge": "Add badge",
"add_card": "[%key:ui::panel::lovelace::editor::edit_card::add%]",
"create_section": "Create section",
"default_section_title": "New 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"
},
"delete_section": {
"title": "Delete section",
Expand Down

0 comments on commit 52fccba

Please sign in to comment.