Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1361 lines
44 KiB

  1. /*! lightgallery - v1.6.12 - 2019-02-19
  2. * http://sachinchoolur.github.io/lightGallery/
  3. * Copyright (c) 2019 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 module === 'object' && module.exports) {
  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(root["jQuery"]);
  17. }
  18. }(this, function ($) {
  19. (function() {
  20. 'use strict';
  21. var defaults = {
  22. mode: 'lg-slide',
  23. // Ex : 'ease'
  24. cssEasing: 'ease',
  25. //'for jquery animation'
  26. easing: 'linear',
  27. speed: 600,
  28. height: '100%',
  29. width: '100%',
  30. addClass: '',
  31. startClass: 'lg-start-zoom',
  32. backdropDuration: 150,
  33. hideBarsDelay: 6000,
  34. useLeft: false,
  35. closable: true,
  36. loop: true,
  37. escKey: true,
  38. keyPress: true,
  39. controls: true,
  40. slideEndAnimatoin: true,
  41. hideControlOnEnd: false,
  42. mousewheel: true,
  43. getCaptionFromTitleOrAlt: true,
  44. // .lg-item || '.lg-sub-html'
  45. appendSubHtmlTo: '.lg-sub-html',
  46. subHtmlSelectorRelative: false,
  47. /**
  48. * @desc number of preload slides
  49. * will exicute only after the current slide is fully loaded.
  50. *
  51. * @ex you clicked on 4th image and if preload = 1 then 3rd slide and 5th
  52. * slide will be loaded in the background after the 4th slide is fully loaded..
  53. * if preload is 2 then 2nd 3rd 5th 6th slides will be preloaded.. ... ...
  54. *
  55. */
  56. preload: 1,
  57. showAfterLoad: true,
  58. selector: '',
  59. selectWithin: '',
  60. nextHtml: '',
  61. prevHtml: '',
  62. // 0, 1
  63. index: false,
  64. iframeMaxWidth: '100%',
  65. download: true,
  66. counter: true,
  67. appendCounterTo: '.lg-toolbar',
  68. swipeThreshold: 50,
  69. enableSwipe: true,
  70. enableDrag: true,
  71. dynamic: false,
  72. dynamicEl: [],
  73. galleryId: 1
  74. };
  75. function Plugin(element, options) {
  76. // Current lightGallery element
  77. this.el = element;
  78. // Current jquery element
  79. this.$el = $(element);
  80. // lightGallery settings
  81. this.s = $.extend({}, defaults, options);
  82. // When using dynamic mode, ensure dynamicEl is an array
  83. if (this.s.dynamic && this.s.dynamicEl !== 'undefined' && this.s.dynamicEl.constructor === Array && !this.s.dynamicEl.length) {
  84. throw ('When using dynamic mode, you must also define dynamicEl as an Array.');
  85. }
  86. // lightGallery modules
  87. this.modules = {};
  88. // false when lightgallery complete first slide;
  89. this.lGalleryOn = false;
  90. this.lgBusy = false;
  91. // Timeout function for hiding controls;
  92. this.hideBartimeout = false;
  93. // To determine browser supports for touch events;
  94. this.isTouch = ('ontouchstart' in document.documentElement);
  95. // Disable hideControlOnEnd if sildeEndAnimation is true
  96. if (this.s.slideEndAnimatoin) {
  97. this.s.hideControlOnEnd = false;
  98. }
  99. // Gallery items
  100. if (this.s.dynamic) {
  101. this.$items = this.s.dynamicEl;
  102. } else {
  103. if (this.s.selector === 'this') {
  104. this.$items = this.$el;
  105. } else if (this.s.selector !== '') {
  106. if (this.s.selectWithin) {
  107. this.$items = $(this.s.selectWithin).find(this.s.selector);
  108. } else {
  109. this.$items = this.$el.find($(this.s.selector));
  110. }
  111. } else {
  112. this.$items = this.$el.children();
  113. }
  114. }
  115. // .lg-item
  116. this.$slide = '';
  117. // .lg-outer
  118. this.$outer = '';
  119. this.init();
  120. return this;
  121. }
  122. Plugin.prototype.init = function() {
  123. var _this = this;
  124. // s.preload should not be more than $item.length
  125. if (_this.s.preload > _this.$items.length) {
  126. _this.s.preload = _this.$items.length;
  127. }
  128. // if dynamic option is enabled execute immediately
  129. var _hash = window.location.hash;
  130. if (_hash.indexOf('lg=' + this.s.galleryId) > 0) {
  131. _this.index = parseInt(_hash.split('&slide=')[1], 10);
  132. $('body').addClass('lg-from-hash');
  133. if (!$('body').hasClass('lg-on')) {
  134. setTimeout(function() {
  135. _this.build(_this.index);
  136. });
  137. $('body').addClass('lg-on');
  138. }
  139. }
  140. if (_this.s.dynamic) {
  141. _this.$el.trigger('onBeforeOpen.lg');
  142. _this.index = _this.s.index || 0;
  143. // prevent accidental double execution
  144. if (!$('body').hasClass('lg-on')) {
  145. setTimeout(function() {
  146. _this.build(_this.index);
  147. $('body').addClass('lg-on');
  148. });
  149. }
  150. } else {
  151. // Using different namespace for click because click event should not unbind if selector is same object('this')
  152. _this.$items.on('click.lgcustom', function(event) {
  153. // For IE8
  154. try {
  155. event.preventDefault();
  156. event.preventDefault();
  157. } catch (er) {
  158. event.returnValue = false;
  159. }
  160. _this.$el.trigger('onBeforeOpen.lg');
  161. _this.index = _this.s.index || _this.$items.index(this);
  162. // prevent accidental double execution
  163. if (!$('body').hasClass('lg-on')) {
  164. _this.build(_this.index);
  165. $('body').addClass('lg-on');
  166. }
  167. });
  168. }
  169. };
  170. Plugin.prototype.build = function(index) {
  171. var _this = this;
  172. _this.structure();
  173. // module constructor
  174. $.each($.fn.lightGallery.modules, function(key) {
  175. _this.modules[key] = new $.fn.lightGallery.modules[key](_this.el);
  176. });
  177. // initiate slide function
  178. _this.slide(index, false, false, false);
  179. if (_this.s.keyPress) {
  180. _this.keyPress();
  181. }
  182. if (_this.$items.length > 1) {
  183. _this.arrow();
  184. setTimeout(function() {
  185. _this.enableDrag();
  186. _this.enableSwipe();
  187. }, 50);
  188. if (_this.s.mousewheel) {
  189. _this.mousewheel();
  190. }
  191. } else {
  192. _this.$slide.on('click.lg', function() {
  193. _this.$el.trigger('onSlideClick.lg');
  194. });
  195. }
  196. _this.counter();
  197. _this.closeGallery();
  198. _this.$el.trigger('onAfterOpen.lg');
  199. // Hide controllers if mouse doesn't move for some period
  200. _this.$outer.on('mousemove.lg click.lg touchstart.lg', function() {
  201. _this.$outer.removeClass('lg-hide-items');
  202. clearTimeout(_this.hideBartimeout);
  203. // Timeout will be cleared on each slide movement also
  204. _this.hideBartimeout = setTimeout(function() {
  205. _this.$outer.addClass('lg-hide-items');
  206. }, _this.s.hideBarsDelay);
  207. });
  208. _this.$outer.trigger('mousemove.lg');
  209. };
  210. Plugin.prototype.structure = function() {
  211. var list = '';
  212. var controls = '';
  213. var i = 0;
  214. var subHtmlCont = '';
  215. var template;
  216. var _this = this;
  217. $('body').append('<div class="lg-backdrop"></div>');
  218. $('.lg-backdrop').css('transition-duration', this.s.backdropDuration + 'ms');
  219. // Create gallery items
  220. for (i = 0; i < this.$items.length; i++) {
  221. list += '<div class="lg-item"></div>';
  222. }
  223. // Create controlls
  224. if (this.s.controls && this.$items.length > 1) {
  225. controls = '<div class="lg-actions">' +
  226. '<button class="lg-prev lg-icon">' + this.s.prevHtml + '</button>' +
  227. '<button class="lg-next lg-icon">' + this.s.nextHtml + '</button>' +
  228. '</div>';
  229. }
  230. if (this.s.appendSubHtmlTo === '.lg-sub-html') {
  231. subHtmlCont = '<div class="lg-sub-html"></div>';
  232. }
  233. template = '<div class="lg-outer ' + this.s.addClass + ' ' + this.s.startClass + '">' +
  234. '<div class="lg" style="width:' + this.s.width + '; height:' + this.s.height + '">' +
  235. '<div class="lg-inner">' + list + '</div>' +
  236. '<div class="lg-toolbar lg-group">' +
  237. '<span class="lg-close lg-icon"></span>' +
  238. '</div>' +
  239. controls +
  240. subHtmlCont +
  241. '</div>' +
  242. '</div>';
  243. $('body').append(template);
  244. this.$outer = $('.lg-outer');
  245. this.$slide = this.$outer.find('.lg-item');
  246. if (this.s.useLeft) {
  247. this.$outer.addClass('lg-use-left');
  248. // Set mode lg-slide if use left is true;
  249. this.s.mode = 'lg-slide';
  250. } else {
  251. this.$outer.addClass('lg-use-css3');
  252. }
  253. // For fixed height gallery
  254. _this.setTop();
  255. $(window).on('resize.lg orientationchange.lg', function() {
  256. setTimeout(function() {
  257. _this.setTop();
  258. }, 100);
  259. });
  260. // add class lg-current to remove initial transition
  261. this.$slide.eq(this.index).addClass('lg-current');
  262. // add Class for css support and transition mode
  263. if (this.doCss()) {
  264. this.$outer.addClass('lg-css3');
  265. } else {
  266. this.$outer.addClass('lg-css');
  267. // Set speed 0 because no animation will happen if browser doesn't support css3
  268. this.s.speed = 0;
  269. }
  270. this.$outer.addClass(this.s.mode);
  271. if (this.s.enableDrag && this.$items.length > 1) {
  272. this.$outer.addClass('lg-grab');
  273. }
  274. if (this.s.showAfterLoad) {
  275. this.$outer.addClass('lg-show-after-load');
  276. }
  277. if (this.doCss()) {
  278. var $inner = this.$outer.find('.lg-inner');
  279. $inner.css('transition-timing-function', this.s.cssEasing);
  280. $inner.css('transition-duration', this.s.speed + 'ms');
  281. }
  282. setTimeout(function() {
  283. $('.lg-backdrop').addClass('in');
  284. });
  285. setTimeout(function() {
  286. _this.$outer.addClass('lg-visible');
  287. }, this.s.backdropDuration);
  288. if (this.s.download) {
  289. this.$outer.find('.lg-toolbar').append('<a id="lg-download" target="_blank" download class="lg-download lg-icon"></a>');
  290. }
  291. // Store the current scroll top value to scroll back after closing the gallery..
  292. this.prevScrollTop = $(window).scrollTop();
  293. };
  294. // For fixed height gallery
  295. Plugin.prototype.setTop = function() {
  296. if (this.s.height !== '100%') {
  297. var wH = $(window).height();
  298. var top = (wH - parseInt(this.s.height, 10)) / 2;
  299. var $lGallery = this.$outer.find('.lg');
  300. if (wH >= parseInt(this.s.height, 10)) {
  301. $lGallery.css('top', top + 'px');
  302. } else {
  303. $lGallery.css('top', '0px');
  304. }
  305. }
  306. };
  307. // Find css3 support
  308. Plugin.prototype.doCss = function() {
  309. // check for css animation support
  310. var support = function() {
  311. var transition = ['transition', 'MozTransition', 'WebkitTransition', 'OTransition', 'msTransition', 'KhtmlTransition'];
  312. var root = document.documentElement;
  313. var i = 0;
  314. for (i = 0; i < transition.length; i++) {
  315. if (transition[i] in root.style) {
  316. return true;
  317. }
  318. }
  319. };
  320. if (support()) {
  321. return true;
  322. }
  323. return false;
  324. };
  325. /**
  326. * @desc Check the given src is video
  327. * @param {String} src
  328. * @return {Object} video type
  329. * Ex:{ youtube : ["//www.youtube.com/watch?v=c0asJgSyxcY", "c0asJgSyxcY"] }
  330. */
  331. Plugin.prototype.isVideo = function(src, index) {
  332. var html;
  333. if (this.s.dynamic) {
  334. html = this.s.dynamicEl[index].html;
  335. } else {
  336. html = this.$items.eq(index).attr('data-html');
  337. }
  338. if (!src) {
  339. if(html) {
  340. return {
  341. html5: true
  342. };
  343. } else {
  344. console.error('lightGallery :- data-src is not pvovided on slide item ' + (index + 1) + '. Please make sure the selector property is properly configured. More info - http://sachinchoolur.github.io/lightGallery/demos/html-markup.html');
  345. return false;
  346. }
  347. }
  348. var youtube = src.match(/\/\/(?:www\.)?youtu(?:\.be|be\.com|be-nocookie\.com)\/(?:watch\?v=|embed\/)?([a-z0-9\-\_\%]+)/i);
  349. var vimeo = src.match(/\/\/(?:www\.)?vimeo.com\/([0-9a-z\-_]+)/i);
  350. var dailymotion = src.match(/\/\/(?:www\.)?dai.ly\/([0-9a-z\-_]+)/i);
  351. var vk = src.match(/\/\/(?:www\.)?(?:vk\.com|vkontakte\.ru)\/(?:video_ext\.php\?)(.*)/i);
  352. if (youtube) {
  353. return {
  354. youtube: youtube
  355. };
  356. } else if (vimeo) {
  357. return {
  358. vimeo: vimeo
  359. };
  360. } else if (dailymotion) {
  361. return {
  362. dailymotion: dailymotion
  363. };
  364. } else if (vk) {
  365. return {
  366. vk: vk
  367. };
  368. }
  369. };
  370. /**
  371. * @desc Create image counter
  372. * Ex: 1/10
  373. */
  374. Plugin.prototype.counter = function() {
  375. if (this.s.counter) {
  376. $(this.s.appendCounterTo).append('<div id="lg-counter"><span id="lg-counter-current">' + (parseInt(this.index, 10) + 1) + '</span> / <span id="lg-counter-all">' + this.$items.length + '</span></div>');
  377. }
  378. };
  379. /**
  380. * @desc add sub-html into the slide
  381. * @param {Number} index - index of the slide
  382. */
  383. Plugin.prototype.addHtml = function(index) {
  384. var subHtml = null;
  385. var subHtmlUrl;
  386. var $currentEle;
  387. if (this.s.dynamic) {
  388. if (this.s.dynamicEl[index].subHtmlUrl) {
  389. subHtmlUrl = this.s.dynamicEl[index].subHtmlUrl;
  390. } else {
  391. subHtml = this.s.dynamicEl[index].subHtml;
  392. }
  393. } else {
  394. $currentEle = this.$items.eq(index);
  395. if ($currentEle.attr('data-sub-html-url')) {
  396. subHtmlUrl = $currentEle.attr('data-sub-html-url');
  397. } else {
  398. subHtml = $currentEle.attr('data-sub-html');
  399. if (this.s.getCaptionFromTitleOrAlt && !subHtml) {
  400. subHtml = $currentEle.attr('title') || $currentEle.find('img').first().attr('alt');
  401. }
  402. }
  403. }
  404. if (!subHtmlUrl) {
  405. if (typeof subHtml !== 'undefined' && subHtml !== null) {
  406. // get first letter of subhtml
  407. // if first letter starts with . or # get the html form the jQuery object
  408. var fL = subHtml.substring(0, 1);
  409. if (fL === '.' || fL === '#') {
  410. if (this.s.subHtmlSelectorRelative && !this.s.dynamic) {
  411. subHtml = $currentEle.find(subHtml).html();
  412. } else {
  413. subHtml = $(subHtml).html();
  414. }
  415. }
  416. } else {
  417. subHtml = '';
  418. }
  419. }
  420. if (this.s.appendSubHtmlTo === '.lg-sub-html') {
  421. if (subHtmlUrl) {
  422. this.$outer.find(this.s.appendSubHtmlTo).load(subHtmlUrl);
  423. } else {
  424. this.$outer.find(this.s.appendSubHtmlTo).html(subHtml);
  425. }
  426. } else {
  427. if (subHtmlUrl) {
  428. this.$slide.eq(index).load(subHtmlUrl);
  429. } else {
  430. this.$slide.eq(index).append(subHtml);
  431. }
  432. }
  433. // Add lg-empty-html class if title doesn't exist
  434. if (typeof subHtml !== 'undefined' && subHtml !== null) {
  435. if (subHtml === '') {
  436. this.$outer.find(this.s.appendSubHtmlTo).addClass('lg-empty-html');
  437. } else {
  438. this.$outer.find(this.s.appendSubHtmlTo).removeClass('lg-empty-html');
  439. }
  440. }
  441. this.$el.trigger('onAfterAppendSubHtml.lg', [index]);
  442. };
  443. /**
  444. * @desc Preload slides
  445. * @param {Number} index - index of the slide
  446. */
  447. Plugin.prototype.preload = function(index) {
  448. var i = 1;
  449. var j = 1;
  450. for (i = 1; i <= this.s.preload; i++) {
  451. if (i >= this.$items.length - index) {
  452. break;
  453. }
  454. this.loadContent(index + i, false, 0);
  455. }
  456. for (j = 1; j <= this.s.preload; j++) {
  457. if (index - j < 0) {
  458. break;
  459. }
  460. this.loadContent(index - j, false, 0);
  461. }
  462. };
  463. /**
  464. * @desc Load slide content into slide.
  465. * @param {Number} index - index of the slide.
  466. * @param {Boolean} rec - if true call loadcontent() function again.
  467. * @param {Boolean} delay - delay for adding complete class. it is 0 except first time.
  468. */
  469. Plugin.prototype.loadContent = function(index, rec, delay) {
  470. var _this = this;
  471. var _hasPoster = false;
  472. var _$img;
  473. var _src;
  474. var _poster;
  475. var _srcset;
  476. var _sizes;
  477. var _html;
  478. var getResponsiveSrc = function(srcItms) {
  479. var rsWidth = [];
  480. var rsSrc = [];
  481. for (var i = 0; i < srcItms.length; i++) {
  482. var __src = srcItms[i].split(' ');
  483. // Manage empty space
  484. if (__src[0] === '') {
  485. __src.splice(0, 1);
  486. }
  487. rsSrc.push(__src[0]);
  488. rsWidth.push(__src[1]);
  489. }
  490. var wWidth = $(window).width();
  491. for (var j = 0; j < rsWidth.length; j++) {
  492. if (parseInt(rsWidth[j], 10) > wWidth) {
  493. _src = rsSrc[j];
  494. break;
  495. }
  496. }
  497. };
  498. if (_this.s.dynamic) {
  499. if (_this.s.dynamicEl[index].poster) {
  500. _hasPoster = true;
  501. _poster = _this.s.dynamicEl[index].poster;
  502. }
  503. _html = _this.s.dynamicEl[index].html;
  504. _src = _this.s.dynamicEl[index].src;
  505. if (_this.s.dynamicEl[index].responsive) {
  506. var srcDyItms = _this.s.dynamicEl[index].responsive.split(',');
  507. getResponsiveSrc(srcDyItms);
  508. }
  509. _srcset = _this.s.dynamicEl[index].srcset;
  510. _sizes = _this.s.dynamicEl[index].sizes;
  511. } else {
  512. if (_this.$items.eq(index).attr('data-poster')) {
  513. _hasPoster = true;
  514. _poster = _this.$items.eq(index).attr('data-poster');
  515. }
  516. _html = _this.$items.eq(index).attr('data-html');
  517. _src = _this.$items.eq(index).attr('href') || _this.$items.eq(index).attr('data-src');
  518. if (_this.$items.eq(index).attr('data-responsive')) {
  519. var srcItms = _this.$items.eq(index).attr('data-responsive').split(',');
  520. getResponsiveSrc(srcItms);
  521. }
  522. _srcset = _this.$items.eq(index).attr('data-srcset');
  523. _sizes = _this.$items.eq(index).attr('data-sizes');
  524. }
  525. //if (_src || _srcset || _sizes || _poster) {
  526. var iframe = false;
  527. if (_this.s.dynamic) {
  528. if (_this.s.dynamicEl[index].iframe) {
  529. iframe = true;
  530. }
  531. } else {
  532. if (_this.$items.eq(index).attr('data-iframe') === 'true') {
  533. iframe = true;
  534. }
  535. }
  536. var _isVideo = _this.isVideo(_src, index);
  537. if (!_this.$slide.eq(index).hasClass('lg-loaded')) {
  538. if (iframe) {
  539. _this.$slide.eq(index).prepend('<div class="lg-video-cont lg-has-iframe" style="max-width:' + _this.s.iframeMaxWidth + '"><div class="lg-video"><iframe class="lg-object" frameborder="0" src="' + _src + '" allowfullscreen="true"></iframe></div></div>');
  540. } else if (_hasPoster) {
  541. var videoClass = '';
  542. if (_isVideo && _isVideo.youtube) {
  543. videoClass = 'lg-has-youtube';
  544. } else if (_isVideo && _isVideo.vimeo) {
  545. videoClass = 'lg-has-vimeo';
  546. } else {
  547. videoClass = 'lg-has-html5';
  548. }
  549. _this.$slide.eq(index).prepend('<div class="lg-video-cont ' + videoClass + ' "><div class="lg-video"><span class="lg-video-play"></span><img class="lg-object lg-has-poster" src="' + _poster + '" /></div></div>');
  550. } else if (_isVideo) {
  551. _this.$slide.eq(index).prepend('<div class="lg-video-cont "><div class="lg-video"></div></div>');
  552. _this.$el.trigger('hasVideo.lg', [index, _src, _html]);
  553. } else {
  554. _this.$slide.eq(index).prepend('<div class="lg-img-wrap"><img class="lg-object lg-image" src="' + _src + '" /></div>');
  555. }
  556. _this.$el.trigger('onAferAppendSlide.lg', [index]);
  557. _$img = _this.$slide.eq(index).find('.lg-object');
  558. if (_sizes) {
  559. _$img.attr('sizes', _sizes);
  560. }
  561. if (_srcset) {
  562. _$img.attr('srcset', _srcset);
  563. try {
  564. picturefill({
  565. elements: [_$img[0]]
  566. });
  567. } catch (e) {
  568. console.warn('lightGallery :- If you want srcset to be supported for older browser please include picturefil version 2 javascript library in your document.');
  569. }
  570. }
  571. if (this.s.appendSubHtmlTo !== '.lg-sub-html') {
  572. _this.addHtml(index);
  573. }
  574. _this.$slide.eq(index).addClass('lg-loaded');
  575. }
  576. _this.$slide.eq(index).find('.lg-object').on('load.lg error.lg', function() {
  577. // For first time add some delay for displaying the start animation.
  578. var _speed = 0;
  579. // Do not change the delay value because it is required for zoom plugin.
  580. // If gallery opened from direct url (hash) speed value should be 0
  581. if (delay && !$('body').hasClass('lg-from-hash')) {
  582. _speed = delay;
  583. }
  584. setTimeout(function() {
  585. _this.$slide.eq(index).addClass('lg-complete');
  586. _this.$el.trigger('onSlideItemLoad.lg', [index, delay || 0]);
  587. }, _speed);
  588. });
  589. // @todo check load state for html5 videos
  590. if (_isVideo && _isVideo.html5 && !_hasPoster) {
  591. _this.$slide.eq(index).addClass('lg-complete');
  592. }
  593. if (rec === true) {
  594. if (!_this.$slide.eq(index).hasClass('lg-complete')) {
  595. _this.$slide.eq(index).find('.lg-object').on('load.lg error.lg', function() {
  596. _this.preload(index);
  597. });
  598. } else {
  599. _this.preload(index);
  600. }
  601. }
  602. //}
  603. };
  604. /**
  605. * @desc slide function for lightgallery
  606. ** Slide() gets call on start
  607. ** ** Set lg.on true once slide() function gets called.
  608. ** Call loadContent() on slide() function inside setTimeout
  609. ** ** On first slide we do not want any animation like slide of fade
  610. ** ** So on first slide( if lg.on if false that is first slide) loadContent() should start loading immediately
  611. ** ** Else loadContent() should wait for the transition to complete.
  612. ** ** So set timeout s.speed + 50
  613. <=> ** loadContent() will load slide content in to the particular slide
  614. ** ** It has recursion (rec) parameter. if rec === true loadContent() will call preload() function.
  615. ** ** preload will execute only when the previous slide is fully loaded (images iframe)
  616. ** ** avoid simultaneous image load
  617. <=> ** Preload() will check for s.preload value and call loadContent() again accoring to preload value
  618. ** loadContent() <====> Preload();
  619. * @param {Number} index - index of the slide
  620. * @param {Boolean} fromTouch - true if slide function called via touch event or mouse drag
  621. * @param {Boolean} fromThumb - true if slide function called via thumbnail click
  622. * @param {String} direction - Direction of the slide(next/prev)
  623. */
  624. Plugin.prototype.slide = function(index, fromTouch, fromThumb, direction) {
  625. var _prevIndex = this.$outer.find('.lg-current').index();
  626. var _this = this;
  627. // Prevent if multiple call
  628. // Required for hsh plugin
  629. if (_this.lGalleryOn && (_prevIndex === index)) {
  630. return;
  631. }
  632. var _length = this.$slide.length;
  633. var _time = _this.lGalleryOn ? this.s.speed : 0;
  634. if (!_this.lgBusy) {
  635. if (this.s.download) {
  636. var _src;
  637. if (_this.s.dynamic) {
  638. _src = _this.s.dynamicEl[index].downloadUrl !== false && (_this.s.dynamicEl[index].downloadUrl || _this.s.dynamicEl[index].src);
  639. } else {
  640. _src = _this.$items.eq(index).attr('data-download-url') !== 'false' && (_this.$items.eq(index).attr('data-download-url') || _this.$items.eq(index).attr('href') || _this.$items.eq(index).attr('data-src'));
  641. }
  642. if (_src) {
  643. $('#lg-download').attr('href', _src);
  644. _this.$outer.removeClass('lg-hide-download');
  645. } else {
  646. _this.$outer.addClass('lg-hide-download');
  647. }
  648. }
  649. this.$el.trigger('onBeforeSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
  650. _this.lgBusy = true;
  651. clearTimeout(_this.hideBartimeout);
  652. // Add title if this.s.appendSubHtmlTo === lg-sub-html
  653. if (this.s.appendSubHtmlTo === '.lg-sub-html') {
  654. // wait for slide animation to complete
  655. setTimeout(function() {
  656. _this.addHtml(index);
  657. }, _time);
  658. }
  659. this.arrowDisable(index);
  660. if (!direction) {
  661. if (index < _prevIndex) {
  662. direction = 'prev';
  663. } else if (index > _prevIndex) {
  664. direction = 'next';
  665. }
  666. }
  667. if (!fromTouch) {
  668. // remove all transitions
  669. _this.$outer.addClass('lg-no-trans');
  670. this.$slide.removeClass('lg-prev-slide lg-next-slide');
  671. if (direction === 'prev') {
  672. //prevslide
  673. this.$slide.eq(index).addClass('lg-prev-slide');
  674. this.$slide.eq(_prevIndex).addClass('lg-next-slide');
  675. } else {
  676. // next slide
  677. this.$slide.eq(index).addClass('lg-next-slide');
  678. this.$slide.eq(_prevIndex).addClass('lg-prev-slide');
  679. }
  680. // give 50 ms for browser to add/remove class
  681. setTimeout(function() {
  682. _this.$slide.removeClass('lg-current');
  683. //_this.$slide.eq(_prevIndex).removeClass('lg-current');
  684. _this.$slide.eq(index).addClass('lg-current');
  685. // reset all transitions
  686. _this.$outer.removeClass('lg-no-trans');
  687. }, 50);
  688. } else {
  689. this.$slide.removeClass('lg-prev-slide lg-current lg-next-slide');
  690. var touchPrev;
  691. var touchNext;
  692. if (_length > 2) {
  693. touchPrev = index - 1;
  694. touchNext = index + 1;
  695. if ((index === 0) && (_prevIndex === _length - 1)) {
  696. // next slide
  697. touchNext = 0;
  698. touchPrev = _length - 1;
  699. } else if ((index === _length - 1) && (_prevIndex === 0)) {
  700. // prev slide
  701. touchNext = 0;
  702. touchPrev = _length - 1;
  703. }
  704. } else {
  705. touchPrev = 0;
  706. touchNext = 1;
  707. }
  708. if (direction === 'prev') {
  709. _this.$slide.eq(touchNext).addClass('lg-next-slide');
  710. } else {
  711. _this.$slide.eq(touchPrev).addClass('lg-prev-slide');
  712. }
  713. _this.$slide.eq(index).addClass('lg-current');
  714. }
  715. if (_this.lGalleryOn) {
  716. setTimeout(function() {
  717. _this.loadContent(index, true, 0);
  718. }, this.s.speed + 50);
  719. setTimeout(function() {
  720. _this.lgBusy = false;
  721. _this.$el.trigger('onAfterSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
  722. }, this.s.speed);
  723. } else {
  724. _this.loadContent(index, true, _this.s.backdropDuration);
  725. _this.lgBusy = false;
  726. _this.$el.trigger('onAfterSlide.lg', [_prevIndex, index, fromTouch, fromThumb]);
  727. }
  728. _this.lGalleryOn = true;
  729. if (this.s.counter) {
  730. $('#lg-counter-current').text(index + 1);
  731. }
  732. }
  733. _this.index = index;
  734. };
  735. /**
  736. * @desc Go to next slide
  737. * @param {Boolean} fromTouch - true if slide function called via touch event
  738. */
  739. Plugin.prototype.goToNextSlide = function(fromTouch) {
  740. var _this = this;
  741. var _loop = _this.s.loop;
  742. if (fromTouch && _this.$slide.length < 3) {
  743. _loop = false;
  744. }
  745. if (!_this.lgBusy) {
  746. if ((_this.index + 1) < _this.$slide.length) {
  747. _this.index++;
  748. _this.$el.trigger('onBeforeNextSlide.lg', [_this.index]);
  749. _this.slide(_this.index, fromTouch, false, 'next');
  750. } else {
  751. if (_loop) {
  752. _this.index = 0;
  753. _this.$el.trigger('onBeforeNextSlide.lg', [_this.index]);
  754. _this.slide(_this.index, fromTouch, false, 'next');
  755. } else if (_this.s.slideEndAnimatoin && !fromTouch) {
  756. _this.$outer.addClass('lg-right-end');
  757. setTimeout(function() {
  758. _this.$outer.removeClass('lg-right-end');
  759. }, 400);
  760. }
  761. }
  762. }
  763. };
  764. /**
  765. * @desc Go to previous slide
  766. * @param {Boolean} fromTouch - true if slide function called via touch event
  767. */
  768. Plugin.prototype.goToPrevSlide = function(fromTouch) {
  769. var _this = this;
  770. var _loop = _this.s.loop;
  771. if (fromTouch && _this.$slide.length < 3) {
  772. _loop = false;
  773. }
  774. if (!_this.lgBusy) {
  775. if (_this.index > 0) {
  776. _this.index--;
  777. _this.$el.trigger('onBeforePrevSlide.lg', [_this.index, fromTouch]);
  778. _this.slide(_this.index, fromTouch, false, 'prev');
  779. } else {
  780. if (_loop) {
  781. _this.index = _this.$items.length - 1;
  782. _this.$el.trigger('onBeforePrevSlide.lg', [_this.index, fromTouch]);
  783. _this.slide(_this.index, fromTouch, false, 'prev');
  784. } else if (_this.s.slideEndAnimatoin && !fromTouch) {
  785. _this.$outer.addClass('lg-left-end');
  786. setTimeout(function() {
  787. _this.$outer.removeClass('lg-left-end');
  788. }, 400);
  789. }
  790. }
  791. }
  792. };
  793. Plugin.prototype.keyPress = function() {
  794. var _this = this;
  795. if (this.$items.length > 1) {
  796. $(window).on('keyup.lg', function(e) {
  797. if (_this.$items.length > 1) {
  798. if (e.keyCode === 37) {
  799. e.preventDefault();
  800. _this.goToPrevSlide();
  801. }
  802. if (e.keyCode === 39) {
  803. e.preventDefault();
  804. _this.goToNextSlide();
  805. }
  806. }
  807. });
  808. }
  809. $(window).on('keydown.lg', function(e) {
  810. if (_this.s.escKey === true && e.keyCode === 27) {
  811. e.preventDefault();
  812. if (!_this.$outer.hasClass('lg-thumb-open')) {
  813. _this.destroy();
  814. } else {
  815. _this.$outer.removeClass('lg-thumb-open');
  816. }
  817. }
  818. });
  819. };
  820. Plugin.prototype.arrow = function() {
  821. var _this = this;
  822. this.$outer.find('.lg-prev').on('click.lg', function() {
  823. _this.goToPrevSlide();
  824. });
  825. this.$outer.find('.lg-next').on('click.lg', function() {
  826. _this.goToNextSlide();
  827. });
  828. };
  829. Plugin.prototype.arrowDisable = function(index) {
  830. // Disable arrows if s.hideControlOnEnd is true
  831. if (!this.s.loop && this.s.hideControlOnEnd) {
  832. if ((index + 1) < this.$slide.length) {
  833. this.$outer.find('.lg-next').removeAttr('disabled').removeClass('disabled');
  834. } else {
  835. this.$outer.find('.lg-next').attr('disabled', 'disabled').addClass('disabled');
  836. }
  837. if (index > 0) {
  838. this.$outer.find('.lg-prev').removeAttr('disabled').removeClass('disabled');
  839. } else {
  840. this.$outer.find('.lg-prev').attr('disabled', 'disabled').addClass('disabled');
  841. }
  842. }
  843. };
  844. Plugin.prototype.setTranslate = function($el, xValue, yValue) {
  845. // jQuery supports Automatic CSS prefixing since jQuery 1.8.0
  846. if (this.s.useLeft) {
  847. $el.css('left', xValue);
  848. } else {
  849. $el.css({
  850. transform: 'translate3d(' + (xValue) + 'px, ' + yValue + 'px, 0px)'
  851. });
  852. }
  853. };
  854. Plugin.prototype.touchMove = function(startCoords, endCoords) {
  855. var distance = endCoords - startCoords;
  856. if (Math.abs(distance) > 15) {
  857. // reset opacity and transition duration
  858. this.$outer.addClass('lg-dragging');
  859. // move current slide
  860. this.setTranslate(this.$slide.eq(this.index), distance, 0);
  861. // move next and prev slide with current slide
  862. this.setTranslate($('.lg-prev-slide'), -this.$slide.eq(this.index).width() + distance, 0);
  863. this.setTranslate($('.lg-next-slide'), this.$slide.eq(this.index).width() + distance, 0);
  864. }
  865. };
  866. Plugin.prototype.touchEnd = function(distance) {
  867. var _this = this;
  868. // keep slide animation for any mode while dragg/swipe
  869. if (_this.s.mode !== 'lg-slide') {
  870. _this.$outer.addClass('lg-slide');
  871. }
  872. this.$slide.not('.lg-current, .lg-prev-slide, .lg-next-slide').css('opacity', '0');
  873. // set transition duration
  874. setTimeout(function() {
  875. _this.$outer.removeClass('lg-dragging');
  876. if ((distance < 0) && (Math.abs(distance) > _this.s.swipeThreshold)) {
  877. _this.goToNextSlide(true);
  878. } else if ((distance > 0) && (Math.abs(distance) > _this.s.swipeThreshold)) {
  879. _this.goToPrevSlide(true);
  880. } else if (Math.abs(distance) < 5) {
  881. // Trigger click if distance is less than 5 pix
  882. _this.$el.trigger('onSlideClick.lg');
  883. }
  884. _this.$slide.removeAttr('style');
  885. });
  886. // remove slide class once drag/swipe is completed if mode is not slide
  887. setTimeout(function() {
  888. if (!_this.$outer.hasClass('lg-dragging') && _this.s.mode !== 'lg-slide') {
  889. _this.$outer.removeClass('lg-slide');
  890. }
  891. }, _this.s.speed + 100);
  892. };
  893. Plugin.prototype.enableSwipe = function() {
  894. var _this = this;
  895. var startCoords = 0;
  896. var endCoords = 0;
  897. var isMoved = false;
  898. if (_this.s.enableSwipe && _this.doCss()) {
  899. _this.$slide.on('touchstart.lg', function(e) {
  900. if (!_this.$outer.hasClass('lg-zoomed') && !_this.lgBusy) {
  901. e.preventDefault();
  902. _this.manageSwipeClass();
  903. startCoords = e.originalEvent.targetTouches[0].pageX;
  904. }
  905. });
  906. _this.$slide.on('touchmove.lg', function(e) {
  907. if (!_this.$outer.hasClass('lg-zoomed')) {
  908. e.preventDefault();
  909. endCoords = e.originalEvent.targetTouches[0].pageX;
  910. _this.touchMove(startCoords, endCoords);
  911. isMoved = true;
  912. }
  913. });
  914. _this.$slide.on('touchend.lg', function() {
  915. if (!_this.$outer.hasClass('lg-zoomed')) {
  916. if (isMoved) {
  917. isMoved = false;
  918. _this.touchEnd(endCoords - startCoords);
  919. } else {
  920. _this.$el.trigger('onSlideClick.lg');
  921. }
  922. }
  923. });
  924. }
  925. };
  926. Plugin.prototype.enableDrag = function() {
  927. var _this = this;
  928. var startCoords = 0;
  929. var endCoords = 0;
  930. var isDraging = false;
  931. var isMoved = false;
  932. if (_this.s.enableDrag && _this.doCss()) {
  933. _this.$slide.on('mousedown.lg', function(e) {
  934. if (!_this.$outer.hasClass('lg-zoomed') && !_this.lgBusy && !$(e.target).text().trim()) {
  935. e.preventDefault();
  936. _this.manageSwipeClass();
  937. startCoords = e.pageX;
  938. isDraging = true;
  939. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  940. _this.$outer.scrollLeft += 1;
  941. _this.$outer.scrollLeft -= 1;
  942. // *
  943. _this.$outer.removeClass('lg-grab').addClass('lg-grabbing');
  944. _this.$el.trigger('onDragstart.lg');
  945. }
  946. });
  947. $(window).on('mousemove.lg', function(e) {
  948. if (isDraging) {
  949. isMoved = true;
  950. endCoords = e.pageX;
  951. _this.touchMove(startCoords, endCoords);
  952. _this.$el.trigger('onDragmove.lg');
  953. }
  954. });
  955. $(window).on('mouseup.lg', function(e) {
  956. if (isMoved) {
  957. isMoved = false;
  958. _this.touchEnd(endCoords - startCoords);
  959. _this.$el.trigger('onDragend.lg');
  960. } else if ($(e.target).hasClass('lg-object') || $(e.target).hasClass('lg-video-play')) {
  961. _this.$el.trigger('onSlideClick.lg');
  962. }
  963. // Prevent execution on click
  964. if (isDraging) {
  965. isDraging = false;
  966. _this.$outer.removeClass('lg-grabbing').addClass('lg-grab');
  967. }
  968. });
  969. }
  970. };
  971. Plugin.prototype.manageSwipeClass = function() {
  972. var _touchNext = this.index + 1;
  973. var _touchPrev = this.index - 1;
  974. if (this.s.loop && this.$slide.length > 2) {
  975. if (this.index === 0) {
  976. _touchPrev = this.$slide.length - 1;
  977. } else if (this.index === this.$slide.length - 1) {
  978. _touchNext = 0;
  979. }
  980. }
  981. this.$slide.removeClass('lg-next-slide lg-prev-slide');
  982. if (_touchPrev > -1) {
  983. this.$slide.eq(_touchPrev).addClass('lg-prev-slide');
  984. }
  985. this.$slide.eq(_touchNext).addClass('lg-next-slide');
  986. };
  987. Plugin.prototype.mousewheel = function() {
  988. var _this = this;
  989. _this.$outer.on('mousewheel.lg', function(e) {
  990. if (!e.deltaY) {
  991. return;
  992. }
  993. if (e.deltaY > 0) {
  994. _this.goToPrevSlide();
  995. } else {
  996. _this.goToNextSlide();
  997. }
  998. e.preventDefault();
  999. });
  1000. };
  1001. Plugin.prototype.closeGallery = function() {
  1002. var _this = this;
  1003. var mousedown = false;
  1004. this.$outer.find('.lg-close').on('click.lg', function() {
  1005. _this.destroy();
  1006. });
  1007. if (_this.s.closable) {
  1008. // If you drag the slide and release outside gallery gets close on chrome
  1009. // for preventing this check mousedown and mouseup happened on .lg-item or lg-outer
  1010. _this.$outer.on('mousedown.lg', function(e) {
  1011. if ($(e.target).is('.lg-outer') || $(e.target).is('.lg-item ') || $(e.target).is('.lg-img-wrap')) {
  1012. mousedown = true;
  1013. } else {
  1014. mousedown = false;
  1015. }
  1016. });
  1017. _this.$outer.on('mousemove.lg', function() {
  1018. mousedown = false;
  1019. });
  1020. _this.$outer.on('mouseup.lg', function(e) {
  1021. if ($(e.target).is('.lg-outer') || $(e.target).is('.lg-item ') || $(e.target).is('.lg-img-wrap') && mousedown) {
  1022. if (!_this.$outer.hasClass('lg-dragging')) {
  1023. _this.destroy();
  1024. }
  1025. }
  1026. });
  1027. }
  1028. };
  1029. Plugin.prototype.destroy = function(d) {
  1030. var _this = this;
  1031. if (!d) {
  1032. _this.$el.trigger('onBeforeClose.lg');
  1033. $(window).scrollTop(_this.prevScrollTop);
  1034. }
  1035. /**
  1036. * if d is false or undefined destroy will only close the gallery
  1037. * plugins instance remains with the element
  1038. *
  1039. * if d is true destroy will completely remove the plugin
  1040. */
  1041. if (d) {
  1042. if (!_this.s.dynamic) {
  1043. // only when not using dynamic mode is $items a jquery collection
  1044. this.$items.off('click.lg click.lgcustom');
  1045. }
  1046. $.removeData(_this.el, 'lightGallery');
  1047. }
  1048. // Unbind all events added by lightGallery
  1049. this.$el.off('.lg.tm');
  1050. // Distroy all lightGallery modules
  1051. $.each($.fn.lightGallery.modules, function(key) {
  1052. if (_this.modules[key]) {
  1053. _this.modules[key].destroy();
  1054. }
  1055. });
  1056. this.lGalleryOn = false;
  1057. clearTimeout(_this.hideBartimeout);
  1058. this.hideBartimeout = false;
  1059. $(window).off('.lg');
  1060. $('body').removeClass('lg-on lg-from-hash');
  1061. if (_this.$outer) {
  1062. _this.$outer.removeClass('lg-visible');
  1063. }
  1064. $('.lg-backdrop').removeClass('in');
  1065. setTimeout(function() {
  1066. if (_this.$outer) {
  1067. _this.$outer.remove();
  1068. }
  1069. $('.lg-backdrop').remove();
  1070. if (!d) {
  1071. _this.$el.trigger('onCloseAfter.lg');
  1072. }
  1073. }, _this.s.backdropDuration + 50);
  1074. };
  1075. $.fn.lightGallery = function(options) {
  1076. return this.each(function() {
  1077. if (!$.data(this, 'lightGallery')) {
  1078. $.data(this, 'lightGallery', new Plugin(this, options));
  1079. } else {
  1080. try {
  1081. $(this).data('lightGallery').init();
  1082. } catch (err) {
  1083. console.error('lightGallery has not initiated properly');
  1084. }
  1085. }
  1086. });
  1087. };
  1088. $.fn.lightGallery.modules = {};
  1089. })();
  1090. }));