Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simply video gallery switcher
var VideoGallery = (function (self, $) {
self.InsertContainerId = 'youtube-player';
self.isInserted = false;
self.findIds = function (url) {
var aVideoRE = new RegExp("[\\?&]{1}v=([a-z\\d-_]{11})",'gi');
var aPlaylistRE = new RegExp ("[\\?&]{1}list=([a-z\\d-_]{34})", 'gi');
var results = {
videoId: false,
playlistId: false
};
var trying = aPlaylistRE.exec(url);
if (Array.isArray(trying) && typeof trying[1] === 'string' && trying != '') {
results.playlistId = trying[1];
}
trying = false;
trying = aVideoRE.exec(url);
if (Array.isArray(trying) && typeof trying[1] === 'string' && trying != '') {
results.videoId = trying[1];
}
return results;
};
self.setUpAttrs = function (e) {
var result = self.findIds(e.currentTarget.href);
if (result.videoId) {
$(e.currentTarget).attr('data-youtube-video-id', result.videoId);
if (result.playlistId) {
$(e.currentTarget).attr('data-youtube-playlist-id', result.playlistId);
}
}
else {
$(e.currentTarget).attr('data-youtube-video-id', 'false');
}
};
self.insertPlayer = function (currentTarget) {
var config = {
width: 642,
height: 482,
videoId: $(currentTarget).attr('data-youtube-video-id'),
//videoId: videoId,
playerVars: {
'autoplay': 1
}
};
if ($(currentTarget).attr('data-youtube-playlist-id')) {
config.playerVars.listType = 'playlist';
config.playerVars.list = $(currentTarget).attr('data-youtube-playlist-id');
}
self.player = new YT.Player (self.InsertContainerId, config);
self.isInserted = true;
};
self.onLinkClick = function (e) {
e.preventDefault();
if (typeof $(e.currentTarget).attr('data-youtube-video-id') !== 'string') {
self.setUpAttrs(e);
}
if (self.isInserted) {
self.player.destroy();
}
if (typeof $(e.currentTarget).attr('data-youtube-video-id') === 'string' && $(e.currentTarget).attr('data-youtube-video-id') != 'false') {
$('.video-gallery-player-place').css ('display', 'block');
self.insertPlayer(e.currentTarget);
}
else {
$('#' + self.InsertContainerId).html ('Что-то пошло не так.');
}
};
return self;
}(VideoGallery || {}, jQuery));
(function onYouTubeIframeAPIReady ($) {
$('.video-gallery .click-to-play').on('click', function (e) {
VideoGallery.onLinkClick(e);
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.