Shopping List: Allow adding items (#647)

* Shopping List: Allow adding items

* Remove unused parts
This commit is contained in:
Paulus Schoutsen 2017-11-24 21:27:55 -08:00 committed by GitHub
parent a960559639
commit c330b87506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 additions and 128 deletions

View File

@ -21,38 +21,38 @@
:host { :host {
height: 100%; height: 100%;
} }
app-toolbar paper-listbox {
width: 150px;
}
app-toolbar paper-item {
cursor: pointer;
}
.content { .content {
padding: 24px 0 32px; padding-bottom: 32px;
max-width: 600px; max-width: 600px;
margin: 0 auto; margin: 0 auto;
} }
paper-card { paper-card {
display: block; display: block;
padding-bottom: 16px;
} }
.list-header { @apply --paper-font-headline; } paper-icon-item {
.list-options { border-top: 1px solid var(--divider-color);
position: absolute;
top: 0;
right: 0;
color: var(--secondary-text-color);
} }
.list-header paper-listbox { paper-icon-item:first-child {
width: 150px; border-top: 0;
}
.list-header paper-item {
cursor: pointer;
}
paper-item {
--paper-item-focused: {
background-color: white;
}
} }
paper-checkbox { paper-checkbox {
padding: 11px 7px; padding: 11px;
} }
paper-input { paper-input {
width: 100%; --paper-input-container-underline: {
display: none;
}
--paper-input-container-underline-focus: {
display: none;
}
position: relative;
top: 1px;
} }
.tip { .tip {
padding: 24px; padding: 24px;
@ -66,70 +66,63 @@
<app-toolbar> <app-toolbar>
<ha-menu-button narrow='[[narrow]]' show-menu='[[showMenu]]'></ha-menu-button> <ha-menu-button narrow='[[narrow]]' show-menu='[[showMenu]]'></ha-menu-button>
<div main-title>Shopping List</div> <div main-title>Shopping List</div>
<ha-start-voice-button hass='[[hass]]'></ha-start-voice-button> <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"
>Clear completed</paper-item>
</paper-listbox>
</paper-menu-button>
</app-toolbar> </app-toolbar>
</app-header> </app-header>
<div class='content'> <div class='content'>
<paper-card> <paper-card>
<div class="card-content list-header">Shopping List <paper-icon-item on-focus='_focusRowInput'>
<div class="list-options"> <paper-icon-button
<paper-menu-button slot="item-icon"
horizontal-align="right" icon="mdi:plus"
horizontal-offset="-5" on-tap='_addItem'
vertical-offset="-5" ></paper-icon-button>
> <paper-item-body>
<paper-icon-button <paper-input
icon="mdi:dots-vertical" id='addBox'
slot="dropdown-trigger" placeholder="Add item"
></paper-icon-button> on-keydown='_addKeyPress'
<paper-listbox slot="dropdown-content"> no-label-float
<paper-item ></paper-input>
on-tap="_clearCompleted" </paper-item-body>
>Clear completed</paper-item> </paper-icon-item>
</paper-listbox>
</paper-menu-button>
</div>
</div>
<template is='dom-if' if='[[!items.length]]'>
<div class='card-content'>
You have no items on your shopping list.
</div>
</template>
<template is='dom-repeat' items='[[items]]'> <template is='dom-repeat' items='[[items]]'>
<paper-icon-item on-tap='_enableEditting'> <paper-icon-item on-tap='_enableEditting'>
<paper-checkbox <paper-checkbox
slot="item-icon" slot="item-icon"
checked='{{item.complete}}' checked='{{item.complete}}'
on-tap='_itemCompleteTapped' on-tap='_itemCompleteTapped'
tabindex='0' tabindex='0'
></paper-checkbox> ></paper-checkbox>
<paper-item-body> <paper-item-body>
<template is='dom-if' if='[[!_computeIsEditting(_editIndex, index)]]'> <paper-input
[[item.name]] id='editBox'
</template> no-label-float
<template is='dom-if' if='[[_computeIsEditting(_editIndex, index)]]' restamp> value='[[item.name]]'
<paper-input on-change='_saveEdit'
tabindex='0' ></paper-input>
autofocus
id='editBox'
no-label-float
value='{{_editValue}}'
on-keydown='_editKeyPress'
></paper-input>
</template>
</paper-item-body> </paper-item-body>
<template is='dom-if' if='[[_computeIsEditting(_editIndex, index)]]'>
<paper-icon-button
icon='mdi:content-save'
on-tap='_finishEditting'
alt='Save item'
></paper-icon-button>
</template>
</paper-icon-item> </paper-icon-item>
</template> </template>
</paper-card> </paper-card>
<div class='tip'> <div class='tip' hidden$='[[!canListen]]'>
Tap the microphone on the top right and say "Add candy to my shopping list" Tap the microphone on the top right and say "Add candy to my shopping list"
</div> </div>
</div> </div>
@ -146,26 +139,14 @@ class HaPanelShoppingList extends Polymer.Element {
hass: Object, hass: Object,
narrow: Boolean, narrow: Boolean,
showMenu: Boolean, showMenu: Boolean,
canListen: Boolean,
items: { items: {
type: Array, type: Array,
value: [], value: [],
}, },
_editIndex: {
type: Number,
value: -1,
},
_editValue: {
type: String,
value: '',
},
}; };
} }
ready() {
super.ready();
this.addEventListener('keydown', ev => this._checkEsc(ev));
}
connectedCallback() { connectedCallback() {
super.connectedCallback(); super.connectedCallback();
this._fetchData = this._fetchData.bind(this); this._fetchData = this._fetchData.bind(this);
@ -183,48 +164,39 @@ class HaPanelShoppingList extends Polymer.Element {
_fetchData() { _fetchData() {
this.hass.callApi('get', 'shopping_list') this.hass.callApi('get', 'shopping_list')
.then(function (items) { .then(function (items) {
items.reverse();
this.items = items; this.items = items;
}.bind(this)); }.bind(this));
} }
_computeIsEditting(editIndex, index) {
return editIndex === index;
}
_itemCompleteTapped(ev) { _itemCompleteTapped(ev) {
ev.stopPropagation(); ev.stopPropagation();
var item = ev.model.item; var item = ev.model.item;
this.hass.callApi('post', 'shopping_list/item/' + item.id, { this.hass.callApi('post', 'shopping_list/item/' + item.id, {
complete: item.complete complete: !item.complete
}).catch(function () { }).catch(() => this._fetchData());
this._fetchData();
}.bind(this));
} }
_enableEditting(ev) { _addItem(ev) {
var item = ev.model.item; this.hass.callApi('post', 'shopping_list/item', {
var index = this.items.indexOf(item); name: this.$.addBox.value,
}).catch(() => this._fetchData());
if (index === this._editIndex) return; this.$.addBox.value = '';
// Presence of 'ev' means tap on "add" button.
this._editValue = item.name; if (ev) {
this._editIndex = index; setTimeout(() => this.$.addBox.focus(), 10);
}
} }
_cancelEditting() { _addKeyPress(ev) {
this._editIndex = -1; if (ev.keyCode === 13) {
this._editValue = ''; this._addItem();
}
} }
_finishEditting(ev) { _saveEdit(ev) {
ev.stopPropagation(); const { index, item } = ev.model;
const name = ev.target.value;
var index = this._editIndex;
var name = this._editValue;
var item = this.items[index];
this._editIndex = -1;
this._editValue = '';
if (name === item.name) { if (name === item.name) {
return; return;
@ -233,21 +205,7 @@ class HaPanelShoppingList extends Polymer.Element {
this.set(['items', index, 'name'], name); this.set(['items', index, 'name'], name);
this.hass.callApi('post', 'shopping_list/item/' + item.id, { this.hass.callApi('post', 'shopping_list/item/' + item.id, {
name: name name: name
}).catch(function () { }).catch(() => this._fetchData());
this._fetchData();
}.bind(this));
}
_editKeyPress(ev) {
if (ev.keyCode === 13) {
this._finishEditting(ev);
}
}
_checkEsc(ev) {
if (ev.keyCode === 27) {
this._cancelEditting();
}
} }
_clearCompleted() { _clearCompleted() {

View File

@ -27,6 +27,7 @@ class HaStartVoiceButton extends window.hassMixins.EventsMixin(Polymer.Element)
canListen: { canListen: {
type: Boolean, type: Boolean,
computed: 'computeCanListen(hass)', computed: 'computeCanListen(hass)',
notify: true,
}, },
}; };
} }