PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
← All changes | modules/theme-tools/responsive-videos/responsive-videos.js +73 -0 16.2-beta → 16.3 View file →
@@ -1,0 +1,73 @@
1 +import './responsive-videos.css';
2 +
3 +let resizeTimer;
4 +
5 +/**
6 + * Resize all videos in the document.
7 + */
8 +function responsiveVideos() {
9 + document.querySelectorAll( '.jetpack-video-wrapper' ).forEach( function ( wrapper ) {
10 + wrapper.querySelectorAll( 'embed, iframe, object' ).forEach( function ( video ) {
11 + let videoMargin = 0;
12 +
13 + const previousSibling = wrapper.previousElementSibling;
14 + if (
15 + previousSibling &&
16 + previousSibling.nodeName === 'P' &&
17 + getComputedStyle( previousSibling )[ 'text-align' ] === 'center'
18 + ) {
19 + videoMargin = '0 auto';
20 + }
21 +
22 + if ( ! video.hasAttribute( 'data-ratio' ) ) {
23 + video.setAttribute( 'data-ratio', ( video.height || 0 ) / ( video.width || 0 ) );
24 + video.setAttribute( 'data-width', video.width );
25 + video.setAttribute( 'data-height', video.height );
26 + video.style.display = 'block';
27 + video.style.margin = videoMargin;
28 + }
29 +
30 + const videoHeight = video.getAttribute( 'data-height' );
31 + const videoRatio = video.getAttribute( 'data-ratio' );
32 + const containerWidth = video.parentElement.clientWidth;
33 +
34 + video.removeAttribute( 'height' );
35 + video.removeAttribute( 'width' );
36 +
37 + if ( videoRatio === 'Infinity' ) {
38 + video.style.width = '100%';
39 + video.style.height = videoHeight + 'px';
40 + return;
41 + }
42 +
43 + const videoWidth = video.getAttribute( 'data-width' );
44 +
45 + if ( parseInt( videoWidth, 10 ) > containerWidth ) {
46 + video.style.width = containerWidth + 'px';
47 + video.style.height = containerWidth * parseFloat( videoRatio ) + 'px';
48 + } else {
49 + video.style.width = videoWidth + 'px';
50 + video.style.height = videoHeight + 'px';
51 + }
52 + } );
53 + } );
54 +}
55 +
56 +/**
57 + * Initialize event listeners and resize everything straight away.
58 + */
59 +function init() {
60 + window.addEventListener( 'load', responsiveVideos );
61 + window.addEventListener( 'resize', function () {
62 + clearTimeout( resizeTimer );
63 + resizeTimer = setTimeout( responsiveVideos, 500 );
64 + } );
65 + window.addEventListener( 'is.post-load', responsiveVideos );
66 + setTimeout( responsiveVideos );
67 +}
68 +
69 +if ( document.readyState !== 'loading' ) {
70 + init();
71 +} else {
72 + document.addEventListener( 'DOMContentLoaded', init );
73 +}