From 47635055d01abc5b8b5321831191b7fc4ec13510 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 13 Dec 2018 21:46:50 +0100 Subject: [PATCH] Clear view cache edit mode (#2291) * Clear view cache when canceling edit mode * Fix enabling edit mode on not first view --- src/panels/lovelace/hui-root.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/panels/lovelace/hui-root.ts b/src/panels/lovelace/hui-root.ts index b814c049bc..d81b8b7f5c 100644 --- a/src/panels/lovelace/hui-root.ts +++ b/src/panels/lovelace/hui-root.ts @@ -93,6 +93,8 @@ class HUIRoot extends hassLocalizeLitMixin(LitElement) { constructor() { super(); + // The view can trigger a re-render when it knows that certain + // web components have been loaded. this._debouncedConfigChanged = debounce( () => this._selectView(this._curView, true), 100 @@ -380,7 +382,7 @@ class HUIRoot extends hassLocalizeLitMixin(LitElement) { protected updated(changedProperties: PropertyValues): void { super.updated(changedProperties); - const view = this._view; + const view = this._viewRoot; const huiView = view.lastChild as HUIView; if (changedProperties.has("columns") && huiView) { @@ -424,18 +426,21 @@ class HUIRoot extends hassLocalizeLitMixin(LitElement) { | undefined; if (!oldLovelace || oldLovelace.config !== this.lovelace!.config) { - this._viewCache = {}; this._loadResources(this.lovelace!.config.resources || []); - // On config change, recreate the view from scratch. + // On config change, recreate the current view from scratch. force = true; } if (!oldLovelace || oldLovelace.editMode !== this.lovelace!.editMode) { + // On edit mode change, recreate the current view from scratch force = true; } } if (newSelectView !== undefined || force) { + if (force && newSelectView === undefined) { + newSelectView = this._curView; + } this._selectView(newSelectView, force); } } @@ -463,7 +468,7 @@ class HUIRoot extends hassLocalizeLitMixin(LitElement) { return this.shadowRoot!.getElementById("layout"); } - private get _view(): HTMLDivElement { + private get _viewRoot(): HTMLDivElement { return this.shadowRoot!.getElementById("view") as HTMLDivElement; } @@ -535,11 +540,8 @@ class HUIRoot extends hassLocalizeLitMixin(LitElement) { } private _handleViewSelected(ev) { - const index = ev.detail.selected; - this._navigateView(index); - } + const viewIndex = ev.detail.selected as number; - private _navigateView(viewIndex: number): void { if (viewIndex !== this._curView) { const path = this.config.views[viewIndex].path || viewIndex; navigate(this, `/lovelace/${path}`); @@ -559,8 +561,12 @@ class HUIRoot extends hassLocalizeLitMixin(LitElement) { this._curView = viewIndex; + if (force) { + this._viewCache = {}; + } + // Recreate a new element to clear the applied themes. - const root = this._view; + const root = this._viewRoot; if (root.lastChild) { root.removeChild(root.lastChild);