1+ // Fetch HTML template
2+ fetch ( "web-components/video-card/video-card.html" )
3+ . then ( stream => stream . text ( ) )
4+ . then ( text => createComponent ( text ) )
5+
6+ // Create web component
7+ function createComponent ( html ) {
8+ // Web component class
9+ class VideoCard extends HTMLElement {
10+
11+ // Creates element with default values
12+ constructor ( ) {
13+ super ( ) ;
14+ this . level = 'Varies' ;
15+ this . time = 'Varies' ;
16+ this . linktext = 'now' ;
17+ }
18+
19+ // Return array of properties to observe
20+ static get observedAttributes ( ) {
21+ return [ 'name' , 'desc' , 'level' , 'time' , 'link' , 'linktext' , 'link2' , 'linktext2' , 'link3' , 'linktext3' ] ;
22+ }
23+
24+ // Called when an attribute is defined or changed
25+ attributeChangedCallback ( property , oldValue , newValue ) {
26+ if ( oldValue === newValue ) return ;
27+ this [ property ] = newValue ;
28+ }
29+
30+ // Invoked when element is added to document
31+ connectedCallback ( ) {
32+ // Create shadow root for element
33+ const shadow = this . attachShadow ( { mode : 'closed' } ) ;
34+ shadow . innerHTML = html ;
35+ // shadow.append(
36+ // // document.getElementById('video-card').content.cloneNode(true) // Use this line to test template in ims-videos.html
37+ // template.content.cloneNode(true)
38+ // );
39+
40+ // Set video name
41+ const videoName = shadow . querySelector ( '.video-name' ) ;
42+ videoName . textContent = `${ this . name } ` ;
43+
44+ // Set video desc
45+ const videoDesc = shadow . querySelector ( '.video-desc' ) ;
46+ videoDesc . textContent = `${ this . desc } ` ;
47+
48+ // Set video level
49+ const videoLevel = shadow . querySelector ( '.video-level' ) ;
50+ videoLevel . textContent = `${ this . level } ` ;
51+
52+ // Set video time
53+ const videoTime = shadow . querySelector ( '.video-time' ) ;
54+ videoTime . textContent = `${ this . time } ` ;
55+
56+ // Set video links
57+ const videoLink = shadow . querySelector ( '.video-link' ) ;
58+ const videoLink2 = shadow . querySelector ( '.video-link-2' ) ;
59+ const videoLink3 = shadow . querySelector ( '.video-link-3' ) ;
60+ const links = [
61+ [ this . link , this . linktext , videoLink ] ,
62+ [ this . link2 , this . linktext2 , videoLink2 ] ,
63+ [ this . link3 , this . linktext3 , videoLink3 ]
64+ ]
65+
66+ links . forEach ( link => {
67+ const url = link [ 0 ] ;
68+ const linkText = link [ 1 ] ;
69+ const linkObj = link [ 2 ] ;
70+
71+ // Check if urls have been defined
72+ if ( url != undefined ) {
73+ linkObj . href = url ;
74+ const altText = ( linkText != 'now' ) ? `Watch ${ this . name } , ${ linkText } ` : `Watch ${ this . name } ` ; // Change link text if defined
75+ linkObj . setAttribute ( 'alt' , altText ) ;
76+ linkObj . textContent = `Watch ${ linkText } →` ;
77+ }
78+ } )
79+ }
80+ }
81+
82+ customElements . define ( 'video-card' , VideoCard ) ;
83+ }
84+
85+ // Register component
86+ // customElements.define('video-card', VideoCard);
0 commit comments