mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 11:16:35 +00:00
Enable reorder mode for choose options (#17422)
This commit is contained in:
parent
5455ce2e0f
commit
74be4ae20a
@ -1,7 +1,13 @@
|
|||||||
import { consume } from "@lit-labs/context";
|
import { consume } from "@lit-labs/context";
|
||||||
import { mdiDelete, mdiPlus } from "@mdi/js";
|
import type { SortableEvent } from "sortablejs";
|
||||||
|
import { mdiDelete, mdiPlus, mdiArrowUp, mdiArrowDown, mdiDrag } from "@mdi/js";
|
||||||
import { CSSResultGroup, LitElement, PropertyValues, css, html } from "lit";
|
import { CSSResultGroup, LitElement, PropertyValues, css, html } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
|
import { repeat } from "lit/directives/repeat";
|
||||||
|
import {
|
||||||
|
loadSortable,
|
||||||
|
SortableInstance,
|
||||||
|
} from "../../../../../resources/sortable.ondemand";
|
||||||
import { ensureArray } from "../../../../../common/array/ensure-array";
|
import { ensureArray } from "../../../../../common/array/ensure-array";
|
||||||
import { fireEvent } from "../../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../../common/dom/fire_event";
|
||||||
import "../../../../../components/ha-button";
|
import "../../../../../components/ha-button";
|
||||||
@ -14,6 +20,7 @@ import { ActionElement } from "../ha-automation-action-row";
|
|||||||
import { describeCondition } from "../../../../../data/automation_i18n";
|
import { describeCondition } from "../../../../../data/automation_i18n";
|
||||||
import { fullEntitiesContext } from "../../../../../data/context";
|
import { fullEntitiesContext } from "../../../../../data/context";
|
||||||
import { EntityRegistryEntry } from "../../../../../data/entity_registry";
|
import { EntityRegistryEntry } from "../../../../../data/entity_registry";
|
||||||
|
import { sortableStyles } from "../../../../../resources/ha-sortable-style";
|
||||||
|
|
||||||
@customElement("ha-automation-action-choose")
|
@customElement("ha-automation-action-choose")
|
||||||
export class HaChooseAction extends LitElement implements ActionElement {
|
export class HaChooseAction extends LitElement implements ActionElement {
|
||||||
@ -27,59 +34,30 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
|
|
||||||
@state() private _showDefault = false;
|
@state() private _showDefault = false;
|
||||||
|
|
||||||
@state() private expandedUpdateFlag = false;
|
@state() private _expandedStates: boolean[] = [];
|
||||||
|
|
||||||
@state()
|
@state()
|
||||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||||
_entityReg!: EntityRegistryEntry[];
|
_entityReg!: EntityRegistryEntry[];
|
||||||
|
|
||||||
|
private _expandLast = false;
|
||||||
|
|
||||||
|
private _sortable?: SortableInstance;
|
||||||
|
|
||||||
public static get defaultConfig() {
|
public static get defaultConfig() {
|
||||||
return { choose: [{ conditions: [], sequence: [] }] };
|
return { choose: [{ conditions: [], sequence: [] }] };
|
||||||
}
|
}
|
||||||
|
|
||||||
protected willUpdate(changedProperties: PropertyValues) {
|
private _expandedChanged(ev) {
|
||||||
if (!changedProperties.has("action")) {
|
this._expandedStates = this._expandedStates.concat();
|
||||||
return;
|
this._expandedStates[ev.target!.index] = ev.detail.expanded;
|
||||||
}
|
|
||||||
|
|
||||||
const oldCnt =
|
|
||||||
changedProperties.get("action") === undefined ||
|
|
||||||
changedProperties.get("action").choose === undefined
|
|
||||||
? 0
|
|
||||||
: ensureArray(changedProperties.get("action").choose).length;
|
|
||||||
const newCnt = this.action.choose
|
|
||||||
? ensureArray(this.action.choose).length
|
|
||||||
: 0;
|
|
||||||
if (newCnt === oldCnt + 1) {
|
|
||||||
this.expand(newCnt - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private expand(i: number) {
|
|
||||||
this.updateComplete.then(() => {
|
|
||||||
this.shadowRoot!.querySelectorAll("ha-expansion-panel")[i].expanded =
|
|
||||||
true;
|
|
||||||
this.expandedUpdateFlag = !this.expandedUpdateFlag;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private isExpanded(i: number) {
|
|
||||||
const nodes = this.shadowRoot!.querySelectorAll("ha-expansion-panel");
|
|
||||||
if (nodes[i]) {
|
|
||||||
return nodes[i].expanded;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private _expandedChanged() {
|
|
||||||
this.expandedUpdateFlag = !this.expandedUpdateFlag;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _getDescription(option, idx: number) {
|
private _getDescription(option, idx: number) {
|
||||||
if (option.alias) {
|
if (option.alias) {
|
||||||
return option.alias;
|
return option.alias;
|
||||||
}
|
}
|
||||||
if (this.isExpanded(idx)) {
|
if (this._expandedStates[idx]) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
const conditions = ensureArray(option.conditions);
|
const conditions = ensureArray(option.conditions);
|
||||||
@ -108,10 +86,14 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
const action = this.action;
|
const action = this.action;
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
${(action.choose ? ensureArray(action.choose) : []).map(
|
<div class="options">
|
||||||
|
${repeat(
|
||||||
|
action.choose ? ensureArray(action.choose) : [],
|
||||||
|
(option) => option,
|
||||||
(option, idx) =>
|
(option, idx) =>
|
||||||
html`<ha-card>
|
html`<ha-card>
|
||||||
<ha-expansion-panel
|
<ha-expansion-panel
|
||||||
|
.index=${idx}
|
||||||
leftChevron
|
leftChevron
|
||||||
@expanded-changed=${this._expandedChanged}
|
@expanded-changed=${this._expandedChanged}
|
||||||
>
|
>
|
||||||
@ -123,7 +105,34 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
)}:
|
)}:
|
||||||
${this._getDescription(option, idx)}
|
${this._getDescription(option, idx)}
|
||||||
</h3>
|
</h3>
|
||||||
|
${this.reOrderMode
|
||||||
|
? html`
|
||||||
|
<ha-icon-button
|
||||||
|
.index=${idx}
|
||||||
|
slot="icons"
|
||||||
|
.label=${this.hass.localize(
|
||||||
|
"ui.panel.config.automation.editor.move_up"
|
||||||
|
)}
|
||||||
|
.path=${mdiArrowUp}
|
||||||
|
@click=${this._moveUp}
|
||||||
|
.disabled=${idx === 0}
|
||||||
|
></ha-icon-button>
|
||||||
|
<ha-icon-button
|
||||||
|
.index=${idx}
|
||||||
|
slot="icons"
|
||||||
|
.label=${this.hass.localize(
|
||||||
|
"ui.panel.config.automation.editor.move_down"
|
||||||
|
)}
|
||||||
|
.path=${mdiArrowDown}
|
||||||
|
@click=${this._moveDown}
|
||||||
|
.disabled=${idx ===
|
||||||
|
ensureArray(this.action.choose).length - 1}
|
||||||
|
></ha-icon-button>
|
||||||
|
<div class="handle" slot="icons">
|
||||||
|
<ha-svg-icon .path=${mdiDrag}></ha-svg-icon>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
: html`
|
||||||
<ha-icon-button
|
<ha-icon-button
|
||||||
slot="icons"
|
slot="icons"
|
||||||
.idx=${idx}
|
.idx=${idx}
|
||||||
@ -134,6 +143,7 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
)}
|
)}
|
||||||
.path=${mdiDelete}
|
.path=${mdiDelete}
|
||||||
></ha-icon-button>
|
></ha-icon-button>
|
||||||
|
`}
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
<h4>
|
<h4>
|
||||||
${this.hass.localize(
|
${this.hass.localize(
|
||||||
@ -169,6 +179,7 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
</ha-expansion-panel>
|
</ha-expansion-panel>
|
||||||
</ha-card>`
|
</ha-card>`
|
||||||
)}
|
)}
|
||||||
|
</div>
|
||||||
<ha-button
|
<ha-button
|
||||||
outlined
|
outlined
|
||||||
.label=${this.hass.localize(
|
.label=${this.hass.localize(
|
||||||
@ -209,6 +220,30 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected firstUpdated() {
|
||||||
|
ensureArray(this.action.choose).forEach(() =>
|
||||||
|
this._expandedStates.push(false)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected updated(changedProps: PropertyValues) {
|
||||||
|
super.updated(changedProps);
|
||||||
|
|
||||||
|
if (changedProps.has("reOrderMode")) {
|
||||||
|
if (this.reOrderMode) {
|
||||||
|
this._createSortable();
|
||||||
|
} else {
|
||||||
|
this._destroySortable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._expandLast) {
|
||||||
|
const nodes = this.shadowRoot!.querySelectorAll("ha-expansion-panel");
|
||||||
|
nodes[nodes.length - 1].expanded = true;
|
||||||
|
this._expandLast = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private _addDefault() {
|
private _addDefault() {
|
||||||
this._showDefault = true;
|
this._showDefault = true;
|
||||||
}
|
}
|
||||||
@ -247,6 +282,38 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
value: { ...this.action, choose },
|
value: { ...this.action, choose },
|
||||||
});
|
});
|
||||||
|
this._expandLast = true;
|
||||||
|
this._expandedStates[choose.length - 1] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _moveUp(ev) {
|
||||||
|
const index = (ev.target as any).index;
|
||||||
|
const newIndex = index - 1;
|
||||||
|
this._move(index, newIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _moveDown(ev) {
|
||||||
|
const index = (ev.target as any).index;
|
||||||
|
const newIndex = index + 1;
|
||||||
|
this._move(index, newIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _dragged(ev: SortableEvent): void {
|
||||||
|
if (ev.oldIndex === ev.newIndex) return;
|
||||||
|
this._move(ev.oldIndex!, ev.newIndex!);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _move(index: number, newIndex: number) {
|
||||||
|
const options = ensureArray(this.action.choose)!.concat();
|
||||||
|
const item = options.splice(index, 1)[0];
|
||||||
|
options.splice(newIndex, 0, item);
|
||||||
|
|
||||||
|
const expanded = this._expandedStates.splice(index, 1)[0];
|
||||||
|
this._expandedStates.splice(newIndex, 0, expanded);
|
||||||
|
|
||||||
|
fireEvent(this, "value-changed", {
|
||||||
|
value: { ...this.action, choose: options },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _removeOption(ev: CustomEvent) {
|
private _removeOption(ev: CustomEvent) {
|
||||||
@ -255,6 +322,7 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
? [...ensureArray(this.action.choose)]
|
? [...ensureArray(this.action.choose)]
|
||||||
: [];
|
: [];
|
||||||
choose.splice(index, 1);
|
choose.splice(index, 1);
|
||||||
|
this._expandedStates.splice(index, 1);
|
||||||
fireEvent(this, "value-changed", {
|
fireEvent(this, "value-changed", {
|
||||||
value: { ...this.action, choose },
|
value: { ...this.action, choose },
|
||||||
});
|
});
|
||||||
@ -271,9 +339,37 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async _createSortable() {
|
||||||
|
const Sortable = await loadSortable();
|
||||||
|
this._sortable = new Sortable(this.shadowRoot!.querySelector(".options")!, {
|
||||||
|
animation: 150,
|
||||||
|
fallbackClass: "sortable-fallback",
|
||||||
|
handle: ".handle",
|
||||||
|
onChoose: (evt: SortableEvent) => {
|
||||||
|
(evt.item as any).placeholder =
|
||||||
|
document.createComment("sort-placeholder");
|
||||||
|
evt.item.after((evt.item as any).placeholder);
|
||||||
|
},
|
||||||
|
onEnd: (evt: SortableEvent) => {
|
||||||
|
// put back in original location
|
||||||
|
if ((evt.item as any).placeholder) {
|
||||||
|
(evt.item as any).placeholder.replaceWith(evt.item);
|
||||||
|
delete (evt.item as any).placeholder;
|
||||||
|
}
|
||||||
|
this._dragged(evt);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private _destroySortable() {
|
||||||
|
this._sortable?.destroy();
|
||||||
|
this._sortable = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
return [
|
return [
|
||||||
haStyle,
|
haStyle,
|
||||||
|
sortableStyles,
|
||||||
css`
|
css`
|
||||||
ha-card {
|
ha-card {
|
||||||
margin: 0 0 16px 0;
|
margin: 0 0 16px 0;
|
||||||
@ -292,8 +388,6 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
font-weight: inherit;
|
font-weight: inherit;
|
||||||
}
|
}
|
||||||
ha-icon-button {
|
ha-icon-button {
|
||||||
position: absolute;
|
|
||||||
right: 0;
|
|
||||||
inset-inline-start: initial;
|
inset-inline-start: initial;
|
||||||
inset-inline-end: 0;
|
inset-inline-end: 0;
|
||||||
direction: var(--direction);
|
direction: var(--direction);
|
||||||
@ -307,6 +401,14 @@ export class HaChooseAction extends LitElement implements ActionElement {
|
|||||||
.card-content {
|
.card-content {
|
||||||
padding: 0 16px 16px 16px;
|
padding: 0 16px 16px 16px;
|
||||||
}
|
}
|
||||||
|
.handle {
|
||||||
|
cursor: move;
|
||||||
|
padding: 12px;
|
||||||
|
}
|
||||||
|
.handle ha-svg-icon {
|
||||||
|
pointer-events: none;
|
||||||
|
height: 24px;
|
||||||
|
}
|
||||||
`,
|
`,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user