@@ -3,8 +3,44 @@ fetch("web-components/video-card/video-card.html")
33 . then ( stream => stream . text ( ) )
44 . then ( text => createComponent ( text ) )
55
6- // Create web component
6+ /**
7+ * Creates a web component using a given HTML template.
8+ * @param {string } html - The HTML template.
9+ * @returns {void }
10+ */
711function createComponent ( html ) {
12+
13+ /**
14+ * Sets the content of an element based on a CSS selector.
15+ * @param {string } cssSelector - The CSS selector of the element to set the content for.
16+ * @param {string } content - The content to set for the element.
17+ * @param {boolean } shadow - A boolean indicating whether to use the shadow DOM for the element.
18+ * @returns {void }
19+ */
20+ function setContent ( cssSelector , content , shadow ) {
21+ const selector = shadow . querySelector ( cssSelector ) ;
22+ selector . textContent = content ;
23+ }
24+
25+ /**
26+ * Sets the link of an element based on a CSS selector.
27+ * @param {string } url - The link url.
28+ * @param {string } linkText - The link text.
29+ * @param {string } cssSelector - The CSS selector of the element to set the content for.
30+ * @param {string } name - The name of the element containing the link.
31+ * @param {boolean } shadow - A boolean indicating whether to use the shadow DOM for the element.
32+ * @returns {void }
33+ */
34+ function setLink ( url , linkText , cssSelector , name , shadow ) {
35+ if ( url != undefined ) {
36+ const linkObj = shadow . querySelector ( cssSelector ) ;
37+ linkObj . href = url ;
38+ const ariaText = ( linkText != 'now' ) ? `Watch ${ name } , ${ linkText } ` : `Watch ${ name } ` ; // Change link text if defined
39+ linkObj . setAttribute ( 'aria-label' , ariaText ) ;
40+ linkObj . textContent = `Watch ${ linkText } →` ;
41+ }
42+ }
43+
844 // Web component class
945 class VideoCard extends HTMLElement {
1046
@@ -38,44 +74,17 @@ function createComponent(html) {
3874 // );
3975
4076 // Set video name
41- const videoName = shadow . querySelector ( '.video-name' ) ;
42- videoName . textContent = this . name ;
43-
77+ setContent ( '.video-name' , this . name , shadow ) ;
4478 // Set video desc
45- const videoDesc = shadow . querySelector ( '.video-desc' ) ;
46- videoDesc . textContent = this . desc ;
47-
79+ setContent ( '.video-desc' , this . desc , shadow ) ;
4880 // Set video level
49- const videoLevel = shadow . querySelector ( '.video-level' ) ;
50- videoLevel . textContent = this . level ;
51-
81+ setContent ( '.video-level' , this . level , shadow ) ;
5282 // Set video time
53- const videoTime = shadow . querySelector ( '.video-time' ) ;
54- videoTime . textContent = this . time ;
55-
83+ setContent ( '.video-time' , this . time , shadow ) ;
5684 // 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 ariaText = ( linkText != 'now' ) ? `Watch ${ this . name } , ${ linkText } ` : `Watch ${ this . name } ` ; // Change link text if defined
75- linkObj . setAttribute ( 'aria-label' , ariaText ) ;
76- linkObj . textContent = `Watch ${ linkText } →` ;
77- }
78- } )
85+ setLink ( this . link , this . linktext , '.video-link' , this . name , shadow ) ;
86+ setLink ( this . link2 , this . linktext2 , '.video-link-2' , this . name , shadow ) ;
87+ setLink ( this . link3 , this . linktext3 , '.video-link-3' , this . name , shadow ) ;
7988 }
8089 }
8190
0 commit comments