mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-03 14:37:47 +00:00
Add vertical and horizontal layout to features
This commit is contained in:
parent
1dbb70b964
commit
c3507bac0b
@ -10,6 +10,7 @@ import {
|
|||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import type { HuiErrorCard } from "../cards/hui-error-card";
|
import type { HuiErrorCard } from "../cards/hui-error-card";
|
||||||
|
import { LovelaceCardFeatureLayout } from "../cards/types";
|
||||||
import { createCardFeatureElement } from "../create-element/create-card-feature-element";
|
import { createCardFeatureElement } from "../create-element/create-card-feature-element";
|
||||||
import type { LovelaceCardFeature } from "../types";
|
import type { LovelaceCardFeature } from "../types";
|
||||||
import type { LovelaceCardFeatureConfig } from "./types";
|
import type { LovelaceCardFeatureConfig } from "./types";
|
||||||
@ -22,6 +23,8 @@ export class HuiCardFeatures extends LitElement {
|
|||||||
|
|
||||||
@property({ attribute: false }) public features?: LovelaceCardFeatureConfig[];
|
@property({ attribute: false }) public features?: LovelaceCardFeatureConfig[];
|
||||||
|
|
||||||
|
@property({ attribute: false }) public layout?: LovelaceCardFeatureLayout;
|
||||||
|
|
||||||
@property({ attribute: false }) public color?: string;
|
@property({ attribute: false }) public color?: string;
|
||||||
|
|
||||||
private _featuresElements = new WeakMap<
|
private _featuresElements = new WeakMap<
|
||||||
@ -58,10 +61,15 @@ export class HuiCardFeatures extends LitElement {
|
|||||||
if (!this.features) {
|
if (!this.features) {
|
||||||
return nothing;
|
return nothing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const containerClass = this.layout?.type ? ` ${this.layout.type}` : "";
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
${this.features.map((featureConf) =>
|
<div class="container${containerClass}">
|
||||||
this.renderFeature(featureConf, this.stateObj)
|
${this.features.map((featureConf) =>
|
||||||
)}
|
this.renderFeature(featureConf, this.stateObj)
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,8 +77,21 @@ export class HuiCardFeatures extends LitElement {
|
|||||||
return css`
|
return css`
|
||||||
:host {
|
:host {
|
||||||
--feature-color: var(--state-icon-color);
|
--feature-color: var(--state-icon-color);
|
||||||
|
--feature-padding: 12px;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
padding: var(--feature-padding);
|
||||||
|
padding-top: 0px;
|
||||||
|
gap: var(--feature-padding);
|
||||||
|
}
|
||||||
|
.container.horizontal {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
}
|
||||||
|
.container.horizontal > * {
|
||||||
|
flex: 1;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
@ -208,7 +208,6 @@ class HuiClimateHvacModesCardFeature
|
|||||||
--control-select-button-border-radius: 10px;
|
--control-select-button-border-radius: 10px;
|
||||||
}
|
}
|
||||||
.container {
|
.container {
|
||||||
padding: 0 12px 12px 12px;
|
|
||||||
width: auto;
|
width: auto;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -216,7 +216,6 @@ class HuiClimatePresetModesCardFeature
|
|||||||
--control-select-button-border-radius: 10px;
|
--control-select-button-border-radius: 10px;
|
||||||
}
|
}
|
||||||
.container {
|
.container {
|
||||||
padding: 0 12px 12px 12px;
|
|
||||||
width: auto;
|
width: auto;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -94,7 +94,6 @@ class HuiLightBrightnessCardFeature
|
|||||||
--control-slider-border-radius: 10px;
|
--control-slider-border-radius: 10px;
|
||||||
}
|
}
|
||||||
.container {
|
.container {
|
||||||
padding: 0 12px 12px 12px;
|
|
||||||
width: auto;
|
width: auto;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -120,7 +120,6 @@ class HuiLightColorTempCardFeature
|
|||||||
--control-slider-border-radius: 10px;
|
--control-slider-border-radius: 10px;
|
||||||
}
|
}
|
||||||
.container {
|
.container {
|
||||||
padding: 0 12px 12px 12px;
|
|
||||||
width: auto;
|
width: auto;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -285,7 +285,6 @@ class HuiTargetTemperatureCardFeature
|
|||||||
static get styles() {
|
static get styles() {
|
||||||
return css`
|
return css`
|
||||||
ha-control-button-group {
|
ha-control-button-group {
|
||||||
margin: 0 12px 12px 12px;
|
|
||||||
--control-button-group-spacing: 12px;
|
--control-button-group-spacing: 12px;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
@ -428,6 +428,7 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
|
|||||||
.stateObj=${stateObj}
|
.stateObj=${stateObj}
|
||||||
.color=${this._config.color}
|
.color=${this._config.color}
|
||||||
.features=${this._config.features}
|
.features=${this._config.features}
|
||||||
|
.layout=${this._config.feature_layout}
|
||||||
></hui-card-features>
|
></hui-card-features>
|
||||||
`
|
`
|
||||||
: nothing}
|
: nothing}
|
||||||
|
@ -24,6 +24,10 @@ export type AlarmPanelCardConfigState =
|
|||||||
| "arm_vacation"
|
| "arm_vacation"
|
||||||
| "arm_custom_bypass";
|
| "arm_custom_bypass";
|
||||||
|
|
||||||
|
export type LovelaceCardFeatureLayout = {
|
||||||
|
type?: "vertical" | "horizontal" | "compact";
|
||||||
|
};
|
||||||
|
|
||||||
export interface AlarmPanelCardConfig extends LovelaceCardConfig {
|
export interface AlarmPanelCardConfig extends LovelaceCardConfig {
|
||||||
entity: string;
|
entity: string;
|
||||||
name?: string;
|
name?: string;
|
||||||
@ -506,4 +510,5 @@ export interface TileCardConfig extends LovelaceCardConfig {
|
|||||||
double_tap_action?: ActionConfig;
|
double_tap_action?: ActionConfig;
|
||||||
icon_tap_action?: ActionConfig;
|
icon_tap_action?: ActionConfig;
|
||||||
features?: LovelaceCardFeatureConfig[];
|
features?: LovelaceCardFeatureConfig[];
|
||||||
|
feature_layout?: LovelaceCardFeatureLayout;
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,16 @@ import { HassEntity } from "home-assistant-js-websocket";
|
|||||||
import { CSSResultGroup, LitElement, css, html, nothing } from "lit";
|
import { CSSResultGroup, LitElement, css, html, nothing } from "lit";
|
||||||
import { customElement, property } from "lit/decorators";
|
import { customElement, property } from "lit/decorators";
|
||||||
import { repeat } from "lit/directives/repeat";
|
import { repeat } from "lit/directives/repeat";
|
||||||
|
import memoizeOne from "memoize-one";
|
||||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||||
import { stopPropagation } from "../../../../common/dom/stop_propagation";
|
import { stopPropagation } from "../../../../common/dom/stop_propagation";
|
||||||
|
import { LocalizeFunc } from "../../../../common/translations/localize";
|
||||||
import "../../../../components/entity/ha-entity-picker";
|
import "../../../../components/entity/ha-entity-picker";
|
||||||
import "../../../../components/ha-button";
|
import "../../../../components/ha-button";
|
||||||
|
import {
|
||||||
|
HaFormSchema,
|
||||||
|
SchemaUnion,
|
||||||
|
} from "../../../../components/ha-form/types";
|
||||||
import "../../../../components/ha-icon-button";
|
import "../../../../components/ha-icon-button";
|
||||||
import "../../../../components/ha-list-item";
|
import "../../../../components/ha-list-item";
|
||||||
import "../../../../components/ha-sortable";
|
import "../../../../components/ha-sortable";
|
||||||
@ -45,7 +51,9 @@ import { supportsUpdateActionsCardFeature } from "../../card-features/hui-update
|
|||||||
import { supportsVacuumCommandsCardFeature } from "../../card-features/hui-vacuum-commands-card-feature";
|
import { supportsVacuumCommandsCardFeature } from "../../card-features/hui-vacuum-commands-card-feature";
|
||||||
import { supportsWaterHeaterOperationModesCardFeature } from "../../card-features/hui-water-heater-operation-modes-card-feature";
|
import { supportsWaterHeaterOperationModesCardFeature } from "../../card-features/hui-water-heater-operation-modes-card-feature";
|
||||||
import { LovelaceCardFeatureConfig } from "../../card-features/types";
|
import { LovelaceCardFeatureConfig } from "../../card-features/types";
|
||||||
|
import { LovelaceCardFeatureLayout } from "../../cards/types";
|
||||||
import { getCardFeatureElementClass } from "../../create-element/create-card-feature-element";
|
import { getCardFeatureElementClass } from "../../create-element/create-card-feature-element";
|
||||||
|
import { capitalizeFirstLetter } from "../../../../common/string/capitalize-first-letter";
|
||||||
|
|
||||||
export type FeatureType = LovelaceCardFeatureConfig["type"];
|
export type FeatureType = LovelaceCardFeatureConfig["type"];
|
||||||
type SupportsFeature = (stateObj: HassEntity) => boolean;
|
type SupportsFeature = (stateObj: HassEntity) => boolean;
|
||||||
@ -142,6 +150,9 @@ declare global {
|
|||||||
"features-changed": {
|
"features-changed": {
|
||||||
features: LovelaceCardFeatureConfig[];
|
features: LovelaceCardFeatureConfig[];
|
||||||
};
|
};
|
||||||
|
"layout-changed": {
|
||||||
|
layout: LovelaceCardFeatureLayout;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,6 +165,9 @@ export class HuiCardFeaturesEditor extends LitElement {
|
|||||||
@property({ attribute: false })
|
@property({ attribute: false })
|
||||||
public features?: LovelaceCardFeatureConfig[];
|
public features?: LovelaceCardFeatureConfig[];
|
||||||
|
|
||||||
|
@property({ attribute: false })
|
||||||
|
public layout?: LovelaceCardFeatureLayout;
|
||||||
|
|
||||||
@property({ attribute: false })
|
@property({ attribute: false })
|
||||||
public featuresTypes?: FeatureType[];
|
public featuresTypes?: FeatureType[];
|
||||||
|
|
||||||
@ -162,6 +176,37 @@ export class HuiCardFeaturesEditor extends LitElement {
|
|||||||
|
|
||||||
private _featuresKeys = new WeakMap<LovelaceCardFeatureConfig, string>();
|
private _featuresKeys = new WeakMap<LovelaceCardFeatureConfig, string>();
|
||||||
|
|
||||||
|
private _optionsSchema = memoizeOne(
|
||||||
|
(_localize: LocalizeFunc) =>
|
||||||
|
[
|
||||||
|
{
|
||||||
|
name: "type",
|
||||||
|
selector: {
|
||||||
|
select: {
|
||||||
|
mode: "dropdown",
|
||||||
|
options: ["vertical", "horizontal", "compact"].map((layout) => ({
|
||||||
|
label: capitalizeFirstLetter(layout),
|
||||||
|
value: layout,
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
] as const satisfies readonly HaFormSchema[]
|
||||||
|
);
|
||||||
|
|
||||||
|
private _computeLabelCallback = (
|
||||||
|
schema: SchemaUnion<ReturnType<typeof this._optionsSchema>>
|
||||||
|
) => {
|
||||||
|
switch (schema.name) {
|
||||||
|
case "type":
|
||||||
|
return "Layout";
|
||||||
|
default:
|
||||||
|
return this.hass!.localize(
|
||||||
|
`ui.panel.lovelace.editor.card.generic.${schema.name}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
)
|
||||||
private _supportsFeatureType(type: string): boolean {
|
private _supportsFeatureType(type: string): boolean {
|
||||||
if (!this.stateObj) return false;
|
if (!this.stateObj) return false;
|
||||||
|
|
||||||
@ -235,6 +280,14 @@ export class HuiCardFeaturesEditor extends LitElement {
|
|||||||
isCustomType(type)
|
isCustomType(type)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const schema = this._optionsSchema(this.hass.localize);
|
||||||
|
|
||||||
|
const data = { ...this.layout };
|
||||||
|
|
||||||
|
if (!data.type) {
|
||||||
|
data.type = "vertical";
|
||||||
|
}
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-expansion-panel outlined>
|
<ha-expansion-panel outlined>
|
||||||
<h3 slot="header">
|
<h3 slot="header">
|
||||||
@ -251,6 +304,17 @@ export class HuiCardFeaturesEditor extends LitElement {
|
|||||||
</ha-alert>
|
</ha-alert>
|
||||||
`
|
`
|
||||||
: nothing}
|
: nothing}
|
||||||
|
${supportedFeaturesType.length > 0
|
||||||
|
? html`
|
||||||
|
<ha-form
|
||||||
|
.hass=${this.hass}
|
||||||
|
.data=${data}
|
||||||
|
.schema=${schema}
|
||||||
|
.computeLabel=${this._computeLabelCallback}
|
||||||
|
@value-changed=${this._layoutChanged}
|
||||||
|
></ha-form>
|
||||||
|
`
|
||||||
|
: nothing}
|
||||||
<ha-sortable
|
<ha-sortable
|
||||||
handle-selector=".handle"
|
handle-selector=".handle"
|
||||||
@item-moved=${this._featureMoved}
|
@item-moved=${this._featureMoved}
|
||||||
@ -385,11 +449,17 @@ export class HuiCardFeaturesEditor extends LitElement {
|
|||||||
|
|
||||||
private _removeFeature(ev: CustomEvent): void {
|
private _removeFeature(ev: CustomEvent): void {
|
||||||
const index = (ev.currentTarget as any).index;
|
const index = (ev.currentTarget as any).index;
|
||||||
const newfeatures = this.features!.concat();
|
const newFeatures = this.features!.concat();
|
||||||
|
|
||||||
newfeatures.splice(index, 1);
|
newFeatures.splice(index, 1);
|
||||||
|
|
||||||
fireEvent(this, "features-changed", { features: newfeatures });
|
fireEvent(this, "features-changed", { features: newFeatures });
|
||||||
|
}
|
||||||
|
|
||||||
|
private _layoutChanged(ev: CustomEvent): void {
|
||||||
|
ev.stopPropagation();
|
||||||
|
const layout = ev.detail.value;
|
||||||
|
fireEvent(this, "layout-changed", { layout });
|
||||||
}
|
}
|
||||||
|
|
||||||
private _editFeature(ev: CustomEvent): void {
|
private _editFeature(ev: CustomEvent): void {
|
||||||
|
@ -4,7 +4,6 @@ import { LitElement, css, html, nothing } from "lit";
|
|||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, state } from "lit/decorators";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import {
|
import {
|
||||||
any,
|
|
||||||
array,
|
array,
|
||||||
assert,
|
assert,
|
||||||
assign,
|
assign,
|
||||||
@ -29,11 +28,15 @@ import {
|
|||||||
LovelaceCardFeatureContext,
|
LovelaceCardFeatureContext,
|
||||||
} from "../../card-features/types";
|
} from "../../card-features/types";
|
||||||
import { getEntityDefaultTileIconAction } from "../../cards/hui-tile-card";
|
import { getEntityDefaultTileIconAction } from "../../cards/hui-tile-card";
|
||||||
import type { TileCardConfig } from "../../cards/types";
|
import type {
|
||||||
|
LovelaceCardFeatureLayout,
|
||||||
|
TileCardConfig,
|
||||||
|
} from "../../cards/types";
|
||||||
import type { LovelaceCardEditor } from "../../types";
|
import type { LovelaceCardEditor } from "../../types";
|
||||||
import "../hui-sub-element-editor";
|
import "../hui-sub-element-editor";
|
||||||
import { actionConfigStruct } from "../structs/action-struct";
|
import { actionConfigStruct } from "../structs/action-struct";
|
||||||
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
|
||||||
|
import { cardFeatureConfig } from "../structs/card-feature-struct";
|
||||||
import { EditSubElementEvent, SubElementEditorConfig } from "../types";
|
import { EditSubElementEvent, SubElementEditorConfig } from "../types";
|
||||||
import { configElementStyle } from "./config-elements-style";
|
import { configElementStyle } from "./config-elements-style";
|
||||||
import "./hui-card-features-editor";
|
import "./hui-card-features-editor";
|
||||||
@ -93,6 +96,7 @@ const HIDDEN_ATTRIBUTES = [
|
|||||||
|
|
||||||
const cardConfigStruct = assign(
|
const cardConfigStruct = assign(
|
||||||
baseLovelaceCardConfig,
|
baseLovelaceCardConfig,
|
||||||
|
cardFeatureConfig,
|
||||||
object({
|
object({
|
||||||
entity: optional(string()),
|
entity: optional(string()),
|
||||||
name: optional(string()),
|
name: optional(string()),
|
||||||
@ -104,7 +108,6 @@ const cardConfigStruct = assign(
|
|||||||
vertical: optional(boolean()),
|
vertical: optional(boolean()),
|
||||||
tap_action: optional(actionConfigStruct),
|
tap_action: optional(actionConfigStruct),
|
||||||
icon_tap_action: optional(actionConfigStruct),
|
icon_tap_action: optional(actionConfigStruct),
|
||||||
features: optional(array(any())),
|
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -299,6 +302,8 @@ export class HuiTileCardEditor
|
|||||||
.stateObj=${stateObj}
|
.stateObj=${stateObj}
|
||||||
.features=${this._config!.features ?? []}
|
.features=${this._config!.features ?? []}
|
||||||
@features-changed=${this._featuresChanged}
|
@features-changed=${this._featuresChanged}
|
||||||
|
.layout=${this._config!.feature_layout}
|
||||||
|
@layout-changed=${this._layoutChanged}
|
||||||
@edit-detail-element=${this._editDetailElement}
|
@edit-detail-element=${this._editDetailElement}
|
||||||
></hui-card-features-editor>
|
></hui-card-features-editor>
|
||||||
`;
|
`;
|
||||||
@ -351,6 +356,21 @@ export class HuiTileCardEditor
|
|||||||
fireEvent(this, "config-changed", { config });
|
fireEvent(this, "config-changed", { config });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _layoutChanged(ev: CustomEvent) {
|
||||||
|
ev.stopPropagation();
|
||||||
|
if (!this._config || !this.hass) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const layout = ev.detail.layout as LovelaceCardFeatureLayout;
|
||||||
|
const config: TileCardConfig = {
|
||||||
|
...this._config,
|
||||||
|
feature_layout: layout,
|
||||||
|
};
|
||||||
|
|
||||||
|
fireEvent(this, "config-changed", { config });
|
||||||
|
}
|
||||||
|
|
||||||
private subElementChanged(ev: CustomEvent): void {
|
private subElementChanged(ev: CustomEvent): void {
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
if (!this._config || !this.hass) {
|
if (!this._config || !this.hass) {
|
||||||
|
10
src/panels/lovelace/editor/structs/card-feature-struct.ts
Normal file
10
src/panels/lovelace/editor/structs/card-feature-struct.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { any, array, enums, object, optional } from "superstruct";
|
||||||
|
|
||||||
|
export const cardFeatureConfig = object({
|
||||||
|
features: optional(array(any())),
|
||||||
|
feature_layout: optional(
|
||||||
|
object({
|
||||||
|
type: enums(["vertical", "horizontal", "compact"]),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user