Skip to content
This repository was archived by the owner on Oct 8, 2020. It is now read-only.

Commit bfc74ba

Browse files
committed
fix rebuild of header and templates
1 parent fe568f2 commit bfc74ba

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

src/config.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,23 @@ export class CustomHeaderConfig {
148148
haElem = ha_elements();
149149
if (!haElem) return;
150150
const editor = haElem.panel ? haElem.panel.shadowRoot.querySelector('hui-editor') : null;
151-
const edit_mode = haElem.root.querySelector('app-toolbar').className == 'edit-mode';
152-
if (!haElem.panel || editor || this.template_failed || edit_mode) return;
151+
if (!haElem.panel || editor || this.template_failed) return;
153152
if (haElem.root && haElem.root.querySelector('custom-header-editor')) return;
154153
this.buildConfig(ch);
155154
}, (60 - new Date().getSeconds()) * 1000);
156155

157156
template_timeout;
158157
window.customHeaderTempTimeout.push(template_timeout);
159158

159+
if (haElem.root.querySelector('app-toolbar').className == 'edit-mode') return;
160+
161+
if (window.customHeaderObservers) {
162+
for (const observer of window.customHeaderObservers) {
163+
observer.disconnect();
164+
}
165+
window.customHeaderObservers = [];
166+
}
167+
160168
styleHeader(config, ch, haElem);
161169
observers(config, ch, haElem);
162170
}

src/helpers.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,20 @@ export const invertNumArray = show_tabs => {
3737

3838
// Subscribe and render Jinja templates.
3939
export const subscribeRenderTemplate = (onChange, params, locale) => {
40+
const default_vars = defaultVariables(locale);
41+
if (!default_vars) {
42+
window.setTimeout(() => {
43+
subscribeRenderTemplate(onChange, params, locale);
44+
return;
45+
}, 500);
46+
}
4047
const hass = ha_elements().hass;
4148
const conn = hass.connection;
4249
const variables = {
4350
user: hass.user.name,
4451
browser: navigator.userAgent,
4552
...params.variables,
46-
...defaultVariables(locale),
53+
...default_vars,
4754
};
4855
const template = params.template;
4956
const entity_ids = params.entity_ids;

src/observers.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { hideMenuItems } from './overflow-menu';
22
import { redirects } from './redirects';
33
import { getLovelace } from 'custom-card-helpers';
44
import { CustomHeaderConfig } from './config';
5+
import { ha_elements } from './ha-elements';
56
import { CustomHeader } from './build-header';
7+
import { styleHeader } from './style-header';
68

79
export const selectTab = (config, ch) => {
810
const headerType = config.compact_mode || config.button_scroll ? ch.header : ch.footer;
@@ -20,6 +22,15 @@ export const selectTab = (config, ch) => {
2022
}
2123
};
2224

25+
const rebuild = config => {
26+
const haElem = ha_elements();
27+
window.last_template_result = {};
28+
const ch = new CustomHeader(haElem);
29+
CustomHeaderConfig.buildConfig(ch);
30+
if (!window.location.href.includes('disable_ch') && haElem) haElem.appHeader.style.display = 'none';
31+
styleHeader(config, ch, haElem);
32+
};
33+
2334
export const observers = (config, ch, haElem) => {
2435
const callback = mutations => {
2536
const headerType = config.split_mode ? ch.footer : ch.header;
@@ -38,15 +49,7 @@ export const observers = (config, ch, haElem) => {
3849
}
3950
} else if (addedNodes.length && target.nodeName == 'PARTIAL-PANEL-RESOLVER') {
4051
// When returning to lovelace/overview from elsewhere in HA.
41-
if (addedNodes[0].nodeName == 'HA-PANEL-LOVELACE') {
42-
if (window.customHeaderObservers) {
43-
for (const observer of window.customHeaderObservers) {
44-
observer.disconnect();
45-
}
46-
window.customHeaderObservers = [];
47-
}
48-
CustomHeaderConfig.buildConfig(ch, addedNodes[0].lovelace);
49-
}
52+
if (addedNodes[0].nodeName == 'HA-PANEL-LOVELACE') rebuild(config);
5053
if (haElem.main.querySelector('ha-panel-lovelace')) {
5154
if (config.compact_mode && !config.footer_mode) {
5255
haElem.sidebar.main.querySelector('.menu').style = 'height:49px;';
@@ -83,9 +86,7 @@ export const observers = (config, ch, haElem) => {
8386
if (haElem.root.querySelector('#ch_animated')) haElem.root.querySelector('#ch_animated').remove();
8487
} else if (target.nodeName === 'APP-HEADER' && addedNodes.length) {
8588
// Exited edit mode.
86-
if (!window.customHeaderDisabled) haElem.appHeader.style.display = 'none';
87-
window.last_template_result = {};
88-
CustomHeaderConfig.buildConfig(new CustomHeader(haElem));
89+
rebuild(config);
8990
}
9091
});
9192
};

src/style-header.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import { insertStyleTags } from './style-tags';
66
import { redirects } from './redirects';
77
import { fireEvent } from 'custom-card-helpers';
88
import { selectTab } from './observers';
9+
import { ha_elements } from './ha-elements';
910

10-
export const styleHeader = (config, ch, haElem) => {
11+
export const styleHeader = (config, ch, haElem = ha_elements()) => {
12+
if (!haElem) return;
1113
insertSettings(ch.header, config, haElem);
1214
insertSettings(ch.header, config, haElem);
1315

@@ -37,7 +39,6 @@ export const styleHeader = (config, ch, haElem) => {
3739
ch.header.menu.style.display = '';
3840
if (ch.header.container) ch.header.container.style.visibility = 'visible';
3941
if (ch.footer.container) ch.footer.container.style.visibility = 'visible';
40-
// insertSettings(ch.header, config);
4142
}
4243

4344
if (!config.compact_mode && !config.button_scroll) {
@@ -50,7 +51,6 @@ export const styleHeader = (config, ch, haElem) => {
5051

5152
if (config.split_mode) {
5253
ch.header.tabContainer.style.display = 'none';
53-
config.compact_mode = false;
5454
if (config.footer_mode) ch.footer.container.setAttribute('slot', 'header');
5555
else ch.footer.container.setAttribute('slot', '');
5656
} else if (!config.footer_mode) {
@@ -60,7 +60,6 @@ export const styleHeader = (config, ch, haElem) => {
6060
}
6161

6262
const headerType = config.compact_mode || config.button_scroll ? ch.header : ch.footer;
63-
// if (!config.split_mode) ch.footer.container.style.display = 'none';
6463

6564
if (!ch.header.tabs.length) config.compact_mode = false;
6665

0 commit comments

Comments
 (0)