lg-thumbnail.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. thumbnail: true,
  8. animateThumb: true,
  9. currentPagerPosition: 'middle',
  10. thumbWidth: 100,
  11. thumbContHeight: 100,
  12. thumbMargin: 5,
  13. exThumbImage: false,
  14. showThumbByDefault: true,
  15. toogleThumb: true,
  16. pullCaptionUp: true,
  17. enableThumbDrag: true,
  18. enableThumbSwipe: true,
  19. swipeThreshold: 50,
  20. loadYoutubeThumbnail: true,
  21. youtubeThumbSize: 1,
  22. loadVimeoThumbnail: true,
  23. vimeoThumbSize: 'thumbnail_small',
  24. loadDailymotionThumbnail: true
  25. };
  26. var Thumbnail = function(element) {
  27. // get lightGallery core plugin data
  28. this.core = $(element).data('lightGallery');
  29. // extend module default settings with lightGallery core settings
  30. this.core.s = $.extend({}, defaults, this.core.s);
  31. this.$el = $(element);
  32. this.$thumbOuter = null;
  33. this.thumbOuterWidth = 0;
  34. this.thumbTotalWidth = (this.core.$items.length * (this.core.s.thumbWidth + this.core.s.thumbMargin));
  35. this.thumbIndex = this.core.index;
  36. // Thumbnail animation value
  37. this.left = 0;
  38. this.init();
  39. return this;
  40. };
  41. Thumbnail.prototype.init = function() {
  42. var _this = this;
  43. if (this.core.s.thumbnail && this.core.$items.length > 1) {
  44. if (this.core.s.showThumbByDefault) {
  45. setTimeout(function(){
  46. _this.core.$outer.addClass('lg-thumb-open');
  47. }, 700);
  48. }
  49. if (this.core.s.pullCaptionUp) {
  50. this.core.$outer.addClass('lg-pull-caption-up');
  51. }
  52. this.build();
  53. if (this.core.s.animateThumb) {
  54. if (this.core.s.enableThumbDrag && !this.core.isTouch && this.core.doCss()) {
  55. this.enableThumbDrag();
  56. }
  57. if (this.core.s.enableThumbSwipe && this.core.isTouch && this.core.doCss()) {
  58. this.enableThumbSwipe();
  59. }
  60. this.thumbClickable = false;
  61. } else {
  62. this.thumbClickable = true;
  63. }
  64. this.toogle();
  65. this.thumbkeyPress();
  66. }
  67. };
  68. Thumbnail.prototype.build = function() {
  69. var _this = this;
  70. var thumbList = '';
  71. var vimeoErrorThumbSize = '';
  72. var $thumb;
  73. var html = '<div class="lg-thumb-outer">' +
  74. '<div class="lg-thumb group">' +
  75. '</div>' +
  76. '</div>';
  77. switch (this.core.s.vimeoThumbSize) {
  78. case 'thumbnail_large':
  79. vimeoErrorThumbSize = '640';
  80. break;
  81. case 'thumbnail_medium':
  82. vimeoErrorThumbSize = '200x150';
  83. break;
  84. case 'thumbnail_small':
  85. vimeoErrorThumbSize = '100x75';
  86. }
  87. _this.core.$outer.addClass('lg-has-thumb');
  88. _this.core.$outer.find('.lg').append(html);
  89. _this.$thumbOuter = _this.core.$outer.find('.lg-thumb-outer');
  90. _this.thumbOuterWidth = _this.$thumbOuter.width();
  91. if (_this.core.s.animateThumb) {
  92. _this.core.$outer.find('.lg-thumb').css({
  93. width: _this.thumbTotalWidth + 'px',
  94. position: 'relative'
  95. });
  96. }
  97. if (this.core.s.animateThumb) {
  98. _this.$thumbOuter.css('height', _this.core.s.thumbContHeight + 'px');
  99. }
  100. function getThumb(src, thumb, index) {
  101. var isVideo = _this.core.isVideo(src, index) || {};
  102. var thumbImg;
  103. var vimeoId = '';
  104. if (isVideo.youtube || isVideo.vimeo || isVideo.dailymotion) {
  105. if (isVideo.youtube) {
  106. if (_this.core.s.loadYoutubeThumbnail) {
  107. thumbImg = '//img.youtube.com/vi/' + isVideo.youtube[1] + '/' + _this.core.s.youtubeThumbSize + '.jpg';
  108. } else {
  109. thumbImg = thumb;
  110. }
  111. } else if (isVideo.vimeo) {
  112. if (_this.core.s.loadVimeoThumbnail) {
  113. thumbImg = '//i.vimeocdn.com/video/error_' + vimeoErrorThumbSize + '.jpg';
  114. vimeoId = isVideo.vimeo[1];
  115. } else {
  116. thumbImg = thumb;
  117. }
  118. } else if (isVideo.dailymotion) {
  119. if (_this.core.s.loadDailymotionThumbnail) {
  120. thumbImg = '//www.dailymotion.com/thumbnail/video/' + isVideo.dailymotion[1];
  121. } else {
  122. thumbImg = thumb;
  123. }
  124. }
  125. } else {
  126. thumbImg = thumb;
  127. }
  128. thumbList += '<div data-vimeo-id="' + vimeoId + '" class="lg-thumb-item" style="width:' + _this.core.s.thumbWidth + 'px; margin-right: ' + _this.core.s.thumbMargin + 'px"><img src="' + thumbImg + '" /></div>';
  129. vimeoId = '';
  130. }
  131. if (_this.core.s.dynamic) {
  132. for (var i = 0; i < _this.core.s.dynamicEl.length; i++) {
  133. getThumb(_this.core.s.dynamicEl[i].src, _this.core.s.dynamicEl[i].thumb, i);
  134. }
  135. } else {
  136. _this.core.$items.each(function(i) {
  137. if (!_this.core.s.exThumbImage) {
  138. getThumb($(this).attr('href') || $(this).attr('data-src'), $(this).find('img').attr('src'), i);
  139. } else {
  140. getThumb($(this).attr('href') || $(this).attr('data-src'), $(this).attr(_this.core.s.exThumbImage), i);
  141. }
  142. });
  143. }
  144. _this.core.$outer.find('.lg-thumb').html(thumbList);
  145. $thumb = _this.core.$outer.find('.lg-thumb-item');
  146. // Load vimeo thumbnails
  147. $thumb.each(function() {
  148. var $this = $(this);
  149. var vimeoVideoId = $this.attr('data-vimeo-id');
  150. if (vimeoVideoId) {
  151. $.getJSON('//www.vimeo.com/api/v2/video/' + vimeoVideoId + '.json?callback=?', {
  152. format: 'json'
  153. }, function(data) {
  154. $this.find('img').attr('src', data[0][_this.core.s.vimeoThumbSize]);
  155. });
  156. }
  157. });
  158. // manage active class for thumbnail
  159. $thumb.eq(_this.core.index).addClass('active');
  160. _this.core.$el.on('onBeforeSlide.lg.tm', function() {
  161. $thumb.removeClass('active');
  162. $thumb.eq(_this.core.index).addClass('active');
  163. });
  164. $thumb.on('click.lg touchend.lg', function() {
  165. var _$this = $(this);
  166. setTimeout(function() {
  167. // In IE9 and bellow touch does not support
  168. // Go to slide if browser does not support css transitions
  169. if ((_this.thumbClickable && !_this.core.lgBusy) || !_this.core.doCss()) {
  170. _this.core.index = _$this.index();
  171. _this.core.slide(_this.core.index, false, true);
  172. }
  173. }, 50);
  174. });
  175. _this.core.$el.on('onBeforeSlide.lg.tm', function() {
  176. _this.animateThumb(_this.core.index);
  177. });
  178. $(window).on('resize.lg.thumb orientationchange.lg.thumb', function() {
  179. setTimeout(function() {
  180. _this.animateThumb(_this.core.index);
  181. _this.thumbOuterWidth = _this.$thumbOuter.width();
  182. }, 200);
  183. });
  184. };
  185. Thumbnail.prototype.setTranslate = function(value) {
  186. // jQuery supports Automatic CSS prefixing since jQuery 1.8.0
  187. this.core.$outer.find('.lg-thumb').css({
  188. transform: 'translate3d(-' + (value) + 'px, 0px, 0px)'
  189. });
  190. };
  191. Thumbnail.prototype.animateThumb = function(index) {
  192. var $thumb = this.core.$outer.find('.lg-thumb');
  193. if (this.core.s.animateThumb) {
  194. var position;
  195. switch (this.core.s.currentPagerPosition) {
  196. case 'left':
  197. position = 0;
  198. break;
  199. case 'middle':
  200. position = (this.thumbOuterWidth / 2) - (this.core.s.thumbWidth / 2);
  201. break;
  202. case 'right':
  203. position = this.thumbOuterWidth - this.core.s.thumbWidth;
  204. }
  205. this.left = ((this.core.s.thumbWidth + this.core.s.thumbMargin) * index - 1) - position;
  206. if (this.left > (this.thumbTotalWidth - this.thumbOuterWidth)) {
  207. this.left = this.thumbTotalWidth - this.thumbOuterWidth;
  208. }
  209. if (this.left < 0) {
  210. this.left = 0;
  211. }
  212. if (this.core.lGalleryOn) {
  213. if (!$thumb.hasClass('on')) {
  214. this.core.$outer.find('.lg-thumb').css('transition-duration', this.core.s.speed + 'ms');
  215. }
  216. if (!this.core.doCss()) {
  217. $thumb.animate({
  218. left: -this.left + 'px'
  219. }, this.core.s.speed);
  220. }
  221. } else {
  222. if (!this.core.doCss()) {
  223. $thumb.css('left', -this.left + 'px');
  224. }
  225. }
  226. this.setTranslate(this.left);
  227. }
  228. };
  229. // Enable thumbnail dragging and swiping
  230. Thumbnail.prototype.enableThumbDrag = function() {
  231. var _this = this;
  232. var startCoords = 0;
  233. var endCoords = 0;
  234. var isDraging = false;
  235. var isMoved = false;
  236. var tempLeft = 0;
  237. _this.$thumbOuter.addClass('lg-grab');
  238. _this.core.$outer.find('.lg-thumb').on('mousedown.lg.thumb', function(e) {
  239. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  240. // execute only on .lg-object
  241. e.preventDefault();
  242. startCoords = e.pageX;
  243. isDraging = true;
  244. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  245. _this.core.$outer.scrollLeft += 1;
  246. _this.core.$outer.scrollLeft -= 1;
  247. // *
  248. _this.thumbClickable = false;
  249. _this.$thumbOuter.removeClass('lg-grab').addClass('lg-grabbing');
  250. }
  251. });
  252. $(window).on('mousemove.lg.thumb', function(e) {
  253. if (isDraging) {
  254. tempLeft = _this.left;
  255. isMoved = true;
  256. endCoords = e.pageX;
  257. _this.$thumbOuter.addClass('lg-dragging');
  258. tempLeft = tempLeft - (endCoords - startCoords);
  259. if (tempLeft > (_this.thumbTotalWidth - _this.thumbOuterWidth)) {
  260. tempLeft = _this.thumbTotalWidth - _this.thumbOuterWidth;
  261. }
  262. if (tempLeft < 0) {
  263. tempLeft = 0;
  264. }
  265. // move current slide
  266. _this.setTranslate(tempLeft);
  267. }
  268. });
  269. $(window).on('mouseup.lg.thumb', function() {
  270. if (isMoved) {
  271. isMoved = false;
  272. _this.$thumbOuter.removeClass('lg-dragging');
  273. _this.left = tempLeft;
  274. if (Math.abs(endCoords - startCoords) < _this.core.s.swipeThreshold) {
  275. _this.thumbClickable = true;
  276. }
  277. } else {
  278. _this.thumbClickable = true;
  279. }
  280. if (isDraging) {
  281. isDraging = false;
  282. _this.$thumbOuter.removeClass('lg-grabbing').addClass('lg-grab');
  283. }
  284. });
  285. };
  286. Thumbnail.prototype.enableThumbSwipe = function() {
  287. var _this = this;
  288. var startCoords = 0;
  289. var endCoords = 0;
  290. var isMoved = false;
  291. var tempLeft = 0;
  292. _this.core.$outer.find('.lg-thumb').on('touchstart.lg', function(e) {
  293. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  294. e.preventDefault();
  295. startCoords = e.originalEvent.targetTouches[0].pageX;
  296. _this.thumbClickable = false;
  297. }
  298. });
  299. _this.core.$outer.find('.lg-thumb').on('touchmove.lg', function(e) {
  300. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  301. e.preventDefault();
  302. endCoords = e.originalEvent.targetTouches[0].pageX;
  303. isMoved = true;
  304. _this.$thumbOuter.addClass('lg-dragging');
  305. tempLeft = _this.left;
  306. tempLeft = tempLeft - (endCoords - startCoords);
  307. if (tempLeft > (_this.thumbTotalWidth - _this.thumbOuterWidth)) {
  308. tempLeft = _this.thumbTotalWidth - _this.thumbOuterWidth;
  309. }
  310. if (tempLeft < 0) {
  311. tempLeft = 0;
  312. }
  313. // move current slide
  314. _this.setTranslate(tempLeft);
  315. }
  316. });
  317. _this.core.$outer.find('.lg-thumb').on('touchend.lg', function() {
  318. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  319. if (isMoved) {
  320. isMoved = false;
  321. _this.$thumbOuter.removeClass('lg-dragging');
  322. if (Math.abs(endCoords - startCoords) < _this.core.s.swipeThreshold) {
  323. _this.thumbClickable = true;
  324. }
  325. _this.left = tempLeft;
  326. } else {
  327. _this.thumbClickable = true;
  328. }
  329. } else {
  330. _this.thumbClickable = true;
  331. }
  332. });
  333. };
  334. Thumbnail.prototype.toogle = function() {
  335. var _this = this;
  336. if (_this.core.s.toogleThumb) {
  337. _this.core.$outer.addClass('lg-can-toggle');
  338. _this.$thumbOuter.append('<span class="lg-toogle-thumb lg-icon"></span>');
  339. _this.core.$outer.find('.lg-toogle-thumb').on('click.lg', function() {
  340. _this.core.$outer.toggleClass('lg-thumb-open');
  341. });
  342. }
  343. };
  344. Thumbnail.prototype.thumbkeyPress = function() {
  345. var _this = this;
  346. $(window).on('keydown.lg.thumb', function(e) {
  347. if (e.keyCode === 38) {
  348. e.preventDefault();
  349. _this.core.$outer.addClass('lg-thumb-open');
  350. } else if (e.keyCode === 40) {
  351. e.preventDefault();
  352. _this.core.$outer.removeClass('lg-thumb-open');
  353. }
  354. });
  355. };
  356. Thumbnail.prototype.destroy = function() {
  357. if (this.core.s.thumbnail && this.core.$items.length > 1) {
  358. $(window).off('resize.lg.thumb orientationchange.lg.thumb keydown.lg.thumb');
  359. this.$thumbOuter.remove();
  360. this.core.$outer.removeClass('lg-has-thumb');
  361. }
  362. };
  363. $.fn.lightGallery.modules.Thumbnail = Thumbnail;
  364. })(jQuery, window, document);