misago-onebox.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. $(function() {
  2. YouTubeOnebox = function(href, movie_id, startfrom) {
  3. var _this = this;
  4. if (startfrom !== undefined) {
  5. player_url = '//www.youtube.com/embed/' + movie_id + '?start=' + startfrom + '&autoplay=1';
  6. } else {
  7. player_url = '//www.youtube.com/embed/' + movie_id + '?autoplay=1';
  8. }
  9. this.$element = $('<div class="post-media"><div class="media-border youtube-player" data-movieid="' + movie_id + '"><div class="media-thumbnail" style="background-image: url(\'//img.youtube.com/vi/' + movie_id + '/0.jpg\');"><a href="' + href + '" class="play-link" data-playerurl="' + player_url + '"><i class="fa fa-youtube-play"></i><strong>' + lang_play_media + '</strong></a></div></div></div>');
  10. this.$element.find('.play-link').click(function() {
  11. $(this).parent().replaceWith('<iframe width="853" height="480" src="' + $(this).data('playerurl') + '" frameborder="0" allowfullscreen></iframe>');
  12. return false;
  13. });
  14. // Fetch title, author name and thumbnail
  15. $.getJSON("//gdata.youtube.com/feeds/api/videos/" + movie_id + "?v=2&alt=json",
  16. function(data, textStatus, jqXHR) {
  17. // Movie details
  18. var movie_title = data.entry.title.$t;
  19. var movie_author = data.entry.author['0'].name.$t
  20. _this.$element.find('.play-link').addClass('movie-title');
  21. _this.$element.find('.play-link strong').text(movie_title);
  22. _this.$element.find('.play-link').append(lang_media_author.replace('{author}', movie_author));
  23. // Movie thumbnail
  24. var thumb = {height: 90, url: '//img.youtube.com/vi/' + movie_id + '/0.jpg'};
  25. $(data.entry['media$group']['media$thumbnail']).each(function(key, yt_image) {
  26. if (thumb.height < yt_image.height) {
  27. thumb = yt_image;
  28. }
  29. });
  30. _this.$element.find('.media-thumbnail').css('background-image', "url('" + thumb.url + "')");
  31. });
  32. this.activate = function($element) {
  33. $element.replaceWith(this.$element.clone());
  34. }
  35. }
  36. MisagoOnebox = function() {
  37. this.boxes = {};
  38. var _this = this;
  39. this.youtube = function(href) {
  40. // Youtube link
  41. var re = /watch\?v=((\w|-)+)/;
  42. if (re.test(href)) {
  43. var media_url = href.match(re);
  44. return new YouTubeOnebox(href, media_url[1]);
  45. }
  46. // Youtube feature=embed
  47. var re = /watch\?feature=player_embedded&v=((\w|-)+)/;
  48. if (re.test(href)) {
  49. var media_url = href.match(re);
  50. return new YouTubeOnebox(href, media_url[1]);
  51. }
  52. // Youtube embed with start time
  53. var re = /youtu.be\/((\w|-)+)\?t=([A-Za-z0-9]+)/;
  54. if (re.test(href)) {
  55. var media_url = href.match(re);
  56. var media_minutes = media_url[2].match(/([0-9]+)m/);
  57. var media_seconds = media_url[2].match(/([0-9]+)s/);
  58. media_url[2] = 0;
  59. if (media_minutes) { media_url[2] += (media_minutes[1] - 0) * 60; }
  60. if (media_seconds) { media_url[2] += (media_seconds[1] - 0); }
  61. return new YouTubeOnebox(href, media_url[1], media_url[2]);
  62. }
  63. // Youtube embed
  64. var re = /youtu.be\/(([A-Za-z0-9]|_|-)+)/;
  65. if (re.test(href)) {
  66. var media_url = href.match(re);
  67. return new YouTubeOnebox(href, media_url[1]);
  68. }
  69. return false;
  70. }
  71. this.oneboxes = [this.youtube];
  72. this.activate = function($element) {
  73. $element.find('a').each(function() {
  74. var href = $.trim($(this).text());
  75. if (_this.boxes[href] === undefined) {
  76. $.each(_this.oneboxes, function(i, onebox) {
  77. _this.boxes[href] = _this.youtube(href);
  78. if (_this.boxes[href] != false) {
  79. return true;
  80. }
  81. })
  82. }
  83. if (_this.boxes[href] !== false) {
  84. _this.boxes[href].activate($(this));
  85. }
  86. });
  87. }
  88. };
  89. Misago.Onebox = new MisagoOnebox();
  90. });