diff --git a/src/data/shopping-list.ts b/src/data/shopping-list.ts index d3dbdd8327e0..a7e4140139a0 100644 --- a/src/data/shopping-list.ts +++ b/src/data/shopping-list.ts @@ -38,3 +38,21 @@ export const addItem = ( type: "shopping_list/items/add", name, }); + +export const moveUpItem = ( + hass: HomeAssistant, + itemId: string +): Promise => + hass.callWS({ + type: "shopping_list/items/move_up", + item_id: itemId, + }); + +export const moveDownItem = ( + hass: HomeAssistant, + itemId: string +): Promise => + hass.callWS({ + type: "shopping_list/items/move_down", + item_id: itemId, + }); diff --git a/src/panels/lovelace/cards/hui-shopping-list-card.ts b/src/panels/lovelace/cards/hui-shopping-list-card.ts index fc1518c94524..4c77b265ff5d 100644 --- a/src/panels/lovelace/cards/hui-shopping-list-card.ts +++ b/src/panels/lovelace/cards/hui-shopping-list-card.ts @@ -22,6 +22,8 @@ import { fetchItems, ShoppingListItem, updateItem, + moveUpItem, + moveDownItem, } from "../../../data/shopping-list"; import { HomeAssistant } from "../../../types"; import { LovelaceCard, LovelaceCardEditor } from "../types"; @@ -51,6 +53,8 @@ class HuiShoppingListCard extends SubscribeMixin(LitElement) @internalProperty() private _checkedItems?: ShoppingListItem[]; + @internalProperty() private _reordering = false; + public getCardSize(): number { return (this._config ? (this._config.title ? 1 : 0) : 0) + 3; } @@ -120,11 +124,20 @@ class HuiShoppingListCard extends SubscribeMixin(LitElement) )} @keydown=${this._addKeyPress} > + + ${repeat( this._uncheckedItems!, (item) => item.id, - (item) => + (item, index) => html`
+ ${this._reordering + ? html` + + + ` + : ""} + ${this._reordering + ? html` + + + ` + : ""}
` )} @@ -250,6 +295,18 @@ class HuiShoppingListCard extends SubscribeMixin(LitElement) } } + private _toggleReorder(): void { + this._reordering = !this._reordering; + } + + private _moveUp(ev): void { + moveUpItem(this.hass!, ev.target.itemId).catch(() => this._fetchData()); + } + + private _moveDown(ev): void { + moveDownItem(this.hass!, ev.target.itemId).catch(() => this._fetchData()); + } + static get styles(): CSSResult { return css` ha-card { @@ -278,6 +335,25 @@ class HuiShoppingListCard extends SubscribeMixin(LitElement) cursor: pointer; } + .reorderButton { + padding-left: 16px; + cursor: pointer; + } + + .reorderButton.up { + padding-left: 16px; + padding-right: 8px; + } + + .reorderButton.down { + padding-left: 8px; + } + + .reorderButton[disabled] { + color: var(--disabled-text-color); + cursor: default; + } + paper-checkbox { padding-left: 4px; padding-right: 20px; diff --git a/src/translations/en.json b/src/translations/en.json index c86c81ea2e0d..87b61a054c73 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -2192,7 +2192,10 @@ "shopping-list": { "checked_items": "Checked items", "clear_items": "Clear checked items", - "add_item": "Add item" + "add_item": "Add item", + "reorder_items": "Reorder items", + "move_up_item": "Move up item", + "move_down_item": "Move down item" }, "picture-elements": { "hold": "Hold:",