ha-frontend-cdce8p/src/layouts/app/disconnect-toast-mixin.ts
Paulus Schoutsen d6887758a9
Convert <home-assistant> to Lit/TS (#2586)
* Convert home-assistant element to Lit/TS

* Fix disconnect toast

* Lint
2019-01-27 10:41:35 -08:00

41 lines
1.4 KiB
TypeScript

import { Constructor, LitElement } from "lit-element";
import { HassBaseEl } from "./hass-base-mixin";
import { hassLocalizeLitMixin } from "../../mixins/lit-localize-mixin";
import { HaToast } from "../../components/ha-toast";
export default (superClass: Constructor<LitElement & HassBaseEl>) =>
class extends hassLocalizeLitMixin(superClass) {
private _discToast?: HaToast;
protected hassConnected() {
super.hassConnected();
// Need to load in advance because when disconnected, can't dynamically load code.
import(/* webpackChunkName: "ha-toast" */ "../../components/ha-toast");
}
protected hassReconnected() {
super.hassReconnected();
if (this._discToast) {
this._discToast.opened = false;
}
}
protected hassDisconnected() {
super.hassDisconnected();
if (!this._discToast) {
const el = document.createElement("ha-toast");
el.duration = 0;
// Temp. Somehow the localize func is not getting recalculated for
// this class. Manually generating one. Will be fixed when we move
// the localize function to the hass object.
const { language, resources } = this.hass!;
el.text = (this as any).__computeLocalize(language, resources)(
"ui.notification_toast.connection_lost"
);
this._discToast = el;
this.shadowRoot!.appendChild(el as any);
}
this._discToast.opened = true;
}
};