Retire use of @query and use ref univerally.

This commit is contained in:
Dermot Duffy
2022-04-18 19:20:03 -07:00
parent 4980fbfa19
commit af07624e51
3 changed files with 33 additions and 32 deletions
+20 -17
View File
@@ -10,6 +10,7 @@ import { HomeAssistant, LovelaceCardEditor, getLovelace } from 'custom-card-help
import { StyleInfo, styleMap } from 'lit/directives/style-map.js'; import { StyleInfo, styleMap } from 'lit/directives/style-map.js';
import { customElement, property, query, state } from 'lit/decorators.js'; import { customElement, property, query, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js'; import { classMap } from 'lit/directives/class-map.js';
import { createRef, ref, Ref } from 'lit/directives/ref.js';
import screenfull from 'screenfull'; import screenfull from 'screenfull';
import { throttle } from 'lodash-es'; import { throttle } from 'lodash-es';
import { until } from 'lit/directives/until.js'; import { until } from 'lit/directives/until.js';
@@ -150,14 +151,9 @@ export class FrigateCard extends LitElement {
@state() @state()
protected _conditionState?: ConditionState; protected _conditionState?: ConditionState;
@query('frigate-card-menu') protected _refMenu: Ref<FrigateCardMenu> = createRef();
protected _menu!: FrigateCardMenu; protected _refElements: Ref<FrigateCardElements> = createRef();
protected _refImage: Ref<FrigateCardImage> = createRef();
@query('frigate-card-elements')
protected _elements?: FrigateCardElements;
@query('frigate-card-image')
protected _image?: FrigateCardImage;
// user interaction timer ("screensaver" functionality, return to default // user interaction timer ("screensaver" functionality, return to default
// view after user interaction). // view after user interaction).
@@ -195,14 +191,14 @@ export class FrigateCard extends LitElement {
// to update, without necessarily re-rendering the entire card (re-rendering // to update, without necessarily re-rendering the entire card (re-rendering
// is expensive). // is expensive).
if (this._hass) { if (this._hass) {
if (this._menu) { if (this._refMenu.value) {
this._menu.hass = this._hass; this._refMenu.value.hass = this._hass;
} }
if (this._elements) { if (this._refElements.value) {
this._elements.hass = this._hass; this._refElements.value.hass = this._hass;
} }
if (this._image) { if (this._refImage.value) {
this._image.hass = this._hass; this._refImage.value.hass = this._hass;
} }
} }
@@ -434,7 +430,9 @@ export class FrigateCard extends LitElement {
if (!this._dynamicMenuButtons.includes(button)) { if (!this._dynamicMenuButtons.includes(button)) {
this._dynamicMenuButtons = [...this._dynamicMenuButtons, button]; this._dynamicMenuButtons = [...this._dynamicMenuButtons, button];
} }
this._menu.buttons = this._getMenuButtons(); if (this._refMenu.value) {
this._refMenu.value.buttons = this._getMenuButtons();
}
} }
/** /**
@@ -445,7 +443,9 @@ export class FrigateCard extends LitElement {
this._dynamicMenuButtons = this._dynamicMenuButtons.filter( this._dynamicMenuButtons = this._dynamicMenuButtons.filter(
(button) => button != target, (button) => button != target,
); );
this._menu.buttons = this._getMenuButtons(); if (this._refMenu.value) {
this._refMenu.value.buttons = this._getMenuButtons();
}
} }
/** /**
@@ -888,7 +888,7 @@ export class FrigateCard extends LitElement {
// This is a rare code path: this would only be used if someone has a // This is a rare code path: this would only be used if someone has a
// menu toggle action configured outside of the menu itself (e.g. // menu toggle action configured outside of the menu itself (e.g.
// picture elements). // picture elements).
this._menu.toggleMenu(); this._refMenu.value?.toggleMenu();
break; break;
case 'camera_select': case 'camera_select':
const camera = frigateCardAction.camera; const camera = frigateCardAction.camera;
@@ -1025,6 +1025,7 @@ export class FrigateCard extends LitElement {
protected _renderMenu(): TemplateResult | void { protected _renderMenu(): TemplateResult | void {
return html` return html`
<frigate-card-menu <frigate-card-menu
${ref(this._refMenu)}
.hass=${this._hass} .hass=${this._hass}
.menuConfig=${this._getConfig().menu} .menuConfig=${this._getConfig().menu}
.buttons=${this._getMenuButtons()} .buttons=${this._getMenuButtons()}
@@ -1239,6 +1240,7 @@ export class FrigateCard extends LitElement {
// be present even if a particular view has an error. Elements // be present even if a particular view has an error. Elements
// need to render after the main views so it can render 'on top'. // need to render after the main views so it can render 'on top'.
html` <frigate-card-elements html` <frigate-card-elements
${ref(this._refElements)}
.hass=${this._hass} .hass=${this._hass}
.elements=${this._getConfig().elements} .elements=${this._getConfig().elements}
.conditionState=${this._conditionState} .conditionState=${this._conditionState}
@@ -1285,6 +1287,7 @@ export class FrigateCard extends LitElement {
return html` return html`
${!this._message && this._view.is('image') ${!this._message && this._view.is('image')
? html` <frigate-card-image ? html` <frigate-card-image
${ref(this._refImage)}
.imageConfig=${this._getConfig().image} .imageConfig=${this._getConfig().image}
.view=${this._view} .view=${this._view}
.hass=${this._hass} .hass=${this._hass}
+6 -9
View File
@@ -1,6 +1,7 @@
import { LitElement, TemplateResult, html, CSSResultGroup, unsafeCSS } from 'lit'; import { LitElement, TemplateResult, html, CSSResultGroup, unsafeCSS } from 'lit';
import { HomeAssistant } from 'custom-card-helpers'; import { HomeAssistant } from 'custom-card-helpers';
import { customElement, property, query } from 'lit/decorators.js'; import { createRef, ref, Ref } from 'lit/directives/ref.js';
import { customElement, property } from 'lit/decorators.js';
import { import {
FrigateConditional, FrigateConditional,
@@ -144,9 +145,6 @@ export class FrigateCardElements extends LitElement {
@property({ attribute: false }) @property({ attribute: false })
protected conditionState?: ConditionState; protected conditionState?: ConditionState;
@query('frigate-card-elements-core')
_core!: FrigateCardElementsCore;
/** /**
* Handle a picture element to be removed from the menu. * Handle a picture element to be removed from the menu.
* @param ev The event. * @param ev The event.
@@ -235,16 +233,14 @@ export class FrigateCardElements extends LitElement {
export class FrigateCardElementsConditional extends LitElement { export class FrigateCardElementsConditional extends LitElement {
protected _config?: FrigateConditional; protected _config?: FrigateConditional;
protected _hass?: HomeAssistant; protected _hass?: HomeAssistant;
protected _refCore: Ref<FrigateCardElementsCore> = createRef() ;
@query('frigate-card-elements-core')
_core?: FrigateCardElementsCore;
/** /**
* Set the Home Assistant object. * Set the Home Assistant object.
*/ */
set hass(hass: HomeAssistant) { set hass(hass: HomeAssistant) {
if (this._core) { if (this._refCore.value) {
this._core.hass = hass; this._refCore.value.hass = hass;
} }
this._hass = hass; this._hass = hass;
} }
@@ -283,6 +279,7 @@ export class FrigateCardElementsConditional extends LitElement {
protected render(): TemplateResult | void { protected render(): TemplateResult | void {
if (fetchStateAndEvaluateCondition(this, this._config.conditions)) { if (fetchStateAndEvaluateCondition(this, this._config.conditions)) {
return html` <frigate-card-elements-core return html` <frigate-card-elements-core
${ref(this._refCore)}
.hass=${this._hass} .hass=${this._hass}
.elements=${this._config.elements} .elements=${this._config.elements}
> >
+7 -6
View File
@@ -7,7 +7,8 @@ import {
unsafeCSS, unsafeCSS,
} from 'lit'; } from 'lit';
import { HomeAssistant } from 'custom-card-helpers'; import { HomeAssistant } from 'custom-card-helpers';
import { customElement, property, query, state } from 'lit/decorators.js'; import { createRef, ref, Ref } from 'lit/directives/ref.js';
import { customElement, property, state } from 'lit/decorators.js';
import { CachedValueController } from '../cached-value-controller.js'; import { CachedValueController } from '../cached-value-controller.js';
import { CameraConfig, ImageViewConfig } from '../types.js'; import { CameraConfig, ImageViewConfig } from '../types.js';
@@ -40,8 +41,7 @@ export class FrigateCardImage extends LitElement {
@state() @state()
protected _imageConfig?: ImageViewConfig; protected _imageConfig?: ImageViewConfig;
@query('img') protected _refImage: Ref<HTMLImageElement> = createRef() ;
protected _image?: HTMLImageElement;
protected _cachedValueController?: CachedValueController<string>; protected _cachedValueController?: CachedValueController<string>;
protected _boundVisibilityHandler = this._visibilityHandler.bind(this); protected _boundVisibilityHandler = this._visibilityHandler.bind(this);
@@ -144,7 +144,7 @@ export class FrigateCardImage extends LitElement {
* Handle document visibility changes. * Handle document visibility changes.
*/ */
protected _visibilityHandler(): void { protected _visibilityHandler(): void {
if (!this._image) { if (!this._refImage.value) {
return; return;
} }
if (document.visibilityState === 'hidden') { if (document.visibilityState === 'hidden') {
@@ -196,8 +196,8 @@ export class FrigateCardImage extends LitElement {
* Force the img element to the stock image. * Force the img element to the stock image.
*/ */
protected _forceStockImage(): void { protected _forceStockImage(): void {
if (this._image) { if (this._refImage.value) {
this._image.src = defaultImage; this._refImage.value.src = defaultImage;
} }
} }
@@ -205,6 +205,7 @@ export class FrigateCardImage extends LitElement {
const src = this._cachedValueController?.value; const src = this._cachedValueController?.value;
return src return src
? html` <img ? html` <img
${ref(this._refImage)}
src=${src} src=${src}
@load=${(ev) => { @load=${(ev) => {
dispatchMediaShowEvent(this, ev); dispatchMediaShowEvent(this, ev);