-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideo-card.js
More file actions
86 lines (72 loc) · 3.1 KB
/
video-card.js
File metadata and controls
86 lines (72 loc) · 3.1 KB
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
// Fetch HTML template
fetch("web-components/video-card/video-card.html")
.then(stream => stream.text())
.then(text => createComponent(text))
// Create web component
function createComponent(html) {
// Web component class
class VideoCard extends HTMLElement {
// Creates element with default values
constructor() {
super();
this.level = 'Varies';
this.time = 'Varies';
this.linktext = 'now';
}
// Return array of properties to observe
static get observedAttributes() {
return ['name', 'desc', 'level', 'time', 'link', 'linktext', 'link2', 'linktext2', 'link3', 'linktext3'];
}
// Called when an attribute is defined or changed
attributeChangedCallback(property, oldValue, newValue) {
if (oldValue === newValue) return;
this[property] = newValue;
}
// Invoked when element is added to document
connectedCallback() {
// Create shadow root for element
const shadow = this.attachShadow({mode: 'closed'});
shadow.innerHTML = html;
// shadow.append(
// // document.getElementById('video-card').content.cloneNode(true) // Use this line to test template in ims-videos.html
// template.content.cloneNode(true)
// );
// Set video name
const videoName = shadow.querySelector('.video-name');
videoName.textContent = this.name;
// Set video desc
const videoDesc = shadow.querySelector('.video-desc');
videoDesc.textContent = this.desc;
// Set video level
const videoLevel = shadow.querySelector('.video-level');
videoLevel.textContent = this.level;
// Set video time
const videoTime = shadow.querySelector('.video-time');
videoTime.textContent = this.time;
// Set video links
const videoLink = shadow.querySelector('.video-link');
const videoLink2 = shadow.querySelector('.video-link-2');
const videoLink3 = shadow.querySelector('.video-link-3');
const links = [
[this.link, this.linktext, videoLink],
[this.link2, this.linktext2, videoLink2],
[this.link3, this.linktext3, videoLink3]
]
links.forEach(link => {
const url = link[0];
const linkText = link[1];
const linkObj = link[2];
// Check if urls have been defined
if (url != undefined) {
linkObj.href = url;
const ariaText = (linkText != 'now') ? `Watch ${this.name}, ${linkText}` : `Watch ${this.name}`; // Change link text if defined
linkObj.setAttribute('aria-label', ariaText);
linkObj.textContent = `Watch ${linkText} →`;
}
})
}
}
customElements.define('video-card', VideoCard);
}
// Register component
// customElements.define('video-card', VideoCard);