Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃Ч cleanup elements #2820

Merged
merged 2 commits into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/panels/lovelace/common/compute-tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { HomeAssistant } from "../../../types";
import { LovelaceElementConfig } from "../elements/types";
import { ActionConfig } from "../../../data/lovelace";

export const computeTooltip = (
hass: HomeAssistant,
config: LovelaceElementConfig
): string => {
interface Config extends LovelaceElementConfig {
entity?: string;
title?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
}

export const computeTooltip = (hass: HomeAssistant, config: Config): string => {
if (config.title) {
return config.title;
}
Expand Down
49 changes: 27 additions & 22 deletions src/panels/lovelace/elements/hui-icon-element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { html, LitElement, TemplateResult } from "lit-element";
import {
html,
LitElement,
TemplateResult,
property,
css,
CSSResult,
customElement,
} from "lit-element";

import "../../../components/ha-icon";

Expand All @@ -7,18 +15,20 @@ import { handleClick } from "../common/handle-click";
import { longPress } from "../common/directives/long-press-directive";
import { LovelaceElement, LovelaceElementConfig } from "./types";
import { HomeAssistant } from "../../../types";
import { ActionConfig } from "../../../data/lovelace";

interface Config extends LovelaceElementConfig {
export interface Config extends LovelaceElementConfig {
entity?: string;
name?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
icon: string;
}

@customElement("hui-icon-element")
export class HuiIconElement extends LitElement implements LovelaceElement {
public hass?: HomeAssistant;
private _config?: Config;

static get properties() {
return { hass: {}, _config: {} };
}
@property() public hass?: HomeAssistant;
@property() private _config?: Config;

public setConfig(config: Config): void {
if (!config.icon) {
Expand All @@ -29,37 +39,34 @@ export class HuiIconElement extends LitElement implements LovelaceElement {
}

protected render(): TemplateResult | void {
if (!this._config) {
if (!this._config || !this.hass) {
return html``;
}

return html`
${this.renderStyle()}
<ha-icon
.icon="${this._config.icon}"
.title="${computeTooltip(this.hass!, this._config)}"
.title="${computeTooltip(this.hass, this._config)}"
@ha-click="${this._handleTap}"
@ha-hold="${this._handleHold}"
.longPress="${longPress()}"
></ha-icon>
`;
}

private _handleTap() {
private _handleTap(): void {
handleClick(this, this.hass!, this._config!, false);
}

private _handleHold() {
private _handleHold(): void {
handleClick(this, this.hass!, this._config!, true);
}

private renderStyle(): TemplateResult {
return html`
<style>
:host {
cursor: pointer;
}
</style>
static get styles(): CSSResult {
return css`
:host {
cursor: pointer;
}
`;
}
}
Expand All @@ -69,5 +76,3 @@ declare global {
"hui-icon-element": HuiIconElement;
}
}

customElements.define("hui-icon-element", HuiIconElement);
58 changes: 31 additions & 27 deletions src/panels/lovelace/elements/hui-image-element.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { html, LitElement, TemplateResult } from "lit-element";
import {
html,
LitElement,
TemplateResult,
property,
customElement,
css,
CSSResult,
} from "lit-element";

import "../components/hui-image";

Expand All @@ -7,8 +15,12 @@ import { handleClick } from "../common/handle-click";
import { longPress } from "../common/directives/long-press-directive";
import { LovelaceElement, LovelaceElementConfig } from "./types";
import { HomeAssistant } from "../../../types";
import { ActionConfig } from "../../../data/lovelace";

interface Config extends LovelaceElementConfig {
export interface Config extends LovelaceElementConfig {
entity?: string;
tap_action?: ActionConfig;
hold_action?: ActionConfig;
image?: string;
state_image?: string;
camera_image?: string;
Expand All @@ -17,13 +29,10 @@ interface Config extends LovelaceElementConfig {
aspect_ratio?: string;
}

@customElement("hui-image-element")
export class HuiImageElement extends LitElement implements LovelaceElement {
public hass?: HomeAssistant;
private _config?: Config;

static get properties() {
return { hass: {}, _config: {} };
}
@property() public hass?: HomeAssistant;
@property() private _config?: Config;

public setConfig(config: Config): void {
if (!config) {
Expand All @@ -38,12 +47,11 @@ export class HuiImageElement extends LitElement implements LovelaceElement {
}

protected render(): TemplateResult | void {
if (!this._config) {
if (!this._config || !this.hass) {
return html``;
}

return html`
${this.renderStyle()}
<hui-image
.hass="${this.hass}"
.entity="${this._config.entity}"
Expand All @@ -52,7 +60,7 @@ export class HuiImageElement extends LitElement implements LovelaceElement {
.cameraImage="${this._config.camera_image}"
.filter="${this._config.filter}"
.stateFilter="${this._config.state_filter}"
.title="${computeTooltip(this.hass!, this._config)}"
.title="${computeTooltip(this.hass, this._config)}"
.aspectRatio="${this._config.aspect_ratio}"
@ha-click="${this._handleTap}"
@ha-hold="${this._handleHold}"
Expand All @@ -61,26 +69,24 @@ export class HuiImageElement extends LitElement implements LovelaceElement {
`;
}

private renderStyle(): TemplateResult {
return html`
<style>
:host(.clickable) {
cursor: pointer;
overflow: hidden;
-webkit-touch-callout: none !important;
}
hui-image {
-webkit-user-select: none !important;
}
</style>
static get styles(): CSSResult {
return css`
:host(.clickable) {
cursor: pointer;
overflow: hidden;
-webkit-touch-callout: none !important;
}
hui-image {
-webkit-user-select: none !important;
}
`;
}

private _handleTap() {
private _handleTap(): void {
handleClick(this, this.hass!, this._config!, false);
}

private _handleHold() {
private _handleHold(): void {
handleClick(this, this.hass!, this._config!, true);
}
}
Expand All @@ -90,5 +96,3 @@ declare global {
"hui-image-element": HuiImageElement;
}
}

customElements.define("hui-image-element", HuiImageElement);
40 changes: 25 additions & 15 deletions src/panels/lovelace/elements/hui-service-button-element.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
import { html, LitElement, TemplateResult } from "lit-element";
import {
html,
LitElement,
TemplateResult,
property,
customElement,
CSSResult,
css,
} from "lit-element";

import "../../../components/buttons/ha-call-service-button";

import { LovelaceElement, LovelaceElementConfig } from "./types";
import { HomeAssistant } from "../../../types";

export interface Config extends LovelaceElementConfig {
title?: string;
service?: string;
service_data?: object;
}

@customElement("hui-service-button-element")
export class HuiServiceButtonElement extends LitElement
implements LovelaceElement {
public hass?: HomeAssistant;
private _config?: LovelaceElementConfig;
@property() private _config?: Config;
private _domain?: string;
private _service?: string;

static get properties() {
return { _config: {} };
}

public setConfig(config: LovelaceElementConfig): void {
public setConfig(config: Config): void {
if (!config || !config.service) {
throw Error("Invalid Configuration: 'service' required");
}
Expand All @@ -37,12 +52,11 @@ export class HuiServiceButtonElement extends LitElement
}

protected render(): TemplateResult | void {
if (!this._config) {
if (!this._config || !this.hass) {
return html``;
}

return html`
${this.renderStyle()}
<ha-call-service-button
.hass="${this.hass}"
.domain="${this._domain}"
Expand All @@ -53,14 +67,12 @@ export class HuiServiceButtonElement extends LitElement
`;
}

private renderStyle(): TemplateResult {
return html`
<style>
ha-call-service-button {
color: var(--primary-color);
white-space: nowrap;
}
</style>
static get styles(): CSSResult {
return css`
ha-call-service-button {
color: var(--primary-color);
white-space: nowrap;
}
`;
}
}
Expand All @@ -70,5 +82,3 @@ declare global {
"hui-service-button-element": HuiServiceButtonElement;
}
}

customElements.define("hui-service-button-element", HuiServiceButtonElement);
51 changes: 33 additions & 18 deletions src/panels/lovelace/elements/hui-state-badge-element.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { html, LitElement, TemplateResult } from "lit-element";
import {
html,
LitElement,
TemplateResult,
customElement,
property,
} from "lit-element";

import "../../../components/entity/ha-state-label-badge";
import "../components/hui-warning";

import computeStateName from "../../../common/entity/compute_state_name";
import { LovelaceElement, LovelaceElementConfig } from "./types";
import { HomeAssistant } from "../../../types";

export interface Config extends LovelaceElementConfig {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should pick unique names for exported constants, so have StateBadgeElementConfig

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and merge this one and I'll swing back around and do that for ALL configs

entity: string;
}

@customElement("hui-state-badge-element")
export class HuiStateBadgeElement extends LitElement
implements LovelaceElement {
public hass?: HomeAssistant;
private _config?: LovelaceElementConfig;

static get properties() {
return { hass: {}, _config: {} };
}
@property() public hass?: HomeAssistant;
@property() private _config?: Config;

public setConfig(config: LovelaceElementConfig): void {
public setConfig(config: Config): void {
if (!config.entity) {
throw Error("Invalid Configuration: 'entity' required");
}
Expand All @@ -24,20 +32,29 @@ export class HuiStateBadgeElement extends LitElement
}

protected render(): TemplateResult | void {
if (
!this._config ||
!this.hass ||
!this.hass.states[this._config.entity!]
) {
if (!this._config || !this.hass) {
return html``;
}

const state = this.hass.states[this._config.entity!];
const stateObj = this.hass.states[this._config.entity!];

if (!stateObj) {
return html`
<hui-warning
>${this.hass.localize(
"ui.panel.lovelace.warning.entity_not_found",
"entity",
this._config.entity
)}</hui-warning
>
`;
}

return html`
<ha-state-label-badge
.hass="${this.hass}"
.state="${state}"
.title="${computeStateName(state)}"
.state="${stateObj}"
.title="${computeStateName(stateObj)}"
></ha-state-label-badge>
`;
}
Expand All @@ -48,5 +65,3 @@ declare global {
"hui-state-badge-element": HuiStateBadgeElement;
}
}

customElements.define("hui-state-badge-element", HuiStateBadgeElement);