summaryrefslogtreecommitdiff
blob: 668cc7a07b06d36e42ba45c4c0c9e7770fdffacb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
( function( $ ) {
	var resizeTimer;

	function responsiveVideos() {
		$( '.jetpack-video-wrapper' )
			.find( 'embed, iframe, object' )
			.each( function() {
				var _this, videoWidth, videoHeight, videoRatio, videoWrapper, videoMargin, containerWidth;

				_this = $( this );
				videoMargin = 0;

				if (
					_this
						.parents( '.jetpack-video-wrapper' )
						.prev( 'p' )
						.css( 'text-align' ) === 'center'
				) {
					videoMargin = '0 auto';
				}

				if ( ! _this.attr( 'data-ratio' ) ) {
					_this
						.attr( 'data-ratio', this.height / this.width )
						.attr( 'data-width', this.width )
						.attr( 'data-height', this.height )
						.css( {
							display: 'block',
							margin: videoMargin,
						} );
				}

				videoWidth = _this.attr( 'data-width' );
				videoHeight = _this.attr( 'data-height' );
				videoRatio = _this.attr( 'data-ratio' );
				videoWrapper = _this.parent();
				containerWidth = videoWrapper.width();

				if ( videoRatio === 'Infinity' ) {
					videoWidth = '100%';
				}

				_this.removeAttr( 'height' ).removeAttr( 'width' );

				if ( videoWidth > containerWidth ) {
					_this.width( containerWidth ).height( containerWidth * videoRatio );
				} else {
					_this.width( videoWidth ).height( videoHeight );
				}
			} );
	}

	$( document ).ready( function() {
		$( window )
			.on( 'load.jetpack', responsiveVideos )
			.on( 'resize.jetpack', function() {
				clearTimeout( resizeTimer );
				resizeTimer = setTimeout( responsiveVideos, 500 );
			} )
			.on( 'post-load.jetpack', responsiveVideos )
			.resize();
	} );
} )( jQuery );