import { html, css, LitElement, TemplateResult, customElement, property, CSSResult, } from "lit-element"; import "@polymer/paper-input/paper-input"; import "../../components/hui-entity-editor"; import "../../components/hui-input-list-editor"; import { struct } from "../../common/structs/struct"; import { EntitiesEditorEvent, EditorTarget } from "../types"; import { HomeAssistant } from "../../../../types"; import { LovelaceCardEditor } from "../../types"; import { fireEvent } from "../../../../common/dom/fire_event"; import { configElementStyle } from "./config-elements-style"; import { processEditorEntities } from "../process-editor-entities"; import { EntityConfig } from "../../entity-rows/types"; import { PolymerChangedEvent } from "../../../../polymer-types"; import { MapCardConfig } from "../../cards/types"; const entitiesConfigStruct = struct.union([ { entity: "entity-id", name: "string?", icon: "icon?", }, "entity-id", ]); const cardConfigStruct = struct({ type: "string", title: "string?", aspect_ratio: "string?", default_zoom: "number?", dark_mode: "boolean?", entities: [entitiesConfigStruct], geo_location_sources: "array?", }); @customElement("hui-map-card-editor") export class HuiMapCardEditor extends LitElement implements LovelaceCardEditor { @property() public hass?: HomeAssistant; @property() private _config?: MapCardConfig; @property() private _configEntities?: EntityConfig[]; public setConfig(config: MapCardConfig): void { config = cardConfigStruct(config); this._config = config; this._configEntities = processEditorEntities(config.entities); } get _title(): string { return this._config!.title || ""; } get _aspect_ratio(): string { return this._config!.aspect_ratio || ""; } get _default_zoom(): number { return this._config!.default_zoom || NaN; } get _geo_location_sources(): string[] { return this._config!.geo_location_sources || []; } get _dark_mode(): boolean { return this._config!.dark_mode || false; } protected render(): TemplateResult | void { if (!this.hass) { return html``; } return html` ${configElementStyle}
Dark Mode?

Geolocation Sources

`; } private _entitiesValueChanged(ev: EntitiesEditorEvent): void { if (!this._config || !this.hass) { return; } if (ev.detail && ev.detail.entities) { this._config.entities = ev.detail.entities; this._configEntities = processEditorEntities(this._config.entities); fireEvent(this, "config-changed", { config: this._config }); } } private _valueChanged(ev: PolymerChangedEvent): void { if (!this._config || !this.hass) { return; } const target = ev.target! as EditorTarget; if ( target.configValue && ev.detail && this[`_${target.configValue}`] === ev.detail.value ) { return; } if (target.configValue && ev.detail) { if ( ev.detail.value === "" || (target.type === "number" && isNaN(Number(ev.detail.value))) ) { delete this._config[target.configValue!]; } else { let value: any = ev.detail.value; if (target.type === "number") { value = Number(value); } this._config = { ...this._config, [target.configValue]: target.checked !== undefined ? target.checked : value, }; } } fireEvent(this, "config-changed", { config: this._config }); } static get styles(): CSSResult { return css` .geo_location_sources { padding-left: 20px; } `; } } declare global { interface HTMLElementTagNameMap { "hui-map-card-editor": HuiMapCardEditor; } }