forked from custom-cards/upcoming-media-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upcoming-media-card.js
393 lines (377 loc) · 14.6 KB
/
upcoming-media-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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
class UpcomingMediaCard extends HTMLElement {
set hass(hass) {
if (!this.content) {
const card = document.createElement("ha-card");
card.header = this.config.title;
this.content = document.createElement("div");
card.style.background = "none";
this.content.style.minWidth = "700px"
this.content.style.display = "grid";
this.content.style.padding = "0px";
card.appendChild(this.content);
this.appendChild(card);
}
const entity = this.config.entity;
if (!hass.states[entity]) return;
let service = this.config.entity.slice(7, 11);
let data = hass.states[entity].attributes.data
const json = typeof(data) == "object" ? hass.states[entity].attributes.data : JSON.parse(hass.states[entity].attributes.data);
if (!json[1] && this.config.hide_empty) this.style.display = "none";
if (!json || !json[1] || this.prev_json == JSON.stringify(json)) return;
this.prev_json = JSON.stringify(json);
const view = this.config.image_style || "poster";
const gridCols = this.config.gridCols ;
this.content.style.gridTemplateColumns = gridCols ? `repeat(${gridCols}, 1fr)` : view == "poster" ? "repeat(4, 1fr)" : "repeat(3, 1fr)"
const dateform = this.config.date || "mmdd";
const icon = this.config.icon || json[0]["icon"];
const icon_hide = this.config.icon == "none" ? "display:none;" : "";
const icon_color = this.config.icon_color || "white";
const flag_color = this.config.flag_color || "var(--primary-color)";
const flag = this.config.flag == undefined ? true : this.config.flag;
const timeform = {
hour12: this.config.clock != 24,
hour: "2-digit",
minute: "2-digit"
};
const title_text = this.config.title_text || json[0]["title_default"];
const line1_text = this.config.line1_text || json[0]["line1_default"];
const line2_text = this.config.line2_text || json[0]["line2_default"];
const line3_text = this.config.line3_text || json[0]["line3_default"];
const line4_text = this.config.line4_text || json[0]["line4_default"];
const title_size = this.config.title_size || "large";
const line1_size =
this.config.line1_size || this.config.line_size || "medium";
const line2_size =
this.config.line2_size || this.config.line_size || "small";
const line3_size =
this.config.line3_size || this.config.line_size || "small";
const line4_size =
this.config.line4_size || this.config.line_size || "small";
const tSize = size =>
size == "large" ? "18" : size == "medium" ? "14" : "12";
const size = [
tSize(title_size),
tSize(line1_size),
tSize(line2_size),
tSize(line3_size),
tSize(line4_size)
];
const defaultClr = (poster, fanart) => (view == "poster" ? poster : fanart);
const title_color =
this.config.title_color ||
defaultClr("var(--primary-text-color)", "#fff");
const line1_color =
this.config.line1_color ||
this.config.line_color ||
defaultClr("var(--primary-text-color)", "#fff");
const line2_color =
this.config.line2_color ||
this.config.line_color ||
defaultClr("var(--primary-text-color)", "#fff");
const line3_color =
this.config.line3_color ||
this.config.line_color ||
defaultClr("var(--primary-text-color)", "#fff");
const line4_color =
this.config.line4_color ||
this.config.line_color ||
defaultClr("var(--primary-text-color)", "#fff");
const accent =
this.config.accent_color || defaultClr("var(--primary-color)", "#000");
const border = this.config.border_color || defaultClr("#fff", "#000");
const shadows = conf =>
this.config.all_shadows == undefined
? conf == undefined
? true
: conf
: this.config.all_shadows;
const boxshdw = shadows(this.config.box_shadows)
? view == "poster"
? "5px 5px 10px"
: "3px 2px 25px"
: "";
const svgshdw = shadows(this.config.box_shadows) ? "url(#grad1)" : accent;
const txtshdw = shadows(this.config.text_shadows) ? "1px 1px 3px" : "";
const max = Math.min(json.length - 1, this.config.max || 5);
window.cardSize = max;
let style = document.createElement("style");
style.setAttribute("id", "umc_style");
if (!this.querySelector('[id="umc_style"]')) {
style.textContent = `
.${service}_${view} {
width: 95%;
margin-left: auto;
margin-right: auto;
margin-bottom: 10px;
position: relative;
overflow: hidden;
}
.${service}_${view} ha-icon {
top: -2px;
right: 3px;
z-index: 2;
width: 17%;
height: 17%;
position:absolute;
color:${icon_color};
filter: drop-shadow( 0px 0px 1px rgba(0,0,0,1));
${icon_hide};
}
.${service}_${view} img {
width:100%;
visibility:hidden;
}
.${service}_svg_${view} {
margin: 0px !important;
position: absolute;
bottom: 0px;
left: 0px;
width: 105%;
background-color: rgba(44, 45, 45, 0.65);
backdrop-filter: blur(1.5px);
-webkit-backdrop-filter: blur(1.5px);
}
.${service}_container_${view} {
border-radius: 8px 8px 0px 0px;
box-shadow: 5px 5px 20px rgba(0, 0, 0, 0.2);
margin: 0px;
padding: 0px;
}
.${service}_flag_${view} {
z-index: 1;
height: 100%;
width: 100%;
position: absolute;
bottom: 0;
right: 0;
fill:${flag_color};
}
.${service}_flag_${view} svg{
float:right;
width: 100%;
height: 100%;
margin:0;
filter: drop-shadow( -1px 1px 1px rgba(0,0,0,.5));
}
.${service}_line0_${view} {
font-size:${size[1]}px;
text-shadow:${txtshdw} rgba(0,0,0,0.9);
fill:${line1_color};
text-wrap: nowrap;
display: block;
padding-top: 5px;
margin-left: 2px;
width: 95%;
overflow: hidden;
}
.${service}_line1_${view} {
font-size:${size[1]}px;
text-shadow:${txtshdw} rgba(0,0,0,0.9);
fill:${line1_color};
text-wrap: nowrap;
display: block;
margin-left: 2px;
width: 95%;
overflow: hidden;
}
.${service}_line2_${view} {
font-size:${size[2]}px;
text-shadow:${txtshdw} rgba(0,0,0,0.9);
fill:${line2_color};
text-wrap: nowrap;
display: block;
margin-left: 3px;
}
.${service}_line3_${view} {
font-size:${size[3]}px;
text-shadow:${txtshdw} rgba(0,0,0,0.9);
fill:${line3_color};
text-wrap: nowrap;
display: block;
margin-left: 3px;
}
.${service}_line4_${view} {
font-size:${size[4]}px;
text-shadow:${txtshdw} rgba(0,0,0,0.9);
fill:${line4_color};
text-wrap: nowrap;
display: block;
margin-left: 3px;
}
`;
}
this.content.innerHTML = "";
// Truncate text...
function truncate(text, chars) {
// When to truncate depending on size
chars = chars == 'large' ? 32 : chars == 'medium' ? 28 : 35;
// Remove parentheses & contents: "Shameless (US)" becomes "Shameless".
text = text.replace(/ *\([^)]*\) */g, ' ');
// Truncate only at whole word w/ no punctuation or space before ellipsis.
if (text.length > chars) {
for (let i = chars; i > 0; i--) {
if (text.charAt(i).match(/( |\s|:|-|;|"|'|,)/) && text.charAt(i - 1).match(/[a-zA-Z0-9_]/)) {
return `${text.substring(0, i)}...`;
}
}
// The cycle above had a really big single word, so we return it anyway
return `${text.substring(0, chars)}...`;
} else {
return text;
}
}
function format_date(input_date) {
// Match UTC ISO formatted date with time
let fd_day, fd_month;
if (String(input_date).match(/[T]\d+[:]\d+[:]\d+[Z]/)) {
fd_day = new Date(input_date).toLocaleDateString([], {
day: "2-digit"
});
fd_month = new Date(input_date).toLocaleDateString([], {
month: "2-digit"
});
// Match date string. ie: 2018-10-31
} else if (String(input_date).match(/\d+[-]\d+[-]\d+/)) {
input_date = input_date.split("-");
fd_month = input_date[1];
fd_day = input_date[2];
} else {
return "";
}
if (dateform == "ddmm") return `${fd_day}/${fd_month}`;
else return `${fd_month}/${fd_day}`;
}
for (let count = 1; count <= max; count++) {
const item = key => json[count][key];
if (!item("airdate")) continue;
if (this.config.hide_flagged && item("flag")) continue;
else if (this.config.hide_unflagged && !item("flag")) continue;
let airdate = new Date(item("airdate"));
let dflag = item("flag") && flag ? "" : "display:none;";
let image =
view == "poster" ? item("poster") : item("fanart") || item("poster");
if (image && !image.includes("http")) {
image = hass.hassUrl().substring(0, hass.hassUrl().length - 1) + image
}
let daysBetween = Math.round(
Math.abs(
(new Date().getTime() - airdate.getTime()) / (24 * 60 * 60 * 1000)
)
);
let day =
daysBetween <= 7
? airdate.toLocaleDateString([], { weekday: "long" })
: airdate.toLocaleDateString([], { weekday: "short" });
// Format runtime as either '23 min' or '01:23' if over an hour
let hrs = String(Math.floor(item("runtime") / 60)).padStart(2, 0);
let min = String(Math.floor(item("runtime") % 60)).padStart(2, 0);
let runtime =
item("runtime") > 0 ? (hrs > 0 ? `${hrs}:${min}` : `${min} min`) : "";
// Shifting images for fanart view since we use poster as fallback image.
let shiftimg = item("fanart")
? "background-position:100% 0;"
: "background-size: 54% auto;background-position:100% 35%;";
// First item in card needs no top margin.
let top;
if (count == 1) top = "margin-top: 20px;";
else top = view == "poster" ? "margin-top: 20px;" : "margin-top: 20px;";
let line = [title_text, line1_text, line2_text, line3_text, line4_text];
let char = [title_size, line1_size, line2_size, line3_size, line4_size];
// Keyword map for replacement, return null if empty so we can hide empty sections
let keywords = /\$title|\$episode|\$genres|\$number|\$rating|\$release|\$runtime|\$studio|\$day|\$date|\$time|\$aired/g;
let keys = {
$title: item("title") || null,
$episode: item("episode") || null,
$genres: item("genres") || null,
$number: item("number") || null,
$rating: item("rating") || null,
$release: item("release") || null,
$studio: item("studio") || null,
$runtime: runtime || null,
$day: day || null,
$time: airdate.toLocaleTimeString([], timeform) || null,
$date: format_date(item("airdate")) || null,
$aired: format_date(item("aired")) || null
};
// Replace keywords in lines
for (let i = 0; i < line.length; i++) {
line[i] = line[i].replace(" - ", "-");
// Split at '-' so we can ignore entire contents if keyword returns null
let text = line[i].replace(keywords, val => keys[val]).split("-");
let filtered = [];
// Rebuild lines, ignoring null
for (let t = 0; t < text.length; t++) {
if (text[t].match(null)) continue;
else filtered.push(text[t]);
}
// Replacing twice to get keywords in component generated strings
text = filtered.join(" - ").replace(keywords, val => keys[val]);
// Shifting header text around depending on view & size
let svgshift, y;
if (i == 0)
size[i].match(/18/)
? (y = "-5")
: size[i].match(/14/)
? (y = "-2")
: (y = "0");
if (view == "fanart")
svgshift =
i == 0 ? `x="15" y="${y}" dy="1.3em" ` : `x="15" dy="1.3em" `;
else
svgshift =
i == 0 ? `x="15" y="${y}" dy="1.3em" ` : `x="15" dy="1.3em" `;
// Build lines HTML or empty line
line[i] = line[i].match("empty")
? `<tspan class="${service}_line${i}_${view}" style=" display: block; height: 0px; margin-top: 4px;" ${svgshift}></tspan>`
: `<tspan class="${service}_line${i}_${view}" ${svgshift}>${truncate(
text,
char[i]
)}</tspan>`;
}
this.content.innerHTML += `
<div id='main' class='${service}_${view}' style='${top}'>
<div class="${service}_container_${view}" style="background-image:url('${image}'); background-size: cover; height: auto">
<div class='${service}_svg_${view}' viewBox="0 0 200 100">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(20,20,20,1);stop-opacity:1" />
<stop offset="2%" style="stop-color:${accent};stop-opacity:1" />
</linearGradient>
</defs>
<text>
${line[0]}
<tspan dy="1.3em" style="display: block; height: 0px;"></tspan>
${line[1]}${line[2]}${line[3]}${line[4]}
</text>
</div>
<img src="${image}"/>
<ha-icon icon="${icon}" style="${dflag}"></ha-icon>
<div class="${service}_flag_${view}" style="${dflag}">
<svg style="${dflag}" preserveAspectRatio="none" viewBox="0 0 100 100">
<polygon points="100 25,65 0,100 0"></polygon>
</svg>
</div>
</div>
</div>
`;
if (!this.querySelector('[id="umc_style"]')) this.appendChild(style);
}
}
setConfig(config) {
if (!config.service && !config.entity)
throw new Error("Define entity.");
this.config = config;
}
getCardSize() {
return window.cardSize * 5;
}
}
customElements.define("upcoming-media-card", UpcomingMediaCard);
// Configure the preview in the Lovelace card picker
window.customCards = window.customCards || [];
window.customCards.push({
type: 'upcoming-media-card',
name: 'Upcoming Media Card',
preview: false,
description: 'The Upcoming Media card displays upcoming episodes and movies from services like: Plex, Kodi, Radarr, Sonarr, and Trakt.',
});