Browse Source

Merge pull request #201 from fancycode/load_youtube_sandbox_lazily

Load YouTube sandbox lazily.
pull/202/head
Simon Eisenmann 10 years ago
parent
commit
4d9076c39f
  1. 109
      static/js/directives/youtubevideo.js

109
static/js/directives/youtubevideo.js

@ -104,61 +104,70 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo
var isPaused = null; var isPaused = null;
var playReceivedNow = null; var playReceivedNow = null;
var initialState = null; var initialState = null;
var sandboxFrame = $("#youtubeplayer", $element)[0]; var sandboxApi = null;
var template = sandboxTemplate; var createSandboxApi = function() {
template = template.replace(/__PARENT_ORIGIN__/g, $window.location.protocol + "//" + $window.location.host); if (!sandboxApi) {
template = template.replace(/__YOUTUBE_SANDBOX_JS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('sandboxes/youtube') + ".js")); var sandboxFrame = $("#youtubeplayer", $element)[0];
var sandboxApi = sandbox.createSandbox(sandboxFrame, template);
var template = sandboxTemplate;
sandboxApi.e.on("message", function(event, message) { template = template.replace(/__PARENT_ORIGIN__/g, $window.location.protocol + "//" + $window.location.host);
var msg = message.data; template = template.replace(/__YOUTUBE_SANDBOX_JS_URL__/g, restURL.createAbsoluteUrl(require.toUrl('sandboxes/youtube') + ".js"));
var data = msg[msg.type] || {}; sandboxApi = sandbox.createSandbox(sandboxFrame, template);
switch (msg.type) {
case "ready": sandboxApi.e.on("message", function(event, message) {
break; var msg = message.data;
case "youtube.apiReady": var data = msg[msg.type] || {};
$scope.$apply(function() { switch (msg.type) {
console.log("YouTube IFrame ready"); case "ready":
isYouTubeIframeAPIReadyDefer.resolve(); sandboxApi.postMessage("loadApi", {"url": $window.location.protocol + YOUTUBE_IFRAME_API_URL});
}); break;
break; case "youtube.apiReady":
case "youtube.playerReady": $scope.$apply(function() {
$scope.$apply(function() { console.log("YouTube IFrame ready");
playerReady.resolve(); isYouTubeIframeAPIReadyDefer.resolve();
}); });
break; break;
case "youtube.volume": case "youtube.playerReady":
$scope.$apply(function(scope) { $scope.$apply(function() {
scope.volume = data.volume; playerReady.resolve();
}); });
break; break;
case "youtube.event": case "youtube.volume":
$scope.$apply(function(scope) { $scope.$apply(function(scope) {
console.log("State change", data); scope.volume = data.volume;
if (player) { });
player.setPlayerState(data.state); break;
case "youtube.event":
$scope.$apply(function(scope) {
console.log("State change", data);
if (player) {
player.setPlayerState(data.state);
}
scope.$emit(data.event, data.position);
});
break;
case "youtube.position":
if (player) {
player.setCurrentTime(data.position);
}
break;
default:
console.log("Unknown message received", message);
break;
} }
scope.$emit(data.event, data.position);
}); });
break;
case "youtube.position":
if (player) {
player.setCurrentTime(data.position);
}
break;
default:
console.log("Unknown message received", message);
break;
} }
}); }
$scope.$on("$destroy", function() { $scope.$on("$destroy", function() {
if (player) { if (player) {
player.destroy(); player.destroy();
} }
sandboxApi.destroy(); if (sandboxApi) {
sandboxApi = null; sandboxApi.destroy();
sandboxApi = null;
}
}); });
var errorIds = { var errorIds = {
@ -506,12 +515,8 @@ define(['require', 'jquery', 'underscore', 'moment', 'text!partials/youtubevideo
} }
}; };
$scope.loadYouTubeAPI = function() {
sandboxApi.postMessage("loadApi", {"url": $window.location.protocol + YOUTUBE_IFRAME_API_URL});
};
$scope.showYouTubeVideo = function() { $scope.showYouTubeVideo = function() {
$scope.loadYouTubeAPI(); createSandboxApi();
$scope.layout.youtubevideo = true; $scope.layout.youtubevideo = true;
$scope.$emit("mainview", "youtubevideo", true); $scope.$emit("mainview", "youtubevideo", true);
if (currentToken) { if (currentToken) {

Loading…
Cancel
Save