Commit a883d253 by Valera Rozuvan Committed by Valera Rozuvan

Fix multiple videos on page

If there are several videos on page, make sure that
the global onYouTubeIframeAPIReady function is properly
setup so that all callbacks are trigerred when the YouTube API
loads.

BLD-972
parent 94966a0e
...@@ -73,7 +73,10 @@ function (VideoPlayer, VideoStorage) { ...@@ -73,7 +73,10 @@ function (VideoPlayer, VideoStorage) {
setSpeed: setSpeed, setSpeed: setSpeed,
trigger: trigger, trigger: trigger,
youtubeId: youtubeId youtubeId: youtubeId
}; },
_youtubeApiDeferred = null,
_oldOnYouTubeIframeAPIReady;
Initialize.prototype = methodsDict; Initialize.prototype = methodsDict;
...@@ -112,7 +115,7 @@ function (VideoPlayer, VideoStorage) { ...@@ -112,7 +115,7 @@ function (VideoPlayer, VideoStorage) {
// Require JS. At the time when we reach this code, the stand alone // Require JS. At the time when we reach this code, the stand alone
// HTML5 player is already loaded, so no further testing in that case // HTML5 player is already loaded, so no further testing in that case
// is required. // is required.
var video, onYTApiReady; var video, onYTApiReady, setupOnYouTubeIframeAPIReady;
if (state.videoType === 'youtube') { if (state.videoType === 'youtube') {
state.youtubeApiAvailable = false; state.youtubeApiAvailable = false;
...@@ -129,13 +132,65 @@ function (VideoPlayer, VideoStorage) { ...@@ -129,13 +132,65 @@ function (VideoPlayer, VideoStorage) {
}; };
if (window.YT) { if (window.YT) {
window.YT.ready(onYTApiReady); // If we have a Deferred object responsible for calling OnYouTubeIframeAPIReady
} else { // callbacks, make sure that they have all been called by trying to resolve the
window.onYouTubeIframeAPIReady = function () { // Deferred object. Upon resolving, all the OnYouTubeIframeAPIReady will be
// called. If the object has been already resolved, the callbacks will not
// be called a second time.
if (_youtubeApiDeferred) {
_youtubeApiDeferred.resolve();
}
window.YT.ready(function () {
onYTApiReady(); onYTApiReady();
});
} else {
// There is only one global variable window.onYouTubeIframeAPIReady which
// is supposed to be a function that will be called by the YouTube API
// when it finished initializing. This function will update this global function
// so that it resolves our Deferred object, which will call all of the
// OnYouTubeIframeAPIReady callbacks.
//
// If this global function is already defined, we store it first, and make
// sure that it gets executed when our Deferred object is resolved.
setupOnYouTubeIframeAPIReady = function () {
_oldOnYouTubeIframeAPIReady = window.onYouTubeIframeAPIReady || undefined;
window.onYouTubeIframeAPIReady = function () {
window.onYouTubeIframeAPIReady.resolve();
};
window.onYouTubeIframeAPIReady.resolve = _youtubeApiDeferred.resolve;
window.onYouTubeIframeAPIReady.done = _youtubeApiDeferred.done;
if (_oldOnYouTubeIframeAPIReady) {
window.onYouTubeIframeAPIReady.done(_oldOnYouTubeIframeAPIReady);
}
}; };
_loadYoutubeApi(state); // If a Deferred object hasn't been created yet, create one now. It will
// be responsible for calling OnYouTubeIframeAPIReady callbacks once the
// YouTube API loads. After creating the Deferred object, load the YouTube
// API.
if (!_youtubeApiDeferred) {
_youtubeApiDeferred = $.Deferred();
setupOnYouTubeIframeAPIReady();
_loadYoutubeApi(state);
} else if (!window.onYouTubeIframeAPIReady || !window.onYouTubeIframeAPIReady.done) {
// The Deferred object could have been already defined in a previous
// initialization of the video module. However, since then the global variable
// window.onYouTubeIframeAPIReady could have been overwritten. If so,
// we should set it up again.
setupOnYouTubeIframeAPIReady();
}
// Attach a callback to our Deferred object to be called once the
// YouTube API loads.
window.onYouTubeIframeAPIReady.done(function () {
window.YT.ready(function () {
onYTApiReady();
});
});
} }
} else { } else {
video = VideoPlayer(state); video = VideoPlayer(state);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment