Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Dashboard change fix
Browse files Browse the repository at this point in the history
  • Loading branch information
maykar authored Jan 16, 2021
1 parent 821df74 commit 9b98fac
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions kiosk-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ Promise.resolve(customElements.whenDefined("hui-view")).then(() => {
entityWatch();
});

function run() {
const lovelace = main.querySelector("ha-panel-lovelace");

function run(lovelace = main.querySelector("ha-panel-lovelace")) {
// Don't run if disabled via query string or not on a lovelace page.
if (queryString("disable_km") || !lovelace) return;

Expand Down Expand Up @@ -202,36 +200,16 @@ async function entityWatch() {
);
}

// Run on element changes.
// Run on dashboard change.
function observer() {
new MutationObserver(lovelaceWatch).observe(panel, { childList: true });

// If new lovelace panel was added watch for hui-root to appear.
function lovelaceWatch(mutations) {
mutationWatch(mutations, "ha-panel-lovelace", rootWatch);
}

// When hui-root appears watch it's children.
function rootWatch(mutations) {
mutationWatch(mutations, "hui-root", appLayoutWatch);
}

// When ha-app-layout appears we can run.
function appLayoutWatch(mutations) {
mutationWatch(mutations, "ha-app-layout", null);
}

// Reusable function for observers.
function mutationWatch(mutations, nodename, observeElem) {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.localName == nodename) {
if (observeElem) new MutationObserver(observeElem).observe(node.shadowRoot, { childList: true });
else run();
}
if (node.localName == "ha-panel-lovelace") run(node);
});
});
}
new MutationObserver(lovelaceWatch).observe(panel, { childList: true });
}

// Overly complicated console tag.
Expand Down

0 comments on commit 9b98fac

Please sign in to comment.