UNPKG

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