-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
list-card.js
227 lines (195 loc) · 7.75 KB
/
list-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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
console.log(`%clist-card\n%cVersion: ${'0.0.1'}`, 'color: rebeccapurple; font-weight: bold;', '');
class ListCard 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);
const columns = cardConfig.columns;
const card = document.createElement('ha-card');
const content = document.createElement('div');
const style = document.createElement('style');
style.textContent = `
ha-card {
/* sample css */
}
table {
width: 100%;
padding: 0 16px 16px 16px;
}
thead th {
text-align: left;
}
tbody tr:nth-child(odd) {
background-color: var(--paper-card-background-color);
}
tbody tr:nth-child(even) {
background-color: var(--secondary-background-color);
}
.button {
overflow: auto;
padding: 16px;
}
paper-button {
float: right;
}
td a {
color: var(--primary-text-color);
text-decoration-line: none;
font-weight: normal;
}
`;
// Go through columns and add CSS sytling to each column that is defined
if (columns) {
for (let column in columns) {
if (columns.hasOwnProperty(column) && columns[column].hasOwnProperty('style')) {
let styles = columns[column]['style'];
style.textContent += `
.${columns[column].field} {`
for (let index in styles) {
if (styles.hasOwnProperty(index)) {
for (let s in styles[index]) {
style.textContent += `
${s}: ${styles[index][s]};`;
}
}
}
style.textContent += `}`;
}
}
}
content.id = "container";
cardConfig.title ? card.header = cardConfig.title : null;
card.appendChild(content);
card.appendChild(style);
root.appendChild(card);
this._config = cardConfig;
}
set hass(hass) {
const config = this._config;
const root = this.shadowRoot;
const card = root.lastChild;
if (hass.states[config.entity]) {
const feed = config.feed_attribute ? hass.states[config.entity].attributes[config.feed_attribute] : hass.states[config.entity].attributes;
const columns = config.columns;
this.style.display = 'block';
const rowLimit = config.row_limit ? config.row_limit : Object.keys(feed).length;
let rows = 0;
if (feed !== undefined && Object.keys(feed).length > 0) {
let card_content = '<table><thread><tr>';
if (!columns) {
card_content += `<tr>`;
for (let column in feed[0]) {
if (feed[0].hasOwnProperty(column)) {
card_content += `<th>${feed[0][column]}</th>`;
}
}
} else {
for (let column in columns) {
if (columns.hasOwnProperty(column)) {
card_content += `<th class=${columns[column].field}>${columns[column].title}</th>`;
}
}
}
card_content += `</tr></thead><tbody>`;
for (let entry in feed) {
if (rows >= rowLimit) break;
if (feed.hasOwnProperty(entry)) {
if (!columns) {
for (let field in feed[entry]) {
if (feed[entry].hasOwnProperty(field)) {
card_content += `<td>${feed[entry][field]}</td>`;
}
}
} else {
let has_field = true;
for (let column in columns) {
if (!feed[entry].hasOwnProperty(columns[column].field)) {
has_field = false;
break;
}
}
if (!has_field) continue;
card_content += `<tr>`;
for (let column in columns) {
if (columns.hasOwnProperty(column)) {
card_content += `<td class=${columns[column].field}>`;
if (columns[column].hasOwnProperty('add_link')) {
card_content += `<a href="${feed[entry][columns[column].add_link]}" target='_blank'>`;
}
if (columns[column].hasOwnProperty('type')) {
if (columns[column].type === 'image') {
if (columns[column].hasOwnProperty('width')) {
var image_width = columns[column].width;
} else {
var image_width = 70;
}
if (columns[column].hasOwnProperty('height')) {
var image_height = columns[column].height;
} else {
var image_height = 90;
}
if (feed[entry][columns[column].field][0].hasOwnProperty('url')) {
var url = feed[entry][columns[column].field][0].url
} else {
var url = feed[entry][columns[column].field]
}
card_content += `<img id="image" src="${url}" width="${image_width}" height="${image_height}">`;
} else if (columns[column].type === 'icon') {
card_content += `<ha-icon class="column-${columns[column].field}" icon=${feed[entry][columns[column].field]}></ha-icon>`;
}
// else if (columns[column].type === 'button') {
// card_content += `<paper-button raised>${feed[entry][columns[column].button_text]}</paper-button>`;
// }
} else {
let newText = feed[entry][columns[column].field];
if (columns[column].hasOwnProperty('regex')) {
newText = new RegExp(columns[column].regex, 'u').exec(feed[entry][columns[column].field]);
}
if (columns[column].hasOwnProperty('prefix')) {
newText = columns[column].prefix + newText;
}
if (columns[column].hasOwnProperty('postfix')) {
newText += columns[column].postfix;
}
card_content += `${newText}`;
}
if (columns[column].hasOwnProperty('add_link')) {
card_content += `</a>`;
}
card_content += `</td>`;
}
}
}
card_content += `</tr>`;
++rows;
}
}
root.lastChild.hass = hass;
card_content += `</tbody></table>`;
root.getElementById('container').innerHTML = card_content;
} else {
this.style.display = 'none';
}
} else {
this.style.display = 'none';
}
}
getCardSize() {
return 1;
}
}
customElements.define('list-card', ListCard);
window.customCards = window.customCards || [];
window.customCards.push({
type: "list-card",
name: "List Card",
preview: false,
description: "The List Card generate table with data from sensor that provides data as a list of attributes."
});