-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-iframe-card.js
95 lines (74 loc) · 2.02 KB
/
custom-iframe-card.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class CustomIframeCard extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
setConfig(config) {
/*
if (!config.entity) {
throw new Error('Please define an entity');
}
*/
const root = this.shadowRoot;
if (root.lastChild) root.removeChild(root.lastChild);
const cardConfig = Object.assign({}, config);
if (!cardConfig.scale) cardConfig.scale = "50px";
if (!cardConfig.from) cardConfig.from = "left";
const card = document.createElement('ha-card');
const content = document.createElement('div');
content.id = "theiframe";
card.appendChild(content);
const resp = document.createElement('div');
resp.classList.add("resp-container");
content.appendChild(resp);
const ifrm = document.createElement("iframe");
ifrm.setAttribute("src", config.url);
ifrm.classList.add("resp-iframe");
ifrm.setAttribute("sandbox", "allow-same-origin allow-scripts allow-popups allow-forms");
ifrm.setAttribute("scrolling", "no");
resp.appendChild(ifrm);
const style = document.createElement('style');
style.textContent = `
ha-card {
height: 100%;
background:none;
box-shadow:none;
border-radius: 0.8vw;
overflow:hidden;
}
#value {
font-size: calc(var(--base-unit) * 1.3);
line-height: calc(var(--base-unit) * 1.3);
color: var(--primary-text-color);
}
.resp-container {
position: relative;
overflow: hidden;
padding-top: 80%;
}
#theiframe{
height: 100%;
}
.resp-iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
`;
card.appendChild(style);
root.appendChild(card);
this._config = cardConfig;
}
set hass(hass) {
const config = this._config;
const root = this.shadowRoot;
root.lastChild.hass = hass;
}
getCardSize() {
return 1;
}
}
customElements.define('custom-iframe', CustomIframeCard);