Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use an existing player if present when video changes. #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/angular-youtube-embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,44 @@ angular.module('youtube-embed', ['ng'])
player.id = playerId;
return player;
}

function isEmpty (obj) {
if (obj == null) return true;
if (Object.keys) return Object.keys(obj).length == 0;
var keys = [];
for (var key in obj) if (_.has(obj, key)) keys.push(key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do obj.hasOwnProperty(key) instead of the underscore dependency?

return keys.length == 0;
};

function loadPlayer () {
if (scope.videoId || scope.playerVars.list) {
// If present, use an existing player
if(scope.player && !isEmpty(scope.player) && scope.player.getIframe() && scope.player.cueVideoById === 'function') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this getIframe() check do?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all of your hard work here @brandly @alexthewilde. scope.player.cueVideoById === 'function' doesn't work for me. It causes the check for the preexisting player to fail when a valid player is instantiated. I think you might mean (typeof scope.player.cueVideoById === 'function') here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brandly I believe this method returns the DOM node for the embedded iframe, maybe the intention is to make sure that it exists/is loaded?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getiframe function doesn't exists. I guess alex forget this one to include

var playerVars = ( playerVars ? playerVars : angular.copy(scope.playerVars) );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would playerVars be defined here? maybe i'm missing something, but i don't understand this ternary operator

playerVars.start = playerVars.start || scope.urlStartTime;
playerVars.end = playerVars.end || scope.urlEndTime;

// Use cueVideoById() instead of loadVideoById() so the video doesn't
// start to play automatically.
if (scope.videoId) {
scope.player.cueVideoById({
videoId:scope.videoId,
startSeconds:playerVars.start,
endSeconds:playerVars.end
});
}
else if(scope.playerVars.list) {
scope.player.cuePlaylist({
playlist:scope.playerVars.list,
startSeconds:playerVars.start,
endSeconds:playerVars.end
});
}
}
// Otherwise, create a new player
else if(scope.videoId || scope.playerVars.list) {
if (scope.player && typeof scope.player.destroy === 'function') {
scope.player.destroy();
}

scope.player = createPlayer();
}
};
Expand Down