media_vimeo.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file media_vimeo/js/media_vimeo.js
  3. */
  4. (function ($) {
  5. Drupal.media_vimeo = {};
  6. Drupal.behaviors.media_vimeo = {
  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-vimeo-preview-wrapper').each(Drupal.media_vimeo.needFlash);
  21. }
  22. // Replace all object tags with iframes.
  23. if (Drupal.settings && Drupal.settings.media_vimeo) {
  24. for (video in Drupal.settings.media_vimeo) {
  25. Drupal.media_vimeo.insertEmbed(video);
  26. }
  27. }
  28. }
  29. };
  30. Drupal.media_vimeo.needFlash = function () {
  31. var id = $(this).attr('id');
  32. var wrapper = $('.media-vimeo-preview-wrapper');
  33. var hw = Drupal.settings.media_vimeo[id].height / Drupal.settings.media_vimeo[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_vimeo.insertEmbed = function (embed_id) {
  38. var videoWrapper = $('#' + embed_id + '.media-vimeo-preview-wrapper');
  39. var settings = Drupal.settings.media_vimeo[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 Vimeo's iframe. This isn't done by the
  43. // theme function because Vimeo doesn't have a no-JS or no-Flash fallback.
  44. var video = $('<iframe class="vimeo-player" type="text/html" frameborder="0"></iframe>');
  45. var src = 'http://player.vimeo.com/video/' + settings.video_id;
  46. // Allow other modules to modify the video settings.
  47. settings.options = settings.options || {};
  48. $(window).trigger('media_vimeo_load', settings);
  49. // Merge Vimeo options (such as autoplay) into the source URL.
  50. var query = $.param(settings.options);
  51. if (query) {
  52. src += '?' + query;
  53. }
  54. // Set up the iframe with its contents and add it to the page.
  55. video
  56. .attr('id', settings.id)
  57. .attr('width', settings.width)
  58. .attr('height', settings.height)
  59. .attr('src', src);
  60. videoWrapper.html(video);
  61. // Bind a resize event to handle fluid layouts.
  62. $(window).bind('resize', Drupal.media_vimeo.resizeEmbeds);
  63. // For some reason Chrome does not properly size the container around the
  64. // embed and it will just render the embed at full size unless we set this
  65. // timeout.
  66. if (!$('.lightbox-stack').length) {
  67. setTimeout(Drupal.media_vimeo.resizeEmbeds, 1);
  68. }
  69. };
  70. Drupal.media_vimeo.resizeEmbeds = function () {
  71. $('.media-vimeo-preview-wrapper').each(Drupal.media_vimeo.resizeEmbed);
  72. };
  73. Drupal.media_vimeo.resizeEmbed = function () {
  74. var context = $(this).parent();
  75. var video = $(this).children(':first-child');
  76. var hw = Drupal.settings.media_vimeo[$(this).attr('id')].hw;
  77. // Change the height of the wrapper that was given a fixed height by the
  78. // Vimeo theming function.
  79. $(this)
  80. .height(context.width() * hw)
  81. .width(context.width());
  82. // Change the attributes on the embed to match the new size.
  83. video
  84. .height(context.width() * hw)
  85. .width(context.width());
  86. };
  87. })(jQuery);