Skip to content

Commit

Permalink
Rework location change and config update handling
Browse files Browse the repository at this point in the history
  • Loading branch information
j-a-n committed Jan 27, 2024
1 parent 3e44de4 commit c21aed2
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions wallpanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ const defaultConfig = {

let dashboardConfig = {};
let config = {};
let activePanelUrl = null;
let activePanelTab = null;
let activePanel = null;
let activeTab = null;
let fullscreen = false;
let screenWakeLock = new ScreenWakeLock();
let wallpanel = null;
Expand Down Expand Up @@ -426,14 +426,14 @@ function updateConfig() {


function isActive() {
if (!config.enabled) {
const params = new URLSearchParams(window.location.search);
if (params.get("edit") == "1") {
return false;
}
if (config.enabled_on_tabs && config.enabled_on_tabs.length > 0 && activePanelTab && !config.enabled_on_tabs.includes(activePanelTab)) {
if (!config.enabled) {
return false;
}
const params = new URLSearchParams(window.location.search);
if (params.get("edit") == "1") {
if (config.enabled_on_tabs && config.enabled_on_tabs.length > 0 && activeTab && !config.enabled_on_tabs.includes(activeTab)) {
return false;
}
return true;
Expand Down Expand Up @@ -490,8 +490,11 @@ function getCurrentView() {

function setSidebarHidden(hidden) {
try {
const menuButton = elHaMain.shadowRoot
.querySelector("ha-panel-lovelace").shadowRoot
const panelLovelace = elHaMain.shadowRoot.querySelector("ha-panel-lovelace");
if (!panelLovelace) {
return;
}
const menuButton = panelLovelace.shadowRoot
.querySelector("hui-root").shadowRoot
.querySelector("ha-menu-button");
if (hidden) {
Expand Down Expand Up @@ -524,9 +527,11 @@ function setSidebarHidden(hidden) {

function setToolbarHidden(hidden) {
try {
const huiRoot = elHaMain.shadowRoot
.querySelector("ha-panel-lovelace").shadowRoot
.querySelector("hui-root").shadowRoot;
const panelLovelace = elHaMain.shadowRoot.querySelector("ha-panel-lovelace");
if (!panelLovelace) {
return;
}
const huiRoot = panelLovelace.shadowRoot.querySelector("hui-root").shadowRoot;
const view = huiRoot.querySelector("#view");
let appToolbar = huiRoot.querySelector("app-toolbar");
if (!appToolbar) {
Expand Down Expand Up @@ -799,7 +804,7 @@ class WallpanelView extends HuiView {
}

timer() {
if (!config.enabled || !activePanelUrl) {
if (!config.enabled || !activePanel) {
return;
}
if (this.screensaverStartedAt) {
Expand Down Expand Up @@ -2300,6 +2305,11 @@ function deactivateWallpanel() {


function reconfigure() {
if (!activePanel || !activeTab) {
deactivateWallpanel();
return;
}

updateConfig();
if (isActive()) {
activateWallpanel();
Expand All @@ -2314,30 +2324,22 @@ function locationChanged() {
if (config.stop_screensaver_on_location_change && !skipDisableScreensaverOnLocationChanged) {
wallpanel.stopScreensaver();
}
let pl = getHaPanelLovelace();
let changed = false;
if (!pl && activePanelUrl) {
logger.debug("No dashboard active");
activePanelUrl = null;
activePanelTab = null;
changed = true;
}
else if (pl && pl.panel && pl.panel.url_path) {
let tab = window.location.pathname.split("/").slice(-1)[0];
if (activePanelUrl != pl.panel.url_path) {
logger.debug(`Active panel switched from '${activePanelUrl}' to '${pl.panel.url_path}'`);
dashboardConfig = {};
activePanelUrl = pl.panel.url_path;
activePanelTab = tab;
changed = true;
}
else if (activePanelTab != tab) {
logger.debug(`Active tab switched from '${activePanelTab}' to '${tab}'`);
activePanelTab = tab;
changed = true;
let panel = null;
let tab = null;
let path = window.location.pathname.split("/");
if (path.length > 1) {
panel = path[1];
if (path.length > 2) {
tab = path[2];
}
}
if (changed) {
if (panel != activePanel || tab != activeTab) {
logger.info(`Location changed from panel '${activePanel}/${activeTab}' to '${panel}/${tab}'`);
if (panel != activePanel) {
dashboardConfig = {};
}
activePanel = panel;
activeTab = tab;
reconfigure();
}
}
Expand All @@ -2361,10 +2363,10 @@ function startup() {
elHass.__hass.connection.subscribeEvents(
function(event) {
logger.debug("lovelace_updated", event);
if (event.data.url_path == activePanelUrl) {
if ((!event.data.url_path) || (event.data.url_path == activePanel)) {
elHass.__hass.connection.sendMessagePromise({
type: "lovelace/config",
url_path: activePanelUrl
url_path: event.data.url_path
})
.then((data) => {
dashboardConfig = {};
Expand Down

0 comments on commit c21aed2

Please sign in to comment.