youtube_embed.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*global jQuery, Drupal, drupalSettings*/
  2. /*jslint white:true, multivar, this, browser:true*/
  3. (function($, Drupal, drupalSettings)
  4. {
  5. "use strict";
  6. function youtubeVideoEnable(context)
  7. {
  8. $(context).find(".youtube-player").once("youtube-video-enable").each(function()
  9. {
  10. var youtubeID, youtubePreview;
  11. youtubeID = $(this).attr("data-youtube-id");
  12. $(this).parent().removeClass("no-js");
  13. if(drupalSettings.urlToVideoFilter.autoload)
  14. {
  15. $(this).empty().append($("<iframe/>").attr("src", "//www.youtube.com/embed/" + youtubeID + "?autoplay=0&autohide=2&border=0&wmode=opaque&enablejsapi=1").attr("frameborder", "0").attr("class", "player-iframe youtube-iframe"));
  16. }
  17. else
  18. {
  19. if(drupalSettings.urlToVideoFilter.youtubeWebp)
  20. {
  21. youtubePreview = "//i.ytimg.com/vi_webp/" + youtubeID + "/sddefault.webp";
  22. }
  23. else
  24. {
  25. youtubePreview = "//i.ytimg.com/vi/" + youtubeID + "/hqdefault.jpg";
  26. }
  27. $(this).empty().append($("<span/>").append($("<div/>", {class:"player-thumb",style:"background-image:url(" + youtubePreview + ")"})).append($("<span/>", {class:"play-button"})).click(function(e)
  28. {
  29. e.stopPropagation();
  30. Drupal.detachBehaviors($(this).parent().parent());
  31. $(this).replaceWith($("<iframe/>").attr("src", "//www.youtube.com/embed/" + youtubeID + "?autoplay=1&autohide=2&border=0&wmode=opaque&enablejsapi=1").attr("frameborder", "0").attr("class", "player-iframe youtube-iframe"));
  32. }));
  33. }
  34. });
  35. }
  36. Drupal.behaviors.urlToVideoFilterYoutube = {
  37. attach:function(context)
  38. {
  39. youtubeVideoEnable(context);
  40. },
  41. detach:function(context)
  42. {
  43. $(".youtube-player", context).unbind("click");
  44. }
  45. };
  46. }(jQuery, Drupal, drupalSettings));