Greetings to everyone!
As title says I'm trying to create custom tool - button to embed YouTube video.
The way it supposed to work - you click button - dialog appears - you insert video URL and it then add youtube iframe embed code into editor. At this moment I expect to see iframe in editor and iframe should show video.
I have most of this done, but last step do not work properly. iframe is added correctly, but video wont load. I get JS error saying "ReferenceError: yt is not defined". It looks like that JS file with YouTube API wont load properly.
Any help will be greatly appreciated.
Here is HTML code (make sure to add Script and CSS references to Kendo and jQuery)
As title says I'm trying to create custom tool - button to embed YouTube video.
The way it supposed to work - you click button - dialog appears - you insert video URL and it then add youtube iframe embed code into editor. At this moment I expect to see iframe in editor and iframe should show video.
I have most of this done, but last step do not work properly. iframe is added correctly, but video wont load. I get JS error saying "ReferenceError: yt is not defined". It looks like that JS file with YouTube API wont load properly.
Any help will be greatly appreciated.
Here is HTML code (make sure to add Script and CSS references to Kendo and jQuery)
<
body
>
<
h2
>Kendo Editor Example</
h2
>
<
textarea
cols
=
"20"
id
=
"Description"
name
=
"Description"
rows
=
"5"
style
=
"width:600px; height:400px"
></
textarea
><
script
>
jQuery(function(){jQuery("#Description").kendoEditor({tools:[{name:"bold"},{name:"italic"},{name:"createLink"},{name:"unlink"},{name:"Insert Video",tooltip:"Insert Video",exec:insertVideo},{name:"insertImage"},{name:"insertUnorderedList"},{name:"formatBlock"}]});});
</
script
>
<
script
type
=
"text/x-kendo-template"
id
=
"insertVideo-template"
>
<
div
>
<
label
for
=
"videoUrl"
>Enter a URL from YouTube or Vimeo:</
label
>
<
input
type
=
"text"
id
=
"videoUrl"
name
=
"videoUrl"
/>
<
div
class
=
"insertVideo-actions"
>
<
button
class
=
"k-button insertVideo-insert"
>Insert</
button
>
<
button
class
=
"k-button insertVideo-cancel"
>Cancel</
button
>
</
div
>
</
div
>
</
script
>
<
script
type
=
"text/x-kendo-template"
id
=
"youTube-template"
>
<
iframe
class
=
"youtube-player"
type
=
"text/html"
width
=
"560"
height
=
"315"
src
=
"http://www.youtube.com/embed/#= source #?fs=1&feature=oembed&wmode=transparent"
frameborder
=
"0"
allowfullscreen></
iframe
>
</
script
>
<
script
>
function insertVideo(e)
{
var editor = $(this).data("kendoEditor");
var dialog = $($("#insertVideo-template").html())
.find(".insertVideo-insert")
.click(function ()
{
var media = testUrlForMedia(dialog.element.find("input").val());
if (media)
{
var template = kendo.template($("#youTube-template").html());
editor.exec("insertHTML", { value: template({ source: media.id }) });
}
dialog.close();
})
.end()
.find(".insertVideo-cancel")
.click(function() {
dialog.close();
})
.end()
.kendoWindow({
modal: true,
title: "Insert Video",
deactivate: function() {
dialog.destroy();
}
}).data("kendoWindow");
dialog.center().open();
}
function testUrlForMedia(pastedData) {
var success = false;
var media = {};
if (pastedData.match('http://(www.)?youtube|youtu\.be')) {
if (pastedData.match('embed')) { youtube_id = pastedData.split(/embed\//)[1].split('"')[0]; }
else { youtube_id = pastedData.split(/v\/|v=|youtu\.be\//)[1].split(/[?&]/)[0]; }
media.type = "youtube";
media.id = youtube_id;
success = true;
}
else if (pastedData.match('http://(player.)?vimeo\.com')) {
vimeo_id = pastedData.split(/video\/|http:\/\/vimeo\.com\//)[1].split(/[?&]/)[0];
media.type = "vimeo";
media.id = vimeo_id;
success = true;
}
else if (pastedData.match('http://player\.soundcloud\.com')) {
soundcloud_url = unescape(pastedData.split(/value="/)[1].split(/["]/)[0]);
soundcloud_id = soundcloud_url.split(/tracks\//)[1].split(/[&"]/)[0];
media.type = "soundcloud";
media.id = soundcloud_id;
success = true;
}
if (success) { return media; }
else { alert("No valid media id detected"); }
return false;
}
</
script
>