-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
323 additions
and
19 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...panels/lovelace/editor/dashboard-strategy-editor/hui-dashboard-strategy-element-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { customElement } from "lit/decorators"; | ||
import { LovelaceConfig } from "../../../../data/lovelace"; | ||
import { getLovelaceStrategy } from "../../strategies/get-strategy"; | ||
import { LovelaceStrategyEditor } from "../../strategies/types"; | ||
import { HuiElementEditor } from "../hui-element-editor"; | ||
|
||
@customElement("hui-dashboard-strategy-element-editor") | ||
export class HuiDashboardStrategyElementEditor extends HuiElementEditor<LovelaceConfig> { | ||
protected async getConfigElement(): Promise< | ||
LovelaceStrategyEditor | undefined | ||
> { | ||
const elClass = await getLovelaceStrategy( | ||
"dashboard", | ||
this.configElementType! | ||
); | ||
|
||
// Check if a GUI editor exists | ||
if (elClass && elClass.getConfigElement) { | ||
return elClass.getConfigElement(); | ||
} | ||
|
||
return undefined; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-dashboard-strategy-element-editor": HuiDashboardStrategyElementEditor; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...ovelace/editor/dashboard-strategy-editor/hui-original-states-dashboard-strategy-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { html, LitElement, nothing } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { fireEvent } from "../../../../common/dom/fire_event"; | ||
import "../../../../components/ha-form/ha-form"; | ||
import type { | ||
HaFormSchema, | ||
SchemaUnion, | ||
} from "../../../../components/ha-form/types"; | ||
import type { HomeAssistant } from "../../../../types"; | ||
import { OriginalStatesDashboardStrategyConfig } from "../../strategies/original-states-dashboard-strategy"; | ||
import { LovelaceStrategyEditor } from "../../strategies/types"; | ||
|
||
const SCHEMA = [ | ||
{ | ||
name: "no_area_group", | ||
selector: { | ||
boolean: {}, | ||
}, | ||
}, | ||
] as const satisfies readonly HaFormSchema[]; | ||
|
||
@customElement("hui-original-states-dashboard-strategy-editor") | ||
export class HuiOriginalStatesDashboarStrategyEditor | ||
extends LitElement | ||
implements LovelaceStrategyEditor | ||
{ | ||
@property({ attribute: false }) public hass?: HomeAssistant; | ||
|
||
@state() | ||
private _config?: OriginalStatesDashboardStrategyConfig; | ||
|
||
public setConfig(config: OriginalStatesDashboardStrategyConfig): void { | ||
this._config = config; | ||
} | ||
|
||
protected render() { | ||
if (!this.hass || !this._config) { | ||
return nothing; | ||
} | ||
|
||
const data = this._config; | ||
|
||
return html` | ||
<ha-form | ||
.hass=${this.hass} | ||
.data=${data} | ||
.schema=${SCHEMA} | ||
.computeLabel=${this._computeLabelCallback} | ||
@value-changed=${this._valueChanged} | ||
></ha-form> | ||
`; | ||
} | ||
|
||
private _valueChanged(ev: CustomEvent): void { | ||
const config = ev.detail.value; | ||
fireEvent(this, "config-changed", { config }); | ||
} | ||
|
||
private _computeLabelCallback = (schema: SchemaUnion<typeof SCHEMA>) => { | ||
switch (schema.name) { | ||
case "no_area_group": | ||
return "Do not group by area"; | ||
default: | ||
return this.hass!.localize( | ||
`ui.panel.lovelace.editor.card.generic.${schema.name}` | ||
); | ||
} | ||
}; | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-original-states-dashboard-strategy-editor": HuiOriginalStatesDashboarStrategyEditor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/panels/lovelace/strategies/device-registry-detail/dialog-dashboard-strategy-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { css, CSSResultGroup, html, LitElement, nothing } from "lit"; | ||
import { customElement, property, query, state } from "lit/decorators"; | ||
import { fireEvent, HASSDomEvent } from "../../../../common/dom/fire_event"; | ||
import { createCloseHeading } from "../../../../components/ha-dialog"; | ||
import { LovelaceStrategyConfig } from "../../../../data/lovelace"; | ||
import { haStyle, haStyleDialog } from "../../../../resources/styles"; | ||
import type { HomeAssistant } from "../../../../types"; | ||
import { showSaveSuccessToast } from "../../../../util/toast-saved-success"; | ||
import "../../editor/dashboard-strategy-editor/hui-dashboard-strategy-element-editor"; | ||
import type { HuiDashboardStrategyElementEditor } from "../../editor/dashboard-strategy-editor/hui-dashboard-strategy-element-editor"; | ||
import { ConfigChangedEvent } from "../../editor/hui-element-editor"; | ||
import { GUIModeChangedEvent } from "../../editor/types"; | ||
import type { DashboardStrategyEditorDialogParams } from "./show-dialog-dashboard-strategy-editor"; | ||
import { cleanStrategyConfig } from "../legacy-strategy"; | ||
|
||
@customElement("dialog-dashboard-strategy-editor") | ||
class DialogDashboardStrategyEditor extends LitElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@state() private _params?: DashboardStrategyEditorDialogParams; | ||
|
||
@state() private _strategyConfig?: LovelaceStrategyConfig; | ||
|
||
@state() private _GUImode = true; | ||
|
||
@state() private _guiModeAvailable? = true; | ||
|
||
@query("hui-dashboard-strategy-element-editor") | ||
private _strategyEditorEl?: HuiDashboardStrategyElementEditor; | ||
|
||
public async showDialog( | ||
params: DashboardStrategyEditorDialogParams | ||
): Promise<void> { | ||
this._params = params; | ||
this._strategyConfig = params.strategyConfig; | ||
await this.updateComplete; | ||
} | ||
|
||
public closeDialog(): void { | ||
this._params = undefined; | ||
this._strategyConfig = undefined; | ||
fireEvent(this, "dialog-closed", { dialog: this.localName }); | ||
} | ||
|
||
private _handleConfigChanged(ev: HASSDomEvent<ConfigChangedEvent>) { | ||
ev.stopPropagation(); | ||
this._guiModeAvailable = ev.detail.guiModeAvailable; | ||
this._strategyConfig = ev.detail.config as LovelaceStrategyConfig; | ||
} | ||
|
||
private _handleGUIModeChanged(ev: HASSDomEvent<GUIModeChangedEvent>): void { | ||
ev.stopPropagation(); | ||
this._GUImode = ev.detail.guiMode; | ||
this._guiModeAvailable = ev.detail.guiModeAvailable; | ||
} | ||
|
||
private _toggleMode(): void { | ||
this._strategyEditorEl?.toggleMode(); | ||
} | ||
|
||
private _opened() { | ||
this._strategyEditorEl?.focusYamlEditor(); | ||
} | ||
|
||
private async _save(): Promise<void> { | ||
await this._params!.saveConfig({ | ||
...this._params!.lovelaceConfig, | ||
strategy: this._strategyConfig, | ||
}); | ||
showSaveSuccessToast(this, this.hass); | ||
this.closeDialog(); | ||
} | ||
|
||
protected render() { | ||
if (!this._params || !this._strategyConfig) { | ||
return nothing; | ||
} | ||
|
||
const config = cleanStrategyConfig(this._strategyConfig); | ||
|
||
return html` | ||
<ha-dialog | ||
open | ||
@closed=${this.closeDialog} | ||
.heading=${createCloseHeading(this.hass, "Edit dashboard")} | ||
@opened=${this._opened} | ||
> | ||
<hui-dashboard-strategy-element-editor | ||
.hass=${this.hass} | ||
.lovelace=${this._params.lovelaceConfig} | ||
.value=${config} | ||
@config-changed=${this._handleConfigChanged} | ||
@GUImode-changed=${this._handleGUIModeChanged} | ||
dialogInitialFocus | ||
></hui-dashboard-strategy-element-editor> | ||
${this._strategyConfig !== undefined | ||
? html` | ||
<mwc-button | ||
slot="secondaryAction" | ||
@click=${this._toggleMode} | ||
.disabled=${!this._guiModeAvailable} | ||
class="gui-mode-button" | ||
> | ||
${this.hass!.localize( | ||
!this._strategyEditorEl || this._GUImode | ||
? "ui.panel.lovelace.editor.edit_card.show_code_editor" | ||
: "ui.panel.lovelace.editor.edit_card.show_visual_editor" | ||
)} | ||
</mwc-button> | ||
<mwc-button @click=${this._save} slot="primaryAction"> | ||
${this.hass!.localize("ui.common.save")} | ||
</mwc-button> | ||
` | ||
: nothing} | ||
</ha-dialog> | ||
`; | ||
} | ||
|
||
static get styles(): CSSResultGroup { | ||
return [haStyle, haStyleDialog, css``]; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"dialog-dashboard-strategy-editor": DialogDashboardStrategyEditor; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...anels/lovelace/strategies/device-registry-detail/show-dialog-dashboard-strategy-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { fireEvent } from "../../../../common/dom/fire_event"; | ||
import { | ||
LovelaceConfig, | ||
LovelaceStrategyConfig, | ||
} from "../../../../data/lovelace"; | ||
|
||
export interface DashboardStrategyEditorDialogParams { | ||
lovelaceConfig: LovelaceConfig; | ||
saveConfig: (config: LovelaceConfig) => void; | ||
strategyConfig: LovelaceStrategyConfig; | ||
} | ||
|
||
export const loadDashboardStrategyEditorDialog = () => | ||
import("./dialog-dashboard-strategy-editor"); | ||
|
||
export const showDashboardStrategyEditorDialog = ( | ||
element: HTMLElement, | ||
params: DashboardStrategyEditorDialogParams | ||
): void => { | ||
fireEvent(element, "show-dialog", { | ||
dialogTag: "dialog-dashboard-strategy-editor", | ||
dialogImport: loadDashboardStrategyEditorDialog, | ||
dialogParams: params, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.