lg-hash.umd.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. (function (global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  9. typeof define === 'function' && define.amd ? define(factory) :
  10. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.lgHash = factory());
  11. }(this, (function () { 'use strict';
  12. /*! *****************************************************************************
  13. Copyright (c) Microsoft Corporation.
  14. Permission to use, copy, modify, and/or distribute this software for any
  15. purpose with or without fee is hereby granted.
  16. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  17. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  19. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  20. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  21. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. PERFORMANCE OF THIS SOFTWARE.
  23. ***************************************************************************** */
  24. var __assign = function() {
  25. __assign = Object.assign || function __assign(t) {
  26. for (var s, i = 1, n = arguments.length; i < n; i++) {
  27. s = arguments[i];
  28. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
  29. }
  30. return t;
  31. };
  32. return __assign.apply(this, arguments);
  33. };
  34. /**
  35. * List of lightGallery events
  36. * All events should be documented here
  37. * Below interfaces are used to build the website documentations
  38. * */
  39. var lGEvents = {
  40. afterAppendSlide: 'lgAfterAppendSlide',
  41. init: 'lgInit',
  42. hasVideo: 'lgHasVideo',
  43. containerResize: 'lgContainerResize',
  44. updateSlides: 'lgUpdateSlides',
  45. afterAppendSubHtml: 'lgAfterAppendSubHtml',
  46. beforeOpen: 'lgBeforeOpen',
  47. afterOpen: 'lgAfterOpen',
  48. slideItemLoad: 'lgSlideItemLoad',
  49. beforeSlide: 'lgBeforeSlide',
  50. afterSlide: 'lgAfterSlide',
  51. posterClick: 'lgPosterClick',
  52. dragStart: 'lgDragStart',
  53. dragMove: 'lgDragMove',
  54. dragEnd: 'lgDragEnd',
  55. beforeNextSlide: 'lgBeforeNextSlide',
  56. beforePrevSlide: 'lgBeforePrevSlide',
  57. beforeClose: 'lgBeforeClose',
  58. afterClose: 'lgAfterClose',
  59. rotateLeft: 'lgRotateLeft',
  60. rotateRight: 'lgRotateRight',
  61. flipHorizontal: 'lgFlipHorizontal',
  62. flipVertical: 'lgFlipVertical',
  63. autoplay: 'lgAutoplay',
  64. autoplayStart: 'lgAutoplayStart',
  65. autoplayStop: 'lgAutoplayStop',
  66. };
  67. var hashSettings = {
  68. hash: true,
  69. galleryId: '1',
  70. customSlideName: false,
  71. };
  72. var Hash = /** @class */ (function () {
  73. function Hash(instance, $LG) {
  74. // get lightGallery core plugin instance
  75. this.core = instance;
  76. this.$LG = $LG;
  77. // extend module default settings with lightGallery core settings
  78. this.settings = __assign(__assign({}, hashSettings), this.core.settings);
  79. return this;
  80. }
  81. Hash.prototype.init = function () {
  82. var _this = this;
  83. if (!this.settings.hash) {
  84. return;
  85. }
  86. this.oldHash = window.location.hash;
  87. setTimeout(function () {
  88. _this.buildFromHash();
  89. }, 100);
  90. // Change hash value on after each slide transition
  91. this.core.LGel.on(lGEvents.afterSlide + ".hash", this.onAfterSlide.bind(this));
  92. this.core.LGel.on(lGEvents.afterClose + ".hash", this.onCloseAfter.bind(this));
  93. // Listen hash change and change the slide according to slide value
  94. this.$LG(window).on("hashchange.lg.hash.global" + this.core.lgId, this.onHashchange.bind(this));
  95. };
  96. Hash.prototype.onAfterSlide = function (event) {
  97. var slideName = this.core.galleryItems[event.detail.index].slideName;
  98. slideName = this.settings.customSlideName
  99. ? slideName || event.detail.index
  100. : event.detail.index;
  101. if (history.replaceState) {
  102. history.replaceState(null, '', window.location.pathname +
  103. window.location.search +
  104. '#lg=' +
  105. this.settings.galleryId +
  106. '&slide=' +
  107. slideName);
  108. }
  109. else {
  110. window.location.hash =
  111. 'lg=' + this.settings.galleryId + '&slide=' + slideName;
  112. }
  113. };
  114. /**
  115. * Get index of the slide from custom slideName. Has to be a public method. Used in hash plugin
  116. * @param {String} hash
  117. * @returns {Number} Index of the slide.
  118. */
  119. Hash.prototype.getIndexFromUrl = function (hash) {
  120. if (hash === void 0) { hash = window.location.hash; }
  121. var slideName = hash.split('&slide=')[1];
  122. var _idx = 0;
  123. if (this.settings.customSlideName) {
  124. for (var index = 0; index < this.core.galleryItems.length; index++) {
  125. var dynamicEl = this.core.galleryItems[index];
  126. if (dynamicEl.slideName === slideName) {
  127. _idx = index;
  128. break;
  129. }
  130. }
  131. }
  132. else {
  133. _idx = parseInt(slideName, 10);
  134. }
  135. return isNaN(_idx) ? 0 : _idx;
  136. };
  137. // Build Gallery if gallery id exist in the URL
  138. Hash.prototype.buildFromHash = function () {
  139. // if dynamic option is enabled execute immediately
  140. var _hash = window.location.hash;
  141. if (_hash.indexOf('lg=' + this.settings.galleryId) > 0) {
  142. // This class is used to remove the initial animation if galleryId present in the URL
  143. this.$LG(document.body).addClass('lg-from-hash');
  144. var index = this.getIndexFromUrl(_hash);
  145. this.core.openGallery(index);
  146. return true;
  147. }
  148. };
  149. Hash.prototype.onCloseAfter = function () {
  150. // Reset to old hash value
  151. if (this.oldHash &&
  152. this.oldHash.indexOf('lg=' + this.settings.galleryId) < 0) {
  153. if (history.replaceState) {
  154. history.replaceState(null, '', this.oldHash);
  155. }
  156. else {
  157. window.location.hash = this.oldHash;
  158. }
  159. }
  160. else {
  161. if (history.replaceState) {
  162. history.replaceState(null, document.title, window.location.pathname + window.location.search);
  163. }
  164. else {
  165. window.location.hash = '';
  166. }
  167. }
  168. };
  169. Hash.prototype.onHashchange = function () {
  170. if (!this.core.lgOpened)
  171. return;
  172. var _hash = window.location.hash;
  173. var index = this.getIndexFromUrl(_hash);
  174. // it galleryId doesn't exist in the url close the gallery
  175. if (_hash.indexOf('lg=' + this.settings.galleryId) > -1) {
  176. this.core.slide(index, false, false);
  177. }
  178. else if (this.core.lGalleryOn) {
  179. this.core.closeGallery();
  180. }
  181. };
  182. Hash.prototype.closeGallery = function () {
  183. if (this.settings.hash) {
  184. this.$LG(document.body).removeClass('lg-from-hash');
  185. }
  186. };
  187. Hash.prototype.destroy = function () {
  188. this.core.LGel.off('.lg.hash');
  189. this.core.LGel.off('.hash');
  190. this.$LG(window).off("hashchange.lg.hash.global" + this.core.lgId);
  191. };
  192. return Hash;
  193. }());
  194. return Hash;
  195. })));
  196. //# sourceMappingURL=lg-hash.umd.js.map