From ea12cc66293e44968a5babe06d9f2555369ffabc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Sun, 9 Jun 2019 20:28:18 +0200 Subject: [PATCH] Stability improvements --- .gitattributes | 1 + card-mod.js | 2 +- src/main.js | 146 +++++++++++++++++++++++++------------------------ 3 files changed, 76 insertions(+), 73 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..fa042e3 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +card-mod.js binary diff --git a/card-mod.js b/card-mod.js index 751a5c5..8e20bd8 100644 --- a/card-mod.js +++ b/card-mod.js @@ -1 +1 @@ -!function(e){var t={};function r(s){if(t[s])return t[s].exports;var a=t[s]={i:s,l:!1,exports:{}};return e[s].call(a.exports,a,a.exports,r),a.l=!0,a.exports}r.m=e,r.c=t,r.d=function(e,t,s){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:s})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var s=Object.create(null);if(r.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)r.d(s,a,function(t){return e[t]}.bind(null,a));return s},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=3)}([function(e,t,r){"use strict";function s(){return document.querySelector("home-assistant").hass}r.d(t,"a",function(){return s})},function(module,__webpack_exports__,__webpack_require__){"use strict";__webpack_require__.d(__webpack_exports__,"a",function(){return parseTemplate});var _hass_js__WEBPACK_IMPORTED_MODULE_0__=__webpack_require__(0),_deviceID_js__WEBPACK_IMPORTED_MODULE_1__=__webpack_require__(2);function hasTemplate(e){return/\[\[\s+.*\s+\]\]/.test(e)}function parseTemplateString(str,specialData={}){if("string"!=typeof str)return text;const FUNCTION=/^[a-zA-Z0-9_]+\(.*\)$/,EXPR=/([^=<>!]+)\s*(==|!=|<|>|<=|>=)\s*([^=<>!]+)/,SPECIAL=/^\{.+\}$/,STRING=/^"[^"]*"|'[^']*'$/;"string"==typeof specialData&&(specialData={}),specialData=Object.assign({user:Object(_hass_js__WEBPACK_IMPORTED_MODULE_0__.a)().user.name,browser:_deviceID_js__WEBPACK_IMPORTED_MODULE_1__.a,hash:location.hash.substr(1)||" "},specialData);const _parse_function=e=>{let t=[e.substr(0,e.indexOf("(")).trim()];for(e=e.substr(e.indexOf("(")+1);e;){let r=0,s=0,a=!1;for(;e[r];){let t=e[r++];if(t===a&&r>1&&"\\"!==e[r-2]?a=!1:"\"'".includes(t)&&(a=t),!a){if("("===t)s+=1;else if(")"===t){s-=1;continue}if(!(s>0)&&",)".includes(t))break}}t.push(e.substr(0,r-1).trim()),e=e.substr(r)}return t},_parse_special=e=>(e=e.substr(1,e.length-2),specialData[e]||`{${e}}`),_parse_entity=e=>{let t;if((e=e.split("."))[0].match(SPECIAL))t=_parse_special(e.shift()),t=Object(_hass_js__WEBPACK_IMPORTED_MODULE_0__.a)().states[t]||t;else if(t=Object(_hass_js__WEBPACK_IMPORTED_MODULE_0__.a)().states[`${e.shift()}.${e.shift()}`],!e.length)return t.state;return e.forEach(e=>t=t[e]),t},_eval_expr=str=>{if(str=EXPR.exec(str),null===str)return!1;const lhs=parseTemplateString(str[1]),rhs=parseTemplateString(str[3]);var expr="";return expr=parseFloat(lhs)!=lhs?`"${lhs}" ${str[2]} "${rhs}"`:`${parseFloat(lhs)} ${str[2]} ${parseFloat(rhs)}`,eval(expr)},_eval_function=e=>{if("if"===e[0])return _eval_expr(e[1])?parseTemplateString(e[2]):parseTemplateString(e[3])};try{return str=str.trim(),str.match(STRING)?str.substr(1,str.length-2):str.match(SPECIAL)?_parse_special(str):str.match(FUNCTION)?_eval_function(_parse_function(str)):str.includes(".")?_parse_entity(str):str}catch(e){return`[[ Template matching failed: ${str} ]]`}}function parseTemplate(e,t={}){if("string"!=typeof e)return e;return e=e.replace(/\[\[\s(.*?)\s\]\]/g,(e,r,s,a)=>parseTemplateString(r,t))}},function(e,t,r){"use strict";r.d(t,"a",function(){return s});let s=function(){if(window.fully&&"function"==typeof fully.getDeviceId)return fully.getDeviceId();if(!localStorage["lovelace-player-device-id"]){const e=()=>Math.floor(1e5*(1+Math.random())).toString(16).substring(1);localStorage["lovelace-player-device-id"]=`${e()}${e()}-${e()}${e()}`}return localStorage["lovelace-player-device-id"]}()},function(e,t,r){"use strict";r.r(t);const s=Object.getPrototypeOf(customElements.get("home-assistant-main"));s.prototype.html,s.prototype.css;var a=r(1);const n=customElements.get("ha-card"),o=function(e){return e.config?e.config:e._config?e._config:e.host?o(e.host):e.parentElement?o(e.parentElement):e.parentNode?o(e.parentNode):null},i=function(e,t){if(e&&t)if("string"==typeof t){e.querySelector(":scope >.card-mod-style")&&e.removeChild(e.querySelector(":scope > .card-mod-style"));const r=document.createElement("style");r.classList="card-mod-style",r.innerHTML=Object(a.a)(t),e.appendChild(r)}else Object.keys(t).forEach(r=>"."===r?i(e,t[r]):"$"===r?i(e.shadowRoot,t[r]):void e.querySelectorAll(`:scope > ${r}`).forEach(e=>{i(e,t[r])}))};n.prototype.updated=function(e){const t=o(this);t&&t.style&&i(this,t.style)},n.prototype.firstUpdated=function(){const e=this.shadowRoot.querySelector(".card-header");e&&this.insertBefore(e,this.children[0]),document.querySelector("home-assistant").provideHass(this),window.addEventListener("location-changed",()=>this._requestUpdate())},Object.defineProperty(n.prototype,"hass",{get(){return this._hass},set(e){if(e!==this._hass){const t=this._hass;this._hass=e,this._requestUpdate("hass",t)}}}),function(e,t,r=null){if((e=new Event(e,{bubbles:!0,cancelable:!1,composed:!0})).detail=t||{},r)r.dispatchEvent(e);else{var s=document.querySelector("home-assistant");(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=s&&s.shadowRoot)&&s.querySelector("home-assistant-main"))&&s.shadowRoot)&&s.querySelector("app-drawer-layout partial-panel-resolver"))&&s.shadowRoot||s)&&s.querySelector("ha-panel-lovelace"))&&s.shadowRoot)&&s.querySelector("hui-root"))&&s.shadowRoot)&&s.querySelector("ha-app-layout #view"))&&s.firstElementChild)&&s.dispatchEvent(e)}}("ll-rebuild",{})}]); \ No newline at end of file +!function(e){var t={};function r(s){if(t[s])return t[s].exports;var a=t[s]={i:s,l:!1,exports:{}};return e[s].call(a.exports,a,a.exports,r),a.l=!0,a.exports}r.m=e,r.c=t,r.d=function(e,t,s){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:s})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var s=Object.create(null);if(r.r(s),Object.defineProperty(s,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)r.d(s,a,function(t){return e[t]}.bind(null,a));return s},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=3)}([function(e,t,r){"use strict";function s(){return document.querySelector("home-assistant").hass}r.d(t,"a",function(){return s})},function(module,__webpack_exports__,__webpack_require__){"use strict";__webpack_require__.d(__webpack_exports__,"a",function(){return parseTemplate});var _hass_js__WEBPACK_IMPORTED_MODULE_0__=__webpack_require__(0),_deviceID_js__WEBPACK_IMPORTED_MODULE_1__=__webpack_require__(2);function hasTemplate(e){return/\[\[\s+.*\s+\]\]/.test(e)}function parseTemplateString(str,specialData={}){if("string"!=typeof str)return text;const FUNCTION=/^[a-zA-Z0-9_]+\(.*\)$/,EXPR=/([^=<>!]+)\s*(==|!=|<|>|<=|>=)\s*([^=<>!]+)/,SPECIAL=/^\{.+\}$/,STRING=/^"[^"]*"|'[^']*'$/;"string"==typeof specialData&&(specialData={}),specialData=Object.assign({user:Object(_hass_js__WEBPACK_IMPORTED_MODULE_0__.a)().user.name,browser:_deviceID_js__WEBPACK_IMPORTED_MODULE_1__.a,hash:location.hash.substr(1)||" "},specialData);const _parse_function=e=>{let t=[e.substr(0,e.indexOf("(")).trim()];for(e=e.substr(e.indexOf("(")+1);e;){let r=0,s=0,a=!1;for(;e[r];){let t=e[r++];if(t===a&&r>1&&"\\"!==e[r-2]?a=!1:"\"'".includes(t)&&(a=t),!a){if("("===t)s+=1;else if(")"===t){s-=1;continue}if(!(s>0)&&",)".includes(t))break}}t.push(e.substr(0,r-1).trim()),e=e.substr(r)}return t},_parse_special=e=>(e=e.substr(1,e.length-2),specialData[e]||`{${e}}`),_parse_entity=e=>{let t;if((e=e.split("."))[0].match(SPECIAL))t=_parse_special(e.shift()),t=Object(_hass_js__WEBPACK_IMPORTED_MODULE_0__.a)().states[t]||t;else if(t=Object(_hass_js__WEBPACK_IMPORTED_MODULE_0__.a)().states[`${e.shift()}.${e.shift()}`],!e.length)return t.state;return e.forEach(e=>t=t[e]),t},_eval_expr=str=>{if(str=EXPR.exec(str),null===str)return!1;const lhs=parseTemplateString(str[1]),rhs=parseTemplateString(str[3]);var expr="";return expr=parseFloat(lhs)!=lhs?`"${lhs}" ${str[2]} "${rhs}"`:`${parseFloat(lhs)} ${str[2]} ${parseFloat(rhs)}`,eval(expr)},_eval_function=e=>{if("if"===e[0])return _eval_expr(e[1])?parseTemplateString(e[2]):parseTemplateString(e[3])};try{return str=str.trim(),str.match(STRING)?str.substr(1,str.length-2):str.match(SPECIAL)?_parse_special(str):str.match(FUNCTION)?_eval_function(_parse_function(str)):str.includes(".")?_parse_entity(str):str}catch(e){return`[[ Template matching failed: ${str} ]]`}}function parseTemplate(e,t={}){if("string"!=typeof e)return e;return e=e.replace(/\[\[\s(.*?)\s\]\]/g,(e,r,s,a)=>parseTemplateString(r,t))}},function(e,t,r){"use strict";r.d(t,"a",function(){return s});let s=function(){if(window.fully&&"function"==typeof fully.getDeviceId)return fully.getDeviceId();if(!localStorage["lovelace-player-device-id"]){const e=()=>Math.floor(1e5*(1+Math.random())).toString(16).substring(1);localStorage["lovelace-player-device-id"]=`${e()}${e()}-${e()}${e()}`}return localStorage["lovelace-player-device-id"]}()},function(e,t,r){"use strict";r.r(t);const s=Object.getPrototypeOf(customElements.get("home-assistant-main"));s.prototype.html,s.prototype.css;var a=r(1);customElements.whenDefined("ha-card").then(()=>{const e=customElements.get("ha-card"),t=function(e){return e.config?e.config:e._config?e._config:e.host?t(e.host):e.parentElement?t(e.parentElement):e.parentNode?t(e.parentNode):null},r=function(e,t){if(e&&t)if("string"==typeof t){e.querySelector(":scope >.card-mod-style")&&e.removeChild(e.querySelector(":scope > .card-mod-style"));const r=document.createElement("style");r.classList="card-mod-style",r.innerHTML=Object(a.a)(t),e.appendChild(r)}else Object.keys(t).forEach(s=>"."===s?r(e,t[s]):"$"===s?r(e.shadowRoot,t[s]):void e.querySelectorAll(`:scope > ${s}`).forEach(e=>{r(e,t[s])}))};e.prototype.updated=function(e){const s=t(this);s&&s.style&&r(this,s.style)},e.prototype.firstUpdated=function(){const e=this.shadowRoot.querySelector(".card-header");e&&this.insertBefore(e,this.children[0]),document.querySelector("home-assistant").provideHass(this),window.addEventListener("location-changed",()=>this._requestUpdate())},Object.defineProperty(e.prototype,"hass",{get(){return this._hass},set(e){if(e!==this._hass){const t=this._hass;this._hass=e,this._requestUpdate("hass",t)}}}),function(e,t,r=null){if((e=new Event(e,{bubbles:!0,cancelable:!1,composed:!0})).detail=t||{},r)r.dispatchEvent(e);else{var s=document.querySelector("home-assistant");(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=(s=s&&s.shadowRoot)&&s.querySelector("home-assistant-main"))&&s.shadowRoot)&&s.querySelector("app-drawer-layout partial-panel-resolver"))&&s.shadowRoot||s)&&s.querySelector("ha-panel-lovelace"))&&s.shadowRoot)&&s.querySelector("hui-root"))&&s.shadowRoot)&&s.querySelector("ha-app-layout #view"))&&s.firstElementChild)&&s.dispatchEvent(e)}}("ll-rebuild",{})})}]); \ No newline at end of file diff --git a/src/main.js b/src/main.js index efb7a16..9e26d1b 100644 --- a/src/main.js +++ b/src/main.js @@ -2,81 +2,83 @@ import {html, css} from "/card-tools/lit-element.js"; import {fireEvent} from "/card-tools/event.js"; import {parseTemplate} from "/card-tools/templates.js"; -const HaCard = customElements.get('ha-card'); +customElements.whenDefined('ha-card').then(() => { + const HaCard = customElements.get('ha-card'); -const findConfig = function(node) { - if(node.config) - return node.config; - if(node._config) - return node._config; - if(node.host) - return findConfig(node.host); - if(node.parentElement) - return findConfig(node.parentElement); - if(node.parentNode) - return findConfig(node.parentNode); - return null; -}; + const findConfig = function(node) { + if(node.config) + return node.config; + if(node._config) + return node._config; + if(node.host) + return findConfig(node.host); + if(node.parentElement) + return findConfig(node.parentElement); + if(node.parentNode) + return findConfig(node.parentNode); + return null; + }; -const applyStyle = function(root, style) { - if(!root || !style) return; + const applyStyle = function(root, style) { + if(!root || !style) return; - if(typeof style === "string") { - // Remove old styles if we're updating - if(root.querySelector(":scope >.card-mod-style")) - root.removeChild(root.querySelector(":scope > .card-mod-style")); + if(typeof style === "string") { + // Remove old styles if we're updating + if(root.querySelector(":scope >.card-mod-style")) + root.removeChild(root.querySelector(":scope > .card-mod-style")); - // Add new style tag to the root element - const styleEl = document.createElement('style'); - styleEl.classList = "card-mod-style"; - styleEl.innerHTML = parseTemplate(style); - root.appendChild(styleEl); - } else { - Object.keys(style).forEach((k) => { - if(k === ".") - return applyStyle(root, style[k]); - else if(k === "$") - return applyStyle(root.shadowRoot, style[k]); - else { - root.querySelectorAll(`:scope > ${k}`).forEach((el) => { - applyStyle(el, style[k]); - }); - return; - } - }); - } -} - -HaCard.prototype.updated = function(_) { - // Apply styles after updates, if specified - const config = findConfig(this); - if(config && config.style) - applyStyle(this, config.style); -} - -HaCard.prototype.firstUpdated = function() { - // Move the header inside the slot instead of in the shadowDOM - // makes it easier to style it consistently - const header = this.shadowRoot.querySelector(".card-header"); - if(header) - { - this.insertBefore(header, this.children[0]); - } - // Listen for changes to hass or the location and update - document.querySelector("home-assistant").provideHass(this); - window.addEventListener("location-changed", () => this._requestUpdate()); -} -Object.defineProperty(HaCard.prototype, 'hass', { - get() { - return this._hass; - }, - set(value) { - if(value !== this._hass) { - const oldval = this._hass; - this._hass = value; - this._requestUpdate('hass', oldval); + // Add new style tag to the root element + const styleEl = document.createElement('style'); + styleEl.classList = "card-mod-style"; + styleEl.innerHTML = parseTemplate(style); + root.appendChild(styleEl); + } else { + Object.keys(style).forEach((k) => { + if(k === ".") + return applyStyle(root, style[k]); + else if(k === "$") + return applyStyle(root.shadowRoot, style[k]); + else { + root.querySelectorAll(`:scope > ${k}`).forEach((el) => { + applyStyle(el, style[k]); + }); + return; + } + }); } - }, -}); + } -fireEvent('ll-rebuild', {}); + HaCard.prototype.updated = function(_) { + // Apply styles after updates, if specified + const config = findConfig(this); + if(config && config.style) + applyStyle(this, config.style); + } + + HaCard.prototype.firstUpdated = function() { + // Move the header inside the slot instead of in the shadowDOM + // makes it easier to style it consistently + const header = this.shadowRoot.querySelector(".card-header"); + if(header) + { + this.insertBefore(header, this.children[0]); + } + // Listen for changes to hass or the location and update + document.querySelector("home-assistant").provideHass(this); + window.addEventListener("location-changed", () => this._requestUpdate()); + } + Object.defineProperty(HaCard.prototype, 'hass', { + get() { + return this._hass; + }, + set(value) { + if(value !== this._hass) { + const oldval = this._hass; + this._hass = value; + this._requestUpdate('hass', oldval); + } + }, + }); + + fireEvent('ll-rebuild', {}); +});