mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-02 05:57:54 +00:00
Code split jQuery roundslider (#2197)
This commit is contained in:
parent
6e3c2bfd6a
commit
c6542e383c
@ -8,8 +8,6 @@ import { TemplateResult } from "lit-html";
|
|||||||
|
|
||||||
import { fireEvent } from "../../../common/dom/fire_event";
|
import { fireEvent } from "../../../common/dom/fire_event";
|
||||||
import { styleMap } from "lit-html/directives/styleMap";
|
import { styleMap } from "lit-html/directives/styleMap";
|
||||||
import { jQuery } from "../../../resources/jquery";
|
|
||||||
import { roundSliderStyle } from "../../../resources/jquery.roundslider";
|
|
||||||
import { HomeAssistant, LightEntity } from "../../../types";
|
import { HomeAssistant, LightEntity } from "../../../types";
|
||||||
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
||||||
import { LovelaceCard } from "../types";
|
import { LovelaceCard } from "../types";
|
||||||
@ -23,6 +21,7 @@ import { hasConfigOrEntityChanged } from "../common/has-changed";
|
|||||||
|
|
||||||
import "../../../components/ha-card";
|
import "../../../components/ha-card";
|
||||||
import "../../../components/ha-icon";
|
import "../../../components/ha-icon";
|
||||||
|
import { loadRoundslider } from "../../../resources/jquery.roundslider.ondemand";
|
||||||
|
|
||||||
const lightConfig = {
|
const lightConfig = {
|
||||||
radius: 80,
|
radius: 80,
|
||||||
@ -49,11 +48,15 @@ export class HuiLightCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
public hass?: HomeAssistant;
|
public hass?: HomeAssistant;
|
||||||
private _config?: Config;
|
private _config?: Config;
|
||||||
private _brightnessTimout?: number;
|
private _brightnessTimout?: number;
|
||||||
|
private _roundSliderStyle?: TemplateResult;
|
||||||
|
private _jQuery?: any;
|
||||||
|
|
||||||
static get properties(): PropertyDeclarations {
|
static get properties(): PropertyDeclarations {
|
||||||
return {
|
return {
|
||||||
hass: {},
|
hass: {},
|
||||||
_config: {},
|
_config: {},
|
||||||
|
roundSliderStyle: {},
|
||||||
|
_jQuery: {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,10 +127,15 @@ export class HuiLightCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
return hasConfigOrEntityChanged(this, changedProps);
|
return hasConfigOrEntityChanged(this, changedProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected firstUpdated(): void {
|
protected async firstUpdated(): Promise<void> {
|
||||||
|
const loaded = await loadRoundslider();
|
||||||
|
|
||||||
|
this._roundSliderStyle = loaded.roundSliderStyle;
|
||||||
|
this._jQuery = loaded.jQuery;
|
||||||
|
|
||||||
const brightness = this.hass!.states[this._config!.entity].attributes
|
const brightness = this.hass!.states[this._config!.entity].attributes
|
||||||
.brightness;
|
.brightness;
|
||||||
jQuery("#light", this.shadowRoot).roundSlider({
|
this._jQuery("#light", this.shadowRoot).roundSlider({
|
||||||
...lightConfig,
|
...lightConfig,
|
||||||
change: (value) => this._setBrightness(value),
|
change: (value) => this._setBrightness(value),
|
||||||
drag: (value) => this._dragEvent(value),
|
drag: (value) => this._dragEvent(value),
|
||||||
@ -139,13 +147,13 @@ export class HuiLightCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected updated(changedProps: PropertyValues): void {
|
protected updated(changedProps: PropertyValues): void {
|
||||||
if (!this._config || !this.hass) {
|
if (!this._config || !this.hass || !this._jQuery) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const attrs = this.hass!.states[this._config!.entity].attributes;
|
const attrs = this.hass!.states[this._config!.entity].attributes;
|
||||||
|
|
||||||
jQuery("#light", this.shadowRoot).roundSlider({
|
this._jQuery("#light", this.shadowRoot).roundSlider({
|
||||||
value: Math.round((attrs.brightness / 254) * 100) || 0,
|
value: Math.round((attrs.brightness / 254) * 100) || 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -157,7 +165,7 @@ export class HuiLightCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
|
|
||||||
private renderStyle(): TemplateResult {
|
private renderStyle(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
${roundSliderStyle}
|
${this._roundSliderStyle}
|
||||||
<style>
|
<style>
|
||||||
:host {
|
:host {
|
||||||
display: block;
|
display: block;
|
||||||
|
@ -6,13 +6,11 @@ import {
|
|||||||
} from "@polymer/lit-element";
|
} from "@polymer/lit-element";
|
||||||
import { classMap } from "lit-html/directives/classMap";
|
import { classMap } from "lit-html/directives/classMap";
|
||||||
import { TemplateResult } from "lit-html";
|
import { TemplateResult } from "lit-html";
|
||||||
import { jQuery } from "../../../resources/jquery";
|
|
||||||
|
|
||||||
import applyThemesOnElement from "../../../common/dom/apply_themes_on_element";
|
import applyThemesOnElement from "../../../common/dom/apply_themes_on_element";
|
||||||
import computeStateName from "../../../common/entity/compute_state_name";
|
import computeStateName from "../../../common/entity/compute_state_name";
|
||||||
|
|
||||||
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
import { hasConfigOrEntityChanged } from "../common/has-changed";
|
||||||
import { roundSliderStyle } from "../../../resources/jquery.roundslider";
|
|
||||||
import { HomeAssistant, ClimateEntity } from "../../../types";
|
import { HomeAssistant, ClimateEntity } from "../../../types";
|
||||||
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
|
||||||
import { LovelaceCard } from "../types";
|
import { LovelaceCard } from "../types";
|
||||||
@ -20,6 +18,7 @@ import { LovelaceCardConfig } from "../../../data/lovelace";
|
|||||||
|
|
||||||
import "../../../components/ha-card";
|
import "../../../components/ha-card";
|
||||||
import "../../../components/ha-icon";
|
import "../../../components/ha-icon";
|
||||||
|
import { loadRoundslider } from "../../../resources/jquery.roundslider.ondemand";
|
||||||
|
|
||||||
const thermostatConfig = {
|
const thermostatConfig = {
|
||||||
radius: 150,
|
radius: 150,
|
||||||
@ -58,11 +57,15 @@ export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
implements LovelaceCard {
|
implements LovelaceCard {
|
||||||
public hass?: HomeAssistant;
|
public hass?: HomeAssistant;
|
||||||
private _config?: Config;
|
private _config?: Config;
|
||||||
|
private _roundSliderStyle?: TemplateResult;
|
||||||
|
private _jQuery?: any;
|
||||||
|
|
||||||
static get properties(): PropertyDeclarations {
|
static get properties(): PropertyDeclarations {
|
||||||
return {
|
return {
|
||||||
hass: {},
|
hass: {},
|
||||||
_config: {},
|
_config: {},
|
||||||
|
roundSliderStyle: {},
|
||||||
|
_jQuery: {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +137,12 @@ export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
return hasConfigOrEntityChanged(this, changedProps);
|
return hasConfigOrEntityChanged(this, changedProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected firstUpdated(): void {
|
protected async firstUpdated(): Promise<void> {
|
||||||
|
const loaded = await loadRoundslider();
|
||||||
|
|
||||||
|
this._roundSliderStyle = loaded.roundSliderStyle;
|
||||||
|
this._jQuery = loaded.jQuery;
|
||||||
|
|
||||||
const stateObj = this.hass!.states[this._config!.entity] as ClimateEntity;
|
const stateObj = this.hass!.states[this._config!.entity] as ClimateEntity;
|
||||||
|
|
||||||
const _sliderType =
|
const _sliderType =
|
||||||
@ -143,7 +151,7 @@ export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
? "range"
|
? "range"
|
||||||
: "min-range";
|
: "min-range";
|
||||||
|
|
||||||
jQuery("#thermostat", this.shadowRoot).roundSlider({
|
this._jQuery("#thermostat", this.shadowRoot).roundSlider({
|
||||||
...thermostatConfig,
|
...thermostatConfig,
|
||||||
radius: this.clientWidth / 3,
|
radius: this.clientWidth / 3,
|
||||||
min: stateObj.attributes.min_temp,
|
min: stateObj.attributes.min_temp,
|
||||||
@ -155,7 +163,7 @@ export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected updated(changedProps: PropertyValues): void {
|
protected updated(changedProps: PropertyValues): void {
|
||||||
if (!this._config || !this.hass) {
|
if (!this._config || !this.hass || !this._jQuery) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +187,7 @@ export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
sliderValue = uiValue = stateObj.attributes.temperature;
|
sliderValue = uiValue = stateObj.attributes.temperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
jQuery("#thermostat", this.shadowRoot).roundSlider({
|
this._jQuery("#thermostat", this.shadowRoot).roundSlider({
|
||||||
value: sliderValue,
|
value: sliderValue,
|
||||||
});
|
});
|
||||||
this.shadowRoot!.querySelector("#set-temperature")!.innerHTML = uiValue;
|
this.shadowRoot!.querySelector("#set-temperature")!.innerHTML = uiValue;
|
||||||
@ -192,7 +200,7 @@ export class HuiThermostatCard extends hassLocalizeLitMixin(LitElement)
|
|||||||
|
|
||||||
private renderStyle(): TemplateResult {
|
private renderStyle(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
${roundSliderStyle}
|
${this._roundSliderStyle}
|
||||||
<style>
|
<style>
|
||||||
:host {
|
:host {
|
||||||
display: block;
|
display: block;
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
import { html } from "@polymer/lit-element";
|
import { html } from "@polymer/lit-element";
|
||||||
import "./jquery";
|
// jQuery import should come before plugin import
|
||||||
|
import { jQuery as jQuery_ } from "./jquery";
|
||||||
import "round-slider";
|
import "round-slider";
|
||||||
|
// eslint-disable-next-line
|
||||||
import roundSliderCSS from "round-slider/dist/roundslider.min.css";
|
import roundSliderCSS from "round-slider/dist/roundslider.min.css";
|
||||||
|
|
||||||
|
export const jQuery = jQuery_;
|
||||||
|
|
||||||
export const roundSliderStyle = html`
|
export const roundSliderStyle = html`
|
||||||
<style>
|
<style>
|
||||||
${roundSliderCSS}
|
${roundSliderCSS}
|
||||||
|
15
src/resources/jquery.roundslider.ondemand.ts
Normal file
15
src/resources/jquery.roundslider.ondemand.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { TemplateResult } from "lit-html";
|
||||||
|
|
||||||
|
type LoadedRoundSlider = Promise<{
|
||||||
|
roundSliderStyle: TemplateResult;
|
||||||
|
jQuery: any;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
let loaded: LoadedRoundSlider;
|
||||||
|
|
||||||
|
export const loadRoundslider = async (): LoadedRoundSlider => {
|
||||||
|
if (!loaded) {
|
||||||
|
loaded = import(/* webpackChunkName: "jquery-roundslider" */ "./jquery.roundslider");
|
||||||
|
}
|
||||||
|
return loaded;
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user