@@ -9,7 +9,7 @@ import videojs from 'video.js';
99 */
1010
1111/**
12- * A class representing a marker.
12+ * A class representing a marker that will be displayed in the progress bar .
1313 *
1414 * @class
1515 * @extends Component
@@ -26,10 +26,12 @@ class MarkerDisplay extends videojs.getComponent('component') {
2626 super ( player , options ) ;
2727 const { gap } = options ;
2828
29- this . updateMarker = this . updateMarker . bind ( this ) ;
29+ this . updateMarkerPlayed = this . updateMarkerPlayed . bind ( this ) ;
30+ this . updateMarkerBuffered = this . updateMarkerBuffered . bind ( this ) ;
3031
3132 this . setMarkerWidth ( this . markerWidth ( ) , gap ) ;
32- this . player ( ) . on ( 'timeupdate' , this . updateMarker ) ;
33+ this . player ( ) . on ( 'timeupdate' , this . updateMarkerPlayed ) ;
34+ this . player ( ) . on ( 'progress' , this . updateMarkerBuffered ) ;
3335 }
3436
3537 /**
@@ -59,35 +61,50 @@ class MarkerDisplay extends videojs.getComponent('component') {
5961 }
6062
6163 /**
62- * Updates the marker background color, which uses the player's current time
63- * to calculate the percentage of the marker that has been played.
64+ * Updates the marker background color based on the provided time and the css
65+ * variable name.
66+ *
67+ * @param {number } time - The time (in seconds).
68+ * @param {string } cssVar - The CSS variable name.
6469 */
65- updateMarker ( ) {
66- const currentTime = this . player ( ) . currentTime ( ) ;
67-
70+ updateMarker ( time = 0 , cssVar ) {
6871 const duration = this . player ( ) . duration ( ) ;
6972 const markersElWidth = this . parentComponent_ . el ( ) . getClientRects ( ) [ 0 ] . width ;
7073 const markerOffsetLeft = this . el ( ) . offsetLeft ;
7174 const markerOffsetWidth = this . el ( ) . offsetWidth ;
7275 const markerStart = ( duration * markerOffsetLeft ) / markersElWidth ;
7376 const markerEnd =
7477 ( duration * ( markerOffsetLeft + markerOffsetWidth ) ) / markersElWidth ;
75- const percent = ( currentTime - markerStart ) / ( markerStart - markerEnd ) ;
78+ const percent = ( time - markerStart ) / ( markerStart - markerEnd ) ;
7679
77- if ( currentTime > markerEnd ) {
80+ if ( time > markerEnd ) {
7881 // Setting the value to 200% avoids losing pixel when resizing the player.
79- this . el ( ) . style . setProperty ( '--_cst-marker-played' , `200%` ) ;
82+ this . el ( ) . style . setProperty ( cssVar , `200%` ) ;
8083 }
8184
82- if ( currentTime < markerStart ) {
83- this . el ( ) . style . setProperty ( '--_cst-marker-played' , `0%` ) ;
85+ if ( time < markerStart ) {
86+ this . el ( ) . style . setProperty ( cssVar , `0%` ) ;
8487 }
8588
86- if ( currentTime >= markerStart && currentTime <= markerEnd ) {
87- this . el ( ) . style . setProperty ( '--_cst-marker-played' , `${ Math . abs ( percent ) * 100 } %` ) ;
89+ if ( time >= markerStart && time <= markerEnd ) {
90+ this . el ( ) . style . setProperty ( cssVar , `${ Math . abs ( percent ) * 100 } %` ) ;
8891 }
8992 }
9093
94+ /**
95+ * Updates the buffered portion of the marker.
96+ */
97+ updateMarkerBuffered ( ) {
98+ this . updateMarker ( this . player ( ) . bufferedEnd ( ) , '--_cst-marker-buffered' ) ;
99+ }
100+
101+ /**
102+ * Updates the played portion of the marker.
103+ */
104+ updateMarkerPlayed ( ) {
105+ this . updateMarker ( this . player ( ) . currentTime ( ) , '--_cst-marker-played' ) ;
106+ }
107+
91108 // ***************************************************************************
92109 // Standard component functions **********************************************
93110 // ***************************************************************************
@@ -116,7 +133,9 @@ class MarkerDisplay extends videojs.getComponent('component') {
116133 * Disposes of the marker component.
117134 */
118135 dispose ( ) {
119- this . player ( ) . off ( 'timeupdate' , this . updateMarker ) ;
136+ this . player ( ) . off ( 'timeupdate' , this . updateMarkerPlayed ) ;
137+ this . player ( ) . off ( 'progress' , this . updateMarkerBuffered ) ;
138+
120139 super . dispose ( ) ;
121140 }
122141}
0 commit comments