UNPKG

5.34 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14var lg_events_1 = require("../../lg-events");
15var lg_hash_settings_1 = require("./lg-hash-settings");
16var Hash = /** @class */ (function () {
17 function Hash(instance, $LG) {
18 // get lightGallery core plugin instance
19 this.core = instance;
20 this.$LG = $LG;
21 // extend module default settings with lightGallery core settings
22 this.settings = __assign(__assign({}, lg_hash_settings_1.hashSettings), this.core.settings);
23 return this;
24 }
25 Hash.prototype.init = function () {
26 var _this = this;
27 if (!this.settings.hash) {
28 return;
29 }
30 this.oldHash = window.location.hash;
31 setTimeout(function () {
32 _this.buildFromHash();
33 }, 100);
34 // Change hash value on after each slide transition
35 this.core.LGel.on(lg_events_1.lGEvents.afterSlide + ".hash", this.onAfterSlide.bind(this));
36 this.core.LGel.on(lg_events_1.lGEvents.afterClose + ".hash", this.onCloseAfter.bind(this));
37 // Listen hash change and change the slide according to slide value
38 this.$LG(window).on("hashchange.lg.hash.global" + this.core.lgId, this.onHashchange.bind(this));
39 };
40 Hash.prototype.onAfterSlide = function (event) {
41 var slideName = this.core.galleryItems[event.detail.index].slideName;
42 slideName = this.settings.customSlideName
43 ? slideName || event.detail.index
44 : event.detail.index;
45 if (history.replaceState) {
46 history.replaceState(null, '', window.location.pathname +
47 window.location.search +
48 '#lg=' +
49 this.settings.galleryId +
50 '&slide=' +
51 slideName);
52 }
53 else {
54 window.location.hash =
55 'lg=' + this.settings.galleryId + '&slide=' + slideName;
56 }
57 };
58 /**
59 * Get index of the slide from custom slideName. Has to be a public method. Used in hash plugin
60 * @param {String} hash
61 * @returns {Number} Index of the slide.
62 */
63 Hash.prototype.getIndexFromUrl = function (hash) {
64 if (hash === void 0) { hash = window.location.hash; }
65 var slideName = hash.split('&slide=')[1];
66 var _idx = 0;
67 if (this.settings.customSlideName) {
68 for (var index = 0; index < this.core.galleryItems.length; index++) {
69 var dynamicEl = this.core.galleryItems[index];
70 if (dynamicEl.slideName === slideName) {
71 _idx = index;
72 break;
73 }
74 }
75 }
76 else {
77 _idx = parseInt(slideName, 10);
78 }
79 return isNaN(_idx) ? 0 : _idx;
80 };
81 // Build Gallery if gallery id exist in the URL
82 Hash.prototype.buildFromHash = function () {
83 // if dynamic option is enabled execute immediately
84 var _hash = window.location.hash;
85 if (_hash.indexOf('lg=' + this.settings.galleryId) > 0) {
86 // This class is used to remove the initial animation if galleryId present in the URL
87 this.$LG(document.body).addClass('lg-from-hash');
88 var index = this.getIndexFromUrl(_hash);
89 this.core.openGallery(index);
90 return true;
91 }
92 };
93 Hash.prototype.onCloseAfter = function () {
94 // Reset to old hash value
95 if (this.oldHash &&
96 this.oldHash.indexOf('lg=' + this.settings.galleryId) < 0) {
97 if (history.replaceState) {
98 history.replaceState(null, '', this.oldHash);
99 }
100 else {
101 window.location.hash = this.oldHash;
102 }
103 }
104 else {
105 if (history.replaceState) {
106 history.replaceState(null, document.title, window.location.pathname + window.location.search);
107 }
108 else {
109 window.location.hash = '';
110 }
111 }
112 };
113 Hash.prototype.onHashchange = function () {
114 if (!this.core.lgOpened)
115 return;
116 var _hash = window.location.hash;
117 var index = this.getIndexFromUrl(_hash);
118 // it galleryId doesn't exist in the url close the gallery
119 if (_hash.indexOf('lg=' + this.settings.galleryId) > -1) {
120 this.core.slide(index, false, false);
121 }
122 else if (this.core.lGalleryOn) {
123 this.core.closeGallery();
124 }
125 };
126 Hash.prototype.closeGallery = function () {
127 if (this.settings.hash) {
128 this.$LG(document.body).removeClass('lg-from-hash');
129 }
130 };
131 Hash.prototype.destroy = function () {
132 this.core.LGel.off('.lg.hash');
133 this.core.LGel.off('.hash');
134 this.$LG(window).off("hashchange.lg.hash.global" + this.core.lgId);
135 };
136 return Hash;
137}());
138exports.default = Hash;
139//# sourceMappingURL=lg-hash.js.map
\No newline at end of file