import { ScopedRegistryHost } from '@lit-labs/scoped-registry-mixin'; import { CSSResultGroup, html, LitElement, PropertyValues, TemplateResult, unsafeCSS, } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { classMap } from 'lit/directives/class-map.js'; import { createRef, ref, Ref } from 'lit/directives/ref.js'; import { CameraManager } from '../camera-manager/manager'; import { ViewManagerEpoch } from '../card-controller/view/types'; import { MediaFilterController, MediaFilterCoreFavoriteSelection, MediaFilterCoreWhen, MediaFilterMediaType, } from '../components-lib/media-filter-controller'; import { CardWideConfig } from '../config/schema/types'; import { HomeAssistant } from '../ha/types'; import { localize } from '../localize/localize'; import mediaFilterStyle from '../scss/media-filter.scss'; import { AdvancedCameraCardDatePicker } from './date-picker'; import './date-picker.js'; import { AdvancedCameraCardSelect } from './select'; import './select.js'; @customElement('advanced-camera-card-media-filter') class AdvancedCameraCardMediaFilter extends ScopedRegistryHost(LitElement) { @property({ attribute: false }) public hass?: HomeAssistant; @property({ attribute: false }) public cameraManager?: CameraManager; @property({ attribute: false }) public viewManagerEpoch?: ViewManagerEpoch; @property({ attribute: false }) public cardWideConfig?: CardWideConfig; static elementDefinitions = { 'advanced-camera-card-select': AdvancedCameraCardSelect, 'advanced-camera-card-date-picker': AdvancedCameraCardDatePicker, }; protected _mediaFilterController = new MediaFilterController(this); protected _refMediaType: Ref = createRef(); protected _refCamera: Ref = createRef(); protected _refWhen: Ref = createRef(); protected _refWhenFrom: Ref = createRef(); protected _refWhenTo: Ref = createRef(); protected _refWhat: Ref = createRef(); protected _refWhere: Ref = createRef(); protected _refFavorite: Ref = createRef(); protected _refTags: Ref = createRef(); protected willUpdate(changedProps: PropertyValues): void { if (changedProps.has('viewManagerEpoch')) { this._mediaFilterController.setViewManager(this.viewManagerEpoch?.manager ?? null); } if (changedProps.has('cameraManager') && this.cameraManager) { this._mediaFilterController.computeCameraOptions(this.cameraManager); this._mediaFilterController.computeMetadataOptions(this.cameraManager); } // The first time the viewManager is set, compute the initial default selections. if ( !changedProps.get('viewManager') && this.viewManagerEpoch && this.cameraManager ) { this._mediaFilterController.computeInitialDefaultsFromView(this.cameraManager); } } protected render(): TemplateResult | void { const valueChange = async () => { if (!this.cameraManager || !this.viewManagerEpoch || !this.cardWideConfig) { return; } await this._mediaFilterController.valueChangeHandler( this.cameraManager, this.cardWideConfig, { camera: this._refCamera.value?.value ?? undefined, mediaType: (this._refMediaType.value?.value ?? undefined) as | MediaFilterMediaType | undefined, when: { selected: this._refWhen.value?.value ?? undefined, from: this._refWhenFrom.value?.value, to: this._refWhenTo.value?.value, }, favorite: (this._refFavorite.value?.value ?? undefined) as | MediaFilterCoreFavoriteSelection | undefined, where: this._refWhere.value?.value ?? undefined, what: this._refWhat.value?.value ?? undefined, tags: this._refTags.value?.value ?? undefined, }, ); }; // Ensure that the "When" selector and the custom calendar to/from selectors // are ~mutually exclusive. const whenChange = async (whenPriority?: 'custom' | 'selected'): Promise => { if (whenPriority === 'custom' && this._refWhen.value) { if (!this._refWhenFrom.value?.value && !this._refWhenTo.value?.value) { this._refWhen.value.reset(); } else { this._refWhen.value.value = MediaFilterCoreWhen.Custom; } } else if (this._refWhen.value?.value !== MediaFilterCoreWhen.Custom) { this._refWhenFrom.value?.reset(); this._refWhenTo.value?.reset(); } await valueChange(); }; if (!this.cameraManager || !this.viewManagerEpoch) { return; } const controls = this._mediaFilterController.getControlsToShow(this.cameraManager); const defaults = this._mediaFilterController.getDefaults(); const whatOptions = this._mediaFilterController.getWhatOptions(); const tagsOptions = this._mediaFilterController.getTagsOptions(); const whereOptions = this._mediaFilterController.getWhereOptions(); return html` valueChange()} >
whenChange('selected')} > whenChange('custom')} > whenChange('custom')} >
valueChange()} > ${controls.events && whatOptions.length ? html` valueChange()} > ` : ''} ${controls.events && tagsOptions.length ? html` valueChange()} > ` : ''} ${controls.events && whereOptions.length ? html` valueChange()} > ` : ''} ${controls.favorites ? html` valueChange()} > ` : ''}`; } static get styles(): CSSResultGroup { return unsafeCSS(mediaFilterStyle); } } declare global { interface HTMLElementTagNameMap { 'advanced-camera-card-media-filter': AdvancedCameraCardMediaFilter; } }