Skip to content

Commit

Permalink
Don't render stuff that doesn't have templates...
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Aug 29, 2019
1 parent 4642b00 commit f412fd4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 36 deletions.
2 changes: 1 addition & 1 deletion card-mod.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 76 additions & 35 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,72 @@ import {html, css} from "/card-tools/lit-element.js";
import {fireEvent} from "/card-tools/event.js";
import {subscribeRenderTemplate} from "/card-tools/templates.js";

class CardMod extends HTMLElement {

disconnectedCallback() {
this._disconnect();
}
connectedCallback() {
this._connect();
}

_has_template(data) {
if(data.template.includes("{%")) return true;
if(data.template.includes("{{")) return true;
return false;
}

set template(data) {
this._data = data;
if(!this._has_template(data)) return;

if(!this._data.entity_ids && this._data.template.includes("config.entity")) {
if(this._data.variables.config && this._data.variables.config.entity) {
this._data.entity_ids = [this._data.variables.config.entity];
}
}
}

update() {
this._disconnect().then(() => this._connect());
}

async _connect() {
if(!this._data) return;

if(!this._has_template(this._data)) {
this.innerHTML = `<style>${this._data.template}</style>`;
}

if(this._unsubRenderTemplate) return;
this._unsubRenderTemplate = subscribeRenderTemplate(null,
(result) => this.innerHTML = `<style>${result}</style>`,
this._data);

this._unsubRenderTemplate.catch(() => {
this.innerHTML = `<style>${this._data.template}</style>`;
this._unsubRenderTemplate = undefined;
});
}

async _disconnect() {
if(this._unsubRenderTemplate) {
try {
const unsub = await this._unsubRenderTemplate;
this._unsubRenderTemplate = undefined;
await unsub();
} catch (e) {
if(e.code !== "not_found")
throw e;
}
}
}

}

customElements.define("card-mod", CardMod);


const applyStyle = async function(root, style, params, debug) {

const debugPrint = function(str){
Expand All @@ -18,31 +84,19 @@ const applyStyle = async function(root, style, params, debug) {
await root.updateComplete;

if(typeof style === "string") {
const oldStyleEl = root.querySelector(".card-mod-style");
if(oldStyleEl)
{
// Remove old styles and cancel template subscriptions
if(oldStyleEl.cancelSubscription)
{
await oldStyleEl.cancelSubscription;
}
root.removeChild(oldStyleEl)
const oldStyleEl = root.querySelector("card-mod");
if(oldStyleEl) {
oldStyleEl.update();
return;
}

// Add new style tag to the root element
const styleEl = document.createElement('style');
styleEl.classList = "card-mod-style";
styleEl.cancelSubscription = subscribeRenderTemplate(
null,
(result) => {
styleEl.innerHTML = result;
},
{
template: style,
variables: params.variables,
entity_ids: params.entity_ids,
}
);
const styleEl = document.createElement('card-mod');
styleEl.template = {
template: style,
variables: params.variables,
entity_ids: params.entity_ids,
};
root.appendChild(styleEl);
debugPrint("Applied styles to:");
debugPrint(root);
Expand All @@ -66,13 +120,6 @@ const applyStyle = async function(root, style, params, debug) {
}
}

const find_entity_ids = function(data) {
if(typeof(data) === "string") {
return data.includes("config.entity");
}
return Object.values(data).some(find_entity_ids);
}


customElements.whenDefined('ha-card').then(() => {
const HaCard = customElements.get('ha-card');
Expand Down Expand Up @@ -105,8 +152,6 @@ customElements.whenDefined('ha-card').then(() => {
if(!config || !config.style) return;

let entity_ids = config.entity_ids;
if(!entity_ids && find_entity_ids(config.style))
entity_ids = [config.entity];

const apply = () => {
applyStyle(this, config.style, {
Expand Down Expand Up @@ -137,8 +182,6 @@ customElements.whenDefined('hui-entities-card').then(() => {
if(!row || !row.updateComplete) return retval;

let entity_ids = config.entity_ids;
if (!entity_ids && find_entity_ids(config.style))
entity_ids = [config.entity];

const apply = () => {
applyStyle(row.shadowRoot, config.style, {
Expand Down Expand Up @@ -194,8 +237,6 @@ customElements.whenDefined('hui-glance-card').then(() => {
const config = e.entityConf;
if(!config.style) return;
let entity_ids = config.entity_ids;
if (!entity_ids && find_entity_ids(config.style))
entity_ids = [config.entity];

const apply = () => {
applyStyle(root, config.style, {
Expand Down

0 comments on commit f412fd4

Please sign in to comment.