Files
advanced-camera-card/src/components/select.ts
T
Dermot Duffy cc376a8be1 feat: Rename the card to advanced-camera-card (#1873)
Whilst the Frigate support in this card is the best among camera
engines, the name incorrectly suggests that Frigate is a requirement.
Instead, to broaden the appeal, change to more camera agnostic name.
This does not suggest any change in priority, role or support for
Frigate.

This change is likely to be bug prone, due to the size of the rename --
the code contains 1500+ references to "Frigate" most of which make sense
to rename, some which do not, all of which needed human assessment.

- Closes #1298


BREAKING CHANGE: References to `frigate-card` in all kinds of
configuration need to be updated to `advanced-camera-card`. An automated
config upgrade should take care of the majority of usecases (click `Edit
-> Upgrade -> Save`), though may not be perfect.
2025-02-06 19:32:32 -08:00

117 lines
3.4 KiB
TypeScript

import {
CSSResultGroup,
html,
LitElement,
PropertyValues,
TemplateResult,
unsafeCSS,
} from 'lit';
import { property } from 'lit/decorators.js';
import { createRef, ref, Ref } from 'lit/directives/ref.js';
import selectStyle from '../scss/select.scss';
import { contentsChanged, dispatchAdvancedCameraCardEvent } from '../utils/basic';
import { ScopedRegistryHost } from '@lit-labs/scoped-registry-mixin';
import { grSelectElements } from '../scoped-elements/gr-select';
import isEqual from 'lodash-es/isEqual';
import '../scoped-elements/gr-select';
export interface SelectOption {
label: string;
value: string;
}
export type SelectValues = string | string[];
type SelectElement = HTMLElement & {
value: SelectValues;
};
export class AdvancedCameraCardSelect extends ScopedRegistryHost(LitElement) {
@property({ attribute: false, hasChanged: contentsChanged })
public options?: SelectOption[];
@property({ attribute: false, hasChanged: contentsChanged })
public value: SelectValues | null = null;
@property({ attribute: false, hasChanged: contentsChanged })
public initialValue?: SelectValues;
@property({ attribute: true })
public label?: string;
@property({ attribute: true })
public placeholder?: string;
@property({ attribute: true, type: Boolean })
public multiple?: boolean = false;
@property({ attribute: true, type: Boolean })
public clearable?: boolean = false;
protected _previouslyReportedValue?: SelectValues;
protected _refSelect: Ref<SelectElement> = createRef();
static elementDefinitions = {
...grSelectElements,
};
public reset(): void {
this.value = null;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected _valueChangedHandler(_ev: CustomEvent<{ value: unknown }>): void {
const value: SelectValues | undefined = this._refSelect.value?.value;
// The underlying gr-select element is very sensitive and occasionally fires
// the change event even if the value has not actually changed. Prevent that
// from propagating upwards.
if (value !== undefined && !isEqual(this.value, value)) {
const initialValueSet = this.value === null;
this.value = value;
// The underlying gr-select element will call on the first first value set
// (even when the user has not interacted with the control). Do not
// dispatch events for this.
if (!initialValueSet) {
dispatchAdvancedCameraCardEvent(this, 'select:change', value);
}
}
}
protected willUpdate(changedProps: PropertyValues): void {
if (changedProps.has('initialValue') && this.initialValue && !this.value) {
this.value = this.initialValue;
}
}
protected render(): TemplateResult | void {
return html` <gr-select
${ref(this._refSelect)}
label=${this.label ?? ''}
placeholder=${this.placeholder ?? ''}
size="small"
?multiple=${this.multiple}
?clearable=${this.clearable}
.value=${this.value ?? []}
@gr-change=${this._valueChangedHandler.bind(this)}
>
${this.options?.map(
(option) =>
html`<gr-menu-item value="${option.value ?? ''}"
>${option.label}</gr-menu-item
>`,
)}
</gr-select>`;
}
static get styles(): CSSResultGroup {
return unsafeCSS(selectStyle);
}
}
declare global {
interface HTMLElementTagNameMap {
'advanced-camera-card-select': AdvancedCameraCardSelect;
}
}