lightgallery.es5.js 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648
  1. /*!
  2. * lightgallery | 2.4.0-beta.0 | December 12th 2021
  3. * http://www.lightgalleryjs.com/
  4. * Copyright (c) 2020 Sachin Neravath;
  5. * @license GPLv3
  6. */
  7. /*! *****************************************************************************
  8. Copyright (c) Microsoft Corporation.
  9. Permission to use, copy, modify, and/or distribute this software for any
  10. purpose with or without fee is hereby granted.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  12. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  13. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  14. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  15. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  16. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. PERFORMANCE OF THIS SOFTWARE.
  18. ***************************************************************************** */
  19. var __assign = function() {
  20. __assign = Object.assign || function __assign(t) {
  21. for (var s, i = 1, n = arguments.length; i < n; i++) {
  22. s = arguments[i];
  23. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  24. }
  25. return t;
  26. };
  27. return __assign.apply(this, arguments);
  28. };
  29. function __spreadArrays() {
  30. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  31. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  32. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  33. r[k] = a[j];
  34. return r;
  35. }
  36. /**
  37. * List of lightGallery events
  38. * All events should be documented here
  39. * Below interfaces are used to build the website documentations
  40. * */
  41. var lGEvents = {
  42. afterAppendSlide: 'lgAfterAppendSlide',
  43. init: 'lgInit',
  44. hasVideo: 'lgHasVideo',
  45. containerResize: 'lgContainerResize',
  46. updateSlides: 'lgUpdateSlides',
  47. afterAppendSubHtml: 'lgAfterAppendSubHtml',
  48. beforeOpen: 'lgBeforeOpen',
  49. afterOpen: 'lgAfterOpen',
  50. slideItemLoad: 'lgSlideItemLoad',
  51. beforeSlide: 'lgBeforeSlide',
  52. afterSlide: 'lgAfterSlide',
  53. posterClick: 'lgPosterClick',
  54. dragStart: 'lgDragStart',
  55. dragMove: 'lgDragMove',
  56. dragEnd: 'lgDragEnd',
  57. beforeNextSlide: 'lgBeforeNextSlide',
  58. beforePrevSlide: 'lgBeforePrevSlide',
  59. beforeClose: 'lgBeforeClose',
  60. afterClose: 'lgAfterClose',
  61. rotateLeft: 'lgRotateLeft',
  62. rotateRight: 'lgRotateRight',
  63. flipHorizontal: 'lgFlipHorizontal',
  64. flipVertical: 'lgFlipVertical',
  65. autoplay: 'lgAutoplay',
  66. autoplayStart: 'lgAutoplayStart',
  67. autoplayStop: 'lgAutoplayStop',
  68. };
  69. var lightGalleryCoreSettings = {
  70. mode: 'lg-slide',
  71. easing: 'ease',
  72. speed: 400,
  73. licenseKey: '0000-0000-000-0000',
  74. height: '100%',
  75. width: '100%',
  76. addClass: '',
  77. startClass: 'lg-start-zoom',
  78. backdropDuration: 300,
  79. container: '',
  80. startAnimationDuration: 400,
  81. zoomFromOrigin: true,
  82. hideBarsDelay: 0,
  83. showBarsAfter: 10000,
  84. slideDelay: 0,
  85. supportLegacyBrowser: true,
  86. allowMediaOverlap: false,
  87. videoMaxSize: '1280-720',
  88. loadYouTubePoster: true,
  89. defaultCaptionHeight: 0,
  90. ariaLabelledby: '',
  91. ariaDescribedby: '',
  92. closable: true,
  93. swipeToClose: true,
  94. closeOnTap: true,
  95. showCloseIcon: true,
  96. showMaximizeIcon: false,
  97. loop: true,
  98. escKey: true,
  99. keyPress: true,
  100. controls: true,
  101. slideEndAnimation: true,
  102. hideControlOnEnd: false,
  103. mousewheel: false,
  104. getCaptionFromTitleOrAlt: true,
  105. appendSubHtmlTo: '.lg-sub-html',
  106. subHtmlSelectorRelative: false,
  107. preload: 2,
  108. numberOfSlideItemsInDom: 10,
  109. selector: '',
  110. selectWithin: '',
  111. nextHtml: '',
  112. prevHtml: '',
  113. index: 0,
  114. iframeWidth: '100%',
  115. iframeHeight: '100%',
  116. iframeMaxWidth: '100%',
  117. iframeMaxHeight: '100%',
  118. download: true,
  119. counter: true,
  120. appendCounterTo: '.lg-toolbar',
  121. swipeThreshold: 50,
  122. enableSwipe: true,
  123. enableDrag: true,
  124. dynamic: false,
  125. dynamicEl: [],
  126. extraProps: [],
  127. exThumbImage: '',
  128. isMobile: undefined,
  129. mobileSettings: {
  130. controls: false,
  131. showCloseIcon: false,
  132. download: false,
  133. },
  134. plugins: [],
  135. strings: {
  136. closeGallery: 'Close gallery',
  137. toggleMaximize: 'Toggle maximize',
  138. previousSlide: 'Previous slide',
  139. nextSlide: 'Next slide',
  140. download: 'Download',
  141. playVideo: 'Play video',
  142. },
  143. };
  144. function initLgPolyfills() {
  145. (function () {
  146. if (typeof window.CustomEvent === 'function')
  147. return false;
  148. function CustomEvent(event, params) {
  149. params = params || {
  150. bubbles: false,
  151. cancelable: false,
  152. detail: null,
  153. };
  154. var evt = document.createEvent('CustomEvent');
  155. evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
  156. return evt;
  157. }
  158. window.CustomEvent = CustomEvent;
  159. })();
  160. (function () {
  161. if (!Element.prototype.matches) {
  162. Element.prototype.matches =
  163. Element.prototype.msMatchesSelector ||
  164. Element.prototype.webkitMatchesSelector;
  165. }
  166. })();
  167. }
  168. var lgQuery = /** @class */ (function () {
  169. function lgQuery(selector) {
  170. this.cssVenderPrefixes = [
  171. 'TransitionDuration',
  172. 'TransitionTimingFunction',
  173. 'Transform',
  174. 'Transition',
  175. ];
  176. this.selector = this._getSelector(selector);
  177. this.firstElement = this._getFirstEl();
  178. return this;
  179. }
  180. lgQuery.generateUUID = function () {
  181. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  182. var r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
  183. return v.toString(16);
  184. });
  185. };
  186. lgQuery.prototype._getSelector = function (selector, context) {
  187. if (context === void 0) { context = document; }
  188. if (typeof selector !== 'string') {
  189. return selector;
  190. }
  191. context = context || document;
  192. var fl = selector.substring(0, 1);
  193. if (fl === '#') {
  194. return context.querySelector(selector);
  195. }
  196. else {
  197. return context.querySelectorAll(selector);
  198. }
  199. };
  200. lgQuery.prototype._each = function (func) {
  201. if (!this.selector) {
  202. return this;
  203. }
  204. if (this.selector.length !== undefined) {
  205. [].forEach.call(this.selector, func);
  206. }
  207. else {
  208. func(this.selector, 0);
  209. }
  210. return this;
  211. };
  212. lgQuery.prototype._setCssVendorPrefix = function (el, cssProperty, value) {
  213. // prettier-ignore
  214. var property = cssProperty.replace(/-([a-z])/gi, function (s, group1) {
  215. return group1.toUpperCase();
  216. });
  217. if (this.cssVenderPrefixes.indexOf(property) !== -1) {
  218. el.style[property.charAt(0).toLowerCase() + property.slice(1)] = value;
  219. el.style['webkit' + property] = value;
  220. el.style['moz' + property] = value;
  221. el.style['ms' + property] = value;
  222. el.style['o' + property] = value;
  223. }
  224. else {
  225. el.style[property] = value;
  226. }
  227. };
  228. lgQuery.prototype._getFirstEl = function () {
  229. if (this.selector && this.selector.length !== undefined) {
  230. return this.selector[0];
  231. }
  232. else {
  233. return this.selector;
  234. }
  235. };
  236. lgQuery.prototype.isEventMatched = function (event, eventName) {
  237. var eventNamespace = eventName.split('.');
  238. return event
  239. .split('.')
  240. .filter(function (e) { return e; })
  241. .every(function (e) {
  242. return eventNamespace.indexOf(e) !== -1;
  243. });
  244. };
  245. lgQuery.prototype.attr = function (attr, value) {
  246. if (value === undefined) {
  247. if (!this.firstElement) {
  248. return '';
  249. }
  250. return this.firstElement.getAttribute(attr);
  251. }
  252. this._each(function (el) {
  253. el.setAttribute(attr, value);
  254. });
  255. return this;
  256. };
  257. lgQuery.prototype.find = function (selector) {
  258. return $LG(this._getSelector(selector, this.selector));
  259. };
  260. lgQuery.prototype.first = function () {
  261. if (this.selector && this.selector.length !== undefined) {
  262. return $LG(this.selector[0]);
  263. }
  264. else {
  265. return $LG(this.selector);
  266. }
  267. };
  268. lgQuery.prototype.eq = function (index) {
  269. return $LG(this.selector[index]);
  270. };
  271. lgQuery.prototype.parent = function () {
  272. return $LG(this.selector.parentElement);
  273. };
  274. lgQuery.prototype.get = function () {
  275. return this._getFirstEl();
  276. };
  277. lgQuery.prototype.removeAttr = function (attributes) {
  278. var attrs = attributes.split(' ');
  279. this._each(function (el) {
  280. attrs.forEach(function (attr) { return el.removeAttribute(attr); });
  281. });
  282. return this;
  283. };
  284. lgQuery.prototype.wrap = function (className) {
  285. if (!this.firstElement) {
  286. return this;
  287. }
  288. var wrapper = document.createElement('div');
  289. wrapper.className = className;
  290. this.firstElement.parentNode.insertBefore(wrapper, this.firstElement);
  291. this.firstElement.parentNode.removeChild(this.firstElement);
  292. wrapper.appendChild(this.firstElement);
  293. return this;
  294. };
  295. lgQuery.prototype.addClass = function (classNames) {
  296. if (classNames === void 0) { classNames = ''; }
  297. this._each(function (el) {
  298. // IE doesn't support multiple arguments
  299. classNames.split(' ').forEach(function (className) {
  300. if (className) {
  301. el.classList.add(className);
  302. }
  303. });
  304. });
  305. return this;
  306. };
  307. lgQuery.prototype.removeClass = function (classNames) {
  308. this._each(function (el) {
  309. // IE doesn't support multiple arguments
  310. classNames.split(' ').forEach(function (className) {
  311. if (className) {
  312. el.classList.remove(className);
  313. }
  314. });
  315. });
  316. return this;
  317. };
  318. lgQuery.prototype.hasClass = function (className) {
  319. if (!this.firstElement) {
  320. return false;
  321. }
  322. return this.firstElement.classList.contains(className);
  323. };
  324. lgQuery.prototype.hasAttribute = function (attribute) {
  325. if (!this.firstElement) {
  326. return false;
  327. }
  328. return this.firstElement.hasAttribute(attribute);
  329. };
  330. lgQuery.prototype.toggleClass = function (className) {
  331. if (!this.firstElement) {
  332. return this;
  333. }
  334. if (this.hasClass(className)) {
  335. this.removeClass(className);
  336. }
  337. else {
  338. this.addClass(className);
  339. }
  340. return this;
  341. };
  342. lgQuery.prototype.css = function (property, value) {
  343. var _this = this;
  344. this._each(function (el) {
  345. _this._setCssVendorPrefix(el, property, value);
  346. });
  347. return this;
  348. };
  349. // Need to pass separate namespaces for separate elements
  350. lgQuery.prototype.on = function (events, listener) {
  351. var _this = this;
  352. if (!this.selector) {
  353. return this;
  354. }
  355. events.split(' ').forEach(function (event) {
  356. if (!Array.isArray(lgQuery.eventListeners[event])) {
  357. lgQuery.eventListeners[event] = [];
  358. }
  359. lgQuery.eventListeners[event].push(listener);
  360. _this.selector.addEventListener(event.split('.')[0], listener);
  361. });
  362. return this;
  363. };
  364. // @todo - test this
  365. lgQuery.prototype.once = function (event, listener) {
  366. var _this = this;
  367. this.on(event, function () {
  368. _this.off(event);
  369. listener(event);
  370. });
  371. return this;
  372. };
  373. lgQuery.prototype.off = function (event) {
  374. var _this = this;
  375. if (!this.selector) {
  376. return this;
  377. }
  378. Object.keys(lgQuery.eventListeners).forEach(function (eventName) {
  379. if (_this.isEventMatched(event, eventName)) {
  380. lgQuery.eventListeners[eventName].forEach(function (listener) {
  381. _this.selector.removeEventListener(eventName.split('.')[0], listener);
  382. });
  383. lgQuery.eventListeners[eventName] = [];
  384. }
  385. });
  386. return this;
  387. };
  388. lgQuery.prototype.trigger = function (event, detail) {
  389. if (!this.firstElement) {
  390. return this;
  391. }
  392. var customEvent = new CustomEvent(event.split('.')[0], {
  393. detail: detail || null,
  394. });
  395. this.firstElement.dispatchEvent(customEvent);
  396. return this;
  397. };
  398. // Does not support IE
  399. lgQuery.prototype.load = function (url) {
  400. var _this = this;
  401. fetch(url).then(function (res) {
  402. _this.selector.innerHTML = res;
  403. });
  404. return this;
  405. };
  406. lgQuery.prototype.html = function (html) {
  407. if (html === undefined) {
  408. if (!this.firstElement) {
  409. return '';
  410. }
  411. return this.firstElement.innerHTML;
  412. }
  413. this._each(function (el) {
  414. el.innerHTML = html;
  415. });
  416. return this;
  417. };
  418. lgQuery.prototype.append = function (html) {
  419. this._each(function (el) {
  420. if (typeof html === 'string') {
  421. el.insertAdjacentHTML('beforeend', html);
  422. }
  423. else {
  424. el.appendChild(html);
  425. }
  426. });
  427. return this;
  428. };
  429. lgQuery.prototype.prepend = function (html) {
  430. this._each(function (el) {
  431. el.insertAdjacentHTML('afterbegin', html);
  432. });
  433. return this;
  434. };
  435. lgQuery.prototype.remove = function () {
  436. this._each(function (el) {
  437. el.parentNode.removeChild(el);
  438. });
  439. return this;
  440. };
  441. lgQuery.prototype.empty = function () {
  442. this._each(function (el) {
  443. el.innerHTML = '';
  444. });
  445. return this;
  446. };
  447. lgQuery.prototype.scrollTop = function (scrollTop) {
  448. if (scrollTop !== undefined) {
  449. document.body.scrollTop = scrollTop;
  450. document.documentElement.scrollTop = scrollTop;
  451. return this;
  452. }
  453. else {
  454. return (window.pageYOffset ||
  455. document.documentElement.scrollTop ||
  456. document.body.scrollTop ||
  457. 0);
  458. }
  459. };
  460. lgQuery.prototype.scrollLeft = function (scrollLeft) {
  461. if (scrollLeft !== undefined) {
  462. document.body.scrollLeft = scrollLeft;
  463. document.documentElement.scrollLeft = scrollLeft;
  464. return this;
  465. }
  466. else {
  467. return (window.pageXOffset ||
  468. document.documentElement.scrollLeft ||
  469. document.body.scrollLeft ||
  470. 0);
  471. }
  472. };
  473. lgQuery.prototype.offset = function () {
  474. if (!this.firstElement) {
  475. return {
  476. left: 0,
  477. top: 0,
  478. };
  479. }
  480. var rect = this.firstElement.getBoundingClientRect();
  481. var bodyMarginLeft = $LG('body').style().marginLeft;
  482. // Minus body margin - https://stackoverflow.com/questions/30711548/is-getboundingclientrect-left-returning-a-wrong-value
  483. return {
  484. left: rect.left - parseFloat(bodyMarginLeft) + this.scrollLeft(),
  485. top: rect.top + this.scrollTop(),
  486. };
  487. };
  488. lgQuery.prototype.style = function () {
  489. if (!this.firstElement) {
  490. return {};
  491. }
  492. return (this.firstElement.currentStyle ||
  493. window.getComputedStyle(this.firstElement));
  494. };
  495. // Width without padding and border even if box-sizing is used.
  496. lgQuery.prototype.width = function () {
  497. var style = this.style();
  498. return (this.firstElement.clientWidth -
  499. parseFloat(style.paddingLeft) -
  500. parseFloat(style.paddingRight));
  501. };
  502. // Height without padding and border even if box-sizing is used.
  503. lgQuery.prototype.height = function () {
  504. var style = this.style();
  505. return (this.firstElement.clientHeight -
  506. parseFloat(style.paddingTop) -
  507. parseFloat(style.paddingBottom));
  508. };
  509. lgQuery.eventListeners = {};
  510. return lgQuery;
  511. }());
  512. function $LG(selector) {
  513. initLgPolyfills();
  514. return new lgQuery(selector);
  515. }
  516. var defaultDynamicOptions = [
  517. 'src',
  518. 'sources',
  519. 'subHtml',
  520. 'subHtmlUrl',
  521. 'html',
  522. 'video',
  523. 'poster',
  524. 'slideName',
  525. 'responsive',
  526. 'srcset',
  527. 'sizes',
  528. 'iframe',
  529. 'downloadUrl',
  530. 'download',
  531. 'width',
  532. 'facebookShareUrl',
  533. 'tweetText',
  534. 'iframeTitle',
  535. 'twitterShareUrl',
  536. 'pinterestShareUrl',
  537. 'pinterestText',
  538. 'fbHtml',
  539. 'disqusIdentifier',
  540. 'disqusUrl',
  541. ];
  542. // Convert html data-attribute to camalcase
  543. function convertToData(attr) {
  544. // FInd a way for lgsize
  545. if (attr === 'href') {
  546. return 'src';
  547. }
  548. attr = attr.replace('data-', '');
  549. attr = attr.charAt(0).toLowerCase() + attr.slice(1);
  550. attr = attr.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
  551. return attr;
  552. }
  553. var utils = {
  554. /**
  555. * get possible width and height from the lgSize attribute. Used for ZoomFromOrigin option
  556. */
  557. getSize: function (el, container, spacing, defaultLgSize) {
  558. if (spacing === void 0) { spacing = 0; }
  559. var LGel = $LG(el);
  560. var lgSize = LGel.attr('data-lg-size') || defaultLgSize;
  561. if (!lgSize) {
  562. return;
  563. }
  564. var isResponsiveSizes = lgSize.split(',');
  565. // if at-least two viewport sizes are available
  566. if (isResponsiveSizes[1]) {
  567. var wWidth = window.innerWidth;
  568. for (var i = 0; i < isResponsiveSizes.length; i++) {
  569. var size_1 = isResponsiveSizes[i];
  570. var responsiveWidth = parseInt(size_1.split('-')[2], 10);
  571. if (responsiveWidth > wWidth) {
  572. lgSize = size_1;
  573. break;
  574. }
  575. // take last item as last option
  576. if (i === isResponsiveSizes.length - 1) {
  577. lgSize = size_1;
  578. }
  579. }
  580. }
  581. var size = lgSize.split('-');
  582. var width = parseInt(size[0], 10);
  583. var height = parseInt(size[1], 10);
  584. var cWidth = container.width();
  585. var cHeight = container.height() - spacing;
  586. var maxWidth = Math.min(cWidth, width);
  587. var maxHeight = Math.min(cHeight, height);
  588. var ratio = Math.min(maxWidth / width, maxHeight / height);
  589. return { width: width * ratio, height: height * ratio };
  590. },
  591. /**
  592. * @desc Get transform value based on the imageSize. Used for ZoomFromOrigin option
  593. * @param {jQuery Element}
  594. * @returns {String} Transform CSS string
  595. */
  596. getTransform: function (el, container, top, bottom, imageSize) {
  597. if (!imageSize) {
  598. return;
  599. }
  600. var LGel = $LG(el).find('img').first();
  601. if (!LGel.get()) {
  602. return;
  603. }
  604. var containerRect = container.get().getBoundingClientRect();
  605. var wWidth = containerRect.width;
  606. // using innerWidth to include mobile safari bottom bar
  607. var wHeight = container.height() - (top + bottom);
  608. var elWidth = LGel.width();
  609. var elHeight = LGel.height();
  610. var elStyle = LGel.style();
  611. var x = (wWidth - elWidth) / 2 -
  612. LGel.offset().left +
  613. (parseFloat(elStyle.paddingLeft) || 0) +
  614. (parseFloat(elStyle.borderLeft) || 0) +
  615. $LG(window).scrollLeft() +
  616. containerRect.left;
  617. var y = (wHeight - elHeight) / 2 -
  618. LGel.offset().top +
  619. (parseFloat(elStyle.paddingTop) || 0) +
  620. (parseFloat(elStyle.borderTop) || 0) +
  621. $LG(window).scrollTop() +
  622. top;
  623. var scX = elWidth / imageSize.width;
  624. var scY = elHeight / imageSize.height;
  625. var transform = 'translate3d(' +
  626. (x *= -1) +
  627. 'px, ' +
  628. (y *= -1) +
  629. 'px, 0) scale3d(' +
  630. scX +
  631. ', ' +
  632. scY +
  633. ', 1)';
  634. return transform;
  635. },
  636. getIframeMarkup: function (iframeWidth, iframeHeight, iframeMaxWidth, iframeMaxHeight, src, iframeTitle) {
  637. var title = iframeTitle ? 'title="' + iframeTitle + '"' : '';
  638. return "<div class=\"lg-video-cont lg-has-iframe\" style=\"width:" + iframeWidth + "; max-width:" + iframeMaxWidth + "; height: " + iframeHeight + "; max-height:" + iframeMaxHeight + "\">\n <iframe class=\"lg-object\" frameborder=\"0\" " + title + " src=\"" + src + "\" allowfullscreen=\"true\"></iframe>\n </div>";
  639. },
  640. getImgMarkup: function (index, src, altAttr, srcset, sizes, sources) {
  641. var srcsetAttr = srcset ? "srcset=\"" + srcset + "\"" : '';
  642. var sizesAttr = sizes ? "sizes=\"" + sizes + "\"" : '';
  643. var imgMarkup = "<img " + altAttr + " " + srcsetAttr + " " + sizesAttr + " class=\"lg-object lg-image\" data-index=\"" + index + "\" src=\"" + src + "\" />";
  644. var sourceTag = '';
  645. if (sources) {
  646. var sourceObj = typeof sources === 'string' ? JSON.parse(sources) : sources;
  647. sourceTag = sourceObj.map(function (source) {
  648. var attrs = '';
  649. Object.keys(source).forEach(function (key) {
  650. // Do not remove the first space as it is required to separate the attributes
  651. attrs += " " + key + "=\"" + source[key] + "\"";
  652. });
  653. return "<source " + attrs + "></source>";
  654. });
  655. }
  656. return "" + sourceTag + imgMarkup;
  657. },
  658. // Get src from responsive src
  659. getResponsiveSrc: function (srcItms) {
  660. var rsWidth = [];
  661. var rsSrc = [];
  662. var src = '';
  663. for (var i = 0; i < srcItms.length; i++) {
  664. var _src = srcItms[i].split(' ');
  665. // Manage empty space
  666. if (_src[0] === '') {
  667. _src.splice(0, 1);
  668. }
  669. rsSrc.push(_src[0]);
  670. rsWidth.push(_src[1]);
  671. }
  672. var wWidth = window.innerWidth;
  673. for (var j = 0; j < rsWidth.length; j++) {
  674. if (parseInt(rsWidth[j], 10) > wWidth) {
  675. src = rsSrc[j];
  676. break;
  677. }
  678. }
  679. return src;
  680. },
  681. isImageLoaded: function (img) {
  682. if (!img)
  683. return false;
  684. // During the onload event, IE correctly identifies any images that
  685. // weren’t downloaded as not complete. Others should too. Gecko-based
  686. // browsers act like NS4 in that they report this incorrectly.
  687. if (!img.complete) {
  688. return false;
  689. }
  690. // However, they do have two very useful properties: naturalWidth and
  691. // naturalHeight. These give the true size of the image. If it failed
  692. // to load, either of these should be zero.
  693. if (img.naturalWidth === 0) {
  694. return false;
  695. }
  696. // No other way of checking: assume it’s ok.
  697. return true;
  698. },
  699. getVideoPosterMarkup: function (_poster, dummyImg, videoContStyle, playVideoString, _isVideo) {
  700. var videoClass = '';
  701. if (_isVideo && _isVideo.youtube) {
  702. videoClass = 'lg-has-youtube';
  703. }
  704. else if (_isVideo && _isVideo.vimeo) {
  705. videoClass = 'lg-has-vimeo';
  706. }
  707. else {
  708. videoClass = 'lg-has-html5';
  709. }
  710. return "<div class=\"lg-video-cont " + videoClass + "\" style=\"" + videoContStyle + "\">\n <div class=\"lg-video-play-button\">\n <svg\n viewBox=\"0 0 20 20\"\n preserveAspectRatio=\"xMidYMid\"\n focusable=\"false\"\n aria-labelledby=\"" + playVideoString + "\"\n role=\"img\"\n class=\"lg-video-play-icon\"\n >\n <title>" + playVideoString + "</title>\n <polygon class=\"lg-video-play-icon-inner\" points=\"1,0 20,10 1,20\"></polygon>\n </svg>\n <svg class=\"lg-video-play-icon-bg\" viewBox=\"0 0 50 50\" focusable=\"false\">\n <circle cx=\"50%\" cy=\"50%\" r=\"20\"></circle></svg>\n <svg class=\"lg-video-play-icon-circle\" viewBox=\"0 0 50 50\" focusable=\"false\">\n <circle cx=\"50%\" cy=\"50%\" r=\"20\"></circle>\n </svg>\n </div>\n " + (dummyImg || '') + "\n <img class=\"lg-object lg-video-poster\" src=\"" + _poster + "\" />\n </div>";
  711. },
  712. /**
  713. * @desc Create dynamic elements array from gallery items when dynamic option is false
  714. * It helps to avoid frequent DOM interaction
  715. * and avoid multiple checks for dynamic elments
  716. *
  717. * @returns {Array} dynamicEl
  718. */
  719. getDynamicOptions: function (items, extraProps, getCaptionFromTitleOrAlt, exThumbImage) {
  720. var dynamicElements = [];
  721. var availableDynamicOptions = __spreadArrays(defaultDynamicOptions, extraProps);
  722. [].forEach.call(items, function (item) {
  723. var dynamicEl = {};
  724. for (var i = 0; i < item.attributes.length; i++) {
  725. var attr = item.attributes[i];
  726. if (attr.specified) {
  727. var dynamicAttr = convertToData(attr.name);
  728. var label = '';
  729. if (availableDynamicOptions.indexOf(dynamicAttr) > -1) {
  730. label = dynamicAttr;
  731. }
  732. if (label) {
  733. dynamicEl[label] = attr.value;
  734. }
  735. }
  736. }
  737. var currentItem = $LG(item);
  738. var alt = currentItem.find('img').first().attr('alt');
  739. var title = currentItem.attr('title');
  740. var thumb = exThumbImage
  741. ? currentItem.attr(exThumbImage)
  742. : currentItem.find('img').first().attr('src');
  743. dynamicEl.thumb = thumb;
  744. if (getCaptionFromTitleOrAlt && !dynamicEl.subHtml) {
  745. dynamicEl.subHtml = title || alt || '';
  746. }
  747. dynamicEl.alt = alt || title || '';
  748. dynamicElements.push(dynamicEl);
  749. });
  750. return dynamicElements;
  751. },
  752. isMobile: function () {
  753. return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
  754. },
  755. /**
  756. * @desc Check the given src is video
  757. * @param {String} src
  758. * @return {Object} video type
  759. * Ex:{ youtube : ["//www.youtube.com/watch?v=c0asJgSyxcY", "c0asJgSyxcY"] }
  760. *
  761. * @todo - this information can be moved to dynamicEl to avoid frequent calls
  762. */
  763. isVideo: function (src, isHTML5VIdeo, index) {
  764. if (!src) {
  765. if (isHTML5VIdeo) {
  766. return {
  767. html5: true,
  768. };
  769. }
  770. else {
  771. console.error('lightGallery :- data-src is not provided on slide item ' +
  772. (index + 1) +
  773. '. Please make sure the selector property is properly configured. More info - https://www.lightgalleryjs.com/demos/html-markup/');
  774. return;
  775. }
  776. }
  777. var youtube = src.match(/\/\/(?:www\.)?youtu(?:\.be|be\.com|be-nocookie\.com)\/(?:watch\?v=|embed\/)?([a-z0-9\-\_\%]+)([\&|?][\S]*)*/i);
  778. var vimeo = src.match(/\/\/(?:www\.)?(?:player\.)?vimeo.com\/(?:video\/)?([0-9a-z\-_]+)(.*)?/i);
  779. var wistia = src.match(/https?:\/\/(.+)?(wistia\.com|wi\.st)\/(medias|embed)\/([0-9a-z\-_]+)(.*)/);
  780. if (youtube) {
  781. return {
  782. youtube: youtube,
  783. };
  784. }
  785. else if (vimeo) {
  786. return {
  787. vimeo: vimeo,
  788. };
  789. }
  790. else if (wistia) {
  791. return {
  792. wistia: wistia,
  793. };
  794. }
  795. },
  796. };
  797. // @ref - https://stackoverflow.com/questions/3971841/how-to-resize-images-proportionally-keeping-the-aspect-ratio
  798. // @ref - https://2ality.com/2017/04/setting-up-multi-platform-packages.html
  799. // Unique id for each gallery
  800. var lgId = 0;
  801. var LightGallery = /** @class */ (function () {
  802. function LightGallery(element, options) {
  803. this.lgOpened = false;
  804. this.index = 0;
  805. // lightGallery modules
  806. this.plugins = [];
  807. // false when lightGallery load first slide content;
  808. this.lGalleryOn = false;
  809. // True when a slide animation is in progress
  810. this.lgBusy = false;
  811. this.currentItemsInDom = [];
  812. // Scroll top value before lightGallery is opened
  813. this.prevScrollTop = 0;
  814. this.isDummyImageRemoved = false;
  815. this.dragOrSwipeEnabled = false;
  816. this.mediaContainerPosition = {
  817. top: 0,
  818. bottom: 0,
  819. };
  820. if (!element) {
  821. return this;
  822. }
  823. lgId++;
  824. this.lgId = lgId;
  825. this.el = element;
  826. this.LGel = $LG(element);
  827. this.generateSettings(options);
  828. this.buildModules();
  829. // When using dynamic mode, ensure dynamicEl is an array
  830. if (this.settings.dynamic &&
  831. this.settings.dynamicEl !== undefined &&
  832. !Array.isArray(this.settings.dynamicEl)) {
  833. throw 'When using dynamic mode, you must also define dynamicEl as an Array.';
  834. }
  835. this.galleryItems = this.getItems();
  836. this.normalizeSettings();
  837. // Gallery items
  838. this.init();
  839. this.validateLicense();
  840. return this;
  841. }
  842. LightGallery.prototype.generateSettings = function (options) {
  843. // lightGallery settings
  844. this.settings = __assign(__assign({}, lightGalleryCoreSettings), options);
  845. if (this.settings.isMobile &&
  846. typeof this.settings.isMobile === 'function'
  847. ? this.settings.isMobile()
  848. : utils.isMobile()) {
  849. var mobileSettings = __assign(__assign({}, this.settings.mobileSettings), this.settings.mobileSettings);
  850. this.settings = __assign(__assign({}, this.settings), mobileSettings);
  851. }
  852. };
  853. LightGallery.prototype.normalizeSettings = function () {
  854. if (this.settings.slideEndAnimation) {
  855. this.settings.hideControlOnEnd = false;
  856. }
  857. if (!this.settings.closable) {
  858. this.settings.swipeToClose = false;
  859. }
  860. // And reset it on close to get the correct value next time
  861. this.zoomFromOrigin = this.settings.zoomFromOrigin;
  862. // At the moment, Zoom from image doesn't support dynamic options
  863. // @todo add zoomFromOrigin support for dynamic images
  864. if (this.settings.dynamic) {
  865. this.zoomFromOrigin = false;
  866. }
  867. if (!this.settings.container) {
  868. this.settings.container = document.body;
  869. }
  870. // settings.preload should not be grater than $item.length
  871. this.settings.preload = Math.min(this.settings.preload, this.galleryItems.length);
  872. };
  873. LightGallery.prototype.init = function () {
  874. var _this = this;
  875. this.addSlideVideoInfo(this.galleryItems);
  876. this.buildStructure();
  877. this.LGel.trigger(lGEvents.init, {
  878. instance: this,
  879. });
  880. if (this.settings.keyPress) {
  881. this.keyPress();
  882. }
  883. setTimeout(function () {
  884. _this.enableDrag();
  885. _this.enableSwipe();
  886. _this.triggerPosterClick();
  887. }, 50);
  888. this.arrow();
  889. if (this.settings.mousewheel) {
  890. this.mousewheel();
  891. }
  892. if (!this.settings.dynamic) {
  893. this.openGalleryOnItemClick();
  894. }
  895. };
  896. LightGallery.prototype.openGalleryOnItemClick = function () {
  897. var _this = this;
  898. var _loop_1 = function (index) {
  899. var element = this_1.items[index];
  900. var $element = $LG(element);
  901. // Using different namespace for click because click event should not unbind if selector is same object('this')
  902. // @todo manage all event listners - should have namespace that represent element
  903. var uuid = lgQuery.generateUUID();
  904. $element
  905. .attr('data-lg-id', uuid)
  906. .on("click.lgcustom-item-" + uuid, function (e) {
  907. e.preventDefault();
  908. var currentItemIndex = _this.settings.index || index;
  909. _this.openGallery(currentItemIndex, element);
  910. });
  911. };
  912. var this_1 = this;
  913. // Using for loop instead of using bubbling as the items can be any html element.
  914. for (var index = 0; index < this.items.length; index++) {
  915. _loop_1(index);
  916. }
  917. };
  918. /**
  919. * Module constructor
  920. * Modules are build incrementally.
  921. * Gallery should be opened only once all the modules are initialized.
  922. * use moduleBuildTimeout to make sure this
  923. */
  924. LightGallery.prototype.buildModules = function () {
  925. var _this = this;
  926. this.settings.plugins.forEach(function (plugin) {
  927. _this.plugins.push(new plugin(_this, $LG));
  928. });
  929. };
  930. LightGallery.prototype.validateLicense = function () {
  931. if (!this.settings.licenseKey) {
  932. console.error('Please provide a valid license key');
  933. }
  934. else if (this.settings.licenseKey === '0000-0000-000-0000') {
  935. console.warn("lightGallery: " + this.settings.licenseKey + " license key is not valid for production use");
  936. }
  937. };
  938. LightGallery.prototype.getSlideItem = function (index) {
  939. return $LG(this.getSlideItemId(index));
  940. };
  941. LightGallery.prototype.getSlideItemId = function (index) {
  942. return "#lg-item-" + this.lgId + "-" + index;
  943. };
  944. LightGallery.prototype.getIdName = function (id) {
  945. return id + "-" + this.lgId;
  946. };
  947. LightGallery.prototype.getElementById = function (id) {
  948. return $LG("#" + this.getIdName(id));
  949. };
  950. LightGallery.prototype.manageSingleSlideClassName = function () {
  951. if (this.galleryItems.length < 2) {
  952. this.outer.addClass('lg-single-item');
  953. }
  954. else {
  955. this.outer.removeClass('lg-single-item');
  956. }
  957. };
  958. LightGallery.prototype.buildStructure = function () {
  959. var _this = this;
  960. var container = this.$container && this.$container.get();
  961. if (container) {
  962. return;
  963. }
  964. var controls = '';
  965. var subHtmlCont = '';
  966. // Create controls
  967. if (this.settings.controls) {
  968. controls = "<button type=\"button\" id=\"" + this.getIdName('lg-prev') + "\" aria-label=\"" + this.settings.strings['previousSlide'] + "\" class=\"lg-prev lg-icon\"> " + this.settings.prevHtml + " </button>\n <button type=\"button\" id=\"" + this.getIdName('lg-next') + "\" aria-label=\"" + this.settings.strings['nextSlide'] + "\" class=\"lg-next lg-icon\"> " + this.settings.nextHtml + " </button>";
  969. }
  970. if (this.settings.appendSubHtmlTo !== '.lg-item') {
  971. subHtmlCont =
  972. '<div class="lg-sub-html" role="status" aria-live="polite"></div>';
  973. }
  974. var addClasses = '';
  975. if (this.settings.allowMediaOverlap) {
  976. // Do not remove space before last single quote
  977. addClasses += 'lg-media-overlap ';
  978. }
  979. var ariaLabelledby = this.settings.ariaLabelledby
  980. ? 'aria-labelledby="' + this.settings.ariaLabelledby + '"'
  981. : '';
  982. var ariaDescribedby = this.settings.ariaDescribedby
  983. ? 'aria-describedby="' + this.settings.ariaDescribedby + '"'
  984. : '';
  985. var containerClassName = "lg-container " + this.settings.addClass + " " + (document.body !== this.settings.container ? 'lg-inline' : '');
  986. var closeIcon = this.settings.closable && this.settings.showCloseIcon
  987. ? "<button type=\"button\" aria-label=\"" + this.settings.strings['closeGallery'] + "\" id=\"" + this.getIdName('lg-close') + "\" class=\"lg-close lg-icon\"></button>"
  988. : '';
  989. var maximizeIcon = this.settings.showMaximizeIcon
  990. ? "<button type=\"button\" aria-label=\"" + this.settings.strings['toggleMaximize'] + "\" id=\"" + this.getIdName('lg-maximize') + "\" class=\"lg-maximize lg-icon\"></button>"
  991. : '';
  992. var template = "\n <div class=\"" + containerClassName + "\" id=\"" + this.getIdName('lg-container') + "\" tabindex=\"-1\" aria-modal=\"true\" " + ariaLabelledby + " " + ariaDescribedby + " role=\"dialog\"\n >\n <div id=\"" + this.getIdName('lg-backdrop') + "\" class=\"lg-backdrop\"></div>\n\n <div id=\"" + this.getIdName('lg-outer') + "\" class=\"lg-outer lg-use-css3 lg-css3 lg-hide-items " + addClasses + " \">\n\n <div id=\"" + this.getIdName('lg-content') + "\" class=\"lg-content\">\n <div id=\"" + this.getIdName('lg-inner') + "\" class=\"lg-inner\">\n </div>\n " + controls + "\n </div>\n <div id=\"" + this.getIdName('lg-toolbar') + "\" class=\"lg-toolbar lg-group\">\n " + maximizeIcon + "\n " + closeIcon + "\n </div>\n " + (this.settings.appendSubHtmlTo === '.lg-outer'
  993. ? subHtmlCont
  994. : '') + "\n <div id=\"" + this.getIdName('lg-components') + "\" class=\"lg-components\">\n " + (this.settings.appendSubHtmlTo === '.lg-sub-html'
  995. ? subHtmlCont
  996. : '') + "\n </div>\n </div>\n </div>\n ";
  997. $LG(this.settings.container).append(template);
  998. if (document.body !== this.settings.container) {
  999. $LG(this.settings.container).css('position', 'relative');
  1000. }
  1001. this.outer = this.getElementById('lg-outer');
  1002. this.$lgComponents = this.getElementById('lg-components');
  1003. this.$backdrop = this.getElementById('lg-backdrop');
  1004. this.$container = this.getElementById('lg-container');
  1005. this.$inner = this.getElementById('lg-inner');
  1006. this.$content = this.getElementById('lg-content');
  1007. this.$toolbar = this.getElementById('lg-toolbar');
  1008. this.$backdrop.css('transition-duration', this.settings.backdropDuration + 'ms');
  1009. var outerClassNames = this.settings.mode + " ";
  1010. this.manageSingleSlideClassName();
  1011. if (this.settings.enableDrag) {
  1012. outerClassNames += 'lg-grab ';
  1013. }
  1014. this.outer.addClass(outerClassNames);
  1015. this.$inner.css('transition-timing-function', this.settings.easing);
  1016. this.$inner.css('transition-duration', this.settings.speed + 'ms');
  1017. if (this.settings.download) {
  1018. this.$toolbar.append("<a id=\"" + this.getIdName('lg-download') + "\" target=\"_blank\" rel=\"noopener\" aria-label=\"" + this.settings.strings['download'] + "\" download class=\"lg-download lg-icon\"></a>");
  1019. }
  1020. this.counter();
  1021. $LG(window).on("resize.lg.global" + this.lgId + " orientationchange.lg.global" + this.lgId, function () {
  1022. _this.refreshOnResize();
  1023. });
  1024. this.hideBars();
  1025. this.manageCloseGallery();
  1026. this.toggleMaximize();
  1027. this.initModules();
  1028. };
  1029. LightGallery.prototype.refreshOnResize = function () {
  1030. if (this.lgOpened) {
  1031. var currentGalleryItem = this.galleryItems[this.index];
  1032. var __slideVideoInfo = currentGalleryItem.__slideVideoInfo;
  1033. this.mediaContainerPosition = this.getMediaContainerPosition();
  1034. var _a = this.mediaContainerPosition, top_1 = _a.top, bottom = _a.bottom;
  1035. this.currentImageSize = utils.getSize(this.items[this.index], this.outer, top_1 + bottom, __slideVideoInfo && this.settings.videoMaxSize);
  1036. if (__slideVideoInfo) {
  1037. this.resizeVideoSlide(this.index, this.currentImageSize);
  1038. }
  1039. if (this.zoomFromOrigin && !this.isDummyImageRemoved) {
  1040. var imgStyle = this.getDummyImgStyles(this.currentImageSize);
  1041. this.outer
  1042. .find('.lg-current .lg-dummy-img')
  1043. .first()
  1044. .attr('style', imgStyle);
  1045. }
  1046. this.LGel.trigger(lGEvents.containerResize);
  1047. }
  1048. };
  1049. LightGallery.prototype.resizeVideoSlide = function (index, imageSize) {
  1050. var lgVideoStyle = this.getVideoContStyle(imageSize);
  1051. var currentSlide = this.getSlideItem(index);
  1052. currentSlide.find('.lg-video-cont').attr('style', lgVideoStyle);
  1053. };
  1054. /**
  1055. * Update slides dynamically.
  1056. * Add, edit or delete slides dynamically when lightGallery is opened.
  1057. * Modify the current gallery items and pass it via updateSlides method
  1058. * @note
  1059. * - Do not mutate existing lightGallery items directly.
  1060. * - Always pass new list of gallery items
  1061. * - You need to take care of thumbnails outside the gallery if any
  1062. * - user this method only if you want to update slides when the gallery is opened. Otherwise, use `refresh()` method.
  1063. * @param items Gallery items
  1064. * @param index After the update operation, which slide gallery should navigate to
  1065. * @category lGPublicMethods
  1066. * @example
  1067. * const plugin = lightGallery();
  1068. *
  1069. * // Adding slides dynamically
  1070. * let galleryItems = [
  1071. * // Access existing lightGallery items
  1072. * // galleryItems are automatically generated internally from the gallery HTML markup
  1073. * // or directly from galleryItems when dynamic gallery is used
  1074. * ...plugin.galleryItems,
  1075. * ...[
  1076. * {
  1077. * src: 'img/img-1.png',
  1078. * thumb: 'img/thumb1.png',
  1079. * },
  1080. * ],
  1081. * ];
  1082. * plugin.updateSlides(
  1083. * galleryItems,
  1084. * plugin.index,
  1085. * );
  1086. *
  1087. *
  1088. * // Remove slides dynamically
  1089. * galleryItems = JSON.parse(
  1090. * JSON.stringify(updateSlideInstance.galleryItems),
  1091. * );
  1092. * galleryItems.shift();
  1093. * updateSlideInstance.updateSlides(galleryItems, 1);
  1094. * @see <a href="/demos/update-slides/">Demo</a>
  1095. */
  1096. LightGallery.prototype.updateSlides = function (items, index) {
  1097. if (this.index > items.length - 1) {
  1098. this.index = items.length - 1;
  1099. }
  1100. if (items.length === 1) {
  1101. this.index = 0;
  1102. }
  1103. if (!items.length) {
  1104. this.closeGallery();
  1105. return;
  1106. }
  1107. var currentSrc = this.galleryItems[index].src;
  1108. this.galleryItems = items;
  1109. this.updateControls();
  1110. this.$inner.empty();
  1111. this.currentItemsInDom = [];
  1112. var _index = 0;
  1113. // Find the current index based on source value of the slide
  1114. this.galleryItems.some(function (galleryItem, itemIndex) {
  1115. if (galleryItem.src === currentSrc) {
  1116. _index = itemIndex;
  1117. return true;
  1118. }
  1119. return false;
  1120. });
  1121. this.currentItemsInDom = this.organizeSlideItems(_index, -1);
  1122. this.loadContent(_index, true);
  1123. this.getSlideItem(_index).addClass('lg-current');
  1124. this.index = _index;
  1125. this.updateCurrentCounter(_index);
  1126. this.LGel.trigger(lGEvents.updateSlides);
  1127. };
  1128. // Get gallery items based on multiple conditions
  1129. LightGallery.prototype.getItems = function () {
  1130. // Gallery items
  1131. this.items = [];
  1132. if (!this.settings.dynamic) {
  1133. if (this.settings.selector === 'this') {
  1134. this.items.push(this.el);
  1135. }
  1136. else if (this.settings.selector) {
  1137. if (typeof this.settings.selector === 'string') {
  1138. if (this.settings.selectWithin) {
  1139. var selectWithin = $LG(this.settings.selectWithin);
  1140. this.items = selectWithin
  1141. .find(this.settings.selector)
  1142. .get();
  1143. }
  1144. else {
  1145. this.items = this.el.querySelectorAll(this.settings.selector);
  1146. }
  1147. }
  1148. else {
  1149. this.items = this.settings.selector;
  1150. }
  1151. }
  1152. else {
  1153. this.items = this.el.children;
  1154. }
  1155. return utils.getDynamicOptions(this.items, this.settings.extraProps, this.settings.getCaptionFromTitleOrAlt, this.settings.exThumbImage);
  1156. }
  1157. else {
  1158. return this.settings.dynamicEl || [];
  1159. }
  1160. };
  1161. /**
  1162. * Open lightGallery.
  1163. * Open gallery with specific slide by passing index of the slide as parameter.
  1164. * @category lGPublicMethods
  1165. * @param {Number} index - index of the slide
  1166. * @param {HTMLElement} element - Which image lightGallery should zoom from
  1167. *
  1168. * @example
  1169. * const $dynamicGallery = document.getElementById('dynamic-gallery-demo');
  1170. * const dynamicGallery = lightGallery($dynamicGallery, {
  1171. * dynamic: true,
  1172. * dynamicEl: [
  1173. * {
  1174. * src: 'img/1.jpg',
  1175. * thumb: 'img/thumb-1.jpg',
  1176. * subHtml: '<h4>Image 1 title</h4><p>Image 1 descriptions.</p>',
  1177. * },
  1178. * ...
  1179. * ],
  1180. * });
  1181. * $dynamicGallery.addEventListener('click', function () {
  1182. * // Starts with third item.(Optional).
  1183. * // This is useful if you want use dynamic mode with
  1184. * // custom thumbnails (thumbnails outside gallery),
  1185. * dynamicGallery.openGallery(2);
  1186. * });
  1187. *
  1188. */
  1189. LightGallery.prototype.openGallery = function (index, element) {
  1190. var _this = this;
  1191. if (index === void 0) { index = this.settings.index; }
  1192. // prevent accidental double execution
  1193. if (this.lgOpened)
  1194. return;
  1195. this.lgOpened = true;
  1196. this.outer.get().focus();
  1197. this.outer.removeClass('lg-hide-items');
  1198. // Add display block, but still has opacity 0
  1199. this.$container.addClass('lg-show');
  1200. var itemsToBeInsertedToDom = this.getItemsToBeInsertedToDom(index, index);
  1201. this.currentItemsInDom = itemsToBeInsertedToDom;
  1202. var items = '';
  1203. itemsToBeInsertedToDom.forEach(function (item) {
  1204. items = items + ("<div id=\"" + item + "\" class=\"lg-item\"></div>");
  1205. });
  1206. this.$inner.append(items);
  1207. this.addHtml(index);
  1208. var transform = '';
  1209. this.mediaContainerPosition = this.getMediaContainerPosition();
  1210. var _a = this.mediaContainerPosition, top = _a.top, bottom = _a.bottom;
  1211. if (!this.settings.allowMediaOverlap) {
  1212. this.setMediaContainerPosition(top, bottom);
  1213. }
  1214. var __slideVideoInfo = this.galleryItems[index].__slideVideoInfo;
  1215. if (this.zoomFromOrigin && element) {
  1216. this.currentImageSize = utils.getSize(element, this.outer, top + bottom, __slideVideoInfo && this.settings.videoMaxSize);
  1217. transform = utils.getTransform(element, this.outer, top, bottom, this.currentImageSize);
  1218. }
  1219. if (!this.zoomFromOrigin || !transform) {
  1220. this.outer.addClass(this.settings.startClass);
  1221. this.getSlideItem(index).removeClass('lg-complete');
  1222. }
  1223. var timeout = this.settings.zoomFromOrigin
  1224. ? 100
  1225. : this.settings.backdropDuration;
  1226. setTimeout(function () {
  1227. _this.outer.addClass('lg-components-open');
  1228. }, timeout);
  1229. this.index = index;
  1230. this.LGel.trigger(lGEvents.beforeOpen);
  1231. // add class lg-current to remove initial transition
  1232. this.getSlideItem(index).addClass('lg-current');
  1233. this.lGalleryOn = false;
  1234. // Store the current scroll top value to scroll back after closing the gallery..
  1235. this.prevScrollTop = $LG(window).scrollTop();
  1236. setTimeout(function () {
  1237. // Need to check both zoomFromOrigin and transform values as we need to set set the
  1238. // default opening animation if user missed to add the lg-size attribute
  1239. if (_this.zoomFromOrigin && transform) {
  1240. var currentSlide_1 = _this.getSlideItem(index);
  1241. currentSlide_1.css('transform', transform);
  1242. setTimeout(function () {
  1243. currentSlide_1
  1244. .addClass('lg-start-progress lg-start-end-progress')
  1245. .css('transition-duration', _this.settings.startAnimationDuration + 'ms');
  1246. _this.outer.addClass('lg-zoom-from-image');
  1247. });
  1248. setTimeout(function () {
  1249. currentSlide_1.css('transform', 'translate3d(0, 0, 0)');
  1250. }, 100);
  1251. }
  1252. setTimeout(function () {
  1253. _this.$backdrop.addClass('in');
  1254. _this.$container.addClass('lg-show-in');
  1255. }, 10);
  1256. // lg-visible class resets gallery opacity to 1
  1257. if (!_this.zoomFromOrigin || !transform) {
  1258. setTimeout(function () {
  1259. _this.outer.addClass('lg-visible');
  1260. }, _this.settings.backdropDuration);
  1261. }
  1262. // initiate slide function
  1263. _this.slide(index, false, false, false);
  1264. _this.LGel.trigger(lGEvents.afterOpen);
  1265. });
  1266. if (document.body === this.settings.container) {
  1267. $LG('html').addClass('lg-on');
  1268. }
  1269. };
  1270. /**
  1271. * Note - Changing the position of the media on every slide transition creates a flickering effect.
  1272. * Therefore, The height of the caption is calculated dynamically, only once based on the first slide caption.
  1273. * if you have dynamic captions for each media,
  1274. * you can provide an appropriate height for the captions via allowMediaOverlap option
  1275. */
  1276. LightGallery.prototype.getMediaContainerPosition = function () {
  1277. if (this.settings.allowMediaOverlap) {
  1278. return {
  1279. top: 0,
  1280. bottom: 0,
  1281. };
  1282. }
  1283. var top = this.$toolbar.get().clientHeight || 0;
  1284. var subHtml = this.outer.find('.lg-components .lg-sub-html').get();
  1285. var captionHeight = this.settings.defaultCaptionHeight ||
  1286. (subHtml && subHtml.clientHeight) ||
  1287. 0;
  1288. var thumbContainer = this.outer.find('.lg-thumb-outer').get();
  1289. var thumbHeight = thumbContainer ? thumbContainer.clientHeight : 0;
  1290. var bottom = thumbHeight + captionHeight;
  1291. return {
  1292. top: top,
  1293. bottom: bottom,
  1294. };
  1295. };
  1296. LightGallery.prototype.setMediaContainerPosition = function (top, bottom) {
  1297. if (top === void 0) { top = 0; }
  1298. if (bottom === void 0) { bottom = 0; }
  1299. this.$content.css('top', top + 'px').css('bottom', bottom + 'px');
  1300. };
  1301. LightGallery.prototype.hideBars = function () {
  1302. var _this = this;
  1303. // Hide controllers if mouse doesn't move for some period
  1304. setTimeout(function () {
  1305. _this.outer.removeClass('lg-hide-items');
  1306. if (_this.settings.hideBarsDelay > 0) {
  1307. _this.outer.on('mousemove.lg click.lg touchstart.lg', function () {
  1308. _this.outer.removeClass('lg-hide-items');
  1309. clearTimeout(_this.hideBarTimeout);
  1310. // Timeout will be cleared on each slide movement also
  1311. _this.hideBarTimeout = setTimeout(function () {
  1312. _this.outer.addClass('lg-hide-items');
  1313. }, _this.settings.hideBarsDelay);
  1314. });
  1315. _this.outer.trigger('mousemove.lg');
  1316. }
  1317. }, this.settings.showBarsAfter);
  1318. };
  1319. LightGallery.prototype.initPictureFill = function ($img) {
  1320. if (this.settings.supportLegacyBrowser) {
  1321. try {
  1322. picturefill({
  1323. elements: [$img.get()],
  1324. });
  1325. }
  1326. catch (e) {
  1327. console.warn('lightGallery :- If you want srcset or picture tag to be supported for older browser please include picturefil javascript library in your document.');
  1328. }
  1329. }
  1330. };
  1331. /**
  1332. * @desc Create image counter
  1333. * Ex: 1/10
  1334. */
  1335. LightGallery.prototype.counter = function () {
  1336. if (this.settings.counter) {
  1337. var counterHtml = "<div class=\"lg-counter\" role=\"status\" aria-live=\"polite\">\n <span id=\"" + this.getIdName('lg-counter-current') + "\" class=\"lg-counter-current\">" + (this.index + 1) + " </span> /\n <span id=\"" + this.getIdName('lg-counter-all') + "\" class=\"lg-counter-all\">" + this.galleryItems.length + " </span></div>";
  1338. this.outer.find(this.settings.appendCounterTo).append(counterHtml);
  1339. }
  1340. };
  1341. /**
  1342. * @desc add sub-html into the slide
  1343. * @param {Number} index - index of the slide
  1344. */
  1345. LightGallery.prototype.addHtml = function (index) {
  1346. var subHtml;
  1347. var subHtmlUrl;
  1348. if (this.galleryItems[index].subHtmlUrl) {
  1349. subHtmlUrl = this.galleryItems[index].subHtmlUrl;
  1350. }
  1351. else {
  1352. subHtml = this.galleryItems[index].subHtml;
  1353. }
  1354. if (!subHtmlUrl) {
  1355. if (subHtml) {
  1356. // get first letter of sub-html
  1357. // if first letter starts with . or # get the html form the jQuery object
  1358. var fL = subHtml.substring(0, 1);
  1359. if (fL === '.' || fL === '#') {
  1360. if (this.settings.subHtmlSelectorRelative &&
  1361. !this.settings.dynamic) {
  1362. subHtml = $LG(this.items)
  1363. .eq(index)
  1364. .find(subHtml)
  1365. .first()
  1366. .html();
  1367. }
  1368. else {
  1369. subHtml = $LG(subHtml).first().html();
  1370. }
  1371. }
  1372. }
  1373. else {
  1374. subHtml = '';
  1375. }
  1376. }
  1377. if (this.settings.appendSubHtmlTo !== '.lg-item') {
  1378. if (subHtmlUrl) {
  1379. this.outer.find('.lg-sub-html').load(subHtmlUrl);
  1380. }
  1381. else {
  1382. this.outer.find('.lg-sub-html').html(subHtml);
  1383. }
  1384. }
  1385. else {
  1386. var currentSlide = $LG(this.getSlideItemId(index));
  1387. if (subHtmlUrl) {
  1388. currentSlide.load(subHtmlUrl);
  1389. }
  1390. else {
  1391. currentSlide.append("<div class=\"lg-sub-html\">" + subHtml + "</div>");
  1392. }
  1393. }
  1394. // Add lg-empty-html class if title doesn't exist
  1395. if (typeof subHtml !== 'undefined' && subHtml !== null) {
  1396. if (subHtml === '') {
  1397. this.outer
  1398. .find(this.settings.appendSubHtmlTo)
  1399. .addClass('lg-empty-html');
  1400. }
  1401. else {
  1402. this.outer
  1403. .find(this.settings.appendSubHtmlTo)
  1404. .removeClass('lg-empty-html');
  1405. }
  1406. }
  1407. this.LGel.trigger(lGEvents.afterAppendSubHtml, {
  1408. index: index,
  1409. });
  1410. };
  1411. /**
  1412. * @desc Preload slides
  1413. * @param {Number} index - index of the slide
  1414. * @todo preload not working for the first slide, Also, should work for the first and last slide as well
  1415. */
  1416. LightGallery.prototype.preload = function (index) {
  1417. for (var i = 1; i <= this.settings.preload; i++) {
  1418. if (i >= this.galleryItems.length - index) {
  1419. break;
  1420. }
  1421. this.loadContent(index + i, false);
  1422. }
  1423. for (var j = 1; j <= this.settings.preload; j++) {
  1424. if (index - j < 0) {
  1425. break;
  1426. }
  1427. this.loadContent(index - j, false);
  1428. }
  1429. };
  1430. LightGallery.prototype.getDummyImgStyles = function (imageSize) {
  1431. if (!imageSize)
  1432. return '';
  1433. return "width:" + imageSize.width + "px;\n margin-left: -" + imageSize.width / 2 + "px;\n margin-top: -" + imageSize.height / 2 + "px;\n height:" + imageSize.height + "px";
  1434. };
  1435. LightGallery.prototype.getVideoContStyle = function (imageSize) {
  1436. if (!imageSize)
  1437. return '';
  1438. return "width:" + imageSize.width + "px;\n height:" + imageSize.height + "px";
  1439. };
  1440. LightGallery.prototype.getDummyImageContent = function ($currentSlide, index, alt) {
  1441. var $currentItem;
  1442. if (!this.settings.dynamic) {
  1443. $currentItem = $LG(this.items).eq(index);
  1444. }
  1445. if ($currentItem) {
  1446. var _dummyImgSrc = void 0;
  1447. if (!this.settings.exThumbImage) {
  1448. _dummyImgSrc = $currentItem.find('img').first().attr('src');
  1449. }
  1450. else {
  1451. _dummyImgSrc = $currentItem.attr(this.settings.exThumbImage);
  1452. }
  1453. if (!_dummyImgSrc)
  1454. return '';
  1455. var imgStyle = this.getDummyImgStyles(this.currentImageSize);
  1456. var dummyImgContent = "<img " + alt + " style=\"" + imgStyle + "\" class=\"lg-dummy-img\" src=\"" + _dummyImgSrc + "\" />";
  1457. $currentSlide.addClass('lg-first-slide');
  1458. this.outer.addClass('lg-first-slide-loading');
  1459. return dummyImgContent;
  1460. }
  1461. return '';
  1462. };
  1463. LightGallery.prototype.setImgMarkup = function (src, $currentSlide, index) {
  1464. var currentGalleryItem = this.galleryItems[index];
  1465. var alt = currentGalleryItem.alt, srcset = currentGalleryItem.srcset, sizes = currentGalleryItem.sizes, sources = currentGalleryItem.sources;
  1466. // Use the thumbnail as dummy image which will be resized to actual image size and
  1467. // displayed on top of actual image
  1468. var imgContent = '';
  1469. var altAttr = alt ? 'alt="' + alt + '"' : '';
  1470. if (this.isFirstSlideWithZoomAnimation()) {
  1471. imgContent = this.getDummyImageContent($currentSlide, index, altAttr);
  1472. }
  1473. else {
  1474. imgContent = utils.getImgMarkup(index, src, altAttr, srcset, sizes, sources);
  1475. }
  1476. var imgMarkup = "<picture class=\"lg-img-wrap\"> " + imgContent + "</picture>";
  1477. $currentSlide.prepend(imgMarkup);
  1478. };
  1479. LightGallery.prototype.onSlideObjectLoad = function ($slide, isHTML5VideoWithoutPoster, onLoad, onError) {
  1480. var mediaObject = $slide.find('.lg-object').first();
  1481. if (utils.isImageLoaded(mediaObject.get()) ||
  1482. isHTML5VideoWithoutPoster) {
  1483. onLoad();
  1484. }
  1485. else {
  1486. mediaObject.on('load.lg error.lg', function () {
  1487. onLoad && onLoad();
  1488. });
  1489. mediaObject.on('error.lg', function () {
  1490. onError && onError();
  1491. });
  1492. }
  1493. };
  1494. /**
  1495. *
  1496. * @param $el Current slide item
  1497. * @param index
  1498. * @param delay Delay is 0 except first time
  1499. * @param speed Speed is same as delay, except it is 0 if gallery is opened via hash plugin
  1500. * @param isFirstSlide
  1501. */
  1502. LightGallery.prototype.onLgObjectLoad = function (currentSlide, index, delay, speed, isFirstSlide, isHTML5VideoWithoutPoster) {
  1503. var _this = this;
  1504. this.onSlideObjectLoad(currentSlide, isHTML5VideoWithoutPoster, function () {
  1505. _this.triggerSlideItemLoad(currentSlide, index, delay, speed, isFirstSlide);
  1506. }, function () {
  1507. currentSlide.addClass('lg-complete lg-complete_');
  1508. currentSlide.html('<span class="lg-error-msg">Oops... Failed to load content...</span>');
  1509. });
  1510. };
  1511. LightGallery.prototype.triggerSlideItemLoad = function ($currentSlide, index, delay, speed, isFirstSlide) {
  1512. var _this = this;
  1513. var currentGalleryItem = this.galleryItems[index];
  1514. // Adding delay for video slides without poster for better performance and user experience
  1515. // Videos should start playing once once the gallery is completely loaded
  1516. var _speed = isFirstSlide &&
  1517. this.getSlideType(currentGalleryItem) === 'video' &&
  1518. !currentGalleryItem.poster
  1519. ? speed
  1520. : 0;
  1521. setTimeout(function () {
  1522. $currentSlide.addClass('lg-complete lg-complete_');
  1523. _this.LGel.trigger(lGEvents.slideItemLoad, {
  1524. index: index,
  1525. delay: delay || 0,
  1526. isFirstSlide: isFirstSlide,
  1527. });
  1528. }, _speed);
  1529. };
  1530. LightGallery.prototype.isFirstSlideWithZoomAnimation = function () {
  1531. return !!(!this.lGalleryOn &&
  1532. this.zoomFromOrigin &&
  1533. this.currentImageSize);
  1534. };
  1535. // Add video slideInfo
  1536. LightGallery.prototype.addSlideVideoInfo = function (items) {
  1537. var _this = this;
  1538. items.forEach(function (element, index) {
  1539. element.__slideVideoInfo = utils.isVideo(element.src, !!element.video, index);
  1540. if (element.__slideVideoInfo &&
  1541. _this.settings.loadYouTubePoster &&
  1542. !element.poster &&
  1543. element.__slideVideoInfo.youtube) {
  1544. element.poster = "//img.youtube.com/vi/" + element.__slideVideoInfo.youtube[1] + "/maxresdefault.jpg";
  1545. }
  1546. });
  1547. };
  1548. /**
  1549. * Load slide content into slide.
  1550. * This is used to load content into slides that is not visible too
  1551. * @param {Number} index - index of the slide.
  1552. * @param {Boolean} rec - if true call loadcontent() function again.
  1553. */
  1554. LightGallery.prototype.loadContent = function (index, rec) {
  1555. var _this = this;
  1556. var currentGalleryItem = this.galleryItems[index];
  1557. var $currentSlide = $LG(this.getSlideItemId(index));
  1558. var poster = currentGalleryItem.poster, srcset = currentGalleryItem.srcset, sizes = currentGalleryItem.sizes, sources = currentGalleryItem.sources;
  1559. var src = currentGalleryItem.src;
  1560. var video = currentGalleryItem.video;
  1561. var _html5Video = video && typeof video === 'string' ? JSON.parse(video) : video;
  1562. if (currentGalleryItem.responsive) {
  1563. var srcDyItms = currentGalleryItem.responsive.split(',');
  1564. src = utils.getResponsiveSrc(srcDyItms) || src;
  1565. }
  1566. var videoInfo = currentGalleryItem.__slideVideoInfo;
  1567. var lgVideoStyle = '';
  1568. var iframe = !!currentGalleryItem.iframe;
  1569. var isFirstSlide = !this.lGalleryOn;
  1570. // delay for adding complete class. it is 0 except first time.
  1571. var delay = 0;
  1572. if (isFirstSlide) {
  1573. if (this.zoomFromOrigin && this.currentImageSize) {
  1574. delay = this.settings.startAnimationDuration + 10;
  1575. }
  1576. else {
  1577. delay = this.settings.backdropDuration + 10;
  1578. }
  1579. }
  1580. if (!$currentSlide.hasClass('lg-loaded')) {
  1581. if (videoInfo) {
  1582. var _a = this.mediaContainerPosition, top_2 = _a.top, bottom = _a.bottom;
  1583. var videoSize = utils.getSize(this.items[index], this.outer, top_2 + bottom, videoInfo && this.settings.videoMaxSize);
  1584. lgVideoStyle = this.getVideoContStyle(videoSize);
  1585. }
  1586. if (iframe) {
  1587. var markup = utils.getIframeMarkup(this.settings.iframeWidth, this.settings.iframeHeight, this.settings.iframeMaxWidth, this.settings.iframeMaxHeight, src, currentGalleryItem.iframeTitle);
  1588. $currentSlide.prepend(markup);
  1589. }
  1590. else if (poster) {
  1591. var dummyImg = '';
  1592. var hasStartAnimation = isFirstSlide &&
  1593. this.zoomFromOrigin &&
  1594. this.currentImageSize;
  1595. if (hasStartAnimation) {
  1596. dummyImg = this.getDummyImageContent($currentSlide, index, '');
  1597. }
  1598. var markup = utils.getVideoPosterMarkup(poster, dummyImg || '', lgVideoStyle, this.settings.strings['playVideo'], videoInfo);
  1599. $currentSlide.prepend(markup);
  1600. }
  1601. else if (videoInfo) {
  1602. var markup = "<div class=\"lg-video-cont \" style=\"" + lgVideoStyle + "\"></div>";
  1603. $currentSlide.prepend(markup);
  1604. }
  1605. else {
  1606. this.setImgMarkup(src, $currentSlide, index);
  1607. if (srcset || sources) {
  1608. var $img = $currentSlide.find('.lg-object');
  1609. this.initPictureFill($img);
  1610. }
  1611. }
  1612. if (poster || videoInfo) {
  1613. this.LGel.trigger(lGEvents.hasVideo, {
  1614. index: index,
  1615. src: src,
  1616. html5Video: _html5Video,
  1617. hasPoster: !!poster,
  1618. });
  1619. }
  1620. this.LGel.trigger(lGEvents.afterAppendSlide, { index: index });
  1621. if (this.lGalleryOn &&
  1622. this.settings.appendSubHtmlTo === '.lg-item') {
  1623. this.addHtml(index);
  1624. }
  1625. }
  1626. // For first time add some delay for displaying the start animation.
  1627. var _speed = 0;
  1628. // Do not change the delay value because it is required for zoom plugin.
  1629. // If gallery opened from direct url (hash) speed value should be 0
  1630. if (delay && !$LG(document.body).hasClass('lg-from-hash')) {
  1631. _speed = delay;
  1632. }
  1633. // Only for first slide and zoomFromOrigin is enabled
  1634. if (this.isFirstSlideWithZoomAnimation()) {
  1635. setTimeout(function () {
  1636. $currentSlide
  1637. .removeClass('lg-start-end-progress lg-start-progress')
  1638. .removeAttr('style');
  1639. }, this.settings.startAnimationDuration + 100);
  1640. if (!$currentSlide.hasClass('lg-loaded')) {
  1641. setTimeout(function () {
  1642. if (_this.getSlideType(currentGalleryItem) === 'image') {
  1643. $currentSlide
  1644. .find('.lg-img-wrap')
  1645. .append(utils.getImgMarkup(index, src, '', srcset, sizes, currentGalleryItem.sources));
  1646. if (srcset || sources) {
  1647. var $img = $currentSlide.find('.lg-object');
  1648. _this.initPictureFill($img);
  1649. }
  1650. }
  1651. if (_this.getSlideType(currentGalleryItem) === 'image' ||
  1652. (_this.getSlideType(currentGalleryItem) === 'video' &&
  1653. poster)) {
  1654. _this.onLgObjectLoad($currentSlide, index, delay, _speed, true, false);
  1655. // load remaining slides once the slide is completely loaded
  1656. _this.onSlideObjectLoad($currentSlide, !!(videoInfo && videoInfo.html5 && !poster), function () {
  1657. _this.loadContentOnFirstSlideLoad(index, $currentSlide, _speed);
  1658. }, function () {
  1659. _this.loadContentOnFirstSlideLoad(index, $currentSlide, _speed);
  1660. });
  1661. }
  1662. }, this.settings.startAnimationDuration + 100);
  1663. }
  1664. }
  1665. // SLide content has been added to dom
  1666. $currentSlide.addClass('lg-loaded');
  1667. if (!this.isFirstSlideWithZoomAnimation() ||
  1668. (this.getSlideType(currentGalleryItem) === 'video' && !poster)) {
  1669. this.onLgObjectLoad($currentSlide, index, delay, _speed, isFirstSlide, !!(videoInfo && videoInfo.html5 && !poster));
  1670. }
  1671. // When gallery is opened once content is loaded (second time) need to add lg-complete class for css styling
  1672. if ((!this.zoomFromOrigin || !this.currentImageSize) &&
  1673. $currentSlide.hasClass('lg-complete_') &&
  1674. !this.lGalleryOn) {
  1675. setTimeout(function () {
  1676. $currentSlide.addClass('lg-complete');
  1677. }, this.settings.backdropDuration);
  1678. }
  1679. // Content loaded
  1680. // Need to set lGalleryOn before calling preload function
  1681. this.lGalleryOn = true;
  1682. if (rec === true) {
  1683. if (!$currentSlide.hasClass('lg-complete_')) {
  1684. $currentSlide
  1685. .find('.lg-object')
  1686. .first()
  1687. .on('load.lg error.lg', function () {
  1688. _this.preload(index);
  1689. });
  1690. }
  1691. else {
  1692. this.preload(index);
  1693. }
  1694. }
  1695. };
  1696. /**
  1697. * @desc Remove dummy image content and load next slides
  1698. * Called only for the first time if zoomFromOrigin animation is enabled
  1699. * @param index
  1700. * @param $currentSlide
  1701. * @param speed
  1702. */
  1703. LightGallery.prototype.loadContentOnFirstSlideLoad = function (index, $currentSlide, speed) {
  1704. var _this = this;
  1705. setTimeout(function () {
  1706. $currentSlide.find('.lg-dummy-img').remove();
  1707. $currentSlide.removeClass('lg-first-slide');
  1708. _this.outer.removeClass('lg-first-slide-loading');
  1709. _this.isDummyImageRemoved = true;
  1710. _this.preload(index);
  1711. }, speed + 300);
  1712. };
  1713. LightGallery.prototype.getItemsToBeInsertedToDom = function (index, prevIndex, numberOfItems) {
  1714. var _this = this;
  1715. if (numberOfItems === void 0) { numberOfItems = 0; }
  1716. var itemsToBeInsertedToDom = [];
  1717. // Minimum 2 items should be there
  1718. var possibleNumberOfItems = Math.max(numberOfItems, 3);
  1719. possibleNumberOfItems = Math.min(possibleNumberOfItems, this.galleryItems.length);
  1720. var prevIndexItem = "lg-item-" + this.lgId + "-" + prevIndex;
  1721. if (this.galleryItems.length <= 3) {
  1722. this.galleryItems.forEach(function (_element, index) {
  1723. itemsToBeInsertedToDom.push("lg-item-" + _this.lgId + "-" + index);
  1724. });
  1725. return itemsToBeInsertedToDom;
  1726. }
  1727. if (index < (this.galleryItems.length - 1) / 2) {
  1728. for (var idx = index; idx > index - possibleNumberOfItems / 2 && idx >= 0; idx--) {
  1729. itemsToBeInsertedToDom.push("lg-item-" + this.lgId + "-" + idx);
  1730. }
  1731. var numberOfExistingItems = itemsToBeInsertedToDom.length;
  1732. for (var idx = 0; idx < possibleNumberOfItems - numberOfExistingItems; idx++) {
  1733. itemsToBeInsertedToDom.push("lg-item-" + this.lgId + "-" + (index + idx + 1));
  1734. }
  1735. }
  1736. else {
  1737. for (var idx = index; idx <= this.galleryItems.length - 1 &&
  1738. idx < index + possibleNumberOfItems / 2; idx++) {
  1739. itemsToBeInsertedToDom.push("lg-item-" + this.lgId + "-" + idx);
  1740. }
  1741. var numberOfExistingItems = itemsToBeInsertedToDom.length;
  1742. for (var idx = 0; idx < possibleNumberOfItems - numberOfExistingItems; idx++) {
  1743. itemsToBeInsertedToDom.push("lg-item-" + this.lgId + "-" + (index - idx - 1));
  1744. }
  1745. }
  1746. if (this.settings.loop) {
  1747. if (index === this.galleryItems.length - 1) {
  1748. itemsToBeInsertedToDom.push("lg-item-" + this.lgId + "-" + 0);
  1749. }
  1750. else if (index === 0) {
  1751. itemsToBeInsertedToDom.push("lg-item-" + this.lgId + "-" + (this.galleryItems.length - 1));
  1752. }
  1753. }
  1754. if (itemsToBeInsertedToDom.indexOf(prevIndexItem) === -1) {
  1755. itemsToBeInsertedToDom.push("lg-item-" + this.lgId + "-" + prevIndex);
  1756. }
  1757. return itemsToBeInsertedToDom;
  1758. };
  1759. LightGallery.prototype.organizeSlideItems = function (index, prevIndex) {
  1760. var _this = this;
  1761. var itemsToBeInsertedToDom = this.getItemsToBeInsertedToDom(index, prevIndex, this.settings.numberOfSlideItemsInDom);
  1762. itemsToBeInsertedToDom.forEach(function (item) {
  1763. if (_this.currentItemsInDom.indexOf(item) === -1) {
  1764. _this.$inner.append("<div id=\"" + item + "\" class=\"lg-item\"></div>");
  1765. }
  1766. });
  1767. this.currentItemsInDom.forEach(function (item) {
  1768. if (itemsToBeInsertedToDom.indexOf(item) === -1) {
  1769. $LG("#" + item).remove();
  1770. }
  1771. });
  1772. return itemsToBeInsertedToDom;
  1773. };
  1774. /**
  1775. * Get previous index of the slide
  1776. */
  1777. LightGallery.prototype.getPreviousSlideIndex = function () {
  1778. var prevIndex = 0;
  1779. try {
  1780. var currentItemId = this.outer
  1781. .find('.lg-current')
  1782. .first()
  1783. .attr('id');
  1784. prevIndex = parseInt(currentItemId.split('-')[3]) || 0;
  1785. }
  1786. catch (error) {
  1787. prevIndex = 0;
  1788. }
  1789. return prevIndex;
  1790. };
  1791. LightGallery.prototype.setDownloadValue = function (index) {
  1792. if (this.settings.download) {
  1793. var currentGalleryItem = this.galleryItems[index];
  1794. var hideDownloadBtn = currentGalleryItem.downloadUrl === false ||
  1795. currentGalleryItem.downloadUrl === 'false';
  1796. if (hideDownloadBtn) {
  1797. this.outer.addClass('lg-hide-download');
  1798. }
  1799. else {
  1800. var $download = this.getElementById('lg-download');
  1801. this.outer.removeClass('lg-hide-download');
  1802. $download.attr('href', currentGalleryItem.downloadUrl ||
  1803. currentGalleryItem.src);
  1804. if (currentGalleryItem.download) {
  1805. $download.attr('download', currentGalleryItem.download);
  1806. }
  1807. }
  1808. }
  1809. };
  1810. LightGallery.prototype.makeSlideAnimation = function (direction, currentSlideItem, previousSlideItem) {
  1811. var _this = this;
  1812. if (this.lGalleryOn) {
  1813. previousSlideItem.addClass('lg-slide-progress');
  1814. }
  1815. setTimeout(function () {
  1816. // remove all transitions
  1817. _this.outer.addClass('lg-no-trans');
  1818. _this.outer
  1819. .find('.lg-item')
  1820. .removeClass('lg-prev-slide lg-next-slide');
  1821. if (direction === 'prev') {
  1822. //prevslide
  1823. currentSlideItem.addClass('lg-prev-slide');
  1824. previousSlideItem.addClass('lg-next-slide');
  1825. }
  1826. else {
  1827. // next slide
  1828. currentSlideItem.addClass('lg-next-slide');
  1829. previousSlideItem.addClass('lg-prev-slide');
  1830. }
  1831. // give 50 ms for browser to add/remove class
  1832. setTimeout(function () {
  1833. _this.outer.find('.lg-item').removeClass('lg-current');
  1834. currentSlideItem.addClass('lg-current');
  1835. // reset all transitions
  1836. _this.outer.removeClass('lg-no-trans');
  1837. }, 50);
  1838. }, this.lGalleryOn ? this.settings.slideDelay : 0);
  1839. };
  1840. /**
  1841. * Goto a specific slide.
  1842. * @param {Number} index - index of the slide
  1843. * @param {Boolean} fromTouch - true if slide function called via touch event or mouse drag
  1844. * @param {Boolean} fromThumb - true if slide function called via thumbnail click
  1845. * @param {String} direction - Direction of the slide(next/prev)
  1846. * @category lGPublicMethods
  1847. * @example
  1848. * const plugin = lightGallery();
  1849. * // to go to 3rd slide
  1850. * plugin.slide(2);
  1851. *
  1852. */
  1853. LightGallery.prototype.slide = function (index, fromTouch, fromThumb, direction) {
  1854. var _this = this;
  1855. var prevIndex = this.getPreviousSlideIndex();
  1856. this.currentItemsInDom = this.organizeSlideItems(index, prevIndex);
  1857. // Prevent multiple call, Required for hsh plugin
  1858. if (this.lGalleryOn && prevIndex === index) {
  1859. return;
  1860. }
  1861. var numberOfGalleryItems = this.galleryItems.length;
  1862. if (!this.lgBusy) {
  1863. if (this.settings.counter) {
  1864. this.updateCurrentCounter(index);
  1865. }
  1866. var currentSlideItem = this.getSlideItem(index);
  1867. var previousSlideItem_1 = this.getSlideItem(prevIndex);
  1868. var currentGalleryItem = this.galleryItems[index];
  1869. var videoInfo = currentGalleryItem.__slideVideoInfo;
  1870. this.outer.attr('data-lg-slide-type', this.getSlideType(currentGalleryItem));
  1871. this.setDownloadValue(index);
  1872. if (videoInfo) {
  1873. var _a = this.mediaContainerPosition, top_3 = _a.top, bottom = _a.bottom;
  1874. var videoSize = utils.getSize(this.items[index], this.outer, top_3 + bottom, videoInfo && this.settings.videoMaxSize);
  1875. this.resizeVideoSlide(index, videoSize);
  1876. }
  1877. this.LGel.trigger(lGEvents.beforeSlide, {
  1878. prevIndex: prevIndex,
  1879. index: index,
  1880. fromTouch: !!fromTouch,
  1881. fromThumb: !!fromThumb,
  1882. });
  1883. this.lgBusy = true;
  1884. clearTimeout(this.hideBarTimeout);
  1885. this.arrowDisable(index);
  1886. if (!direction) {
  1887. if (index < prevIndex) {
  1888. direction = 'prev';
  1889. }
  1890. else if (index > prevIndex) {
  1891. direction = 'next';
  1892. }
  1893. }
  1894. if (!fromTouch) {
  1895. this.makeSlideAnimation(direction, currentSlideItem, previousSlideItem_1);
  1896. }
  1897. else {
  1898. this.outer
  1899. .find('.lg-item')
  1900. .removeClass('lg-prev-slide lg-current lg-next-slide');
  1901. var touchPrev = void 0;
  1902. var touchNext = void 0;
  1903. if (numberOfGalleryItems > 2) {
  1904. touchPrev = index - 1;
  1905. touchNext = index + 1;
  1906. if (index === 0 && prevIndex === numberOfGalleryItems - 1) {
  1907. // next slide
  1908. touchNext = 0;
  1909. touchPrev = numberOfGalleryItems - 1;
  1910. }
  1911. else if (index === numberOfGalleryItems - 1 &&
  1912. prevIndex === 0) {
  1913. // prev slide
  1914. touchNext = 0;
  1915. touchPrev = numberOfGalleryItems - 1;
  1916. }
  1917. }
  1918. else {
  1919. touchPrev = 0;
  1920. touchNext = 1;
  1921. }
  1922. if (direction === 'prev') {
  1923. this.getSlideItem(touchNext).addClass('lg-next-slide');
  1924. }
  1925. else {
  1926. this.getSlideItem(touchPrev).addClass('lg-prev-slide');
  1927. }
  1928. currentSlideItem.addClass('lg-current');
  1929. }
  1930. // Do not put load content in set timeout as it needs to load immediately when the gallery is opened
  1931. if (!this.lGalleryOn) {
  1932. this.loadContent(index, true);
  1933. }
  1934. else {
  1935. setTimeout(function () {
  1936. _this.loadContent(index, true);
  1937. // Add title if this.settings.appendSubHtmlTo === lg-sub-html
  1938. if (_this.settings.appendSubHtmlTo !== '.lg-item') {
  1939. _this.addHtml(index);
  1940. }
  1941. }, this.settings.speed + 50 + (fromTouch ? 0 : this.settings.slideDelay));
  1942. }
  1943. setTimeout(function () {
  1944. _this.lgBusy = false;
  1945. previousSlideItem_1.removeClass('lg-slide-progress');
  1946. _this.LGel.trigger(lGEvents.afterSlide, {
  1947. prevIndex: prevIndex,
  1948. index: index,
  1949. fromTouch: fromTouch,
  1950. fromThumb: fromThumb,
  1951. });
  1952. }, (this.lGalleryOn ? this.settings.speed + 100 : 100) + (fromTouch ? 0 : this.settings.slideDelay));
  1953. }
  1954. this.index = index;
  1955. };
  1956. LightGallery.prototype.updateCurrentCounter = function (index) {
  1957. this.getElementById('lg-counter-current').html(index + 1 + '');
  1958. };
  1959. LightGallery.prototype.updateCounterTotal = function () {
  1960. this.getElementById('lg-counter-all').html(this.galleryItems.length + '');
  1961. };
  1962. LightGallery.prototype.getSlideType = function (item) {
  1963. if (item.__slideVideoInfo) {
  1964. return 'video';
  1965. }
  1966. else if (item.iframe) {
  1967. return 'iframe';
  1968. }
  1969. else {
  1970. return 'image';
  1971. }
  1972. };
  1973. LightGallery.prototype.touchMove = function (startCoords, endCoords, e) {
  1974. var distanceX = endCoords.pageX - startCoords.pageX;
  1975. var distanceY = endCoords.pageY - startCoords.pageY;
  1976. var allowSwipe = false;
  1977. if (this.swipeDirection) {
  1978. allowSwipe = true;
  1979. }
  1980. else {
  1981. if (Math.abs(distanceX) > 15) {
  1982. this.swipeDirection = 'horizontal';
  1983. allowSwipe = true;
  1984. }
  1985. else if (Math.abs(distanceY) > 15) {
  1986. this.swipeDirection = 'vertical';
  1987. allowSwipe = true;
  1988. }
  1989. }
  1990. if (!allowSwipe) {
  1991. return;
  1992. }
  1993. var $currentSlide = this.getSlideItem(this.index);
  1994. if (this.swipeDirection === 'horizontal') {
  1995. e === null || e === void 0 ? void 0 : e.preventDefault();
  1996. // reset opacity and transition duration
  1997. this.outer.addClass('lg-dragging');
  1998. // move current slide
  1999. this.setTranslate($currentSlide, distanceX, 0);
  2000. // move next and prev slide with current slide
  2001. var width = $currentSlide.get().offsetWidth;
  2002. var slideWidthAmount = (width * 15) / 100;
  2003. var gutter = slideWidthAmount - Math.abs((distanceX * 10) / 100);
  2004. this.setTranslate(this.outer.find('.lg-prev-slide').first(), -width + distanceX - gutter, 0);
  2005. this.setTranslate(this.outer.find('.lg-next-slide').first(), width + distanceX + gutter, 0);
  2006. }
  2007. else if (this.swipeDirection === 'vertical') {
  2008. if (this.settings.swipeToClose) {
  2009. e === null || e === void 0 ? void 0 : e.preventDefault();
  2010. this.$container.addClass('lg-dragging-vertical');
  2011. var opacity = 1 - Math.abs(distanceY) / window.innerHeight;
  2012. this.$backdrop.css('opacity', opacity);
  2013. var scale = 1 - Math.abs(distanceY) / (window.innerWidth * 2);
  2014. this.setTranslate($currentSlide, 0, distanceY, scale, scale);
  2015. if (Math.abs(distanceY) > 100) {
  2016. this.outer
  2017. .addClass('lg-hide-items')
  2018. .removeClass('lg-components-open');
  2019. }
  2020. }
  2021. }
  2022. };
  2023. LightGallery.prototype.touchEnd = function (endCoords, startCoords, event) {
  2024. var _this = this;
  2025. var distance;
  2026. // keep slide animation for any mode while dragg/swipe
  2027. if (this.settings.mode !== 'lg-slide') {
  2028. this.outer.addClass('lg-slide');
  2029. }
  2030. // set transition duration
  2031. setTimeout(function () {
  2032. _this.$container.removeClass('lg-dragging-vertical');
  2033. _this.outer
  2034. .removeClass('lg-dragging lg-hide-items')
  2035. .addClass('lg-components-open');
  2036. var triggerClick = true;
  2037. if (_this.swipeDirection === 'horizontal') {
  2038. distance = endCoords.pageX - startCoords.pageX;
  2039. var distanceAbs = Math.abs(endCoords.pageX - startCoords.pageX);
  2040. if (distance < 0 &&
  2041. distanceAbs > _this.settings.swipeThreshold) {
  2042. _this.goToNextSlide(true);
  2043. triggerClick = false;
  2044. }
  2045. else if (distance > 0 &&
  2046. distanceAbs > _this.settings.swipeThreshold) {
  2047. _this.goToPrevSlide(true);
  2048. triggerClick = false;
  2049. }
  2050. }
  2051. else if (_this.swipeDirection === 'vertical') {
  2052. distance = Math.abs(endCoords.pageY - startCoords.pageY);
  2053. if (_this.settings.closable &&
  2054. _this.settings.swipeToClose &&
  2055. distance > 100) {
  2056. _this.closeGallery();
  2057. return;
  2058. }
  2059. else {
  2060. _this.$backdrop.css('opacity', 1);
  2061. }
  2062. }
  2063. _this.outer.find('.lg-item').removeAttr('style');
  2064. if (triggerClick &&
  2065. Math.abs(endCoords.pageX - startCoords.pageX) < 5) {
  2066. // Trigger click if distance is less than 5 pix
  2067. var target = $LG(event.target);
  2068. if (_this.isPosterElement(target)) {
  2069. _this.LGel.trigger(lGEvents.posterClick);
  2070. }
  2071. }
  2072. _this.swipeDirection = undefined;
  2073. });
  2074. // remove slide class once drag/swipe is completed if mode is not slide
  2075. setTimeout(function () {
  2076. if (!_this.outer.hasClass('lg-dragging') &&
  2077. _this.settings.mode !== 'lg-slide') {
  2078. _this.outer.removeClass('lg-slide');
  2079. }
  2080. }, this.settings.speed + 100);
  2081. };
  2082. LightGallery.prototype.enableSwipe = function () {
  2083. var _this = this;
  2084. var startCoords = {};
  2085. var endCoords = {};
  2086. var isMoved = false;
  2087. var isSwiping = false;
  2088. if (this.settings.enableSwipe) {
  2089. this.$inner.on('touchstart.lg', function (e) {
  2090. _this.dragOrSwipeEnabled = true;
  2091. var $item = _this.getSlideItem(_this.index);
  2092. if (($LG(e.target).hasClass('lg-item') ||
  2093. $item.get().contains(e.target)) &&
  2094. !_this.outer.hasClass('lg-zoomed') &&
  2095. !_this.lgBusy &&
  2096. e.targetTouches.length === 1) {
  2097. isSwiping = true;
  2098. _this.touchAction = 'swipe';
  2099. _this.manageSwipeClass();
  2100. startCoords = {
  2101. pageX: e.targetTouches[0].pageX,
  2102. pageY: e.targetTouches[0].pageY,
  2103. };
  2104. }
  2105. });
  2106. this.$inner.on('touchmove.lg', function (e) {
  2107. if (isSwiping &&
  2108. _this.touchAction === 'swipe' &&
  2109. e.targetTouches.length === 1) {
  2110. endCoords = {
  2111. pageX: e.targetTouches[0].pageX,
  2112. pageY: e.targetTouches[0].pageY,
  2113. };
  2114. _this.touchMove(startCoords, endCoords, e);
  2115. isMoved = true;
  2116. }
  2117. });
  2118. this.$inner.on('touchend.lg', function (event) {
  2119. if (_this.touchAction === 'swipe') {
  2120. if (isMoved) {
  2121. isMoved = false;
  2122. _this.touchEnd(endCoords, startCoords, event);
  2123. }
  2124. else if (isSwiping) {
  2125. var target = $LG(event.target);
  2126. if (_this.isPosterElement(target)) {
  2127. _this.LGel.trigger(lGEvents.posterClick);
  2128. }
  2129. }
  2130. _this.touchAction = undefined;
  2131. isSwiping = false;
  2132. }
  2133. });
  2134. }
  2135. };
  2136. LightGallery.prototype.enableDrag = function () {
  2137. var _this = this;
  2138. var startCoords = {};
  2139. var endCoords = {};
  2140. var isDraging = false;
  2141. var isMoved = false;
  2142. if (this.settings.enableDrag) {
  2143. this.outer.on('mousedown.lg', function (e) {
  2144. _this.dragOrSwipeEnabled = true;
  2145. var $item = _this.getSlideItem(_this.index);
  2146. if ($LG(e.target).hasClass('lg-item') ||
  2147. $item.get().contains(e.target)) {
  2148. if (!_this.outer.hasClass('lg-zoomed') && !_this.lgBusy) {
  2149. e.preventDefault();
  2150. if (!_this.lgBusy) {
  2151. _this.manageSwipeClass();
  2152. startCoords = {
  2153. pageX: e.pageX,
  2154. pageY: e.pageY,
  2155. };
  2156. isDraging = true;
  2157. // ** Fix for webkit cursor issue https://code.google.com/p/chromium/issues/detail?id=26723
  2158. _this.outer.get().scrollLeft += 1;
  2159. _this.outer.get().scrollLeft -= 1;
  2160. // *
  2161. _this.outer
  2162. .removeClass('lg-grab')
  2163. .addClass('lg-grabbing');
  2164. _this.LGel.trigger(lGEvents.dragStart);
  2165. }
  2166. }
  2167. }
  2168. });
  2169. $LG(window).on("mousemove.lg.global" + this.lgId, function (e) {
  2170. if (isDraging && _this.lgOpened) {
  2171. isMoved = true;
  2172. endCoords = {
  2173. pageX: e.pageX,
  2174. pageY: e.pageY,
  2175. };
  2176. _this.touchMove(startCoords, endCoords);
  2177. _this.LGel.trigger(lGEvents.dragMove);
  2178. }
  2179. });
  2180. $LG(window).on("mouseup.lg.global" + this.lgId, function (event) {
  2181. if (!_this.lgOpened) {
  2182. return;
  2183. }
  2184. var target = $LG(event.target);
  2185. if (isMoved) {
  2186. isMoved = false;
  2187. _this.touchEnd(endCoords, startCoords, event);
  2188. _this.LGel.trigger(lGEvents.dragEnd);
  2189. }
  2190. else if (_this.isPosterElement(target)) {
  2191. _this.LGel.trigger(lGEvents.posterClick);
  2192. }
  2193. // Prevent execution on click
  2194. if (isDraging) {
  2195. isDraging = false;
  2196. _this.outer.removeClass('lg-grabbing').addClass('lg-grab');
  2197. }
  2198. });
  2199. }
  2200. };
  2201. LightGallery.prototype.triggerPosterClick = function () {
  2202. var _this = this;
  2203. this.$inner.on('click.lg', function (event) {
  2204. if (!_this.dragOrSwipeEnabled &&
  2205. _this.isPosterElement($LG(event.target))) {
  2206. _this.LGel.trigger(lGEvents.posterClick);
  2207. }
  2208. });
  2209. };
  2210. LightGallery.prototype.manageSwipeClass = function () {
  2211. var _touchNext = this.index + 1;
  2212. var _touchPrev = this.index - 1;
  2213. if (this.settings.loop && this.galleryItems.length > 2) {
  2214. if (this.index === 0) {
  2215. _touchPrev = this.galleryItems.length - 1;
  2216. }
  2217. else if (this.index === this.galleryItems.length - 1) {
  2218. _touchNext = 0;
  2219. }
  2220. }
  2221. this.outer.find('.lg-item').removeClass('lg-next-slide lg-prev-slide');
  2222. if (_touchPrev > -1) {
  2223. this.getSlideItem(_touchPrev).addClass('lg-prev-slide');
  2224. }
  2225. this.getSlideItem(_touchNext).addClass('lg-next-slide');
  2226. };
  2227. /**
  2228. * Go to next slide
  2229. * @param {Boolean} fromTouch - true if slide function called via touch event
  2230. * @category lGPublicMethods
  2231. * @example
  2232. * const plugin = lightGallery();
  2233. * plugin.goToNextSlide();
  2234. * @see <a href="/demos/methods/">Demo</a>
  2235. */
  2236. LightGallery.prototype.goToNextSlide = function (fromTouch) {
  2237. var _this = this;
  2238. var _loop = this.settings.loop;
  2239. if (fromTouch && this.galleryItems.length < 3) {
  2240. _loop = false;
  2241. }
  2242. if (!this.lgBusy) {
  2243. if (this.index + 1 < this.galleryItems.length) {
  2244. this.index++;
  2245. this.LGel.trigger(lGEvents.beforeNextSlide, {
  2246. index: this.index,
  2247. });
  2248. this.slide(this.index, !!fromTouch, false, 'next');
  2249. }
  2250. else {
  2251. if (_loop) {
  2252. this.index = 0;
  2253. this.LGel.trigger(lGEvents.beforeNextSlide, {
  2254. index: this.index,
  2255. });
  2256. this.slide(this.index, !!fromTouch, false, 'next');
  2257. }
  2258. else if (this.settings.slideEndAnimation && !fromTouch) {
  2259. this.outer.addClass('lg-right-end');
  2260. setTimeout(function () {
  2261. _this.outer.removeClass('lg-right-end');
  2262. }, 400);
  2263. }
  2264. }
  2265. }
  2266. };
  2267. /**
  2268. * Go to previous slides
  2269. * @param {Boolean} fromTouch - true if slide function called via touch event
  2270. * @category lGPublicMethods
  2271. * @example
  2272. * const plugin = lightGallery({});
  2273. * plugin.goToPrevSlide();
  2274. * @see <a href="/demos/methods/">Demo</a>
  2275. *
  2276. */
  2277. LightGallery.prototype.goToPrevSlide = function (fromTouch) {
  2278. var _this = this;
  2279. var _loop = this.settings.loop;
  2280. if (fromTouch && this.galleryItems.length < 3) {
  2281. _loop = false;
  2282. }
  2283. if (!this.lgBusy) {
  2284. if (this.index > 0) {
  2285. this.index--;
  2286. this.LGel.trigger(lGEvents.beforePrevSlide, {
  2287. index: this.index,
  2288. fromTouch: fromTouch,
  2289. });
  2290. this.slide(this.index, !!fromTouch, false, 'prev');
  2291. }
  2292. else {
  2293. if (_loop) {
  2294. this.index = this.galleryItems.length - 1;
  2295. this.LGel.trigger(lGEvents.beforePrevSlide, {
  2296. index: this.index,
  2297. fromTouch: fromTouch,
  2298. });
  2299. this.slide(this.index, !!fromTouch, false, 'prev');
  2300. }
  2301. else if (this.settings.slideEndAnimation && !fromTouch) {
  2302. this.outer.addClass('lg-left-end');
  2303. setTimeout(function () {
  2304. _this.outer.removeClass('lg-left-end');
  2305. }, 400);
  2306. }
  2307. }
  2308. }
  2309. };
  2310. LightGallery.prototype.keyPress = function () {
  2311. var _this = this;
  2312. $LG(window).on("keydown.lg.global" + this.lgId, function (e) {
  2313. if (_this.lgOpened &&
  2314. _this.settings.escKey === true &&
  2315. e.keyCode === 27) {
  2316. e.preventDefault();
  2317. if (_this.settings.allowMediaOverlap &&
  2318. _this.outer.hasClass('lg-can-toggle') &&
  2319. _this.outer.hasClass('lg-components-open')) {
  2320. _this.outer.removeClass('lg-components-open');
  2321. }
  2322. else {
  2323. _this.closeGallery();
  2324. }
  2325. }
  2326. if (_this.lgOpened && _this.galleryItems.length > 1) {
  2327. if (e.keyCode === 37) {
  2328. e.preventDefault();
  2329. _this.goToPrevSlide();
  2330. }
  2331. if (e.keyCode === 39) {
  2332. e.preventDefault();
  2333. _this.goToNextSlide();
  2334. }
  2335. }
  2336. });
  2337. };
  2338. LightGallery.prototype.arrow = function () {
  2339. var _this = this;
  2340. this.getElementById('lg-prev').on('click.lg', function () {
  2341. _this.goToPrevSlide();
  2342. });
  2343. this.getElementById('lg-next').on('click.lg', function () {
  2344. _this.goToNextSlide();
  2345. });
  2346. };
  2347. LightGallery.prototype.arrowDisable = function (index) {
  2348. // Disable arrows if settings.hideControlOnEnd is true
  2349. if (!this.settings.loop && this.settings.hideControlOnEnd) {
  2350. var $prev = this.getElementById('lg-prev');
  2351. var $next = this.getElementById('lg-next');
  2352. if (index + 1 === this.galleryItems.length) {
  2353. $next.attr('disabled', 'disabled').addClass('disabled');
  2354. }
  2355. else {
  2356. $next.removeAttr('disabled').removeClass('disabled');
  2357. }
  2358. if (index === 0) {
  2359. $prev.attr('disabled', 'disabled').addClass('disabled');
  2360. }
  2361. else {
  2362. $prev.removeAttr('disabled').removeClass('disabled');
  2363. }
  2364. }
  2365. };
  2366. LightGallery.prototype.setTranslate = function ($el, xValue, yValue, scaleX, scaleY) {
  2367. if (scaleX === void 0) { scaleX = 1; }
  2368. if (scaleY === void 0) { scaleY = 1; }
  2369. $el.css('transform', 'translate3d(' +
  2370. xValue +
  2371. 'px, ' +
  2372. yValue +
  2373. 'px, 0px) scale3d(' +
  2374. scaleX +
  2375. ', ' +
  2376. scaleY +
  2377. ', 1)');
  2378. };
  2379. LightGallery.prototype.mousewheel = function () {
  2380. var _this = this;
  2381. var lastCall = 0;
  2382. this.outer.on('wheel.lg', function (e) {
  2383. if (!e.deltaY || _this.galleryItems.length < 2) {
  2384. return;
  2385. }
  2386. e.preventDefault();
  2387. var now = new Date().getTime();
  2388. if (now - lastCall < 1000) {
  2389. return;
  2390. }
  2391. lastCall = now;
  2392. if (e.deltaY > 0) {
  2393. _this.goToNextSlide();
  2394. }
  2395. else if (e.deltaY < 0) {
  2396. _this.goToPrevSlide();
  2397. }
  2398. });
  2399. };
  2400. LightGallery.prototype.isSlideElement = function (target) {
  2401. return (target.hasClass('lg-outer') ||
  2402. target.hasClass('lg-item') ||
  2403. target.hasClass('lg-img-wrap'));
  2404. };
  2405. LightGallery.prototype.isPosterElement = function (target) {
  2406. var playButton = this.getSlideItem(this.index)
  2407. .find('.lg-video-play-button')
  2408. .get();
  2409. return (target.hasClass('lg-video-poster') ||
  2410. target.hasClass('lg-video-play-button') ||
  2411. (playButton && playButton.contains(target.get())));
  2412. };
  2413. /**
  2414. * Maximize minimize inline gallery.
  2415. * @category lGPublicMethods
  2416. */
  2417. LightGallery.prototype.toggleMaximize = function () {
  2418. var _this = this;
  2419. this.getElementById('lg-maximize').on('click.lg', function () {
  2420. _this.$container.toggleClass('lg-inline');
  2421. _this.refreshOnResize();
  2422. });
  2423. };
  2424. LightGallery.prototype.invalidateItems = function () {
  2425. for (var index = 0; index < this.items.length; index++) {
  2426. var element = this.items[index];
  2427. var $element = $LG(element);
  2428. $element.off("click.lgcustom-item-" + $element.attr('data-lg-id'));
  2429. }
  2430. };
  2431. LightGallery.prototype.manageCloseGallery = function () {
  2432. var _this = this;
  2433. if (!this.settings.closable)
  2434. return;
  2435. var mousedown = false;
  2436. this.getElementById('lg-close').on('click.lg', function () {
  2437. _this.closeGallery();
  2438. });
  2439. if (this.settings.closeOnTap) {
  2440. // If you drag the slide and release outside gallery gets close on chrome
  2441. // for preventing this check mousedown and mouseup happened on .lg-item or lg-outer
  2442. this.outer.on('mousedown.lg', function (e) {
  2443. var target = $LG(e.target);
  2444. if (_this.isSlideElement(target)) {
  2445. mousedown = true;
  2446. }
  2447. else {
  2448. mousedown = false;
  2449. }
  2450. });
  2451. this.outer.on('mousemove.lg', function () {
  2452. mousedown = false;
  2453. });
  2454. this.outer.on('mouseup.lg', function (e) {
  2455. var target = $LG(e.target);
  2456. if (_this.isSlideElement(target) && mousedown) {
  2457. if (!_this.outer.hasClass('lg-dragging')) {
  2458. _this.closeGallery();
  2459. }
  2460. }
  2461. });
  2462. }
  2463. };
  2464. /**
  2465. * Close lightGallery if it is opened.
  2466. *
  2467. * @description If closable is false in the settings, you need to pass true via closeGallery method to force close gallery
  2468. * @return returns the estimated time to close gallery completely including the close animation duration
  2469. * @category lGPublicMethods
  2470. * @example
  2471. * const plugin = lightGallery();
  2472. * plugin.closeGallery();
  2473. *
  2474. */
  2475. LightGallery.prototype.closeGallery = function (force) {
  2476. var _this = this;
  2477. if (!this.lgOpened || (!this.settings.closable && !force)) {
  2478. return 0;
  2479. }
  2480. this.LGel.trigger(lGEvents.beforeClose);
  2481. $LG(window).scrollTop(this.prevScrollTop);
  2482. var currentItem = this.items[this.index];
  2483. var transform;
  2484. if (this.zoomFromOrigin && currentItem) {
  2485. var _a = this.mediaContainerPosition, top_4 = _a.top, bottom = _a.bottom;
  2486. var _b = this.galleryItems[this.index], __slideVideoInfo = _b.__slideVideoInfo, poster = _b.poster;
  2487. var imageSize = utils.getSize(currentItem, this.outer, top_4 + bottom, __slideVideoInfo && poster && this.settings.videoMaxSize);
  2488. transform = utils.getTransform(currentItem, this.outer, top_4, bottom, imageSize);
  2489. }
  2490. if (this.zoomFromOrigin && transform) {
  2491. this.outer.addClass('lg-closing lg-zoom-from-image');
  2492. this.getSlideItem(this.index)
  2493. .addClass('lg-start-end-progress')
  2494. .css('transition-duration', this.settings.startAnimationDuration + 'ms')
  2495. .css('transform', transform);
  2496. }
  2497. else {
  2498. this.outer.addClass('lg-hide-items');
  2499. // lg-zoom-from-image is used for setting the opacity to 1 if zoomFromOrigin is true
  2500. // If the closing item doesn't have the lg-size attribute, remove this class to avoid the closing css conflicts
  2501. this.outer.removeClass('lg-zoom-from-image');
  2502. }
  2503. // Unbind all events added by lightGallery
  2504. // @todo
  2505. //this.$el.off('.lg.tm');
  2506. this.destroyModules();
  2507. this.lGalleryOn = false;
  2508. this.isDummyImageRemoved = false;
  2509. this.zoomFromOrigin = this.settings.zoomFromOrigin;
  2510. clearTimeout(this.hideBarTimeout);
  2511. this.hideBarTimeout = false;
  2512. $LG('html').removeClass('lg-on');
  2513. this.outer.removeClass('lg-visible lg-components-open');
  2514. // Resetting opacity to 0 isd required as vertical swipe to close function adds inline opacity.
  2515. this.$backdrop.removeClass('in').css('opacity', 0);
  2516. var removeTimeout = this.zoomFromOrigin && transform
  2517. ? Math.max(this.settings.startAnimationDuration, this.settings.backdropDuration)
  2518. : this.settings.backdropDuration;
  2519. this.$container.removeClass('lg-show-in');
  2520. // Once the closign animation is completed and gallery is invisible
  2521. setTimeout(function () {
  2522. if (_this.zoomFromOrigin && transform) {
  2523. _this.outer.removeClass('lg-zoom-from-image');
  2524. }
  2525. _this.$container.removeClass('lg-show');
  2526. // Need to remove inline opacity as it is used in the stylesheet as well
  2527. _this.$backdrop
  2528. .removeAttr('style')
  2529. .css('transition-duration', _this.settings.backdropDuration + 'ms');
  2530. _this.outer.removeClass("lg-closing " + _this.settings.startClass);
  2531. _this.getSlideItem(_this.index).removeClass('lg-start-end-progress');
  2532. _this.$inner.empty();
  2533. if (_this.lgOpened) {
  2534. _this.LGel.trigger(lGEvents.afterClose, {
  2535. instance: _this,
  2536. });
  2537. }
  2538. if (_this.outer.get()) {
  2539. _this.outer.get().blur();
  2540. }
  2541. _this.lgOpened = false;
  2542. }, removeTimeout + 100);
  2543. return removeTimeout + 100;
  2544. };
  2545. LightGallery.prototype.initModules = function () {
  2546. this.plugins.forEach(function (module) {
  2547. try {
  2548. module.init();
  2549. }
  2550. catch (err) {
  2551. console.warn("lightGallery:- make sure lightGallery module is properly initiated");
  2552. }
  2553. });
  2554. };
  2555. LightGallery.prototype.destroyModules = function (destroy) {
  2556. this.plugins.forEach(function (module) {
  2557. try {
  2558. if (destroy) {
  2559. module.destroy();
  2560. }
  2561. else {
  2562. module.closeGallery && module.closeGallery();
  2563. }
  2564. }
  2565. catch (err) {
  2566. console.warn("lightGallery:- make sure lightGallery module is properly destroyed");
  2567. }
  2568. });
  2569. };
  2570. /**
  2571. * Refresh lightGallery with new set of children.
  2572. *
  2573. * @description This is useful to update the gallery when the child elements are changed without calling destroy method.
  2574. *
  2575. * If you are using dynamic mode, you can pass the modified array of dynamicEl as the first parameter to refresh the dynamic gallery
  2576. * @see <a href="/demos/dynamic-mode/">Demo</a>
  2577. * @category lGPublicMethods
  2578. * @example
  2579. * const plugin = lightGallery();
  2580. * // Delete or add children, then call
  2581. * plugin.refresh();
  2582. *
  2583. */
  2584. LightGallery.prototype.refresh = function (galleryItems) {
  2585. if (!this.settings.dynamic) {
  2586. this.invalidateItems();
  2587. }
  2588. if (galleryItems) {
  2589. this.galleryItems = galleryItems;
  2590. }
  2591. else {
  2592. this.galleryItems = this.getItems();
  2593. }
  2594. this.updateControls();
  2595. this.openGalleryOnItemClick();
  2596. this.LGel.trigger(lGEvents.updateSlides);
  2597. };
  2598. LightGallery.prototype.updateControls = function () {
  2599. this.addSlideVideoInfo(this.galleryItems);
  2600. this.updateCounterTotal();
  2601. this.manageSingleSlideClassName();
  2602. };
  2603. /**
  2604. * Destroy lightGallery.
  2605. * Destroy lightGallery and its plugin instances completely
  2606. *
  2607. * @description This method also calls CloseGallery function internally. Returns the time takes to completely close and destroy the instance.
  2608. * In case if you want to re-initialize lightGallery right after destroying it, initialize it only once the destroy process is completed.
  2609. * You can use refresh method most of the times.
  2610. * @category lGPublicMethods
  2611. * @example
  2612. * const plugin = lightGallery();
  2613. * plugin.destroy();
  2614. *
  2615. */
  2616. LightGallery.prototype.destroy = function () {
  2617. var _this = this;
  2618. var closeTimeout = this.closeGallery(true);
  2619. setTimeout(function () {
  2620. _this.destroyModules(true);
  2621. if (!_this.settings.dynamic) {
  2622. _this.invalidateItems();
  2623. }
  2624. $LG(window).off(".lg.global" + _this.lgId);
  2625. _this.LGel.off('.lg');
  2626. _this.$container.remove();
  2627. }, closeTimeout);
  2628. return closeTimeout;
  2629. };
  2630. return LightGallery;
  2631. }());
  2632. function lightGallery(el, options) {
  2633. return new LightGallery(el, options);
  2634. }
  2635. export default lightGallery;
  2636. //# sourceMappingURL=lightgallery.es5.js.map