UNPKG

7.38 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/*! *****************************************************************************
9Copyright (c) Microsoft Corporation.
10
11Permission to use, copy, modify, and/or distribute this software for any
12purpose with or without fee is hereby granted.
13
14THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20PERFORMANCE OF THIS SOFTWARE.
21***************************************************************************** */
22
23var __assign = function() {
24 __assign = Object.assign || function __assign(t) {
25 for (var s, i = 1, n = arguments.length; i < n; i++) {
26 s = arguments[i];
27 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
28 }
29 return t;
30 };
31 return __assign.apply(this, arguments);
32};
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 * */
39var 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
68var hashSettings = {
69 hash: true,
70 galleryId: '1',
71 customSlideName: false,
72};
73
74var Hash = /** @class */ (function () {
75 function Hash(instance, $LG) {
76 // get lightGallery core plugin instance
77 this.core = instance;
78 this.$LG = $LG;
79 // extend module default settings with lightGallery core settings
80 this.settings = __assign(__assign({}, hashSettings), this.core.settings);
81 return this;
82 }
83 Hash.prototype.init = function () {
84 var _this = this;
85 if (!this.settings.hash) {
86 return;
87 }
88 this.oldHash = window.location.hash;
89 setTimeout(function () {
90 _this.buildFromHash();
91 }, 100);
92 // Change hash value on after each slide transition
93 this.core.LGel.on(lGEvents.afterSlide + ".hash", this.onAfterSlide.bind(this));
94 this.core.LGel.on(lGEvents.afterClose + ".hash", this.onCloseAfter.bind(this));
95 // Listen hash change and change the slide according to slide value
96 this.$LG(window).on("hashchange.lg.hash.global" + this.core.lgId, this.onHashchange.bind(this));
97 };
98 Hash.prototype.onAfterSlide = function (event) {
99 var slideName = this.core.galleryItems[event.detail.index].slideName;
100 slideName = this.settings.customSlideName
101 ? slideName || event.detail.index
102 : event.detail.index;
103 if (history.replaceState) {
104 history.replaceState(null, '', window.location.pathname +
105 window.location.search +
106 '#lg=' +
107 this.settings.galleryId +
108 '&slide=' +
109 slideName);
110 }
111 else {
112 window.location.hash =
113 'lg=' + this.settings.galleryId + '&slide=' + slideName;
114 }
115 };
116 /**
117 * Get index of the slide from custom slideName. Has to be a public method. Used in hash plugin
118 * @param {String} hash
119 * @returns {Number} Index of the slide.
120 */
121 Hash.prototype.getIndexFromUrl = function (hash) {
122 if (hash === void 0) { hash = window.location.hash; }
123 var slideName = hash.split('&slide=')[1];
124 var _idx = 0;
125 if (this.settings.customSlideName) {
126 for (var index = 0; index < this.core.galleryItems.length; index++) {
127 var dynamicEl = this.core.galleryItems[index];
128 if (dynamicEl.slideName === slideName) {
129 _idx = index;
130 break;
131 }
132 }
133 }
134 else {
135 _idx = parseInt(slideName, 10);
136 }
137 return isNaN(_idx) ? 0 : _idx;
138 };
139 // Build Gallery if gallery id exist in the URL
140 Hash.prototype.buildFromHash = function () {
141 // if dynamic option is enabled execute immediately
142 var _hash = window.location.hash;
143 if (_hash.indexOf('lg=' + this.settings.galleryId) > 0) {
144 // This class is used to remove the initial animation if galleryId present in the URL
145 this.$LG(document.body).addClass('lg-from-hash');
146 var index = this.getIndexFromUrl(_hash);
147 this.core.openGallery(index);
148 return true;
149 }
150 };
151 Hash.prototype.onCloseAfter = function () {
152 // Reset to old hash value
153 if (this.oldHash &&
154 this.oldHash.indexOf('lg=' + this.settings.galleryId) < 0) {
155 if (history.replaceState) {
156 history.replaceState(null, '', this.oldHash);
157 }
158 else {
159 window.location.hash = this.oldHash;
160 }
161 }
162 else {
163 if (history.replaceState) {
164 history.replaceState(null, document.title, window.location.pathname + window.location.search);
165 }
166 else {
167 window.location.hash = '';
168 }
169 }
170 };
171 Hash.prototype.onHashchange = function () {
172 if (!this.core.lgOpened)
173 return;
174 var _hash = window.location.hash;
175 var index = this.getIndexFromUrl(_hash);
176 // it galleryId doesn't exist in the url close the gallery
177 if (_hash.indexOf('lg=' + this.settings.galleryId) > -1) {
178 this.core.slide(index, false, false);
179 }
180 else if (this.core.lGalleryOn) {
181 this.core.closeGallery();
182 }
183 };
184 Hash.prototype.closeGallery = function () {
185 if (this.settings.hash) {
186 this.$LG(document.body).removeClass('lg-from-hash');
187 }
188 };
189 Hash.prototype.destroy = function () {
190 this.core.LGel.off('.lg.hash');
191 this.core.LGel.off('.hash');
192 this.$LG(window).off("hashchange.lg.hash.global" + this.core.lgId);
193 };
194 return Hash;
195}());
196
197export default Hash;
198//# sourceMappingURL=lg-hash.es5.js.map