Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions hassio/src/dialogs/snapshot/dialog-hassio-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
showAlertDialog,
showConfirmationDialog,
} from "../../../../src/dialogs/generic/show-dialog-box";
import { HassDialog } from "../../../../src/dialogs/make-dialog-manager";
import { PolymerChangedEvent } from "../../../../src/polymer-types";
import { haStyle, haStyleDialog } from "../../../../src/resources/styles";
import { HomeAssistant } from "../../../../src/types";
Expand Down Expand Up @@ -76,7 +77,9 @@ interface FolderItem {
}

@customElement("dialog-hassio-snapshot")
class HassioSnapshotDialog extends LitElement {
class HassioSnapshotDialog
extends LitElement
implements HassDialog<HassioSnapshotDialogParams> {
@property({ attribute: false }) public hass!: HomeAssistant;

@property({ attribute: false }) public supervisor?: Supervisor;
Expand Down Expand Up @@ -114,12 +117,21 @@ class HassioSnapshotDialog extends LitElement {
}
}

public closeDialog() {
this._dialogParams = undefined;
this._snapshot = undefined;
this._snapshotPassword = "";
this._folders = [];
this._addons = [];
fireEvent(this, "dialog-closed", { dialog: this.localName });
}

protected render(): TemplateResult {
if (!this._dialogParams || !this._snapshot) {
return html``;
}
return html`
<ha-dialog open @closing=${this._closeDialog} .heading=${true}>
<ha-dialog open @closing=${this.closeDialog} .heading=${true}>
<div slot="heading">
<ha-header-bar>
<span slot="title"> ${this._computeName} </span>
Expand Down Expand Up @@ -367,7 +379,7 @@ class HassioSnapshotDialog extends LitElement {
.then(
() => {
alert("Snapshot restored!");
this._closeDialog();
this.closeDialog();
},
(error) => {
this._error = error.body.message;
Expand All @@ -379,7 +391,7 @@ class HassioSnapshotDialog extends LitElement {
method: "POST",
body: JSON.stringify(data),
});
this._closeDialog();
this.closeDialog();
}
}

Expand Down Expand Up @@ -418,7 +430,7 @@ class HassioSnapshotDialog extends LitElement {
.then(
() => {
alert("Snapshot restored!");
this._closeDialog();
this.closeDialog();
},
(error) => {
this._error = error.body.message;
Expand All @@ -430,7 +442,7 @@ class HassioSnapshotDialog extends LitElement {
method: "POST",
body: JSON.stringify(data),
});
this._closeDialog();
this.closeDialog();
}
}

Expand All @@ -453,7 +465,7 @@ class HassioSnapshotDialog extends LitElement {
if (this._dialogParams!.onDelete) {
this._dialogParams!.onDelete();
}
this._closeDialog();
this.closeDialog();
},
(error) => {
this._error = error.body.message;
Expand Down Expand Up @@ -504,14 +516,6 @@ class HassioSnapshotDialog extends LitElement {
private get _computeSize() {
return Math.ceil(this._snapshot!.size * 10) / 10 + " MB";
}

private _closeDialog() {
this._dialogParams = undefined;
this._snapshot = undefined;
this._snapshotPassword = "";
this._folders = [];
this._addons = [];
}
}

declare global {
Expand Down
6 changes: 6 additions & 0 deletions hassio/src/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ body {
padding: 0;
height: 100vh;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #111111;
color: #e1e1e1;
}
}
`;
document.head.appendChild(styleEl);
3 changes: 2 additions & 1 deletion hassio/src/hassio-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { atLeastVersion } from "../../src/common/config/version";
import { applyThemesOnElement } from "../../src/common/dom/apply_themes_on_element";
import { fireEvent } from "../../src/common/dom/fire_event";
import { isNavigationClick } from "../../src/common/dom/is-navigation-click";
import { mainWindow } from "../../src/common/dom/get_main_window";
import { navigate } from "../../src/common/navigate";
import { HassioPanelInfo } from "../../src/data/hassio/supervisor";
import { Supervisor } from "../../src/data/supervisor/supervisor";
Expand Down Expand Up @@ -49,7 +50,7 @@ export class HassioMain extends SupervisorBaseElement {

// Joakim - April 26, 2021
// Due to changes in behavior in Google Chrome, we changed navigate to listen on the top element
top.addEventListener("location-changed", (ev) =>
mainWindow.addEventListener("location-changed", (ev) =>
// @ts-ignore
fireEvent(this, ev.type, ev.detail, {
bubbles: false,
Expand Down
8 changes: 8 additions & 0 deletions src/common/dom/get_main_window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { MAIN_WINDOW_NAME } from "../../data/main_window";

export const mainWindow =
window.name === MAIN_WINDOW_NAME
? window
: parent.name === MAIN_WINDOW_NAME
? parent
: top;
17 changes: 9 additions & 8 deletions src/common/navigate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fireEvent } from "./dom/fire_event";
import { mainWindow } from "./dom/get_main_window";

declare global {
// for fire event
Expand All @@ -12,24 +13,24 @@ declare global {
export const navigate = (_node: any, path: string, replace = false) => {
if (__DEMO__) {
if (replace) {
top.history.replaceState(
top.history.state?.root ? { root: true } : null,
mainWindow.history.replaceState(
mainWindow.history.state?.root ? { root: true } : null,
"",
`${top.location.pathname}#${path}`
`${mainWindow.location.pathname}#${path}`
);
} else {
top.location.hash = path;
mainWindow.location.hash = path;
}
} else if (replace) {
top.history.replaceState(
top.history.state?.root ? { root: true } : null,
mainWindow.history.replaceState(
mainWindow.history.state?.root ? { root: true } : null,
"",
path
);
} else {
top.history.pushState(null, "", path);
mainWindow.history.pushState(null, "", path);
}
fireEvent(top, "location-changed", {
fireEvent(mainWindow, "location-changed", {
replace,
});
};
1 change: 1 addition & 0 deletions src/data/main_window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MAIN_WINDOW_NAME = "ha-main-window";
17 changes: 11 additions & 6 deletions src/dialogs/make-dialog-manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HASSDomEvent, ValidHassDomEvent } from "../common/dom/fire_event";
import { mainWindow } from "../common/dom/get_main_window";
import { ProvideHassElement } from "../mixins/provide-hass-lit-mixin";

declare global {
Expand Down Expand Up @@ -61,25 +62,26 @@ export const showDialog = async (
}

if (addHistory) {
top.history.replaceState(
mainWindow.history.replaceState(
{
dialog: dialogTag,
open: false,
oldState:
top.history.state?.open && top.history.state?.dialog !== dialogTag
? top.history.state
mainWindow.history.state?.open &&
mainWindow.history.state?.dialog !== dialogTag
? mainWindow.history.state
: null,
},
""
);
try {
top.history.pushState(
mainWindow.history.pushState(
{ dialog: dialogTag, dialogParams: dialogParams, open: true },
""
);
} catch (err) {
// dialogParams could not be cloned, probably contains callback
top.history.pushState(
mainWindow.history.pushState(
{ dialog: dialogTag, dialogParams: null, open: true },
""
);
Expand All @@ -90,7 +92,10 @@ export const showDialog = async (
};

export const replaceDialog = () => {
top.history.replaceState({ ...top.history.state, replaced: true }, "");
mainWindow.history.replaceState(
{ ...mainWindow.history.state, replaced: true },
""
);
};

export const closeDialog = async (dialogTag: string): Promise<boolean> => {
Expand Down
3 changes: 3 additions & 0 deletions src/entrypoints/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { subscribeUser } from "../data/ws-user";
import type { ExternalAuth } from "../external_app/external_auth";
import "../resources/safari-14-attachshadow-patch";
import { HomeAssistant } from "../types";
import { MAIN_WINDOW_NAME } from "../data/main_window";

window.name = MAIN_WINDOW_NAME;

declare global {
interface Window {
Expand Down
8 changes: 7 additions & 1 deletion src/entrypoints/custom-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ function initialize(
) {
const style = document.createElement("style");

style.innerHTML = "body{margin:0}";
style.innerHTML = `body { margin:0; }
@media (prefers-color-scheme: dark) {
body {
background-color: #111111;
color: #e1e1e1;
}
}`;
document.head.appendChild(style);

const config = panel.config._panel_custom;
Expand Down
22 changes: 13 additions & 9 deletions src/state/url-sync-mixin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
import { ReactiveElement } from "lit-element";
import { HASSDomEvent } from "../common/dom/fire_event";
import { mainWindow } from "../common/dom/get_main_window";
import {
closeDialog,
DialogClosedParams,
Expand Down Expand Up @@ -28,13 +29,16 @@ export const urlSyncMixin = <
if (history.length === 1) {
history.replaceState({ ...history.state, root: true }, "");
}
top.addEventListener("popstate", this._popstateChangeListener);
mainWindow.addEventListener("popstate", this._popstateChangeListener);
this.addEventListener("dialog-closed", this._dialogClosedListener);
}

public disconnectedCallback(): void {
super.disconnectedCallback();
top.removeEventListener("popstate", this._popstateChangeListener);
mainWindow.removeEventListener(
"popstate",
this._popstateChangeListener
);
this.removeEventListener("dialog-closed", this._dialogClosedListener);
}

Expand All @@ -45,21 +49,21 @@ export const urlSyncMixin = <
console.log("dialog closed", ev.detail.dialog);
console.log(
"open",
top.history.state?.open,
mainWindow.history.state?.open,
"dialog",
top.history.state?.dialog
mainWindow.history.state?.dialog
);
}
// If not closed by navigating back, and not a new dialog is open, remove the open state from history
if (
top.history.state?.open &&
top.history.state?.dialog === ev.detail.dialog
mainWindow.history.state?.open &&
mainWindow.history.state?.dialog === ev.detail.dialog
) {
if (DEBUG) {
console.log("remove state", ev.detail.dialog);
}
this._ignoreNextPopState = true;
top.history.back();
mainWindow.history.back();
}
};

Expand All @@ -73,7 +77,7 @@ export const urlSyncMixin = <
if (DEBUG) {
console.log("remove old state", ev.state.oldState);
}
top.history.back();
mainWindow.history.back();
return;
}
this._ignoreNextPopState = false;
Expand All @@ -98,7 +102,7 @@ export const urlSyncMixin = <
console.log("dialog could not be closed");
}
// dialog could not be closed, push state again
top.history.pushState(
mainWindow.history.pushState(
{
dialog: state.dialog,
open: true,
Expand Down