Files
home-assistant/custom_components/fontawesome/main.js
2022-06-04 14:23:34 -07:00

1 line
3.6 KiB
JavaScript

(()=>{const t="fontawesome",o={},e={"fa-primary":"primary","fa-secondary":"secondary",primary:"primary",secondary:"secondary"},s=(s,n)=>new Promise((async(a,r)=>{const c=`${s}:${n}`;o[c]&&a(o[c]),o[c]=(async(o,s)=>{const[n,a]=s.split("#"),r=await fetch(`/${t}/icons/${o}/${n}.svg`),c=await r.text(),i=(new DOMParser).parseFromString(c,"text/html");if(!i||!i.querySelector("svg"))return{};const l=i.querySelector("svg").getAttribute("viewBox"),d=i.querySelectorAll("path"),h={};let p="";for(const t of d){p+=t.getAttribute("d");const o=t.classList[0];e[o]&&(h[e[o]]=t.getAttribute("d"))}let m=null;const y=i.querySelector("svg");return Array.from(y.attributes).some((t=>t.name.startsWith("on")))||y.getElementsByTagName("script").length||(m=y),{viewBox:l,path:p,paths:h,format:a,fullCode:m}})(s,n),a(o[c])})),n=async o=>{const e=await fetch(`/${t}/list/${o}`),s=await e.text();return JSON.parse(s)};"customIconsets"in window||(window.customIconsets={}),"customIcons"in window||(window.customIcons={}),window.customIcons.fab={getIcon:t=>s("brands",t),getIconList:()=>n("brands")},window.customIcons.far={getIcon:t=>s("regular",t),getIconList:()=>n("regular")},window.customIcons.fas={getIcon:t=>s("solid",t),getIconList:()=>n("solid")},window.customIcons.fapro={getIcon:t=>s("pro",t),getIconList:()=>n("pro")},window.customIconsets.facustom=t=>s("pro",t),customElements.whenDefined("ha-icon").then((()=>{customElements.get("ha-icon").prototype._setCustomPath=async function(t,o){const e=await t;if(o!==this.icon)return;this._path=e.path,this._viewBox=e.viewBox,await this.UpdateComplete;const s=this.shadowRoot.querySelector("ha-svg-icon");if(s&&s.setPaths)if(s.clearPaths(),e.fullCode&&"fullcolor"===e.format){await s.updateComplete;const t=s.shadowRoot.querySelector("svg"),o=document.createElement("style");o.innerHTML="\n svg:first-child>g:first-of-type>path {\n display: none;\n }\n ",t.appendChild(o),t.appendChild(e.fullCode.cloneNode(!0))}else s.setPaths(e.paths),e.format&&s.classList.add(...e.format.split("-"))}})),customElements.whenDefined("ha-svg-icon").then((()=>{const t=customElements.get("ha-svg-icon");t.prototype.clearPaths=async function(){await this.updateComplete;const t=this.shadowRoot.querySelector("svg");for(;t&&t.children.length>1;)t.removeChild(t.lastChild);const o=this.shadowRoot.querySelector("g");for(;o&&o.children.length>1;)o.removeChild(o.lastChild);for(;this.shadowRoot.querySelector("style.fontawesome");){const t=this.shadowRoot.querySelector("style.fontawesome");t.parentNode.removeChild(t)}},t.prototype.setPaths=async function(t){if(await this.updateComplete,null==t||0===Object.keys(t).length)return;const o=this.shadowRoot.querySelector("style.fontawesome")||document.createElement("style");o.classList.add("fontawesome"),o.innerHTML="\n .secondary {\n opacity: 0.4;\n }\n :host(.invert) .secondary {\n opacity: 1;\n }\n :host(.invert) .primary {\n opacity: 0.4;\n }\n :host(.color) .primary {\n opacity: 1;\n }\n :host(.color) .secondary {\n opacity: 1;\n }\n :host(.color:not(.invert)) .secondary {\n fill: var(--icon-secondary-color, var(--disabled-text-color));\n }\n :host(.color.invert) .primary {\n fill: var(--icon-secondary-color, var(--disabled-text-color));\n }\n path:not(.primary):not(.secondary) {\n opacity: 0;\n }\n ",this.shadowRoot.appendChild(o);const e=this.shadowRoot.querySelector("g");for(const o in t){const s=document.createElementNS("http://www.w3.org/2000/svg","path");s.setAttribute("d",t[o]),s.classList.add(o),e.appendChild(s)}}}))})();