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

Commit

Permalink
Replace last timeout with mutationObserver & cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maykar authored Nov 3, 2020
1 parent 0722dff commit ff0b1e2
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions kiosk-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const main = document
.querySelector("home-assistant")
.shadowRoot.querySelector("home-assistant-main").shadowRoot;
const panel = main.querySelector("partial-panel-resolver");
const sidebar = main.querySelector("app-drawer-layout");
const drawerLayout = main.querySelector("app-drawer-layout");

// Return true if any keyword is found in location.
function locIncludes(keywords) {
Expand Down Expand Up @@ -52,17 +52,19 @@ function kiosk_mode() {
// Disable styling if "disable_kiosk" in URL.
if (url.includes("disable_kiosk")) return;

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

// Only run if location includes one of the keywords.
if (locIncludes(["kiosk", "hide_header", "hide_sidebar"]) || run) {
const lovelace = main.querySelector("ha-panel-lovelace");
const header = lovelace
const huiRoot = lovelace
? lovelace.shadowRoot.querySelector("hui-root").shadowRoot
: null;

// Insert style element for kiosk or hide_header options.
if (
(locIncludes(["kiosk", "hide_header"]) || hide_header) &&
header &&
styleCheck(header)
huiRoot &&
styleCheck(huiRoot)
) {
const css = `
#view {
Expand All @@ -72,9 +74,7 @@ function kiosk_mode() {
display: none;
}
`;
setTimeout(function () {
addStyles(css, header);
}, 100);
addStyles(css, huiRoot);

// Set localStorage cache for hiding header.
if (url.includes("cache")) setCache("kmHeader", "true");
Expand All @@ -83,7 +83,7 @@ function kiosk_mode() {
// Insert style element for kiosk or hide_sidebar options.
if (
(locIncludes(["kiosk", "hide_sidebar"]) || hide_sidebar) &&
styleCheck(sidebar)
styleCheck(drawerLayout)
) {
const css = `
:host {
Expand All @@ -93,7 +93,7 @@ function kiosk_mode() {
display: none;
}
`;
addStyles(css, sidebar);
addStyles(css, drawerLayout);

// Set localStorage cache for hiding sidebar.
if (url.includes("cache")) setCache("kmSidebar", "true");
Expand All @@ -120,11 +120,24 @@ function lovelaceWatch(mutations) {
});
}

// When hui-root appears run kiosk mode again.
// When hui-root appears watch for children.
function rootWatch(mutations) {
mutations.forEach(({ addedNodes }) => {
addedNodes.forEach((e) => {
if (e.localName == "hui-root") kiosk_mode();
if (e.localName == "hui-root") {
new MutationObserver(rootShadowWatch).observe(e.shadowRoot, {
childList: true,
});
}
});
});
}

// When app-layout appears we can run.
function rootShadowWatch(mutations) {
mutations.forEach(({ addedNodes }) => {
addedNodes.forEach((e) => {
if (e.localName == "ha-app-layout") kiosk_mode();
});
});
}
Expand Down

0 comments on commit ff0b1e2

Please sign in to comment.