-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYouTubeEmbed.js
138 lines (112 loc) · 4.82 KB
/
YouTubeEmbed.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* ######################################################################################################
* JS/jQuery for Typesetter CMS plugin YouTube Embed
* Authors: J. Krausz, Mahotilo
* Date: 2019-11-09
* Version: 1.0-b4
* partially based on https://stackoverflow.com/questions/34375655, https://jsfiddle.net/kmsdev/gsfkL6xL/
* ######################################################################################################
*/
// Load the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
$(function(){
$('.youtube-embed-player > iframe').each(function(){
var dynamic_id = 'ytp-' + Math.random().toString(16).slice(2);
$(this).attr('id', dynamic_id);
});
// That's for YouTube embeddings in SliderFactoy slides:
// Stop playing all vids in current slider on slide change
$('.gpPrevSlide, .gpNextSlide').on('click', function(){
$wrapper = $(this).closest('.gpSlideWrapper');
stopAllYouTubeEmbeddingsInside($wrapper);
});
// SliderFactoy as of 1.0.2 triggers gpSlideChangeBefore and gpSlideChangeAfter events
$(document).on('gpSlideChangeAfter', '.gpSlideWrapper', function(evt){
$wrapper = $(evt.target);
stopAllYouTubeEmbeddingsInside($wrapper);
});
function stopAllYouTubeEmbeddingsInside($wrapper){
var $youTubeIframes = $wrapper.find(".youtube-embed-player > iframe");
if( $youTubeIframes.length ){
$youTubeIframes.each( function(){
$(this)[0].contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
});
}
}
});
// Played video will pause when scrolled out of sight and resume when becoming visible again
// Playback starts by the user. If user paused video, it stays paused
onYouTubeIframeAPIReady = function(){
var LoadVideo = function(player_id){
var Program = {
Init : function(){
this.NewPlayer();
this.EventHandler();
},
NewPlayer : function(){
this.Player = new YT.Player(player_id, {});
this.Player.element = document.getElementById(player_id);
this.Player.Paused = 0;
},
Play : function(){
if( typeof(this.Player.getPlayerState) == 'function' && this.Player.getPlayerState() === 2 && this.Player.Paused == 1 ){
this.Player.playVideo();
this.Player.Paused = 0;
}
},
Pause : function(){
if( typeof(this.Player.getPlayerState) == 'function' && this.Player.getPlayerState() === 1 ){
this.Player.pauseVideo();
this.Player.Paused = 1;
}
},
ScrollControl : function(){
if( Utils.IsElementInViewport(this.Player.element) ){
this.Play();
}else{
this.Pause();
}
},
EventHandler : function(){
var _this = this;
$(window).on('scroll', function(){
var now = +new Date;
if( _this.lastExec && now < _this.lastExec + 250 ){
clearTimeout(_this.deferTimer);
_this.deferTimer = setTimeout(function(){
_this.lastExec = now;
_this.ScrollControl();
}, 250);
}else{
_this.lastExec = now;
_this.ScrollControl();
}
});
}
};
var Utils = {
IsElementInViewport : function(el){
var rect = el.getBoundingClientRect();
var it_is = (
rect.top + rect.height * 0.5 >= 0 &&
rect.left + rect.width * 0.5 >= 0 &&
rect.bottom - rect.height * 0.5 <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right - rect.width * 0.5 <= (window.innerWidth || document.documentElement.clientWidth)
);
return it_is;
}
};
return Program.Init();
};
$('.youtube-embed-player > iframe').each(function(){
LoadVideo($(this).attr('id'));
});
$(document).on('SectionAdded SectionCopied', function(evt){
var dynamic_id = 'ytp-' + Math.random().toString(16).slice(2);
$(evt.target).find('iframe').attr('id', dynamic_id);
// LoadVideo(dynamic_id); // won't work this way
});
};