mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-17 06:16:33 +00:00
Calendar card to HA Form (#11784)
This commit is contained in:
parent
3204dbfc4d
commit
8db22d4f88
@ -1,7 +1,8 @@
|
|||||||
import "@material/mwc-list/mwc-list-item";
|
import "../../../../components/entity/ha-entities-picker";
|
||||||
import "@material/mwc-select/mwc-select";
|
import "../../../../components/ha-form/ha-form";
|
||||||
import { CSSResultGroup, html, LitElement, TemplateResult } from "lit";
|
import { css, html, LitElement, TemplateResult } from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import {
|
import {
|
||||||
array,
|
array,
|
||||||
assert,
|
assert,
|
||||||
@ -13,16 +14,12 @@ import {
|
|||||||
union,
|
union,
|
||||||
} from "superstruct";
|
} from "superstruct";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { stopPropagation } from "../../../../common/dom/stop_propagation";
|
import { LocalizeFunc } from "../../../../common/translations/localize";
|
||||||
import "../../../../components/entity/ha-entities-picker";
|
import type { HaFormSchema } from "../../../../components/ha-form/types";
|
||||||
import type { HomeAssistant } from "../../../../types";
|
import type { HomeAssistant } from "../../../../types";
|
||||||
import type { CalendarCardConfig } from "../../cards/types";
|
import type { CalendarCardConfig } from "../../cards/types";
|
||||||
import "../../components/hui-entity-editor";
|
|
||||||
import "../../components/hui-theme-select-editor";
|
|
||||||
import type { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
import type { EditorTarget, EntitiesEditorEvent } from "../types";
|
|
||||||
import { configElementStyle } from "./config-elements-style";
|
|
||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
@ -43,75 +40,54 @@ export class HuiCalendarCardEditor
|
|||||||
{
|
{
|
||||||
@property({ attribute: false }) public hass?: HomeAssistant;
|
@property({ attribute: false }) public hass?: HomeAssistant;
|
||||||
|
|
||||||
@property({ attribute: false }) private _config?: CalendarCardConfig;
|
@state() private _config?: CalendarCardConfig;
|
||||||
|
|
||||||
@state() private _configEntities?: string[];
|
|
||||||
|
|
||||||
public setConfig(config: CalendarCardConfig): void {
|
public setConfig(config: CalendarCardConfig): void {
|
||||||
assert(config, cardConfigStruct);
|
assert(config, cardConfigStruct);
|
||||||
this._config = config;
|
this._config = config;
|
||||||
this._configEntities = config.entities;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get _title(): string {
|
private _schema = memoizeOne((localize: LocalizeFunc) => [
|
||||||
return this._config!.title || "";
|
{
|
||||||
}
|
name: "",
|
||||||
|
type: "grid",
|
||||||
get _initial_view(): string {
|
schema: [
|
||||||
return this._config!.initial_view || "dayGridMonth";
|
{ name: "title", required: false, selector: { text: {} } },
|
||||||
}
|
{
|
||||||
|
name: "initial_view",
|
||||||
get _theme(): string {
|
required: false,
|
||||||
return this._config!.theme || "";
|
selector: {
|
||||||
}
|
select: {
|
||||||
|
options: views.map((view) => [
|
||||||
|
view,
|
||||||
|
localize(
|
||||||
|
`ui.panel.lovelace.editor.card.calendar.views.${view}`
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ name: "theme", required: false, selector: { theme: {} } },
|
||||||
|
]);
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.hass || !this._config) {
|
if (!this.hass || !this._config) {
|
||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const schema = this._schema(this.hass.localize);
|
||||||
|
const data = { initial_view: "dayGridMonth", ...this._config };
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<div class="card-config">
|
<ha-form
|
||||||
<div class="side-by-side">
|
.hass=${this.hass}
|
||||||
<paper-input
|
.data=${data}
|
||||||
.label="${this.hass.localize(
|
.schema=${schema}
|
||||||
"ui.panel.lovelace.editor.card.generic.title"
|
.computeLabel=${this._computeLabelCallback}
|
||||||
)} (${this.hass.localize(
|
@value-changed=${this._valueChanged}
|
||||||
"ui.panel.lovelace.editor.card.config.optional"
|
></ha-form>
|
||||||
)})"
|
|
||||||
.value=${this._title}
|
|
||||||
.configValue=${"title"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></paper-input>
|
|
||||||
<mwc-select
|
|
||||||
.label=${this.hass.localize(
|
|
||||||
"ui.panel.lovelace.editor.card.calendar.inital_view"
|
|
||||||
)}
|
|
||||||
.value=${this._initial_view}
|
|
||||||
.configValue=${"initial_view"}
|
|
||||||
@selected=${this._viewChanged}
|
|
||||||
@closed=${stopPropagation}
|
|
||||||
naturalMenuWidth
|
|
||||||
fixedMenuPosition
|
|
||||||
>
|
|
||||||
${views.map(
|
|
||||||
(view) => html`
|
|
||||||
<mwc-list-item .value=${view}
|
|
||||||
>${this.hass!.localize(
|
|
||||||
`ui.panel.lovelace.editor.card.calendar.views.${view}`
|
|
||||||
)}
|
|
||||||
</mwc-list-item>
|
|
||||||
`
|
|
||||||
)}
|
|
||||||
</mwc-select>
|
|
||||||
</div>
|
|
||||||
<hui-theme-select-editor
|
|
||||||
.hass=${this.hass}
|
|
||||||
.value=${this._theme}
|
|
||||||
.configValue=${"theme"}
|
|
||||||
@value-changed=${this._valueChanged}
|
|
||||||
></hui-theme-select-editor>
|
|
||||||
</div>
|
|
||||||
<h3>
|
<h3>
|
||||||
${this.hass.localize(
|
${this.hass.localize(
|
||||||
"ui.panel.lovelace.editor.card.calendar.calendar_entities"
|
"ui.panel.lovelace.editor.card.calendar.calendar_entities"
|
||||||
@ -122,62 +98,40 @@ export class HuiCalendarCardEditor
|
|||||||
</h3>
|
</h3>
|
||||||
<ha-entities-picker
|
<ha-entities-picker
|
||||||
.hass=${this.hass!}
|
.hass=${this.hass!}
|
||||||
.value=${this._configEntities}
|
.value=${this._config.entities}
|
||||||
.includeDomains=${["calendar"]}
|
.includeDomains=${["calendar"]}
|
||||||
@value-changed=${this._valueChanged}
|
@value-changed=${this._entitiesChanged}
|
||||||
>
|
>
|
||||||
</ha-entities-picker>
|
</ha-entities-picker>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _valueChanged(ev: EntitiesEditorEvent | CustomEvent): void {
|
private _valueChanged(ev: CustomEvent): void {
|
||||||
if (!this._config || !this.hass) {
|
const config = ev.detail.value;
|
||||||
return;
|
fireEvent(this, "config-changed", { config });
|
||||||
}
|
|
||||||
|
|
||||||
const target = ev.target! as EditorTarget;
|
|
||||||
|
|
||||||
if (this[`_${target.configValue}`] === target.value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ev.detail && ev.detail.value && Array.isArray(ev.detail.value)) {
|
|
||||||
this._config = { ...this._config, entities: ev.detail.value };
|
|
||||||
} else if (target.configValue) {
|
|
||||||
if (target.value === "") {
|
|
||||||
this._config = { ...this._config };
|
|
||||||
delete this._config[target.configValue!];
|
|
||||||
} else {
|
|
||||||
this._config = {
|
|
||||||
...this._config,
|
|
||||||
[target.configValue]: target.value,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fireEvent(this, "config-changed", { config: this._config });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _viewChanged(ev): void {
|
private _entitiesChanged(ev): void {
|
||||||
if (!this._config || !this.hass) {
|
const config = { ...this._config!, entities: ev.detail.value };
|
||||||
return;
|
fireEvent(this, "config-changed", { config });
|
||||||
}
|
|
||||||
|
|
||||||
if (ev.target.value === "") {
|
|
||||||
this._config = { ...this._config };
|
|
||||||
delete this._config.initial_view;
|
|
||||||
} else {
|
|
||||||
this._config = {
|
|
||||||
...this._config,
|
|
||||||
initial_view: ev.target.value,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
fireEvent(this, "config-changed", { config: this._config });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
private _computeLabelCallback = (schema: HaFormSchema) => {
|
||||||
return configElementStyle;
|
if (schema.name === "title") {
|
||||||
}
|
return this.hass!.localize("ui.panel.lovelace.editor.card.generic.title");
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.hass!.localize(
|
||||||
|
`ui.panel.lovelace.editor.card.calendar.${schema.name}`
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
static styles = css`
|
||||||
|
ha-form {
|
||||||
|
display: block;
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
|
@ -3449,7 +3449,7 @@
|
|||||||
"calendar": {
|
"calendar": {
|
||||||
"name": "Calendar",
|
"name": "Calendar",
|
||||||
"description": "The Calendar card displays a calendar including day, week and list views",
|
"description": "The Calendar card displays a calendar including day, week and list views",
|
||||||
"inital_view": "Initial View",
|
"initial_view": "Initial View",
|
||||||
"calendar_entities": "Calendar Entities",
|
"calendar_entities": "Calendar Entities",
|
||||||
"views": {
|
"views": {
|
||||||
"dayGridMonth": "Month",
|
"dayGridMonth": "Month",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user