lightgallery.js 56 KB

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