lg-video.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*! lightgallery - v1.2.21 - 2016-06-28
  2. * http://sachinchoolur.github.io/lightGallery/
  3. * Copyright (c) 2016 Sachin N; Licensed Apache 2.0 */
  4. (function($, window, document, undefined) {
  5. 'use strict';
  6. var defaults = {
  7. videoMaxWidth: '855px',
  8. youtubePlayerParams: false,
  9. vimeoPlayerParams: false,
  10. dailymotionPlayerParams: false,
  11. vkPlayerParams: false,
  12. videojs: false,
  13. videojsOptions: {}
  14. };
  15. var Video = function(element) {
  16. this.core = $(element).data('lightGallery');
  17. this.$el = $(element);
  18. this.core.s = $.extend({}, defaults, this.core.s);
  19. this.videoLoaded = false;
  20. this.init();
  21. return this;
  22. };
  23. Video.prototype.init = function() {
  24. var _this = this;
  25. // Event triggered when video url found without poster
  26. _this.core.$el.on('hasVideo.lg.tm', function(event, index, src, html) {
  27. _this.core.$slide.eq(index).find('.lg-video').append(_this.loadVideo(src, 'lg-object', true, index, html));
  28. if (html) {
  29. if (_this.core.s.videojs) {
  30. try {
  31. videojs(_this.core.$slide.eq(index).find('.lg-html5').get(0), _this.core.s.videojsOptions, function() {
  32. if (!_this.videoLoaded) {
  33. this.play();
  34. }
  35. });
  36. } catch (e) {
  37. console.error('Make sure you have included videojs');
  38. }
  39. } else {
  40. _this.core.$slide.eq(index).find('.lg-html5').get(0).play();
  41. }
  42. }
  43. });
  44. // Set max width for video
  45. _this.core.$el.on('onAferAppendSlide.lg.tm', function(event, index) {
  46. _this.core.$slide.eq(index).find('.lg-video-cont').css('max-width', _this.core.s.videoMaxWidth);
  47. _this.videoLoaded = true;
  48. });
  49. var loadOnClick = function($el) {
  50. // check slide has poster
  51. if ($el.find('.lg-object').hasClass('lg-has-poster') && $el.find('.lg-object').is(':visible')) {
  52. // check already video element present
  53. if (!$el.hasClass('lg-has-video')) {
  54. $el.addClass('lg-video-playing lg-has-video');
  55. var _src;
  56. var _html;
  57. var _loadVideo = function(_src, _html) {
  58. $el.find('.lg-video').append(_this.loadVideo(_src, '', false, _this.core.index, _html));
  59. if (_html) {
  60. if (_this.core.s.videojs) {
  61. try {
  62. videojs(_this.core.$slide.eq(_this.core.index).find('.lg-html5').get(0), _this.core.s.videojsOptions, function() {
  63. this.play();
  64. });
  65. } catch (e) {
  66. console.error('Make sure you have included videojs');
  67. }
  68. } else {
  69. _this.core.$slide.eq(_this.core.index).find('.lg-html5').get(0).play();
  70. }
  71. }
  72. };
  73. if (_this.core.s.dynamic) {
  74. _src = _this.core.s.dynamicEl[_this.core.index].src;
  75. _html = _this.core.s.dynamicEl[_this.core.index].html;
  76. _loadVideo(_src, _html);
  77. } else {
  78. _src = _this.core.$items.eq(_this.core.index).attr('href') || _this.core.$items.eq(_this.core.index).attr('data-src');
  79. _html = _this.core.$items.eq(_this.core.index).attr('data-html');
  80. _loadVideo(_src, _html);
  81. }
  82. var $tempImg = $el.find('.lg-object');
  83. $el.find('.lg-video').append($tempImg);
  84. // @todo loading icon for html5 videos also
  85. // for showing the loading indicator while loading video
  86. if (!$el.find('.lg-video-object').hasClass('lg-html5')) {
  87. $el.removeClass('lg-complete');
  88. $el.find('.lg-video-object').on('load.lg error.lg', function() {
  89. $el.addClass('lg-complete');
  90. });
  91. }
  92. } else {
  93. var youtubePlayer = $el.find('.lg-youtube').get(0);
  94. var vimeoPlayer = $el.find('.lg-vimeo').get(0);
  95. var dailymotionPlayer = $el.find('.lg-dailymotion').get(0);
  96. var html5Player = $el.find('.lg-html5').get(0);
  97. if (youtubePlayer) {
  98. youtubePlayer.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
  99. } else if (vimeoPlayer) {
  100. try {
  101. $f(vimeoPlayer).api('play');
  102. } catch (e) {
  103. console.error('Make sure you have included froogaloop2 js');
  104. }
  105. } else if (dailymotionPlayer) {
  106. dailymotionPlayer.contentWindow.postMessage('play', '*');
  107. } else if (html5Player) {
  108. if (_this.core.s.videojs) {
  109. try {
  110. videojs(html5Player).play();
  111. } catch (e) {
  112. console.error('Make sure you have included videojs');
  113. }
  114. } else {
  115. html5Player.play();
  116. }
  117. }
  118. $el.addClass('lg-video-playing');
  119. }
  120. }
  121. };
  122. if (_this.core.doCss() && _this.core.$items.length > 1 && ((_this.core.s.enableSwipe && _this.core.isTouch) || (_this.core.s.enableDrag && !_this.core.isTouch))) {
  123. _this.core.$el.on('onSlideClick.lg.tm', function() {
  124. var $el = _this.core.$slide.eq(_this.core.index);
  125. loadOnClick($el);
  126. });
  127. } else {
  128. // For IE 9 and bellow
  129. _this.core.$slide.on('click.lg', function() {
  130. loadOnClick($(this));
  131. });
  132. }
  133. _this.core.$el.on('onBeforeSlide.lg.tm', function(event, prevIndex, index) {
  134. var $videoSlide = _this.core.$slide.eq(prevIndex);
  135. var youtubePlayer = $videoSlide.find('.lg-youtube').get(0);
  136. var vimeoPlayer = $videoSlide.find('.lg-vimeo').get(0);
  137. var dailymotionPlayer = $videoSlide.find('.lg-dailymotion').get(0);
  138. var vkPlayer = $videoSlide.find('.lg-vk').get(0);
  139. var html5Player = $videoSlide.find('.lg-html5').get(0);
  140. if (youtubePlayer) {
  141. youtubePlayer.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  142. } else if (vimeoPlayer) {
  143. try {
  144. $f(vimeoPlayer).api('pause');
  145. } catch (e) {
  146. console.error('Make sure you have included froogaloop2 js');
  147. }
  148. } else if (dailymotionPlayer) {
  149. dailymotionPlayer.contentWindow.postMessage('pause', '*');
  150. } else if (html5Player) {
  151. if (_this.core.s.videojs) {
  152. try {
  153. videojs(html5Player).pause();
  154. } catch (e) {
  155. console.error('Make sure you have included videojs');
  156. }
  157. } else {
  158. html5Player.pause();
  159. }
  160. } if (vkPlayer) {
  161. $(vkPlayer).attr('src', $(vkPlayer).attr('src').replace('&autoplay', '&noplay'));
  162. }
  163. var _src;
  164. if (_this.core.s.dynamic) {
  165. _src = _this.core.s.dynamicEl[index].src;
  166. } else {
  167. _src = _this.core.$items.eq(index).attr('href') || _this.core.$items.eq(index).attr('data-src');
  168. }
  169. var _isVideo = _this.core.isVideo(_src, index) || {};
  170. if (_isVideo.youtube || _isVideo.vimeo || _isVideo.dailymotion || _isVideo.vk) {
  171. _this.core.$outer.addClass('lg-hide-download');
  172. }
  173. //$videoSlide.addClass('lg-complete');
  174. });
  175. _this.core.$el.on('onAfterSlide.lg.tm', function(event, prevIndex) {
  176. _this.core.$slide.eq(prevIndex).removeClass('lg-video-playing');
  177. });
  178. };
  179. Video.prototype.loadVideo = function(src, addClass, noposter, index, html) {
  180. var video = '';
  181. var autoplay = 1;
  182. var a = '';
  183. var isVideo = this.core.isVideo(src, index) || {};
  184. // Enable autoplay for first video if poster doesn't exist
  185. if (noposter) {
  186. if (this.videoLoaded) {
  187. autoplay = 0;
  188. } else {
  189. autoplay = 1;
  190. }
  191. }
  192. if (isVideo.youtube) {
  193. a = '?wmode=opaque&autoplay=' + autoplay + '&enablejsapi=1';
  194. if (this.core.s.youtubePlayerParams) {
  195. a = a + '&' + $.param(this.core.s.youtubePlayerParams);
  196. }
  197. video = '<iframe class="lg-video-object lg-youtube ' + addClass + '" width="560" height="315" src="//www.youtube.com/embed/' + isVideo.youtube[1] + a + '" frameborder="0" allowfullscreen></iframe>';
  198. } else if (isVideo.vimeo) {
  199. a = '?autoplay=' + autoplay + '&api=1';
  200. if (this.core.s.vimeoPlayerParams) {
  201. a = a + '&' + $.param(this.core.s.vimeoPlayerParams);
  202. }
  203. video = '<iframe class="lg-video-object lg-vimeo ' + addClass + '" width="560" height="315" src="//player.vimeo.com/video/' + isVideo.vimeo[1] + a + '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
  204. } else if (isVideo.dailymotion) {
  205. a = '?wmode=opaque&autoplay=' + autoplay + '&api=postMessage';
  206. if (this.core.s.dailymotionPlayerParams) {
  207. a = a + '&' + $.param(this.core.s.dailymotionPlayerParams);
  208. }
  209. video = '<iframe class="lg-video-object lg-dailymotion ' + addClass + '" width="560" height="315" src="//www.dailymotion.com/embed/video/' + isVideo.dailymotion[1] + a + '" frameborder="0" allowfullscreen></iframe>';
  210. } else if (isVideo.html5) {
  211. var fL = html.substring(0, 1);
  212. if (fL === '.' || fL === '#') {
  213. html = $(html).html();
  214. }
  215. video = html;
  216. } else if (isVideo.vk) {
  217. a = '&autoplay=' + autoplay;
  218. if (this.core.s.vkPlayerParams) {
  219. a = a + '&' + $.param(this.core.s.vkPlayerParams);
  220. }
  221. video = '<iframe class="lg-video-object lg-vk ' + addClass + '" width="560" height="315" src="http://vk.com/video_ext.php?' + isVideo.vk[1] + a + '" frameborder="0" allowfullscreen></iframe>';
  222. }
  223. return video;
  224. };
  225. Video.prototype.destroy = function() {
  226. this.videoLoaded = false;
  227. };
  228. $.fn.lightGallery.modules.video = Video;
  229. })(jQuery, window, document);