media_youtube.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @file media_youtube/js/media_youtube.js
  3. */
  4. (function ($) {
  5. Drupal.media_youtube = {};
  6. Drupal.behaviors.media_youtube = {
  7. attach: function (context, settings) {
  8. // Check the browser to see if it supports html5 video.
  9. var video = document.createElement('video');
  10. var html5 = video.canPlayType ? true : false;
  11. // If it has video, does it support the correct codecs?
  12. if (html5) {
  13. html5 = false;
  14. if (video.canPlayType( 'video/webm; codecs="vp8, vorbis"' ) || video.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"')) {
  15. html5 = true;
  16. }
  17. }
  18. // Put a prompt in the video wrappers to let users know they need flash
  19. if (!FlashDetect.installed && !html5){
  20. $('.media-youtube-preview-wrapper').each(Drupal.media_youtube.needFlash);
  21. }
  22. // Replace all object tags with iframes.
  23. if (Drupal.settings && Drupal.settings.media_youtube) {
  24. for (video in Drupal.settings.media_youtube) {
  25. Drupal.media_youtube.insertEmbed(video);
  26. }
  27. }
  28. }
  29. };
  30. Drupal.media_youtube.needFlash = function () {
  31. var id = $(this).attr('id');
  32. var wrapper = $('.media-youtube-preview-wrapper');
  33. var hw = Drupal.settings.media_youtube[id].height / Drupal.settings.media_youtube[id].width;
  34. wrapper.html('<div class="js-fallback">' + Drupal.t('You need Flash to watch this video. <a href="@flash">Get Flash</a>', {'@flash':'http://get.adobe.com/flashplayer'}) + '</div>');
  35. wrapper.height(wrapper.width() * hw);
  36. };
  37. Drupal.media_youtube.insertEmbed = function (embed_id) {
  38. var videoWrapper = $('#' + embed_id + '.media-youtube-preview-wrapper');
  39. var settings = Drupal.settings.media_youtube[embed_id];
  40. // Calculate the ratio of the dimensions of the embed.
  41. settings.hw = settings.height / settings.width;
  42. // Replace the object embed with YouTube's iframe. This isn't done by the
  43. // theme function because YouTube doesn't have a no-JS or no-Flash fallback.
  44. var video = $('<iframe class="youtube-player" type="text/html" frameborder="0"></iframe>');
  45. var src = 'http://www.youtube.com/embed/' + settings.video_id;
  46. // Allow other modules to modify the video settings.
  47. settings.options = settings.options || {};
  48. settings.options.wmode = 'opaque';
  49. $(window).trigger('media_youtube_load', settings);
  50. // Merge YouTube options (such as autoplay) into the source URL.
  51. var query = $.param(settings.options);
  52. if (query) {
  53. src += '?' + query;
  54. }
  55. // Set up the iframe with its contents and add it to the page.
  56. video
  57. .attr('id', settings.id)
  58. .attr('width', settings.width)
  59. .attr('height', settings.height)
  60. .attr('src', src);
  61. videoWrapper.html(video);
  62. // Bind a resize event to handle fluid layouts.
  63. $(window).bind('resize', Drupal.media_youtube.resizeEmbeds);
  64. // For some reason Chrome does not properly size the container around the
  65. // embed and it will just render the embed at full size unless we set this
  66. // timeout.
  67. if (!$('.lightbox-stack').length) {
  68. setTimeout(Drupal.media_youtube.resizeEmbeds, 1);
  69. }
  70. };
  71. Drupal.media_youtube.resizeEmbeds = function () {
  72. $('.media-youtube-preview-wrapper').each(Drupal.media_youtube.resizeEmbed);
  73. };
  74. Drupal.media_youtube.resizeEmbed = function () {
  75. var context = $(this).parent();
  76. var video = $(this).children(':first-child');
  77. var hw = Drupal.settings.media_youtube[$(this).attr('id')].hw;
  78. // Change the height of the wrapper that was given a fixed height by the
  79. // YouTube theming function.
  80. $(this)
  81. .height(context.width() * hw)
  82. .width(context.width());
  83. // Change the attributes on the embed to match the new size.
  84. video
  85. .height(context.width() * hw)
  86. .width(context.width());
  87. };
  88. })(jQuery);