Hi
I created a youtube plugin with the following features:
- Embedd a youtube video in two sizes
- Create a textlink to the video in four sizes (In my case this is
loaded in a fancybox with the appropriate width and height. If the
fancybox isn't a possibility the link will redirect to the video on
youtube, opening in a new window)
- Create a imagelink to the video in four sizes, where the image is a
shot from the given video (In my case this is loaded in a fancybox
with the appropriate width and height. If the fancybox isn't a
possibility the link will redirect to the video on youtube, opening in
a new window)
- Enter the complete url or the video id
- On setup additional fields for the different options are shown or
hidden
Any comments or improvements are really appreciated, I'm not that well
of a jquery star.
Here is a link for a online demo:
http://www.chaosbydesign.nl/cleditor/youtube/index.php
And here is the source code:
(function($) {
// Set smallest width and height as basic
height = '340';
width = '560';
$.
cleditor.buttons.youtube = {
name: "youtube",
image: "youtube.gif",
title: "Add a youtube video",
command: "inserthtml",
buttonClick: youtube
};
function youtube(e, data)
{
$.fancybox(
'<h2>Add a youtube video</h2>' +
'<div id="errorEmptyUrl" class="error">' +
'Please fill in a youtube url' +
'</div>' +
'<div id="errorInvalidUrl" class="error">' +
'This is not a valid youtube url' +
'</div>' +
'<table border="0">' +
'<tr>' +
'<td>Add the youtube url:</td>' +
'<td><input name="videourl" type="text"></td>' +
'</tr>' +
'<tr>' +
'<td></td>' +
'<td><input type="radio" value="embed" name="setMode" checked>
Embed my video in the page' +
'<br/><input type="radio" value="link" name="setMode"> Create a
link to the video' +
'<br/><div id="displayOptionsLinkYoutube">' +
'<br/><input type="radio" value="image" name="setLink" checked>
Display image from the video' +
'<br/><input type="radio" value="text" name="setLink"> Display
text' +
'</div>' +
'<div id="displayOptionsLinkYoutubeText">' +
'Link text: <input name="linkUrlText" type="text">' +
'</div></td>' +
'</tr>' +
'<tr>' +
'<td>Choose a size:</td>' +
'<td><button class="youtubeSize youtubeSizeActive"
value="560/340">560x340</button>' +
'<button class="youtubeSize" value="640/385">640x385</button>' +
'<button class="embed disabled" disabled value="853/505">853x505</
button>' +
'<button class="embed disabled" disabled
value="1280/745">1280x745</button></td>' +
'</tr>' +
'<tr>' +
'<td></td>' +
'<td><input class="submit" type="submit" value="Add video"></td>'
+
'</tr>' +
'</table>',
{
'autoDimensions' : true,
'width' : 'auto',
'height' : 'auto',
'transitionIn' : 'none',
'transitionOut' : 'none'
}
);
// Hide errors
$('#errorEmptyUrl').hide()
$('#errorInvalidUrl').hide()
// hide Options
$('#displayOptionsLinkYoutube').hide();
$('#displayOptionsLinkYoutubeText').hide();
// Functions for clicking a button [1] Change active state [2] Get
new values for width and height
$('button').click(function()
{
$('button').removeClass('youtubeSizeActive');
$(this).addClass('youtubeSizeActive');
if($(this).hasClass('youtubeSizeActive'))
{
var value = $(this).val();
var splitted = value.split("/");
height = splitted[1];
width = splitted[0];
}
});
// Expand more options
$('[name=setMode]').click(function()
{
var value = $("[name=setMode]:checked").val();
var valueOptions = $("[name=setLink]:checked").val();
if(value == 'link')
{
$('button').removeClass('disabled');
$('button').addClass('youtubeSize');
$('button').removeAttr('disabled');
$('#displayOptionsLinkYoutube').show();
if(valueOptions == 'text')
{
$('#displayOptionsLinkYoutubeText').show();
}
$('[name=setLink]').click(function()
{
var valueOptions = $("[name=setLink]:checked").val();
if(valueOptions == 'text')
{
$('#displayOptionsLinkYoutubeText').show();
}
else
{
$('#displayOptionsLinkYoutubeText').hide();
};
});
}
else
{
$('.embed').addClass('disabled');
$('.embed').removeClass('youtubeSize');
$('#displayOptionsLinkYoutube').hide();
$('#displayOptionsLinkYoutubeText').hide();
if($('button').hasClass('disabled'))
{
$('button:eq(2)').attr('disabled', true);
$('button:eq(3)').attr('disabled', true);
$('button').removeClass('youtubeSizeActive');
$('button').first().addClass('youtubeSizeActive');
height = '340';
width = '560';
};
};
});
// On submit check values and construct the proper url
$('[type=submit]').click(function()
{
var value = $("[name=setMode]:checked").val();
var valueOptions = $("[name=setLink]:checked").val();
var youtubeUrl = $('[name=videourl]').val();
if(youtubeUrl.length)
{
checkUrl = new RegExp( /https?:\/\/(?:[a-zA_Z]{2,3}.)?(?:youtube
\.com\/watch\?)((?:[\w\d\-\_\=]+&(?:amp;)?)*v(?:<[A-Z]+>)?
=([0-9a-zA-Z\-\_]+))/i);
if(checkUrl.test(youtubeUrl))
{
var youtubeUrl = $('[name=videourl]').val().match("[\\?
&]v=([^&#]*)");
var youtubeUrl = youtubeUrl[1];
}
if(value == 'link')
{
if(valueOptions == 'text')
{
var linkItem = $('[name=linkUrlText]').val();
}
else
{
var linkItem = '<img src="
http://img.youtube.com/vi/' +
youtubeUrl + '/2.jpg" height="137px" width="200px">';
}
var html = '<a target="_blank" class="youtubeFancyboxLink"
href="
http://www.youtube.com/watch?v=' + youtubeUrl +
'&feature=player_embedded&width=' + width + '&height=' + height + '">'
+ linkItem + '</a>';
}
else
{
// New embed code is worthless until we can set the wmode
properly
//var html = '<iframe title="YouTube video player" class="youtube-
player" type="text/html" width="' + width + '" height="' + height + '"
src="
http://www.youtube.com/embed/' + youtubeUrl[1] + '?rel=0&"
frameborder="0" wmode="transparant"></iframe>';
var html = '<object type="application/x-shockwave-flash"
style="width:' + width + 'px; height:' + height + 'px;" data="http://
www.youtube.com/v/' + youtubeUrl + '"><param name="wmode"
value="transparent" /><param name="movie" value="http://
www.youtube.com/v/' + youtubeUrl + '" /></object>';
}
data.editor.execCommand(data.command, html);
data.editor.focus();
window.top.window.$.fancybox.close();
}
else
{
$('#errorEmptyUrl').show();
}
});
return false;
};
})(jQuery);