1+ /* Get elements */
2+ const player = document . querySelector ( '.player' ) ;
3+ const video = player . querySelector ( '.viewer' ) ;
4+ const progress = player . querySelector ( '.progress' ) ;
5+ const progressBar = player . querySelector ( '.progress__filled' ) ;
6+ const toggle = player . querySelector ( '.toggle' ) ;
7+ const skipButtons = player . querySelectorAll ( '[data-skip]' ) ;
8+ const ranges = player . querySelectorAll ( '.player__slider' ) ;
9+ const fullscreen = player . querySelector ( '.fullscreen' ) ;
10+
11+ /* Functions */
12+ function togglePlay ( ) {
13+ //Simplified method
14+ //const method = video.paused ? 'play' : 'pause';
15+ //video[method]();
16+
17+ //Longer method
18+ if ( video . paused ) {
19+ video . play ( ) ;
20+ } else {
21+ video . pause ( ) ;
22+ }
23+ }
24+ function updateButton ( ) {
25+ const icon = this . paused ? '►' : '❚ ❚' ;
26+ toggle . textContent = icon ;
27+ }
28+
29+ function skip ( ) {
30+ console . log ( this . dataset ) ;
31+ video . currentTime += parseFloat ( this . dataset . skip ) ;
32+ } ;
33+
34+ function handleRangeUpdate ( ) {
35+ video [ this . name ] = this . value ;
36+ }
37+
38+ function handleProgress ( ) {
39+ const percent = ( video . currentTime / video . duration ) * 100 ;
40+ progressBar . style . flexBasis = `${ percent } %` ;
41+ }
42+
43+ function scrub ( e ) {
44+ const scrubTime = ( e . offsetX / progress . offsetWidth ) * video . duration ;
45+ video . currentTime = scrubTime ;
46+ }
47+
48+ function makeFullscreen ( ) {
49+ video . webkitRequestFullscreen ( ) ;
50+ }
51+
52+ /* Event listeners */
53+ video . addEventListener ( 'click' , togglePlay ) ;
54+ video . addEventListener ( 'play' , updateButton ) ;
55+ video . addEventListener ( 'pause' , updateButton ) ;
56+ video . addEventListener ( 'timeupdate' , handleProgress ) ;
57+
58+ toggle . addEventListener ( 'click' , togglePlay ) ;
59+ skipButtons . forEach ( button => button . addEventListener ( 'click' , skip ) ) ;
60+ ranges . forEach ( range => range . addEventListener ( 'change' , handleRangeUpdate ) ) ;
61+ ranges . forEach ( range => range . addEventListener ( 'mousemove' , handleRangeUpdate ) ) ;
62+
63+ let mousedown = false ;
64+ progress . addEventListener ( 'click' , scrub ) ;
65+ // progress.addEventListener('mousemove', () => {
66+ // if(mousedown) {
67+ // scrub();
68+ // }
69+ // });
70+ progress . addEventListener ( 'mousemove' , ( e ) => mousedown && scrub ( e ) ) ;
71+ progress . addEventListener ( 'mousedown' , ( ) => mousedown = true ) ;
72+ progress . addEventListener ( 'mouseup' , ( ) => mousedown = false ) ;
73+
74+ fullscreen . addEventListener ( 'click' , makeFullscreen ) ;
0 commit comments