2020-05-05 16:40:11 +02:00
|
|
|
import "./ha-icon-button";
|
2020-01-26 21:41:11 +01:00
|
|
|
import "@polymer/paper-input/paper-input";
|
|
|
|
import "@polymer/paper-item/paper-item";
|
|
|
|
import "@polymer/paper-item/paper-item-body";
|
|
|
|
import "@polymer/paper-listbox/paper-listbox";
|
2020-04-14 18:05:45 +02:00
|
|
|
import "@vaadin/vaadin-combo-box/theme/material/vaadin-combo-box-light";
|
|
|
|
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
2020-01-26 21:41:11 +01:00
|
|
|
import {
|
|
|
|
css,
|
|
|
|
CSSResult,
|
|
|
|
customElement,
|
2020-04-14 18:05:45 +02:00
|
|
|
html,
|
|
|
|
LitElement,
|
2020-01-26 21:41:11 +01:00
|
|
|
property,
|
2020-07-15 06:38:36 +02:00
|
|
|
internalProperty,
|
2020-04-14 18:05:45 +02:00
|
|
|
TemplateResult,
|
2020-01-26 21:41:11 +01:00
|
|
|
} from "lit-element";
|
|
|
|
import { fireEvent } from "../common/dom/fire_event";
|
|
|
|
import {
|
|
|
|
AreaRegistryEntry,
|
|
|
|
createAreaRegistryEntry,
|
2020-04-14 18:05:45 +02:00
|
|
|
subscribeAreaRegistry,
|
2020-01-26 21:41:11 +01:00
|
|
|
} from "../data/area_registry";
|
|
|
|
import {
|
|
|
|
showAlertDialog,
|
2020-04-14 18:05:45 +02:00
|
|
|
showPromptDialog,
|
2020-01-26 21:41:11 +01:00
|
|
|
} from "../dialogs/generic/show-dialog-box";
|
2020-04-14 18:05:45 +02:00
|
|
|
import { SubscribeMixin } from "../mixins/subscribe-mixin";
|
|
|
|
import { PolymerChangedEvent } from "../polymer-types";
|
|
|
|
import { HomeAssistant } from "../types";
|
2020-01-26 21:41:11 +01:00
|
|
|
|
|
|
|
const rowRenderer = (
|
|
|
|
root: HTMLElement,
|
|
|
|
_owner,
|
|
|
|
model: { item: AreaRegistryEntry }
|
|
|
|
) => {
|
|
|
|
if (!root.firstElementChild) {
|
|
|
|
root.innerHTML = `
|
|
|
|
<style>
|
|
|
|
paper-item {
|
|
|
|
margin: -10px 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
paper-item.add-new {
|
|
|
|
font-weight: 500;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<paper-item>
|
|
|
|
<paper-item-body two-line>
|
|
|
|
<div class='name'>[[item.name]]</div>
|
|
|
|
</paper-item-body>
|
|
|
|
</paper-item>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
root.querySelector(".name")!.textContent = model.item.name!;
|
|
|
|
if (model.item.area_id === "add_new") {
|
|
|
|
root.querySelector("paper-item")!.className = "add-new";
|
|
|
|
} else {
|
|
|
|
root.querySelector("paper-item")!.classList.remove("add-new");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@customElement("ha-area-picker")
|
|
|
|
export class HaAreaPicker extends SubscribeMixin(LitElement) {
|
2020-07-15 06:38:36 +02:00
|
|
|
@property({ attribute: false }) public hass!: HomeAssistant;
|
2020-04-14 18:05:45 +02:00
|
|
|
|
2020-01-26 21:41:11 +01:00
|
|
|
@property() public label?: string;
|
2020-04-14 18:05:45 +02:00
|
|
|
|
2020-01-26 21:41:11 +01:00
|
|
|
@property() public value?: string;
|
2020-04-14 18:05:45 +02:00
|
|
|
|
2020-01-26 21:41:11 +01:00
|
|
|
@property() public _areas?: AreaRegistryEntry[];
|
2020-04-14 18:05:45 +02:00
|
|
|
|
2020-01-26 21:41:11 +01:00
|
|
|
@property({ type: Boolean, attribute: "no-add" })
|
|
|
|
public noAdd?: boolean;
|
2020-04-14 18:05:45 +02:00
|
|
|
|
2020-07-15 06:38:36 +02:00
|
|
|
@internalProperty() private _opened?: boolean;
|
2020-01-26 21:41:11 +01:00
|
|
|
|
|
|
|
public hassSubscribe(): UnsubscribeFunc[] {
|
|
|
|
return [
|
|
|
|
subscribeAreaRegistry(this.hass.connection!, (areas) => {
|
|
|
|
this._areas = this.noAdd
|
|
|
|
? areas
|
|
|
|
: [
|
|
|
|
...areas,
|
|
|
|
{
|
|
|
|
area_id: "add_new",
|
|
|
|
name: this.hass.localize("ui.components.area-picker.add_new"),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-01-27 17:34:22 +01:00
|
|
|
protected render(): TemplateResult {
|
2020-01-26 21:41:11 +01:00
|
|
|
if (!this._areas) {
|
2020-01-27 17:34:22 +01:00
|
|
|
return html``;
|
2020-01-26 21:41:11 +01:00
|
|
|
}
|
|
|
|
return html`
|
|
|
|
<vaadin-combo-box-light
|
|
|
|
item-value-path="area_id"
|
|
|
|
item-id-path="area_id"
|
|
|
|
item-label-path="name"
|
|
|
|
.items=${this._areas}
|
|
|
|
.value=${this._value}
|
|
|
|
.renderer=${rowRenderer}
|
|
|
|
@opened-changed=${this._openedChanged}
|
|
|
|
@value-changed=${this._areaChanged}
|
|
|
|
>
|
|
|
|
<paper-input
|
|
|
|
.label=${this.label === undefined && this.hass
|
|
|
|
? this.hass.localize("ui.components.area-picker.area")
|
|
|
|
: this.label}
|
|
|
|
class="input"
|
|
|
|
autocapitalize="none"
|
|
|
|
autocomplete="off"
|
|
|
|
autocorrect="off"
|
|
|
|
spellcheck="false"
|
|
|
|
>
|
|
|
|
${this.value
|
|
|
|
? html`
|
2020-05-05 16:40:11 +02:00
|
|
|
<ha-icon-button
|
2020-01-26 21:41:11 +01:00
|
|
|
aria-label=${this.hass.localize(
|
|
|
|
"ui.components.area-picker.clear"
|
|
|
|
)}
|
|
|
|
slot="suffix"
|
|
|
|
class="clear-button"
|
|
|
|
icon="hass:close"
|
|
|
|
@click=${this._clearValue}
|
|
|
|
no-ripple
|
|
|
|
>
|
|
|
|
${this.hass.localize("ui.components.area-picker.clear")}
|
2020-05-05 16:40:11 +02:00
|
|
|
</ha-icon-button>
|
2020-01-26 21:41:11 +01:00
|
|
|
`
|
|
|
|
: ""}
|
|
|
|
${this._areas.length > 0
|
|
|
|
? html`
|
2020-05-05 16:40:11 +02:00
|
|
|
<ha-icon-button
|
2020-01-26 21:41:11 +01:00
|
|
|
aria-label=${this.hass.localize(
|
|
|
|
"ui.components.area-picker.show_areas"
|
|
|
|
)}
|
|
|
|
slot="suffix"
|
|
|
|
class="toggle-button"
|
|
|
|
.icon=${this._opened ? "hass:menu-up" : "hass:menu-down"}
|
|
|
|
>
|
|
|
|
${this.hass.localize("ui.components.area-picker.toggle")}
|
2020-05-05 16:40:11 +02:00
|
|
|
</ha-icon-button>
|
2020-01-26 21:41:11 +01:00
|
|
|
`
|
|
|
|
: ""}
|
|
|
|
</paper-input>
|
|
|
|
</vaadin-combo-box-light>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _clearValue(ev: Event) {
|
|
|
|
ev.stopPropagation();
|
|
|
|
this._setValue("");
|
|
|
|
}
|
|
|
|
|
|
|
|
private get _value() {
|
|
|
|
return this.value || "";
|
|
|
|
}
|
|
|
|
|
|
|
|
private _openedChanged(ev: PolymerChangedEvent<boolean>) {
|
|
|
|
this._opened = ev.detail.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private _areaChanged(ev: PolymerChangedEvent<string>) {
|
|
|
|
const newValue = ev.detail.value;
|
|
|
|
|
|
|
|
if (newValue !== "add_new") {
|
|
|
|
if (newValue !== this._value) {
|
|
|
|
this._setValue(newValue);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(ev.target as any).value = this._value;
|
|
|
|
showPromptDialog(this, {
|
|
|
|
title: this.hass.localize("ui.components.area-picker.add_dialog.title"),
|
|
|
|
text: this.hass.localize("ui.components.area-picker.add_dialog.text"),
|
|
|
|
confirmText: this.hass.localize(
|
|
|
|
"ui.components.area-picker.add_dialog.add"
|
|
|
|
),
|
|
|
|
inputLabel: this.hass.localize(
|
|
|
|
"ui.components.area-picker.add_dialog.name"
|
|
|
|
),
|
|
|
|
confirm: async (name) => {
|
|
|
|
if (!name) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const area = await createAreaRegistryEntry(this.hass, {
|
|
|
|
name,
|
|
|
|
});
|
|
|
|
this._areas = [...this._areas!, area];
|
|
|
|
this._setValue(area.area_id);
|
|
|
|
} catch (err) {
|
|
|
|
showAlertDialog(this, {
|
|
|
|
text: this.hass.localize(
|
|
|
|
"ui.components.area-picker.add_dialog.failed_create_area"
|
|
|
|
),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _setValue(value: string) {
|
|
|
|
this.value = value;
|
|
|
|
setTimeout(() => {
|
|
|
|
fireEvent(this, "value-changed", { value });
|
|
|
|
fireEvent(this, "change");
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static get styles(): CSSResult {
|
|
|
|
return css`
|
2020-05-05 16:40:11 +02:00
|
|
|
paper-input > ha-icon-button {
|
|
|
|
--mdc-icon-button-size: 24px;
|
2020-01-26 21:41:11 +01:00
|
|
|
padding: 2px;
|
|
|
|
color: var(--secondary-text-color);
|
|
|
|
}
|
|
|
|
[hidden] {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
"ha-area-picker": HaAreaPicker;
|
|
|
|
}
|
|
|
|
}
|