lg-hash.es5.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /**
  30. * List of lightGallery events
  31. * All events should be documented here
  32. * Below interfaces are used to build the website documentations
  33. * */
  34. var lGEvents = {
  35. afterAppendSlide: 'lgAfterAppendSlide',
  36. init: 'lgInit',
  37. hasVideo: 'lgHasVideo',
  38. containerResize: 'lgContainerResize',
  39. updateSlides: 'lgUpdateSlides',
  40. afterAppendSubHtml: 'lgAfterAppendSubHtml',
  41. beforeOpen: 'lgBeforeOpen',
  42. afterOpen: 'lgAfterOpen',
  43. slideItemLoad: 'lgSlideItemLoad',
  44. beforeSlide: 'lgBeforeSlide',
  45. afterSlide: 'lgAfterSlide',
  46. posterClick: 'lgPosterClick',
  47. dragStart: 'lgDragStart',
  48. dragMove: 'lgDragMove',
  49. dragEnd: 'lgDragEnd',
  50. beforeNextSlide: 'lgBeforeNextSlide',
  51. beforePrevSlide: 'lgBeforePrevSlide',
  52. beforeClose: 'lgBeforeClose',
  53. afterClose: 'lgAfterClose',
  54. rotateLeft: 'lgRotateLeft',
  55. rotateRight: 'lgRotateRight',
  56. flipHorizontal: 'lgFlipHorizontal',
  57. flipVertical: 'lgFlipVertical',
  58. autoplay: 'lgAutoplay',
  59. autoplayStart: 'lgAutoplayStart',
  60. autoplayStop: 'lgAutoplayStop',
  61. };
  62. var hashSettings = {
  63. hash: true,
  64. galleryId: '1',
  65. customSlideName: false,
  66. };
  67. var Hash = /** @class */ (function () {
  68. function Hash(instance, $LG) {
  69. // get lightGallery core plugin instance
  70. this.core = instance;
  71. this.$LG = $LG;
  72. // extend module default settings with lightGallery core settings
  73. this.settings = __assign(__assign({}, hashSettings), this.core.settings);
  74. return this;
  75. }
  76. Hash.prototype.init = function () {
  77. var _this = this;
  78. if (!this.settings.hash) {
  79. return;
  80. }
  81. this.oldHash = window.location.hash;
  82. setTimeout(function () {
  83. _this.buildFromHash();
  84. }, 100);
  85. // Change hash value on after each slide transition
  86. this.core.LGel.on(lGEvents.afterSlide + ".hash", this.onAfterSlide.bind(this));
  87. this.core.LGel.on(lGEvents.afterClose + ".hash", this.onCloseAfter.bind(this));
  88. // Listen hash change and change the slide according to slide value
  89. this.$LG(window).on("hashchange.lg.hash.global" + this.core.lgId, this.onHashchange.bind(this));
  90. };
  91. Hash.prototype.onAfterSlide = function (event) {
  92. var slideName = this.core.galleryItems[event.detail.index].slideName;
  93. slideName = this.settings.customSlideName
  94. ? slideName || event.detail.index
  95. : event.detail.index;
  96. if (history.replaceState) {
  97. history.replaceState(null, '', window.location.pathname +
  98. window.location.search +
  99. '#lg=' +
  100. this.settings.galleryId +
  101. '&slide=' +
  102. slideName);
  103. }
  104. else {
  105. window.location.hash =
  106. 'lg=' + this.settings.galleryId + '&slide=' + slideName;
  107. }
  108. };
  109. /**
  110. * Get index of the slide from custom slideName. Has to be a public method. Used in hash plugin
  111. * @param {String} hash
  112. * @returns {Number} Index of the slide.
  113. */
  114. Hash.prototype.getIndexFromUrl = function (hash) {
  115. if (hash === void 0) { hash = window.location.hash; }
  116. var slideName = hash.split('&slide=')[1];
  117. var _idx = 0;
  118. if (this.settings.customSlideName) {
  119. for (var index = 0; index < this.core.galleryItems.length; index++) {
  120. var dynamicEl = this.core.galleryItems[index];
  121. if (dynamicEl.slideName === slideName) {
  122. _idx = index;
  123. break;
  124. }
  125. }
  126. }
  127. else {
  128. _idx = parseInt(slideName, 10);
  129. }
  130. return isNaN(_idx) ? 0 : _idx;
  131. };
  132. // Build Gallery if gallery id exist in the URL
  133. Hash.prototype.buildFromHash = function () {
  134. // if dynamic option is enabled execute immediately
  135. var _hash = window.location.hash;
  136. if (_hash.indexOf('lg=' + this.settings.galleryId) > 0) {
  137. // This class is used to remove the initial animation if galleryId present in the URL
  138. this.$LG(document.body).addClass('lg-from-hash');
  139. var index = this.getIndexFromUrl(_hash);
  140. this.core.openGallery(index);
  141. return true;
  142. }
  143. };
  144. Hash.prototype.onCloseAfter = function () {
  145. // Reset to old hash value
  146. if (this.oldHash &&
  147. this.oldHash.indexOf('lg=' + this.settings.galleryId) < 0) {
  148. if (history.replaceState) {
  149. history.replaceState(null, '', this.oldHash);
  150. }
  151. else {
  152. window.location.hash = this.oldHash;
  153. }
  154. }
  155. else {
  156. if (history.replaceState) {
  157. history.replaceState(null, document.title, window.location.pathname + window.location.search);
  158. }
  159. else {
  160. window.location.hash = '';
  161. }
  162. }
  163. };
  164. Hash.prototype.onHashchange = function () {
  165. if (!this.core.lgOpened)
  166. return;
  167. var _hash = window.location.hash;
  168. var index = this.getIndexFromUrl(_hash);
  169. // it galleryId doesn't exist in the url close the gallery
  170. if (_hash.indexOf('lg=' + this.settings.galleryId) > -1) {
  171. this.core.slide(index, false, false);
  172. }
  173. else if (this.core.lGalleryOn) {
  174. this.core.closeGallery();
  175. }
  176. };
  177. Hash.prototype.closeGallery = function () {
  178. if (this.settings.hash) {
  179. this.$LG(document.body).removeClass('lg-from-hash');
  180. }
  181. };
  182. Hash.prototype.destroy = function () {
  183. this.core.LGel.off('.lg.hash');
  184. this.core.LGel.off('.hash');
  185. this.$LG(window).off("hashchange.lg.hash.global" + this.core.lgId);
  186. };
  187. return Hash;
  188. }());
  189. export default Hash;
  190. //# sourceMappingURL=lg-hash.es5.js.map