ha-frontend-cdce8p/panels/shopping-list/ha-panel-shopping-list.html
c727 10c07673c1 Fix mark items as (un)complete in shopping list (#887)
* Fix mark items as (un)complete in shopping list

Fix: #885

* Update ha-panel-shopping-list.html
2018-02-12 21:09:16 -08:00

221 lines
6.6 KiB
HTML

<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../bower_components/app-layout/app-header-layout/app-header-layout.html">
<link rel="import" href="../../bower_components/app-layout/app-header/app-header.html">
<link rel="import" href="../../bower_components/app-layout/app-toolbar/app-toolbar.html">
<link rel="import" href="../../bower_components/paper-card/paper-card.html">
<link rel="import" href="../../bower_components/paper-input/paper-input.html">
<link rel="import" href="../../bower_components/paper-item/paper-icon-item.html">
<link rel="import" href="../../bower_components/paper-item/paper-item-body.html">
<link rel="import" href="../../bower_components/paper-checkbox/paper-checkbox.html">
<link rel="import" href="../../bower_components/paper-icon-button/paper-icon-button.html">
<link rel="import" href="../../bower_components/paper-menu-button/paper-menu-button.html">
<link rel="import" href="../../bower_components/paper-listbox/paper-listbox.html">
<link rel="import" href="../../bower_components/paper-item/paper-item.html">
<link rel='import' href='../../src/util/hass-mixins.html'>
<link rel="import" href="../../src/components/ha-menu-button.html">
<link rel="import" href="../../src/components/ha-start-voice-button.html">
<dom-module id="ha-panel-shopping-list">
<template>
<style include="ha-style">
:host {
height: 100%;
}
app-toolbar paper-listbox {
width: 150px;
}
app-toolbar paper-item {
cursor: pointer;
}
.content {
padding-bottom: 32px;
max-width: 600px;
margin: 0 auto;
}
paper-card {
display: block;
}
paper-icon-item {
border-top: 1px solid var(--divider-color);
}
paper-icon-item:first-child {
border-top: 0;
}
paper-checkbox {
padding: 11px;
}
paper-input {
--paper-input-container-underline: {
display: none;
}
--paper-input-container-underline-focus: {
display: none;
}
position: relative;
top: 1px;
}
.tip {
padding: 24px;
text-align: center;
color: var(--secondary-text-color);
}
</style>
<app-header-layout has-scrolling-region>
<app-header slot="header" fixed>
<app-toolbar>
<ha-menu-button narrow='[[narrow]]' show-menu='[[showMenu]]'></ha-menu-button>
<div main-title>[[localize('panel.shopping_list')]]</div>
<ha-start-voice-button hass='[[hass]]' can-listen='{{canListen}}'></ha-start-voice-button>
<paper-menu-button
horizontal-align="right"
horizontal-offset="-5"
vertical-offset="-5"
>
<paper-icon-button
icon="mdi:dots-vertical"
slot="dropdown-trigger"
></paper-icon-button>
<paper-listbox slot="dropdown-content">
<paper-item
on-tap="_clearCompleted"
>[[localize('ui.panel.shopping-list.clear_completed')]]</paper-item>
</paper-listbox>
</paper-menu-button>
</app-toolbar>
</app-header>
<div class='content'>
<paper-card>
<paper-icon-item on-focus='_focusRowInput'>
<paper-icon-button
slot="item-icon"
icon="mdi:plus"
on-tap='_addItem'
></paper-icon-button>
<paper-item-body>
<paper-input
id='addBox'
placeholder="[[localize('ui.panel.shopping-list.add_item')]]"
on-keydown='_addKeyPress'
no-label-float
></paper-input>
</paper-item-body>
</paper-icon-item>
<template is='dom-repeat' items='[[items]]'>
<paper-icon-item>
<paper-checkbox
slot="item-icon"
checked='{{item.complete}}'
on-tap='_itemCompleteTapped'
tabindex='0'
></paper-checkbox>
<paper-item-body>
<paper-input
id='editBox'
no-label-float
value='[[item.name]]'
on-change='_saveEdit'
></paper-input>
</paper-item-body>
</paper-icon-item>
</template>
</paper-card>
<div class='tip' hidden$='[[!canListen]]'>
[[localize('ui.panel.shopping-list.microphone_tip')]]
</div>
</div>
</app-header-layout>
</template>
</dom-module>
<script>
/*
* @appliesMixin window.hassMixins.LocalizeMixin
*/
class HaPanelShoppingList extends window.hassMixins.LocalizeMixin(Polymer.Element) {
static get is() { return 'ha-panel-shopping-list'; }
static get properties() {
return {
hass: Object,
narrow: Boolean,
showMenu: Boolean,
canListen: Boolean,
items: {
type: Array,
value: [],
},
};
}
connectedCallback() {
super.connectedCallback();
this._fetchData = this._fetchData.bind(this);
this.hass.connection.subscribeEvents(this._fetchData, 'shopping_list_updated')
.then(function (unsub) { this._unsubEvents = unsub; }.bind(this));
this._fetchData();
}
disconnectedCallback() {
super.disconnectedCallback();
if (this._unsubEvents) this._unsubEvents();
}
_fetchData() {
this.hass.callApi('get', 'shopping_list')
.then(function (items) {
items.reverse();
this.items = items;
}.bind(this));
}
_itemCompleteTapped(ev) {
ev.stopPropagation();
this.hass.callApi('post', 'shopping_list/item/' + ev.model.item.id, {
complete: ev.target.checked
}).catch(() => this._fetchData());
}
_addItem(ev) {
this.hass.callApi('post', 'shopping_list/item', {
name: this.$.addBox.value,
}).catch(() => this._fetchData());
this.$.addBox.value = '';
// Presence of 'ev' means tap on "add" button.
if (ev) {
setTimeout(() => this.$.addBox.focus(), 10);
}
}
_addKeyPress(ev) {
if (ev.keyCode === 13) {
this._addItem();
}
}
_saveEdit(ev) {
const { index, item } = ev.model;
const name = ev.target.value;
if (name === item.name) {
return;
}
this.set(['items', index, 'name'], name);
this.hass.callApi('post', 'shopping_list/item/' + item.id, {
name: name
}).catch(() => this._fetchData());
}
_clearCompleted() {
this.hass.callApi('POST', 'shopping_list/clear_completed');
}
}
customElements.define(HaPanelShoppingList.is, HaPanelShoppingList);
</script>