lg-thumbnail.js 15 KB

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