UNPKG

13 kBJavaScriptView Raw
1"use strict";
2var __spreadArrays = (this && this.__spreadArrays) || function () {
3 for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
4 for (var r = Array(s), k = 0, i = 0; i < il; i++)
5 for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
6 r[k] = a[j];
7 return r;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.convertToData = void 0;
11var lgQuery_1 = require("./lgQuery");
12var defaultDynamicOptions = [
13 'src',
14 'sources',
15 'subHtml',
16 'subHtmlUrl',
17 'html',
18 'video',
19 'poster',
20 'slideName',
21 'responsive',
22 'srcset',
23 'sizes',
24 'iframe',
25 'downloadUrl',
26 'download',
27 'width',
28 'facebookShareUrl',
29 'tweetText',
30 'iframeTitle',
31 'twitterShareUrl',
32 'pinterestShareUrl',
33 'pinterestText',
34 'fbHtml',
35 'disqusIdentifier',
36 'disqusUrl',
37];
38// Convert html data-attribute to camalcase
39function convertToData(attr) {
40 // FInd a way for lgsize
41 if (attr === 'href') {
42 return 'src';
43 }
44 attr = attr.replace('data-', '');
45 attr = attr.charAt(0).toLowerCase() + attr.slice(1);
46 attr = attr.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
47 return attr;
48}
49exports.convertToData = convertToData;
50var utils = {
51 /**
52 * get possible width and height from the lgSize attribute. Used for ZoomFromOrigin option
53 */
54 getSize: function (el, container, spacing, defaultLgSize) {
55 if (spacing === void 0) { spacing = 0; }
56 var LGel = lgQuery_1.$LG(el);
57 var lgSize = LGel.attr('data-lg-size') || defaultLgSize;
58 if (!lgSize) {
59 return;
60 }
61 var isResponsiveSizes = lgSize.split(',');
62 // if at-least two viewport sizes are available
63 if (isResponsiveSizes[1]) {
64 var wWidth = window.innerWidth;
65 for (var i = 0; i < isResponsiveSizes.length; i++) {
66 var size_1 = isResponsiveSizes[i];
67 var responsiveWidth = parseInt(size_1.split('-')[2], 10);
68 if (responsiveWidth > wWidth) {
69 lgSize = size_1;
70 break;
71 }
72 // take last item as last option
73 if (i === isResponsiveSizes.length - 1) {
74 lgSize = size_1;
75 }
76 }
77 }
78 var size = lgSize.split('-');
79 var width = parseInt(size[0], 10);
80 var height = parseInt(size[1], 10);
81 var cWidth = container.width();
82 var cHeight = container.height() - spacing;
83 var maxWidth = Math.min(cWidth, width);
84 var maxHeight = Math.min(cHeight, height);
85 var ratio = Math.min(maxWidth / width, maxHeight / height);
86 return { width: width * ratio, height: height * ratio };
87 },
88 /**
89 * @desc Get transform value based on the imageSize. Used for ZoomFromOrigin option
90 * @param {jQuery Element}
91 * @returns {String} Transform CSS string
92 */
93 getTransform: function (el, container, top, bottom, imageSize) {
94 if (!imageSize) {
95 return;
96 }
97 var LGel = lgQuery_1.$LG(el).find('img').first();
98 if (!LGel.get()) {
99 return;
100 }
101 var containerRect = container.get().getBoundingClientRect();
102 var wWidth = containerRect.width;
103 // using innerWidth to include mobile safari bottom bar
104 var wHeight = container.height() - (top + bottom);
105 var elWidth = LGel.width();
106 var elHeight = LGel.height();
107 var elStyle = LGel.style();
108 var x = (wWidth - elWidth) / 2 -
109 LGel.offset().left +
110 (parseFloat(elStyle.paddingLeft) || 0) +
111 (parseFloat(elStyle.borderLeft) || 0) +
112 lgQuery_1.$LG(window).scrollLeft() +
113 containerRect.left;
114 var y = (wHeight - elHeight) / 2 -
115 LGel.offset().top +
116 (parseFloat(elStyle.paddingTop) || 0) +
117 (parseFloat(elStyle.borderTop) || 0) +
118 lgQuery_1.$LG(window).scrollTop() +
119 top;
120 var scX = elWidth / imageSize.width;
121 var scY = elHeight / imageSize.height;
122 var transform = 'translate3d(' +
123 (x *= -1) +
124 'px, ' +
125 (y *= -1) +
126 'px, 0) scale3d(' +
127 scX +
128 ', ' +
129 scY +
130 ', 1)';
131 return transform;
132 },
133 getIframeMarkup: function (iframeWidth, iframeHeight, iframeMaxWidth, iframeMaxHeight, src, iframeTitle) {
134 var title = iframeTitle ? 'title="' + iframeTitle + '"' : '';
135 return "<div class=\"lg-video-cont lg-has-iframe\" style=\"width:" + iframeWidth + "; max-width:" + iframeMaxWidth + "; height: " + iframeHeight + "; max-height:" + iframeMaxHeight + "\">\n <iframe class=\"lg-object\" frameborder=\"0\" " + title + " src=\"" + src + "\" allowfullscreen=\"true\"></iframe>\n </div>";
136 },
137 getImgMarkup: function (index, src, altAttr, srcset, sizes, sources) {
138 var srcsetAttr = srcset ? "srcset=\"" + srcset + "\"" : '';
139 var sizesAttr = sizes ? "sizes=\"" + sizes + "\"" : '';
140 var imgMarkup = "<img " + altAttr + " " + srcsetAttr + " " + sizesAttr + " class=\"lg-object lg-image\" data-index=\"" + index + "\" src=\"" + src + "\" />";
141 var sourceTag = '';
142 if (sources) {
143 var sourceObj = typeof sources === 'string' ? JSON.parse(sources) : sources;
144 sourceTag = sourceObj.map(function (source) {
145 var attrs = '';
146 Object.keys(source).forEach(function (key) {
147 // Do not remove the first space as it is required to separate the attributes
148 attrs += " " + key + "=\"" + source[key] + "\"";
149 });
150 return "<source " + attrs + "></source>";
151 });
152 }
153 return "" + sourceTag + imgMarkup;
154 },
155 // Get src from responsive src
156 getResponsiveSrc: function (srcItms) {
157 var rsWidth = [];
158 var rsSrc = [];
159 var src = '';
160 for (var i = 0; i < srcItms.length; i++) {
161 var _src = srcItms[i].split(' ');
162 // Manage empty space
163 if (_src[0] === '') {
164 _src.splice(0, 1);
165 }
166 rsSrc.push(_src[0]);
167 rsWidth.push(_src[1]);
168 }
169 var wWidth = window.innerWidth;
170 for (var j = 0; j < rsWidth.length; j++) {
171 if (parseInt(rsWidth[j], 10) > wWidth) {
172 src = rsSrc[j];
173 break;
174 }
175 }
176 return src;
177 },
178 isImageLoaded: function (img) {
179 if (!img)
180 return false;
181 // During the onload event, IE correctly identifies any images that
182 // weren’t downloaded as not complete. Others should too. Gecko-based
183 // browsers act like NS4 in that they report this incorrectly.
184 if (!img.complete) {
185 return false;
186 }
187 // However, they do have two very useful properties: naturalWidth and
188 // naturalHeight. These give the true size of the image. If it failed
189 // to load, either of these should be zero.
190 if (img.naturalWidth === 0) {
191 return false;
192 }
193 // No other way of checking: assume it’s ok.
194 return true;
195 },
196 getVideoPosterMarkup: function (_poster, dummyImg, videoContStyle, playVideoString, _isVideo) {
197 var videoClass = '';
198 if (_isVideo && _isVideo.youtube) {
199 videoClass = 'lg-has-youtube';
200 }
201 else if (_isVideo && _isVideo.vimeo) {
202 videoClass = 'lg-has-vimeo';
203 }
204 else {
205 videoClass = 'lg-has-html5';
206 }
207 return "<div class=\"lg-video-cont " + videoClass + "\" style=\"" + videoContStyle + "\">\n <div class=\"lg-video-play-button\">\n <svg\n viewBox=\"0 0 20 20\"\n preserveAspectRatio=\"xMidYMid\"\n focusable=\"false\"\n aria-labelledby=\"" + playVideoString + "\"\n role=\"img\"\n class=\"lg-video-play-icon\"\n >\n <title>" + playVideoString + "</title>\n <polygon class=\"lg-video-play-icon-inner\" points=\"1,0 20,10 1,20\"></polygon>\n </svg>\n <svg class=\"lg-video-play-icon-bg\" viewBox=\"0 0 50 50\" focusable=\"false\">\n <circle cx=\"50%\" cy=\"50%\" r=\"20\"></circle></svg>\n <svg class=\"lg-video-play-icon-circle\" viewBox=\"0 0 50 50\" focusable=\"false\">\n <circle cx=\"50%\" cy=\"50%\" r=\"20\"></circle>\n </svg>\n </div>\n " + (dummyImg || '') + "\n <img class=\"lg-object lg-video-poster\" src=\"" + _poster + "\" />\n </div>";
208 },
209 getFocusableElements: function (container) {
210 var elements = container.querySelectorAll('a[href]:not([disabled]), button:not([disabled]), textarea:not([disabled]), input[type="text"]:not([disabled]), input[type="radio"]:not([disabled]), input[type="checkbox"]:not([disabled]), select:not([disabled])');
211 var visibleElements = [].filter.call(elements, function (element) {
212 var style = window.getComputedStyle(element);
213 return style.display !== 'none' && style.visibility !== 'hidden';
214 });
215 return visibleElements;
216 },
217 /**
218 * @desc Create dynamic elements array from gallery items when dynamic option is false
219 * It helps to avoid frequent DOM interaction
220 * and avoid multiple checks for dynamic elments
221 *
222 * @returns {Array} dynamicEl
223 */
224 getDynamicOptions: function (items, extraProps, getCaptionFromTitleOrAlt, exThumbImage) {
225 var dynamicElements = [];
226 var availableDynamicOptions = __spreadArrays(defaultDynamicOptions, extraProps);
227 [].forEach.call(items, function (item) {
228 var dynamicEl = {};
229 for (var i = 0; i < item.attributes.length; i++) {
230 var attr = item.attributes[i];
231 if (attr.specified) {
232 var dynamicAttr = convertToData(attr.name);
233 var label = '';
234 if (availableDynamicOptions.indexOf(dynamicAttr) > -1) {
235 label = dynamicAttr;
236 }
237 if (label) {
238 dynamicEl[label] = attr.value;
239 }
240 }
241 }
242 var currentItem = lgQuery_1.$LG(item);
243 var alt = currentItem.find('img').first().attr('alt');
244 var title = currentItem.attr('title');
245 var thumb = exThumbImage
246 ? currentItem.attr(exThumbImage)
247 : currentItem.find('img').first().attr('src');
248 dynamicEl.thumb = thumb;
249 if (getCaptionFromTitleOrAlt && !dynamicEl.subHtml) {
250 dynamicEl.subHtml = title || alt || '';
251 }
252 dynamicEl.alt = alt || title || '';
253 dynamicElements.push(dynamicEl);
254 });
255 return dynamicElements;
256 },
257 isMobile: function () {
258 return /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
259 },
260 /**
261 * @desc Check the given src is video
262 * @param {String} src
263 * @return {Object} video type
264 * Ex:{ youtube : ["//www.youtube.com/watch?v=c0asJgSyxcY", "c0asJgSyxcY"] }
265 *
266 * @todo - this information can be moved to dynamicEl to avoid frequent calls
267 */
268 isVideo: function (src, isHTML5VIdeo, index) {
269 if (!src) {
270 if (isHTML5VIdeo) {
271 return {
272 html5: true,
273 };
274 }
275 else {
276 console.error('lightGallery :- data-src is not provided on slide item ' +
277 (index + 1) +
278 '. Please make sure the selector property is properly configured. More info - https://www.lightgalleryjs.com/demos/html-markup/');
279 return;
280 }
281 }
282 var youtube = src.match(/\/\/(?:www\.)?youtu(?:\.be|be\.com|be-nocookie\.com)\/(?:watch\?v=|embed\/)?([a-z0-9\-\_\%]+)([\&|?][\S]*)*/i);
283 var vimeo = src.match(/\/\/(?:www\.)?(?:player\.)?vimeo.com\/(?:video\/)?([0-9a-z\-_]+)(.*)?/i);
284 var wistia = src.match(/https?:\/\/(.+)?(wistia\.com|wi\.st)\/(medias|embed)\/([0-9a-z\-_]+)(.*)/);
285 if (youtube) {
286 return {
287 youtube: youtube,
288 };
289 }
290 else if (vimeo) {
291 return {
292 vimeo: vimeo,
293 };
294 }
295 else if (wistia) {
296 return {
297 wistia: wistia,
298 };
299 }
300 },
301};
302exports.default = utils;
303//# sourceMappingURL=lg-utils.js.map
\No newline at end of file