mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-18 23:06:40 +00:00
Simplify Lovelace mode (#2243)
This commit is contained in:
parent
07e35ff81a
commit
d9d92c8766
@ -10,6 +10,7 @@ import { LitElement, html, PropertyValues } from "@polymer/lit-element";
|
|||||||
import { hassLocalizeLitMixin } from "../../mixins/lit-localize-mixin";
|
import { hassLocalizeLitMixin } from "../../mixins/lit-localize-mixin";
|
||||||
import { TemplateResult } from "lit-html";
|
import { TemplateResult } from "lit-html";
|
||||||
import { showSaveDialog } from "./editor/show-save-config-dialog";
|
import { showSaveDialog } from "./editor/show-save-config-dialog";
|
||||||
|
import { generateLovelaceConfig } from "./common/generate-lovelace-config";
|
||||||
|
|
||||||
interface LovelacePanelConfig {
|
interface LovelacePanelConfig {
|
||||||
mode: "yaml" | "storage";
|
mode: "yaml" | "storage";
|
||||||
@ -121,12 +122,11 @@ class LovelacePanel extends hassLocalizeLitMixin(LitElement) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async _fetchConfig(force) {
|
private async _fetchConfig(force) {
|
||||||
let conf;
|
let conf: LovelaceConfig;
|
||||||
let gen: boolean;
|
let confMode: Lovelace["mode"] = this.panel!.config.mode;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
conf = await fetchConfig(this.hass!, force);
|
conf = await fetchConfig(this.hass!, force);
|
||||||
gen = false;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.code !== "config_not_found") {
|
if (err.code !== "config_not_found") {
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
@ -135,21 +135,17 @@ class LovelacePanel extends hassLocalizeLitMixin(LitElement) {
|
|||||||
this._errorMsg = err.message;
|
this._errorMsg = err.message;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const {
|
|
||||||
generateLovelaceConfig,
|
|
||||||
} = await import("./common/generate-lovelace-config");
|
|
||||||
conf = generateLovelaceConfig(this.hass!, this.localize);
|
conf = generateLovelaceConfig(this.hass!, this.localize);
|
||||||
gen = true;
|
confMode = "generated";
|
||||||
}
|
}
|
||||||
|
|
||||||
this._state = "loaded";
|
this._state = "loaded";
|
||||||
this.lovelace = {
|
this.lovelace = {
|
||||||
config: conf,
|
config: conf,
|
||||||
autoGen: gen,
|
|
||||||
editMode: this.lovelace ? this.lovelace.editMode : false,
|
editMode: this.lovelace ? this.lovelace.editMode : false,
|
||||||
mode: this.panel!.config.mode,
|
mode: confMode,
|
||||||
setEditMode: (editMode: boolean) => {
|
setEditMode: (editMode: boolean) => {
|
||||||
if (!editMode || !this.lovelace!.autoGen) {
|
if (!editMode || this.lovelace!.mode !== "generated") {
|
||||||
this._updateLovelace({ editMode });
|
this._updateLovelace({ editMode });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -158,16 +154,16 @@ class LovelacePanel extends hassLocalizeLitMixin(LitElement) {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
|
saveConfig: async (newConfig: LovelaceConfig): Promise<void> => {
|
||||||
const { config, autoGen } = this.lovelace!;
|
const { config, mode } = this.lovelace!;
|
||||||
try {
|
try {
|
||||||
// Optimistic update
|
// Optimistic update
|
||||||
this._updateLovelace({ config: newConfig, autoGen: false });
|
this._updateLovelace({ config: newConfig, mode: "storage" });
|
||||||
await saveConfig(this.hass!, newConfig);
|
await saveConfig(this.hass!, newConfig);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// tslint:disable-next-line
|
// tslint:disable-next-line
|
||||||
console.error(err);
|
console.error(err);
|
||||||
// Rollback the optimistic update
|
// Rollback the optimistic update
|
||||||
this._updateLovelace({ config, autoGen });
|
this._updateLovelace({ config, mode });
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -4,8 +4,7 @@ import { LovelaceCardConfig, LovelaceConfig } from "../../data/lovelace";
|
|||||||
export interface Lovelace {
|
export interface Lovelace {
|
||||||
config: LovelaceConfig;
|
config: LovelaceConfig;
|
||||||
editMode: boolean;
|
editMode: boolean;
|
||||||
autoGen: boolean;
|
mode: "generated" | "yaml" | "storage";
|
||||||
mode: "yaml" | "storage";
|
|
||||||
setEditMode: (editMode: boolean) => void;
|
setEditMode: (editMode: boolean) => void;
|
||||||
saveConfig: (newConfig: LovelaceConfig) => Promise<void>;
|
saveConfig: (newConfig: LovelaceConfig) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user