Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/panels/lovelace/editor/card-editor/hui-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
import type { HomeAssistant } from "../../../../types";
import { handleStructError } from "../../common/structs/handle-errors";
import { getCardElementClass } from "../../create-element/create-card-element";
import type { EntityConfig } from "../../entity-rows/types";
import type { LovelaceRowConfig } from "../../entity-rows/types";
import type { LovelaceCardEditor } from "../../types";
import { GUISupportError } from "../gui-support-error";
import type { GUIModeChangedEvent } from "../types";
Expand All @@ -38,7 +38,7 @@ export interface ConfigChangedEvent {
declare global {
interface HASSDomEvents {
"entities-changed": {
entities: EntityConfig[];
entities: LovelaceRowConfig[];
};
"config-changed": ConfigChangedEvent;
"GUImode-changed": GUIModeChangedEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@ import "@polymer/paper-listbox/paper-listbox";
import {
customElement,
html,
internalProperty,
LitElement,
property,
internalProperty,
TemplateResult,
} from "lit-element";
import {
array,
assert,
boolean,
object,
optional,
string,
union,
} from "superstruct";
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
import "../../../../components/entity/state-badge";
import "../../../../components/ha-card";
import "../../../../components/ha-formfield";
import "../../../../components/ha-icon";
import "../../../../components/ha-switch";
import "../../../../components/ha-formfield";
import { HomeAssistant } from "../../../../types";
import {
EntitiesCardConfig,
EntitiesCardEntityConfig,
} from "../../cards/types";
import "../../components/hui-entity-editor";
import "../../components/hui-theme-select-editor";
import { headerFooterConfigStructs } from "../../header-footer/types";
import { LovelaceCardEditor } from "../../types";
import "../hui-entities-card-row-editor";
import { processEditorEntities } from "../process-editor-entities";
import {
EditorTarget,
entitiesConfigStruct,
EntitiesEditorEvent,
} from "../types";
import { configElementStyle } from "./config-elements-style";
import { computeRTLDirection } from "../../../../common/util/compute_rtl";
import {
string,
optional,
object,
boolean,
array,
union,
assert,
} from "superstruct";

const cardConfigStruct = object({
type: string(),
Expand Down Expand Up @@ -127,11 +127,12 @@ export class HuiEntitiesCardEditor extends LitElement
</ha-formfield>
</div>
</div>
<hui-entity-editor

<hui-entities-card-row-editor
.hass=${this.hass}
.entities=${this._configEntities}
@entities-changed=${this._valueChanged}
></hui-entity-editor>
></hui-entities-card-row-editor>
`;
}

Expand Down
271 changes: 271 additions & 0 deletions src/panels/lovelace/editor/hui-entities-card-row-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,271 @@
import { mdiClose, mdiDrag } from "@mdi/js";
import {
css,
CSSResult,
customElement,
html,
internalProperty,
LitElement,
property,
PropertyValues,
TemplateResult,
} from "lit-element";
import { guard } from "lit-html/directives/guard";
import type { SortableEvent } from "sortablejs";
import Sortable, {
AutoScroll,
OnSpill,
} from "sortablejs/modular/sortable.core.esm";
import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/entity/ha-entity-picker";
import type { HaEntityPicker } from "../../../components/entity/ha-entity-picker";
import "../../../components/ha-icon-button";
import { sortableStyles } from "../../../resources/ha-sortable-style";
import { HomeAssistant } from "../../../types";
import { EntityConfig, LovelaceRowConfig } from "../entity-rows/types";

@customElement("hui-entities-card-row-editor")
export class HuiEntitiesCardRowEditor extends LitElement {
@property({ attribute: false }) protected hass?: HomeAssistant;

@property({ attribute: false }) protected entities?: LovelaceRowConfig[];

@property() protected label?: string;

@internalProperty() private _attached = false;

@internalProperty() private _renderEmptySortable = false;

private _sortable?: Sortable;

public connectedCallback() {
super.connectedCallback();
this._attached = true;
}

public disconnectedCallback() {
super.disconnectedCallback();
this._attached = false;
}

protected render(): TemplateResult {
if (!this.entities || !this.hass) {
return html``;
}

return html`
<h3>
${this.label ||
`${this.hass!.localize(
"ui.panel.lovelace.editor.card.generic.entities"
)} (${this.hass!.localize(
"ui.panel.lovelace.editor.card.config.required"
)})`}
</h3>
<div class="entities">
${guard([this.entities, this._renderEmptySortable], () =>
this._renderEmptySortable
? ""
: this.entities!.map((entityConf, index) => {
return html`
<div class="entity">
<ha-svg-icon class="handle" .path=${mdiDrag}></ha-svg-icon>
${entityConf.type
? html`
<div class="special-row">
<div>
<span>
${this.hass!.localize(
`ui.panel.lovelace.editor.card.entities.entity_row.${entityConf.type}`
)}
${this.hass!.localize(
"ui.panel.lovelace.editor.card.entities.special_row"
)}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should combine this in 1 localize:

Suggested change
${this.hass!.localize(
`ui.panel.lovelace.editor.card.entities.entity_row.${entityConf.type}`
)}
${this.hass!.localize(
"ui.panel.lovelace.editor.card.entities.special_row"
)}
${this.hass!.localize(
"ui.panel.lovelace.editor.card.entities.special_row", "row_type", this.hass!.localize(
`ui.panel.lovelace.editor.card.entities.entity_row.${entityConf.type}`
)
)}

But maybe we should just drop the special_row? Just "Button row" would be enough?

</span>
<span class="secondary"
>${this.hass!.localize(
"ui.panel.lovelace.editor.card.entities.edit_special_row"
)}</span
>
</div>
<mwc-icon-button
aria-label=${this.hass!.localize(
"ui.components.entity.entity-picker.clear"
)}
.index=${index}
@click=${this._removeSpecialRow}
>
<ha-svg-icon .path=${mdiClose}></ha-svg-icon>
</mwc-icon-button>
</div>
Comment thread
zsarnett marked this conversation as resolved.
`
: html`
<ha-entity-picker
allow-custom-entity
.hass=${this.hass}
.value=${(entityConf as EntityConfig).entity}
.index=${index}
@value-changed=${this._valueChanged}
></ha-entity-picker>
`}
</div>
`;
})
)}
</div>
<ha-entity-picker
.hass=${this.hass}
@value-changed=${this._addEntity}
></ha-entity-picker>
`;
}

protected firstUpdated(): void {
Sortable.mount(OnSpill);
Sortable.mount(new AutoScroll());
}

protected updated(changedProps: PropertyValues): void {
super.updated(changedProps);

const attachedChanged = changedProps.has("_attached");
const entitiesChanged = changedProps.has("entities");

if (!entitiesChanged && !attachedChanged) {
return;
}

if (attachedChanged && !this._attached) {
// Tear down sortable, if available
this._sortable?.destroy();
this._sortable = undefined;
return;
}

if (!this._sortable && this.entities) {
this._createSortable();
return;
}

if (entitiesChanged) {
this._handleEntitiesChanged();
}
}

private async _handleEntitiesChanged() {
this._renderEmptySortable = true;
await this.updateComplete;
const container = this.shadowRoot!.querySelector(".entities")!;
while (container.lastElementChild) {
container.removeChild(container.lastElementChild);
}
this._renderEmptySortable = false;
}

private _createSortable() {
this._sortable = new Sortable(this.shadowRoot!.querySelector(".entities"), {
animation: 150,
fallbackClass: "sortable-fallback",
handle: ".handle",
onEnd: async (evt: SortableEvent) => this._entityMoved(evt),
});
}

private async _addEntity(ev: CustomEvent): Promise<void> {
const value = ev.detail.value;
if (value === "") {
return;
}
const newConfigEntities = this.entities!.concat({
entity: value as string,
});
(ev.target as HaEntityPicker).value = "";
fireEvent(this, "entities-changed", { entities: newConfigEntities });
}

private _entityMoved(ev: SortableEvent): void {
if (ev.oldIndex === ev.newIndex) {
return;
}

const newEntities = this.entities!.concat();

newEntities.splice(ev.newIndex!, 0, newEntities.splice(ev.oldIndex!, 1)[0]);

fireEvent(this, "entities-changed", { entities: newEntities });
}

private _removeSpecialRow(ev: CustomEvent): void {
const index = (ev.currentTarget as any).index;
const newConfigEntities = this.entities!.concat();

newConfigEntities.splice(index, 1);

fireEvent(this, "entities-changed", { entities: newConfigEntities });
}

private _valueChanged(ev: CustomEvent): void {
const value = ev.detail.value;
const index = (ev.target as any).index;
const newConfigEntities = this.entities!.concat();

if (value === "") {
newConfigEntities.splice(index, 1);
} else {
newConfigEntities[index] = {
...newConfigEntities[index],
entity: value!,
};
}

fireEvent(this, "entities-changed", { entities: newConfigEntities });
}

static get styles(): CSSResult[] {
return [
sortableStyles,
css`
.entity {
display: flex;
align-items: center;
}
.entity .handle {
padding-right: 8px;
cursor: move;
}
.entity ha-entity-picker {
flex-grow: 1;
}
.special-row {
height: 60px;
font-size: 16px;
display: flex;
align-items: center;
justify-content: space-between;
flex-grow: 1;
}

.special-row div {
display: flex;
flex-direction: column;
}

.special-row mwc-icon-button {
--mdc-icon-button-size: 36px;
color: var(--secondary-text-color);
}

.secondary {
font-size: 12px;
color: var(--secondary-text-color);
}
`,
];
}
}

declare global {
interface HTMLElementTagNameMap {
"hui-entities-card-row-editor": HuiEntitiesCardRowEditor;
}
}
Loading