@@ -31,23 +31,26 @@ export const video = (() => {
3131 vid . className = wrap . getAttribute ( 'data-vid-class' ) ;
3232 vid . loop = true ;
3333 vid . muted = true ;
34- vid . controls = true ;
34+ vid . controls = false ;
3535 vid . autoplay = false ;
3636 vid . playsInline = true ;
3737 vid . preload = 'metadata' ;
38- vid . disableRemotePlayback = true ;
39- vid . disablePictureInPicture = true ;
40- vid . controlsList = 'noremoteplayback nodownload noplaybackrate' ;
4138
4239 const observer = new IntersectionObserver ( ( es ) => es . forEach ( ( e ) => e . isIntersecting ? vid . play ( ) : vid . pause ( ) ) ) ;
4340
41+ vid . addEventListener ( 'error' , ( ) => progress . invalid ( 'video' ) ) ;
42+ vid . addEventListener ( 'loadedmetadata' , ( ) => {
43+ const height = vid . getBoundingClientRect ( ) . width * ( vid . videoHeight / vid . videoWidth ) ;
44+ vid . style . height = `${ height } px` ;
45+ wrap . style . height = `${ height } px` ;
46+ } ) ;
47+
4448 /**
4549 * @param {Response } res
4650 * @returns {Promise<Response> }
4751 */
4852 const resToVideo = ( res ) => {
4953 vid . addEventListener ( 'loadedmetadata' , ( ) => {
50- vid . style . removeProperty ( 'height' ) ;
5154 document . getElementById ( 'video-love-stroy-loading' ) ?. remove ( ) ;
5255 } , { once : true } ) ;
5356
@@ -64,56 +67,50 @@ export const video = (() => {
6467 const bar = document . getElementById ( 'progress-bar-video-love-stroy' ) ;
6568 const inf = document . getElementById ( 'progress-info-video-love-stroy' ) ;
6669
67- return request ( HTTP_GET , src )
68- . withCancel ( new Promise ( ( re ) => vid . addEventListener ( 'undangan.video.prefetch' , re , { once : true } ) ) )
69- . default ( { 'Range' : 'bytes=0-1' } )
70- . then ( ( res ) => {
71- vid . dispatchEvent ( new Event ( 'undangan.video.prefetch' ) ) ;
70+ return request ( HTTP_GET , src ) . withNoBody ( ) . default ( { 'Range' : 'bytes=0-1' } ) . then ( ( res ) => {
7271
73- if ( res . status === HTTP_STATUS_OK ) {
74- wrap . appendChild ( vid ) ;
75- return Promise . resolve ( ) ;
76- }
72+ if ( res . status === HTTP_STATUS_OK ) {
73+ vid . preload = 'none' ;
74+ vid . src = util . escapeHtml ( src ) ;
75+ wrap . appendChild ( vid ) ;
7776
78- if ( res . status !== HTTP_STATUS_PARTIAL_CONTENT ) {
79- throw new Error ( 'failed to fetch video' ) ;
80- }
77+ return Promise . resolve ( ) ;
78+ }
8179
82- vid . addEventListener ( 'error' , ( ) => progress . invalid ( 'video' ) , { once : true } ) ;
83- const loaded = new Promise ( ( r ) => vid . addEventListener ( 'loadedmetadata' , r , { once : true } ) ) ;
80+ if ( res . status !== HTTP_STATUS_PARTIAL_CONTENT ) {
81+ throw new Error ( 'failed to fetch video' ) ;
82+ }
8483
85- vid . src = util . escapeHtml ( src ) ;
86- wrap . appendChild ( vid ) ;
84+ const loaded = new Promise ( ( r ) => vid . addEventListener ( 'loadedmetadata' , r , { once : true } ) ) ;
85+
86+ vid . src = util . escapeHtml ( src ) ;
87+ wrap . appendChild ( vid ) ;
88+
89+ return loaded ;
90+ } ) . then ( ( ) => {
91+ vid . pause ( ) ;
92+ progress . complete ( 'video' ) ;
93+
94+ return request ( HTTP_GET , src ) . withRetry ( ) . withProgressFunc ( ( a , b ) => {
95+ const result = Number ( ( a / b ) * 100 ) . toFixed ( 0 ) + '%' ;
96+
97+ bar . style . width = result ;
98+ inf . innerText = result ;
99+ } ) . default ( ) . then ( resToVideo ) ;
100+ } ) . then ( ( res ) => {
101+ vid . controls = true ;
102+ vid . disableRemotePlayback = true ;
103+ vid . disablePictureInPicture = true ;
104+ vid . controlsList = 'noremoteplayback nodownload noplaybackrate' ;
87105
88- return loaded ;
89- } )
90- . then ( ( ) => {
91- progress . complete ( 'video' ) ;
92-
93- const height = vid . getBoundingClientRect ( ) . width * ( vid . videoHeight / vid . videoWidth ) ;
94- vid . style . height = `${ height } px` ;
95-
96- return request ( HTTP_GET , src )
97- . withProgressFunc ( ( a , b ) => {
98- const result = Number ( ( a / b ) * 100 ) . toFixed ( 0 ) + '%' ;
99-
100- bar . style . width = result ;
101- inf . innerText = result ;
102- } )
103- . withRetry ( )
104- . default ( )
105- . then ( resToVideo )
106- . then ( ( v ) => {
107- vid . load ( ) ;
108- observer . observe ( vid ) ;
109- return v ;
110- } )
111- . catch ( ( err ) => {
112- bar . style . backgroundColor = 'red' ;
113- inf . innerText = `Error loading video` ;
114- console . error ( err ) ;
115- } ) ;
116- } ) ;
106+ vid . load ( ) ;
107+ observer . observe ( vid ) ;
108+ return res ;
109+ } ) . catch ( ( err ) => {
110+ bar . style . backgroundColor = 'red' ;
111+ inf . innerText = `Error loading video` ;
112+ console . error ( err ) ;
113+ } ) ;
117114 } ;
118115
119116 if ( ! window . isSecureContext ) {
@@ -125,10 +122,10 @@ export const video = (() => {
125122 return c . del ( src ) . then ( fetchBasic ) . then ( ( r ) => c . set ( src , r ) ) ;
126123 }
127124
128- progress . complete ( 'video' ) ;
129125 return resToVideo ( res ) . then ( ( ) => {
130126 wrap . appendChild ( vid ) ;
131127 observer . observe ( vid ) ;
128+ progress . complete ( 'video' ) ;
132129 } ) ;
133130 } ) ;
134131 } ;
0 commit comments