@@ -5,6 +5,7 @@ class QualitySelector {
55 this . player = player ;
66 this . sources = [ ] ;
77 this . callback = undefined ;
8+ this . defaultFormat = undefined ;
89 this . containerDropdownElement = undefined ;
910 this . defaults = { } ;
1011 }
@@ -17,35 +18,55 @@ class QualitySelector {
1718 this . callback ( quality ) ;
1819 }
1920
21+ let source = this . setPlayerSource ( quality . code ) ;
22+
23+ if ( source ) {
24+ this . player . on ( 'loadedmetadata' , ( ) => {
25+ this . player . play ( ) ;
26+
27+ Array . from ( this . containerDropdownElement . firstChild . childNodes ) . forEach ( ele => {
28+ if ( ele . dataset . code === quality . code ) {
29+ ele . setAttribute ( 'class' , 'current' ) ;
30+ } else {
31+ ele . removeAttribute ( 'class' ) ;
32+ }
33+ } ) ;
34+ } ) ;
35+ this . changeQualitySelectorLabel ( quality . name ) ;
36+ }
37+
38+ this . onToggleDropdown ( ) ;
39+ }
40+
41+ /**
42+ * Set the main video source of the player with the given format code
43+ * @param {string } formatCode
44+ * @returns {Object } the source if the player has been updated
45+ */
46+ setPlayerSource ( formatCode ) {
2047 if ( this . sources ) {
2148 // tries to find the source with this quality
22- let source = this . sources . find ( ss => ss . format === quality . code ) ;
49+ let source = this . sources . find ( ss => ss . format === formatCode ) ;
2350
2451 if ( source ) {
2552 this . player . src ( { src : source . src , type : source . type } ) ;
26-
27- this . player . on ( 'loadedmetadata' , ( ) => {
28- this . player . play ( ) ;
29-
30- Array . from ( this . containerDropdownElement . firstChild . childNodes ) . forEach ( ele => {
31- if ( ele . dataset . code === quality . code ) {
32- ele . setAttribute ( 'class' , 'current' ) ;
33- } else {
34- ele . removeAttribute ( 'class' ) ;
35- }
36- } ) ;
37- } ) ;
53+ return source ;
3854 }
55+ }
56+ return ;
57+ }
3958
40- const player = document . getElementById ( this . player . id_ ) ;
41- const qualitySelector = player . getElementsByClassName ( 'vjs-brand-quality-link' ) ;
59+ /**
60+ * Change the displayed quality label in the controlbar
61+ * @param {string } label
62+ */
63+ changeQualitySelectorLabel ( label ) {
64+ const player = document . getElementById ( this . player . id_ ) ;
65+ const qualitySelector = player . getElementsByClassName ( 'vjs-brand-quality-link' ) ;
4266
43- if ( qualitySelector && qualitySelector . length > 0 ) {
44- qualitySelector [ 0 ] . innerText = quality . name ;
45- }
67+ if ( qualitySelector && qualitySelector . length > 0 ) {
68+ qualitySelector [ 0 ] . innerText = label ;
4669 }
47-
48- this . onToggleDropdown ( ) ;
4970 }
5071
5172 /**
@@ -84,7 +105,7 @@ class QualitySelector {
84105
85106 buttonElement . className = 'vjs-brand-quality-link' ;
86107 buttonElement . onclick = ( event ) => this . onToggleDropdown ( event ) ;
87- buttonElement . innerText = options . text || 'Quality' ;
108+ buttonElement . innerText = 'Quality' ;
88109
89110 let ulElement = document . createElement ( 'ul' ) ;
90111
@@ -100,10 +121,18 @@ class QualitySelector {
100121 this . sources = options . sources ;
101122 }
102123
124+ if ( options . defaultFormat ) {
125+ this . defaultFormat = options . defaultFormat ;
126+ }
127+
103128 options . formats . map ( ( format ) => {
104129 let liElement = document . createElement ( 'li' ) ;
105130
106131 liElement . dataset . code = format . code ;
132+ if ( format . code === this . defaultFormat ) {
133+ liElement . setAttribute ( 'class' , 'current' ) ;
134+ buttonElement . innerText = format . name ;
135+ }
107136
108137 let linkElement = document . createElement ( 'a' ) ;
109138
@@ -127,6 +156,10 @@ class QualitySelector {
127156 this . player . controlBar . el ( ) . insertBefore ( containerElement , fullScreenToggle ) ;
128157
129158 this . player . addClass ( 'vjs-qualityselector' ) ;
159+
160+ if ( this . defaultFormat ) {
161+ this . setPlayerSource ( ) ;
162+ }
130163 }
131164}
132165
0 commit comments