Skip to content

Instantly share code, notes, and snippets.

@hussani
Created July 30, 2013 19:40
Show Gist options
  • Save hussani/6116204 to your computer and use it in GitHub Desktop.
Save hussani/6116204 to your computer and use it in GitHub Desktop.
Get Youtube videos and show into modal box
/*
Uses http://github.com/kylefox/jquery-modal to open video into modal box
*/
var playlistId, key, maxResults, nextPageToken, prevPageToken;
playlistId = "playlistID";
key = "apiConsumerKey";
maxResults = 50;
function YtVideo (data) {
$.extend(this, data);
this.render = function(){
var html = "";
html += '<div class="video-entry" data-video-id="' + this.getId() + '">';
html += '<img src="'+ this.getImage() +'" />';
html += '<p>' + this.getTitle() + '</p>';
html += '</div>';
return html;
};
this.getTitle = function(){
return this.snippet.title;
};
this.getImage = function(){
return this.snippet.thumbnails.medium.url;
};
this.getDescription = function(){
return this.snippet.description;
};
this.getId = function(){
return this.snippet.resourceId.videoId;
};
}
function YtPlaylist () {
this.render = function(){
var html = '<div class="row">';
this.forEach(function(ytv, index, array){
if((index % 3 === 0) && (index !== 0)){
html += '</div><div class="row">';
}
html += ytv.render();
});
html += "</div>"
return html;
};
}
YtPlaylist.prototype = new Array();
ytPlaylist = new YtPlaylist;
$(function() {
$.getJSON(
'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=' + playlistId + '&key=' + key + '&maxResults=' + maxResults,
function(data) {
nextPageToken = data.nextPageToken;
prevPageToken = data.prevPageToken;
$.each(data.items, function(i, ytv){
ytPlaylist.push(new YtVideo(ytv));
});
var html = ytPlaylist.render()
$(html).appendTo($('.video-container'));
$(".video-entry").click(function(){
$("#modal").remove();
var embed = getEmbed($(this).data('video-id'));
$('body').append('<div id="modal">' + embed + '</div>');
$("#modal").modal();
});
}
);
});
function getEmbed(videoId){
var html = '<object width="425" height="355">' +
'<param name="movie" value="https://www.youtube.com/v/' + videoId + '?rel=0&fs=1"></param>' +
'<param name="allowFullScreen" value="true"></param>' +
'<embed src="https://www.youtube.com/v/' + videoId + '?rel=1&fs=1"' +
'type="application/x-shockwave-flash"' +
'width="640" height="360"' +
'allowfullscreen="true"></embed>'+
'</object>';
return html;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment