Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. angular
  2. .module( "filters" )
  3. .filter( "youtubeFilter", [ "$filter", "$sce",
  4. function ( $filter, $sce )
  5. {
  6. "use strict";
  7.  
  8. return function( message )
  9. {
  10. var re = /https?://(?:[0-9A-Z-]+.)?(?:youtu.be/|youtube(?:-nocookie)?.comS*[^ws-])([w-]{11})(?=[^w-]|$)(?![?=&+%w.-]*(?:['"][^<>]*>|</a>))[?=&+%w.-]*/ig;
  11.  
  12. message = message.replace(re, "<div fitvids><iframe width='640' height='360' src='//www.youtube.com/embed/$1' frameborder='0' allowfullscreen></iframe></div>");
  13.  
  14. // message = "";
  15.  
  16. return $sce.trustAsHtml( message );
  17. } ;
  18. }
  19. ] );
  20.  
  21. angular.module('fitVids', []).directive('fitVids', [function() {
  22. 'use strict';
  23.  
  24. if (!document.getElementById('fit-vids-style')) {
  25. var div = document.createElement('div');
  26. var ref = document.getElementsByTagName('base')[0] || document.getElementsByTagName('script')[0];
  27. var cssStyles = '&shy;<style>.fluid-width-video-wrapper{width:100%;position:relative;padding:0;}.fluid-width-video-wrapper iframe,.fluid-width-video-wrapper object,.fluid-width-video-wrapper embed {position:absolute;top:0;left:0;width:100%;height:100%;}</style>';
  28. div.className = 'fit-vids-style';
  29. div.id = 'fit-vids-style';
  30. div.style.display = 'none';
  31. div.innerHTML = cssStyles;
  32. ref.parentNode.insertBefore(div, ref);
  33. }
  34.  
  35. return {
  36. restrict: 'A',
  37. link: function (scope, element, attr) {
  38.  
  39. var selectors = [
  40. "iframe[src*='player.vimeo.com']",
  41. "iframe[src*='youtube.com']",
  42. "iframe[src*='youtube-nocookie.com']",
  43. "iframe[src*='kickstarter.com'][src*='video.html']",
  44. "object",
  45. "embed"
  46. ];
  47.  
  48. var videos;
  49.  
  50. if (attr.customSelector) {
  51. selectors.push(attr.customSelector);
  52. }
  53.  
  54. videos = element[0].querySelectorAll(selectors.join(','));
  55.  
  56. angular.forEach(videos, function (item) {
  57.  
  58. var $item = angular.element(item);
  59. var height, width, aspectRatio;
  60.  
  61. if (item.tagName.toLowerCase() === 'embed' &&
  62. ($item.parent().tagName === 'object' && $item.parent().length) ||
  63. $item.parent().hasClass('.fluid-width-video-wrapper')) {
  64. return;
  65. }
  66.  
  67. height = (item.tagName.toLowerCase() === 'object' || $item.attr('height')) ? parseInt($item.attr('height'), 10) : $item.height();
  68. width = !isNaN(parseInt($item.attr('width'), 10)) ? parseInt($item.attr('width'), 10) : $item.width();
  69. aspectRatio = height / width;
  70.  
  71. if (!$item.attr('id')) {
  72. var videoID = 'fitvid' + Math.floor(Math.random()*999999);
  73. $item.attr('id', videoID);
  74. }
  75.  
  76. $item.wrap('<div class="fluid-width-video-wrapper" />').parent().css('padding-top', (aspectRatio * 100) + "%");
  77. $item.removeAttr('height').removeAttr('width');
  78.  
  79. });
  80.  
  81. }
  82. };
  83.  
  84. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement