lg-thumbnail.es5.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*!
  2. * lightgallery | 2.4.0-beta.0 | December 12th 2021
  3. * http://www.lightgalleryjs.com/
  4. * Copyright (c) 2020 Sachin Neravath;
  5. * @license GPLv3
  6. */
  7. /*! *****************************************************************************
  8. Copyright (c) Microsoft Corporation.
  9. Permission to use, copy, modify, and/or distribute this software for any
  10. purpose with or without fee is hereby granted.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  12. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  13. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  14. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  16. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. PERFORMANCE OF THIS SOFTWARE.
  18. ***************************************************************************** */
  19. var __assign = function() {
  20. __assign = Object.assign || function __assign(t) {
  21. for (var s, i = 1, n = arguments.length; i < n; i++) {
  22. s = arguments[i];
  23. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  24. }
  25. return t;
  26. };
  27. return __assign.apply(this, arguments);
  28. };
  29. var thumbnailsSettings = {
  30. thumbnail: true,
  31. animateThumb: true,
  32. currentPagerPosition: 'middle',
  33. alignThumbnails: 'middle',
  34. thumbWidth: 100,
  35. thumbHeight: '80px',
  36. thumbMargin: 5,
  37. appendThumbnailsTo: '.lg-components',
  38. toggleThumb: false,
  39. enableThumbDrag: true,
  40. enableThumbSwipe: true,
  41. thumbnailSwipeThreshold: 10,
  42. loadYouTubeThumbnail: true,
  43. youTubeThumbSize: 1,
  44. thumbnailPluginStrings: { toggleThumbnails: 'Toggle thumbnails' },
  45. };
  46. /**
  47. * List of lightGallery events
  48. * All events should be documented here
  49. * Below interfaces are used to build the website documentations
  50. * */
  51. var lGEvents = {
  52. afterAppendSlide: 'lgAfterAppendSlide',
  53. init: 'lgInit',
  54. hasVideo: 'lgHasVideo',
  55. containerResize: 'lgContainerResize',
  56. updateSlides: 'lgUpdateSlides',
  57. afterAppendSubHtml: 'lgAfterAppendSubHtml',
  58. beforeOpen: 'lgBeforeOpen',
  59. afterOpen: 'lgAfterOpen',
  60. slideItemLoad: 'lgSlideItemLoad',
  61. beforeSlide: 'lgBeforeSlide',
  62. afterSlide: 'lgAfterSlide',
  63. posterClick: 'lgPosterClick',
  64. dragStart: 'lgDragStart',
  65. dragMove: 'lgDragMove',
  66. dragEnd: 'lgDragEnd',
  67. beforeNextSlide: 'lgBeforeNextSlide',
  68. beforePrevSlide: 'lgBeforePrevSlide',
  69. beforeClose: 'lgBeforeClose',
  70. afterClose: 'lgAfterClose',
  71. rotateLeft: 'lgRotateLeft',
  72. rotateRight: 'lgRotateRight',
  73. flipHorizontal: 'lgFlipHorizontal',
  74. flipVertical: 'lgFlipVertical',
  75. autoplay: 'lgAutoplay',
  76. autoplayStart: 'lgAutoplayStart',
  77. autoplayStop: 'lgAutoplayStop',
  78. };
  79. var Thumbnail = /** @class */ (function () {
  80. function Thumbnail(instance, $LG) {
  81. this.thumbOuterWidth = 0;
  82. this.thumbTotalWidth = 0;
  83. this.translateX = 0;
  84. this.thumbClickable = false;
  85. // get lightGallery core plugin instance
  86. this.core = instance;
  87. this.$LG = $LG;
  88. return this;
  89. }
  90. Thumbnail.prototype.init = function () {
  91. // extend module default settings with lightGallery core settings
  92. this.settings = __assign(__assign({}, thumbnailsSettings), this.core.settings);
  93. this.thumbOuterWidth = 0;
  94. this.thumbTotalWidth =
  95. this.core.galleryItems.length *
  96. (this.settings.thumbWidth + this.settings.thumbMargin);
  97. // Thumbnail animation value
  98. this.translateX = 0;
  99. this.setAnimateThumbStyles();
  100. if (!this.core.settings.allowMediaOverlap) {
  101. this.settings.toggleThumb = false;
  102. }
  103. if (this.settings.thumbnail) {
  104. this.build();
  105. if (this.settings.animateThumb) {
  106. if (this.settings.enableThumbDrag) {
  107. this.enableThumbDrag();
  108. }
  109. if (this.settings.enableThumbSwipe) {
  110. this.enableThumbSwipe();
  111. }
  112. this.thumbClickable = false;
  113. }
  114. else {
  115. this.thumbClickable = true;
  116. }
  117. this.toggleThumbBar();
  118. this.thumbKeyPress();
  119. }
  120. };
  121. Thumbnail.prototype.build = function () {
  122. var _this = this;
  123. this.setThumbMarkup();
  124. this.manageActiveClassOnSlideChange();
  125. this.$lgThumb.first().on('click.lg touchend.lg', function (e) {
  126. var $target = _this.$LG(e.target);
  127. if (!$target.hasAttribute('data-lg-item-id')) {
  128. return;
  129. }
  130. setTimeout(function () {
  131. // In IE9 and bellow touch does not support
  132. // Go to slide if browser does not support css transitions
  133. if (_this.thumbClickable && !_this.core.lgBusy) {
  134. var index = parseInt($target.attr('data-lg-item-id'));
  135. _this.core.slide(index, false, true, false);
  136. }
  137. }, 50);
  138. });
  139. this.core.LGel.on(lGEvents.beforeSlide + ".thumb", function (event) {
  140. var index = event.detail.index;
  141. _this.animateThumb(index);
  142. });
  143. this.core.LGel.on(lGEvents.beforeOpen + ".thumb", function () {
  144. _this.thumbOuterWidth = _this.core.outer.get().offsetWidth;
  145. });
  146. this.core.LGel.on(lGEvents.updateSlides + ".thumb", function () {
  147. _this.rebuildThumbnails();
  148. });
  149. this.core.LGel.on(lGEvents.containerResize + ".thumb", function () {
  150. if (!_this.core.lgOpened)
  151. return;
  152. setTimeout(function () {
  153. _this.thumbOuterWidth = _this.core.outer.get().offsetWidth;
  154. _this.animateThumb(_this.core.index);
  155. _this.thumbOuterWidth = _this.core.outer.get().offsetWidth;
  156. }, 50);
  157. });
  158. };
  159. Thumbnail.prototype.setThumbMarkup = function () {
  160. var thumbOuterClassNames = 'lg-thumb-outer ';
  161. if (this.settings.alignThumbnails) {
  162. thumbOuterClassNames += "lg-thumb-align-" + this.settings.alignThumbnails;
  163. }
  164. var html = "<div class=\"" + thumbOuterClassNames + "\">\n <div class=\"lg-thumb lg-group\">\n </div>\n </div>";
  165. this.core.outer.addClass('lg-has-thumb');
  166. if (this.settings.appendThumbnailsTo === '.lg-components') {
  167. this.core.$lgComponents.append(html);
  168. }
  169. else {
  170. this.core.outer.append(html);
  171. }
  172. this.$thumbOuter = this.core.outer.find('.lg-thumb-outer').first();
  173. this.$lgThumb = this.core.outer.find('.lg-thumb').first();
  174. if (this.settings.animateThumb) {
  175. this.core.outer
  176. .find('.lg-thumb')
  177. .css('transition-duration', this.core.settings.speed + 'ms')
  178. .css('width', this.thumbTotalWidth + 'px')
  179. .css('position', 'relative');
  180. }
  181. this.setThumbItemHtml(this.core.galleryItems);
  182. };
  183. Thumbnail.prototype.enableThumbDrag = function () {
  184. var _this = this;
  185. var thumbDragUtils = {
  186. cords: {
  187. startX: 0,
  188. endX: 0,
  189. },
  190. isMoved: false,
  191. newTranslateX: 0,
  192. startTime: new Date(),
  193. endTime: new Date(),
  194. touchMoveTime: 0,
  195. };
  196. var isDragging = false;
  197. this.$thumbOuter.addClass('lg-grab');
  198. this.core.outer
  199. .find('.lg-thumb')
  200. .first()
  201. .on('mousedown.lg.thumb', function (e) {
  202. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  203. // execute only on .lg-object
  204. e.preventDefault();
  205. thumbDragUtils.cords.startX = e.pageX;
  206. thumbDragUtils.startTime = new Date();
  207. _this.thumbClickable = false;
  208. isDragging = true;
  209. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  210. _this.core.outer.get().scrollLeft += 1;
  211. _this.core.outer.get().scrollLeft -= 1;
  212. // *
  213. _this.$thumbOuter
  214. .removeClass('lg-grab')
  215. .addClass('lg-grabbing');
  216. }
  217. });
  218. this.$LG(window).on("mousemove.lg.thumb.global" + this.core.lgId, function (e) {
  219. if (!_this.core.lgOpened)
  220. return;
  221. if (isDragging) {
  222. thumbDragUtils.cords.endX = e.pageX;
  223. thumbDragUtils = _this.onThumbTouchMove(thumbDragUtils);
  224. }
  225. });
  226. this.$LG(window).on("mouseup.lg.thumb.global" + this.core.lgId, function () {
  227. if (!_this.core.lgOpened)
  228. return;
  229. if (thumbDragUtils.isMoved) {
  230. thumbDragUtils = _this.onThumbTouchEnd(thumbDragUtils);
  231. }
  232. else {
  233. _this.thumbClickable = true;
  234. }
  235. if (isDragging) {
  236. isDragging = false;
  237. _this.$thumbOuter.removeClass('lg-grabbing').addClass('lg-grab');
  238. }
  239. });
  240. };
  241. Thumbnail.prototype.enableThumbSwipe = function () {
  242. var _this = this;
  243. var thumbDragUtils = {
  244. cords: {
  245. startX: 0,
  246. endX: 0,
  247. },
  248. isMoved: false,
  249. newTranslateX: 0,
  250. startTime: new Date(),
  251. endTime: new Date(),
  252. touchMoveTime: 0,
  253. };
  254. this.$lgThumb.on('touchstart.lg', function (e) {
  255. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  256. e.preventDefault();
  257. thumbDragUtils.cords.startX = e.targetTouches[0].pageX;
  258. _this.thumbClickable = false;
  259. thumbDragUtils.startTime = new Date();
  260. }
  261. });
  262. this.$lgThumb.on('touchmove.lg', function (e) {
  263. if (_this.thumbTotalWidth > _this.thumbOuterWidth) {
  264. e.preventDefault();
  265. thumbDragUtils.cords.endX = e.targetTouches[0].pageX;
  266. thumbDragUtils = _this.onThumbTouchMove(thumbDragUtils);
  267. }
  268. });
  269. this.$lgThumb.on('touchend.lg', function () {
  270. if (thumbDragUtils.isMoved) {
  271. thumbDragUtils = _this.onThumbTouchEnd(thumbDragUtils);
  272. }
  273. else {
  274. _this.thumbClickable = true;
  275. }
  276. });
  277. };
  278. // Rebuild thumbnails
  279. Thumbnail.prototype.rebuildThumbnails = function () {
  280. var _this = this;
  281. // Remove transitions
  282. this.$thumbOuter.addClass('lg-rebuilding-thumbnails');
  283. setTimeout(function () {
  284. _this.thumbTotalWidth =
  285. _this.core.galleryItems.length *
  286. (_this.settings.thumbWidth + _this.settings.thumbMargin);
  287. _this.$lgThumb.css('width', _this.thumbTotalWidth + 'px');
  288. _this.$lgThumb.empty();
  289. _this.setThumbItemHtml(_this.core.galleryItems);
  290. _this.animateThumb(_this.core.index);
  291. }, 50);
  292. setTimeout(function () {
  293. _this.$thumbOuter.removeClass('lg-rebuilding-thumbnails');
  294. }, 200);
  295. };
  296. // @ts-check
  297. Thumbnail.prototype.setTranslate = function (value) {
  298. this.$lgThumb.css('transform', 'translate3d(-' + value + 'px, 0px, 0px)');
  299. };
  300. Thumbnail.prototype.getPossibleTransformX = function (left) {
  301. if (left > this.thumbTotalWidth - this.thumbOuterWidth) {
  302. left = this.thumbTotalWidth - this.thumbOuterWidth;
  303. }
  304. if (left < 0) {
  305. left = 0;
  306. }
  307. return left;
  308. };
  309. Thumbnail.prototype.animateThumb = function (index) {
  310. this.$lgThumb.css('transition-duration', this.core.settings.speed + 'ms');
  311. if (this.settings.animateThumb) {
  312. var position = 0;
  313. switch (this.settings.currentPagerPosition) {
  314. case 'left':
  315. position = 0;
  316. break;
  317. case 'middle':
  318. position =
  319. this.thumbOuterWidth / 2 - this.settings.thumbWidth / 2;
  320. break;
  321. case 'right':
  322. position = this.thumbOuterWidth - this.settings.thumbWidth;
  323. }
  324. this.translateX =
  325. (this.settings.thumbWidth + this.settings.thumbMargin) * index -
  326. 1 -
  327. position;
  328. if (this.translateX > this.thumbTotalWidth - this.thumbOuterWidth) {
  329. this.translateX = this.thumbTotalWidth - this.thumbOuterWidth;
  330. }
  331. if (this.translateX < 0) {
  332. this.translateX = 0;
  333. }
  334. this.setTranslate(this.translateX);
  335. }
  336. };
  337. Thumbnail.prototype.onThumbTouchMove = function (thumbDragUtils) {
  338. thumbDragUtils.newTranslateX = this.translateX;
  339. thumbDragUtils.isMoved = true;
  340. thumbDragUtils.touchMoveTime = new Date().valueOf();
  341. thumbDragUtils.newTranslateX -=
  342. thumbDragUtils.cords.endX - thumbDragUtils.cords.startX;
  343. thumbDragUtils.newTranslateX = this.getPossibleTransformX(thumbDragUtils.newTranslateX);
  344. // move current slide
  345. this.setTranslate(thumbDragUtils.newTranslateX);
  346. this.$thumbOuter.addClass('lg-dragging');
  347. return thumbDragUtils;
  348. };
  349. Thumbnail.prototype.onThumbTouchEnd = function (thumbDragUtils) {
  350. thumbDragUtils.isMoved = false;
  351. thumbDragUtils.endTime = new Date();
  352. this.$thumbOuter.removeClass('lg-dragging');
  353. var touchDuration = thumbDragUtils.endTime.valueOf() -
  354. thumbDragUtils.startTime.valueOf();
  355. var distanceXnew = thumbDragUtils.cords.endX - thumbDragUtils.cords.startX;
  356. var speedX = Math.abs(distanceXnew) / touchDuration;
  357. // Some magical numbers
  358. // Can be improved
  359. if (speedX > 0.15 &&
  360. thumbDragUtils.endTime.valueOf() - thumbDragUtils.touchMoveTime < 30) {
  361. speedX += 1;
  362. if (speedX > 2) {
  363. speedX += 1;
  364. }
  365. speedX =
  366. speedX +
  367. speedX * (Math.abs(distanceXnew) / this.thumbOuterWidth);
  368. this.$lgThumb.css('transition-duration', Math.min(speedX - 1, 2) + 'settings');
  369. distanceXnew = distanceXnew * speedX;
  370. this.translateX = this.getPossibleTransformX(this.translateX - distanceXnew);
  371. this.setTranslate(this.translateX);
  372. }
  373. else {
  374. this.translateX = thumbDragUtils.newTranslateX;
  375. }
  376. if (Math.abs(thumbDragUtils.cords.endX - thumbDragUtils.cords.startX) <
  377. this.settings.thumbnailSwipeThreshold) {
  378. this.thumbClickable = true;
  379. }
  380. return thumbDragUtils;
  381. };
  382. Thumbnail.prototype.getThumbHtml = function (thumb, index) {
  383. var slideVideoInfo = this.core.galleryItems[index].__slideVideoInfo || {};
  384. var thumbImg;
  385. if (slideVideoInfo.youtube) {
  386. if (this.settings.loadYouTubeThumbnail) {
  387. thumbImg =
  388. '//img.youtube.com/vi/' +
  389. slideVideoInfo.youtube[1] +
  390. '/' +
  391. this.settings.youTubeThumbSize +
  392. '.jpg';
  393. }
  394. else {
  395. thumbImg = thumb;
  396. }
  397. }
  398. else {
  399. thumbImg = thumb;
  400. }
  401. return "<div data-lg-item-id=\"" + index + "\" class=\"lg-thumb-item " + (index === this.core.index ? ' active' : '') + "\" \n style=\"width:" + this.settings.thumbWidth + "px; height: " + this.settings.thumbHeight + ";\n margin-right: " + this.settings.thumbMargin + "px;\">\n <img data-lg-item-id=\"" + index + "\" src=\"" + thumbImg + "\" />\n </div>";
  402. };
  403. Thumbnail.prototype.getThumbItemHtml = function (items) {
  404. var thumbList = '';
  405. for (var i = 0; i < items.length; i++) {
  406. thumbList += this.getThumbHtml(items[i].thumb, i);
  407. }
  408. return thumbList;
  409. };
  410. Thumbnail.prototype.setThumbItemHtml = function (items) {
  411. var thumbList = this.getThumbItemHtml(items);
  412. this.$lgThumb.html(thumbList);
  413. };
  414. Thumbnail.prototype.setAnimateThumbStyles = function () {
  415. if (this.settings.animateThumb) {
  416. this.core.outer.addClass('lg-animate-thumb');
  417. }
  418. };
  419. // Manage thumbnail active calss
  420. Thumbnail.prototype.manageActiveClassOnSlideChange = function () {
  421. var _this = this;
  422. // manage active class for thumbnail
  423. this.core.LGel.on(lGEvents.beforeSlide + ".thumb", function (event) {
  424. var $thumb = _this.core.outer.find('.lg-thumb-item');
  425. var index = event.detail.index;
  426. $thumb.removeClass('active');
  427. $thumb.eq(index).addClass('active');
  428. });
  429. };
  430. // Toggle thumbnail bar
  431. Thumbnail.prototype.toggleThumbBar = function () {
  432. var _this = this;
  433. if (this.settings.toggleThumb) {
  434. this.core.outer.addClass('lg-can-toggle');
  435. this.core.$toolbar.append('<button type="button" aria-label="' +
  436. this.settings.thumbnailPluginStrings['toggleThumbnails'] +
  437. '" class="lg-toggle-thumb lg-icon"></button>');
  438. this.core.outer
  439. .find('.lg-toggle-thumb')
  440. .first()
  441. .on('click.lg', function () {
  442. _this.core.outer.toggleClass('lg-components-open');
  443. });
  444. }
  445. };
  446. Thumbnail.prototype.thumbKeyPress = function () {
  447. var _this = this;
  448. this.$LG(window).on("keydown.lg.thumb.global" + this.core.lgId, function (e) {
  449. if (!_this.core.lgOpened || !_this.settings.toggleThumb)
  450. return;
  451. if (e.keyCode === 38) {
  452. e.preventDefault();
  453. _this.core.outer.addClass('lg-components-open');
  454. }
  455. else if (e.keyCode === 40) {
  456. e.preventDefault();
  457. _this.core.outer.removeClass('lg-components-open');
  458. }
  459. });
  460. };
  461. Thumbnail.prototype.destroy = function () {
  462. if (this.settings.thumbnail) {
  463. this.$LG(window).off(".lg.thumb.global" + this.core.lgId);
  464. this.core.LGel.off('.lg.thumb');
  465. this.core.LGel.off('.thumb');
  466. this.$thumbOuter.remove();
  467. this.core.outer.removeClass('lg-has-thumb');
  468. }
  469. };
  470. return Thumbnail;
  471. }());
  472. export default Thumbnail;
  473. //# sourceMappingURL=lg-thumbnail.es5.js.map