UNPKG

39.9 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
34var zoomSettings = {
35 scale: 1,
36 zoom: true,
37 infiniteZoom: true,
38 actualSize: true,
39 showZoomInOutIcons: false,
40 actualSizeIcons: {
41 zoomIn: 'lg-zoom-in',
42 zoomOut: 'lg-zoom-out',
43 },
44 enableZoomAfter: 300,
45 zoomPluginStrings: {
46 zoomIn: 'Zoom in',
47 zoomOut: 'Zoom out',
48 viewActualSize: 'View actual size',
49 },
50};
51
52/**
53 * List of lightGallery events
54 * All events should be documented here
55 * Below interfaces are used to build the website documentations
56 * */
57var lGEvents = {
58 afterAppendSlide: 'lgAfterAppendSlide',
59 init: 'lgInit',
60 hasVideo: 'lgHasVideo',
61 containerResize: 'lgContainerResize',
62 updateSlides: 'lgUpdateSlides',
63 afterAppendSubHtml: 'lgAfterAppendSubHtml',
64 beforeOpen: 'lgBeforeOpen',
65 afterOpen: 'lgAfterOpen',
66 slideItemLoad: 'lgSlideItemLoad',
67 beforeSlide: 'lgBeforeSlide',
68 afterSlide: 'lgAfterSlide',
69 posterClick: 'lgPosterClick',
70 dragStart: 'lgDragStart',
71 dragMove: 'lgDragMove',
72 dragEnd: 'lgDragEnd',
73 beforeNextSlide: 'lgBeforeNextSlide',
74 beforePrevSlide: 'lgBeforePrevSlide',
75 beforeClose: 'lgBeforeClose',
76 afterClose: 'lgAfterClose',
77 rotateLeft: 'lgRotateLeft',
78 rotateRight: 'lgRotateRight',
79 flipHorizontal: 'lgFlipHorizontal',
80 flipVertical: 'lgFlipVertical',
81 autoplay: 'lgAutoplay',
82 autoplayStart: 'lgAutoplayStart',
83 autoplayStop: 'lgAutoplayStop',
84};
85
86var ZOOM_TRANSITION_DURATION = 500;
87var Zoom = /** @class */ (function () {
88 function Zoom(instance, $LG) {
89 // get lightGallery core plugin instance
90 this.core = instance;
91 this.$LG = $LG;
92 this.settings = __assign(__assign({}, zoomSettings), this.core.settings);
93 return this;
94 }
95 // Append Zoom controls. Actual size, Zoom-in, Zoom-out
96 Zoom.prototype.buildTemplates = function () {
97 var zoomIcons = this.settings.showZoomInOutIcons
98 ? "<button id=\"" + this.core.getIdName('lg-zoom-in') + "\" type=\"button\" aria-label=\"" + this.settings.zoomPluginStrings['zoomIn'] + "\" class=\"lg-zoom-in lg-icon\"></button><button id=\"" + this.core.getIdName('lg-zoom-out') + "\" type=\"button\" aria-label=\"" + this.settings.zoomPluginStrings['zoomIn'] + "\" class=\"lg-zoom-out lg-icon\"></button>"
99 : '';
100 if (this.settings.actualSize) {
101 zoomIcons += "<button id=\"" + this.core.getIdName('lg-actual-size') + "\" type=\"button\" aria-label=\"" + this.settings.zoomPluginStrings['viewActualSize'] + "\" class=\"" + this.settings.actualSizeIcons.zoomIn + " lg-icon\"></button>";
102 }
103 this.core.outer.addClass('lg-use-transition-for-zoom');
104 this.core.$toolbar.first().append(zoomIcons);
105 };
106 /**
107 * @desc Enable zoom option only once the image is completely loaded
108 * If zoomFromOrigin is true, Zoom is enabled once the dummy image has been inserted
109 *
110 * Zoom styles are defined under lg-zoomable CSS class.
111 */
112 Zoom.prototype.enableZoom = function (event) {
113 var _this = this;
114 // delay will be 0 except first time
115 var _speed = this.settings.enableZoomAfter + event.detail.delay;
116 // set _speed value 0 if gallery opened from direct url and if it is first slide
117 if (this.$LG('body').first().hasClass('lg-from-hash') &&
118 event.detail.delay) {
119 // will execute only once
120 _speed = 0;
121 }
122 else {
123 // Remove lg-from-hash to enable starting animation.
124 this.$LG('body').first().removeClass('lg-from-hash');
125 }
126 this.zoomableTimeout = setTimeout(function () {
127 if (!_this.isImageSlide(_this.core.index)) {
128 return;
129 }
130 _this.core.getSlideItem(event.detail.index).addClass('lg-zoomable');
131 if (event.detail.index === _this.core.index) {
132 _this.setZoomEssentials();
133 }
134 }, _speed + 30);
135 };
136 Zoom.prototype.enableZoomOnSlideItemLoad = function () {
137 // Add zoomable class
138 this.core.LGel.on(lGEvents.slideItemLoad + ".zoom", this.enableZoom.bind(this));
139 };
140 Zoom.prototype.getDragCords = function (e) {
141 return {
142 x: e.pageX,
143 y: e.pageY,
144 };
145 };
146 Zoom.prototype.getSwipeCords = function (e) {
147 var x = e.touches[0].pageX;
148 var y = e.touches[0].pageY;
149 return {
150 x: x,
151 y: y,
152 };
153 };
154 Zoom.prototype.getDragAllowedAxises = function (scale, scaleDiff) {
155 var $image = this.core
156 .getSlideItem(this.core.index)
157 .find('.lg-image')
158 .first()
159 .get();
160 var height = 0;
161 var width = 0;
162 var rect = $image.getBoundingClientRect();
163 if (scale) {
164 height = $image.offsetHeight * scale;
165 width = $image.offsetWidth * scale;
166 }
167 else if (scaleDiff) {
168 height = rect.height + scaleDiff * rect.height;
169 width = rect.width + scaleDiff * rect.width;
170 }
171 else {
172 height = rect.height;
173 width = rect.width;
174 }
175 var allowY = height > this.containerRect.height;
176 var allowX = width > this.containerRect.width;
177 return {
178 allowX: allowX,
179 allowY: allowY,
180 };
181 };
182 Zoom.prototype.setZoomEssentials = function () {
183 this.containerRect = this.core.$content.get().getBoundingClientRect();
184 };
185 /**
186 * @desc Image zoom
187 * Translate the wrap and scale the image to get better user experience
188 *
189 * @param {String} scale - Zoom decrement/increment value
190 */
191 Zoom.prototype.zoomImage = function (scale, scaleDiff, reposition, resetToMax) {
192 if (Math.abs(scaleDiff) <= 0)
193 return;
194 var offsetX = this.containerRect.width / 2 + this.containerRect.left;
195 var offsetY = this.containerRect.height / 2 +
196 this.containerRect.top +
197 this.scrollTop;
198 var originalX;
199 var originalY;
200 if (scale === 1) {
201 this.positionChanged = false;
202 }
203 var dragAllowedAxises = this.getDragAllowedAxises(0, scaleDiff);
204 var allowY = dragAllowedAxises.allowY, allowX = dragAllowedAxises.allowX;
205 if (this.positionChanged) {
206 originalX = this.left / (this.scale - scaleDiff);
207 originalY = this.top / (this.scale - scaleDiff);
208 this.pageX = offsetX - originalX;
209 this.pageY = offsetY - originalY;
210 this.positionChanged = false;
211 }
212 var possibleSwipeCords = this.getPossibleSwipeDragCords(scaleDiff);
213 var x;
214 var y;
215 var _x = offsetX - this.pageX;
216 var _y = offsetY - this.pageY;
217 if (scale - scaleDiff > 1) {
218 var scaleVal = (scale - scaleDiff) / Math.abs(scaleDiff);
219 _x =
220 (scaleDiff < 0 ? -_x : _x) +
221 this.left * (scaleVal + (scaleDiff < 0 ? -1 : 1));
222 _y =
223 (scaleDiff < 0 ? -_y : _y) +
224 this.top * (scaleVal + (scaleDiff < 0 ? -1 : 1));
225 x = _x / scaleVal;
226 y = _y / scaleVal;
227 }
228 else {
229 var scaleVal = (scale - scaleDiff) * scaleDiff;
230 x = _x * scaleVal;
231 y = _y * scaleVal;
232 }
233 if (reposition) {
234 if (allowX) {
235 if (this.isBeyondPossibleLeft(x, possibleSwipeCords.minX)) {
236 x = possibleSwipeCords.minX;
237 }
238 else if (this.isBeyondPossibleRight(x, possibleSwipeCords.maxX)) {
239 x = possibleSwipeCords.maxX;
240 }
241 }
242 else {
243 if (scale > 1) {
244 if (x < possibleSwipeCords.minX) {
245 x = possibleSwipeCords.minX;
246 }
247 else if (x > possibleSwipeCords.maxX) {
248 x = possibleSwipeCords.maxX;
249 }
250 }
251 }
252 // @todo fix this
253 if (allowY) {
254 if (this.isBeyondPossibleTop(y, possibleSwipeCords.minY)) {
255 y = possibleSwipeCords.minY;
256 }
257 else if (this.isBeyondPossibleBottom(y, possibleSwipeCords.maxY)) {
258 y = possibleSwipeCords.maxY;
259 }
260 }
261 else {
262 // If the translate value based on index of beyond the viewport, utilize the available space to prevent image being cut out
263 if (scale > 1) {
264 //If image goes beyond viewport top, use the minim possible translate value
265 if (y < possibleSwipeCords.minY) {
266 y = possibleSwipeCords.minY;
267 }
268 else if (y > possibleSwipeCords.maxY) {
269 y = possibleSwipeCords.maxY;
270 }
271 }
272 }
273 }
274 this.setZoomStyles({
275 x: x,
276 y: y,
277 scale: scale,
278 });
279 this.left = x;
280 this.top = y;
281 if (resetToMax) {
282 this.setZoomImageSize();
283 }
284 };
285 Zoom.prototype.resetImageTranslate = function (index) {
286 if (!this.isImageSlide(index)) {
287 return;
288 }
289 var $image = this.core.getSlideItem(index).find('.lg-image').first();
290 this.imageReset = false;
291 $image.removeClass('reset-transition reset-transition-y reset-transition-x');
292 this.core.outer.removeClass('lg-actual-size');
293 $image.css('width', 'auto').css('height', 'auto');
294 setTimeout(function () {
295 $image.removeClass('no-transition');
296 }, 10);
297 };
298 Zoom.prototype.setZoomImageSize = function () {
299 var _this = this;
300 var $image = this.core
301 .getSlideItem(this.core.index)
302 .find('.lg-image')
303 .first();
304 setTimeout(function () {
305 var actualSizeScale = _this.getCurrentImageActualSizeScale();
306 if (_this.scale >= actualSizeScale) {
307 $image.addClass('no-transition');
308 _this.imageReset = true;
309 }
310 }, ZOOM_TRANSITION_DURATION);
311 setTimeout(function () {
312 var actualSizeScale = _this.getCurrentImageActualSizeScale();
313 if (_this.scale >= actualSizeScale) {
314 var dragAllowedAxises = _this.getDragAllowedAxises(_this.scale);
315 $image
316 .css('width', $image.get().naturalWidth + 'px')
317 .css('height', $image.get().naturalHeight + 'px');
318 _this.core.outer.addClass('lg-actual-size');
319 if (dragAllowedAxises.allowX && dragAllowedAxises.allowY) {
320 $image.addClass('reset-transition');
321 }
322 else if (dragAllowedAxises.allowX &&
323 !dragAllowedAxises.allowY) {
324 $image.addClass('reset-transition-x');
325 }
326 else if (!dragAllowedAxises.allowX &&
327 dragAllowedAxises.allowY) {
328 $image.addClass('reset-transition-y');
329 }
330 }
331 }, ZOOM_TRANSITION_DURATION + 50);
332 };
333 /**
334 * @desc apply scale3d to image and translate to image wrap
335 * @param {style} X,Y and scale
336 */
337 Zoom.prototype.setZoomStyles = function (style) {
338 var $imageWrap = this.core
339 .getSlideItem(this.core.index)
340 .find('.lg-img-wrap')
341 .first();
342 var $image = this.core
343 .getSlideItem(this.core.index)
344 .find('.lg-image')
345 .first();
346 var $dummyImage = this.core.outer
347 .find('.lg-current .lg-dummy-img')
348 .first();
349 this.scale = style.scale;
350 $image.css('transform', 'scale3d(' + style.scale + ', ' + style.scale + ', 1)');
351 $dummyImage.css('transform', 'scale3d(' + style.scale + ', ' + style.scale + ', 1)');
352 var transform = 'translate3d(' + style.x + 'px, ' + style.y + 'px, 0)';
353 $imageWrap.css('transform', transform);
354 };
355 /**
356 * @param index - Index of the current slide
357 * @param event - event will be available only if the function is called on clicking/taping the imags
358 */
359 Zoom.prototype.setActualSize = function (index, event) {
360 var _this = this;
361 if (this.zoomInProgress) {
362 return;
363 }
364 this.zoomInProgress = true;
365 var currentItem = this.core.galleryItems[this.core.index];
366 this.resetImageTranslate(index);
367 setTimeout(function () {
368 // Allow zoom only on image
369 if (!currentItem.src ||
370 _this.core.outer.hasClass('lg-first-slide-loading')) {
371 return;
372 }
373 var scale = _this.getCurrentImageActualSizeScale();
374 var prevScale = _this.scale;
375 if (_this.core.outer.hasClass('lg-zoomed')) {
376 _this.scale = 1;
377 }
378 else {
379 _this.scale = _this.getScale(scale);
380 }
381 _this.setPageCords(event);
382 _this.beginZoom(_this.scale);
383 _this.zoomImage(_this.scale, _this.scale - prevScale, true, true);
384 }, 50);
385 setTimeout(function () {
386 _this.core.outer.removeClass('lg-grabbing').addClass('lg-grab');
387 }, 60);
388 setTimeout(function () {
389 _this.zoomInProgress = false;
390 }, ZOOM_TRANSITION_DURATION + 110);
391 };
392 Zoom.prototype.getNaturalWidth = function (index) {
393 var $image = this.core.getSlideItem(index).find('.lg-image').first();
394 var naturalWidth = this.core.galleryItems[index].width;
395 return naturalWidth
396 ? parseFloat(naturalWidth)
397 : $image.get().naturalWidth;
398 };
399 Zoom.prototype.getActualSizeScale = function (naturalWidth, width) {
400 var _scale;
401 var scale;
402 if (naturalWidth >= width) {
403 _scale = naturalWidth / width;
404 scale = _scale || 2;
405 }
406 else {
407 scale = 1;
408 }
409 return scale;
410 };
411 Zoom.prototype.getCurrentImageActualSizeScale = function () {
412 var $image = this.core
413 .getSlideItem(this.core.index)
414 .find('.lg-image')
415 .first();
416 var width = $image.get().offsetWidth;
417 var naturalWidth = this.getNaturalWidth(this.core.index) || width;
418 return this.getActualSizeScale(naturalWidth, width);
419 };
420 Zoom.prototype.getPageCords = function (event) {
421 var cords = {};
422 if (event) {
423 cords.x = event.pageX || event.touches[0].pageX;
424 cords.y = event.pageY || event.touches[0].pageY;
425 }
426 else {
427 var containerRect = this.core.$content
428 .get()
429 .getBoundingClientRect();
430 cords.x = containerRect.width / 2 + containerRect.left;
431 cords.y =
432 containerRect.height / 2 + this.scrollTop + containerRect.top;
433 }
434 return cords;
435 };
436 Zoom.prototype.setPageCords = function (event) {
437 var pageCords = this.getPageCords(event);
438 this.pageX = pageCords.x;
439 this.pageY = pageCords.y;
440 };
441 Zoom.prototype.manageActualPixelClassNames = function () {
442 var $actualSize = this.core.getElementById('lg-actual-size');
443 $actualSize
444 .removeClass(this.settings.actualSizeIcons.zoomIn)
445 .addClass(this.settings.actualSizeIcons.zoomOut);
446 };
447 // If true, zoomed - in else zoomed out
448 Zoom.prototype.beginZoom = function (scale) {
449 this.core.outer.removeClass('lg-zoom-drag-transition lg-zoom-dragging');
450 if (scale > 1) {
451 this.core.outer.addClass('lg-zoomed');
452 this.manageActualPixelClassNames();
453 }
454 else {
455 this.resetZoom();
456 }
457 return scale > 1;
458 };
459 Zoom.prototype.getScale = function (scale) {
460 var actualSizeScale = this.getCurrentImageActualSizeScale();
461 if (scale < 1) {
462 scale = 1;
463 }
464 else if (scale > actualSizeScale) {
465 scale = actualSizeScale;
466 }
467 return scale;
468 };
469 Zoom.prototype.init = function () {
470 var _this = this;
471 if (!this.settings.zoom) {
472 return;
473 }
474 this.buildTemplates();
475 this.enableZoomOnSlideItemLoad();
476 var tapped = null;
477 this.core.outer.on('dblclick.lg', function (event) {
478 if (!_this.$LG(event.target).hasClass('lg-image')) {
479 return;
480 }
481 _this.setActualSize(_this.core.index, event);
482 });
483 this.core.outer.on('touchstart.lg', function (event) {
484 var $target = _this.$LG(event.target);
485 if (event.touches.length === 1 && $target.hasClass('lg-image')) {
486 if (!tapped) {
487 tapped = setTimeout(function () {
488 tapped = null;
489 }, 300);
490 }
491 else {
492 clearTimeout(tapped);
493 tapped = null;
494 event.preventDefault();
495 _this.setActualSize(_this.core.index, event);
496 }
497 }
498 });
499 this.core.LGel.on(lGEvents.containerResize + ".zoom " + lGEvents.rotateRight + ".zoom " + lGEvents.rotateLeft + ".zoom " + lGEvents.flipHorizontal + ".zoom " + lGEvents.flipVertical + ".zoom", function () {
500 if (!_this.core.lgOpened ||
501 !_this.isImageSlide(_this.core.index) ||
502 _this.core.touchAction) {
503 return;
504 }
505 var _LGel = _this.core
506 .getSlideItem(_this.core.index)
507 .find('.lg-img-wrap')
508 .first();
509 _this.top = 0;
510 _this.left = 0;
511 _this.setZoomEssentials();
512 _this.setZoomSwipeStyles(_LGel, { x: 0, y: 0 });
513 _this.positionChanged = true;
514 });
515 // Update zoom on resize and orientationchange
516 this.$LG(window).on("scroll.lg.zoom.global" + this.core.lgId, function () {
517 if (!_this.core.lgOpened)
518 return;
519 _this.scrollTop = _this.$LG(window).scrollTop();
520 });
521 this.core.getElementById('lg-zoom-out').on('click.lg', function () {
522 // Allow zoom only on image
523 if (!_this.isImageSlide(_this.core.index)) {
524 return;
525 }
526 var timeout = 0;
527 if (_this.imageReset) {
528 _this.resetImageTranslate(_this.core.index);
529 timeout = 50;
530 }
531 setTimeout(function () {
532 var scale = _this.scale - _this.settings.scale;
533 if (scale < 1) {
534 scale = 1;
535 }
536 _this.beginZoom(scale);
537 _this.zoomImage(scale, -_this.settings.scale, true, !_this.settings.infiniteZoom);
538 }, timeout);
539 });
540 this.core.getElementById('lg-zoom-in').on('click.lg', function () {
541 _this.zoomIn();
542 });
543 this.core.getElementById('lg-actual-size').on('click.lg', function () {
544 _this.setActualSize(_this.core.index);
545 });
546 this.core.LGel.on(lGEvents.beforeOpen + ".zoom", function () {
547 _this.core.outer.find('.lg-item').removeClass('lg-zoomable');
548 });
549 this.core.LGel.on(lGEvents.afterOpen + ".zoom", function () {
550 _this.scrollTop = _this.$LG(window).scrollTop();
551 // Set the initial value center
552 _this.pageX = _this.core.outer.width() / 2;
553 _this.pageY = _this.core.outer.height() / 2 + _this.scrollTop;
554 _this.scale = 1;
555 });
556 // Reset zoom on slide change
557 this.core.LGel.on(lGEvents.afterSlide + ".zoom", function (event) {
558 var prevIndex = event.detail.prevIndex;
559 _this.scale = 1;
560 _this.positionChanged = false;
561 _this.zoomInProgress = false;
562 _this.resetZoom(prevIndex);
563 _this.resetImageTranslate(prevIndex);
564 if (_this.isImageSlide(_this.core.index)) {
565 _this.setZoomEssentials();
566 }
567 });
568 // Drag option after zoom
569 this.zoomDrag();
570 this.pinchZoom();
571 this.zoomSwipe();
572 // Store the zoomable timeout value just to clear it while closing
573 this.zoomableTimeout = false;
574 this.positionChanged = false;
575 this.zoomInProgress = false;
576 };
577 Zoom.prototype.zoomIn = function () {
578 // Allow zoom only on image
579 if (!this.isImageSlide(this.core.index)) {
580 return;
581 }
582 var scale = this.scale + this.settings.scale;
583 if (!this.settings.infiniteZoom) {
584 scale = this.getScale(scale);
585 }
586 this.beginZoom(scale);
587 this.zoomImage(scale, Math.min(this.settings.scale, scale - this.scale), true, !this.settings.infiniteZoom);
588 };
589 // Reset zoom effect
590 Zoom.prototype.resetZoom = function (index) {
591 this.core.outer.removeClass('lg-zoomed lg-zoom-drag-transition');
592 var $actualSize = this.core.getElementById('lg-actual-size');
593 var $item = this.core.getSlideItem(index !== undefined ? index : this.core.index);
594 $actualSize
595 .removeClass(this.settings.actualSizeIcons.zoomOut)
596 .addClass(this.settings.actualSizeIcons.zoomIn);
597 $item.find('.lg-img-wrap').first().removeAttr('style');
598 $item.find('.lg-image').first().removeAttr('style');
599 this.scale = 1;
600 this.left = 0;
601 this.top = 0;
602 // Reset pagx pagy values to center
603 this.setPageCords();
604 };
605 Zoom.prototype.getTouchDistance = function (e) {
606 return Math.sqrt((e.touches[0].pageX - e.touches[1].pageX) *
607 (e.touches[0].pageX - e.touches[1].pageX) +
608 (e.touches[0].pageY - e.touches[1].pageY) *
609 (e.touches[0].pageY - e.touches[1].pageY));
610 };
611 Zoom.prototype.pinchZoom = function () {
612 var _this = this;
613 var startDist = 0;
614 var pinchStarted = false;
615 var initScale = 1;
616 var prevScale = 0;
617 var $item = this.core.getSlideItem(this.core.index);
618 this.core.outer.on('touchstart.lg', function (e) {
619 $item = _this.core.getSlideItem(_this.core.index);
620 if (!_this.isImageSlide(_this.core.index)) {
621 return;
622 }
623 if (e.touches.length === 2) {
624 e.preventDefault();
625 if (_this.core.outer.hasClass('lg-first-slide-loading')) {
626 return;
627 }
628 initScale = _this.scale || 1;
629 _this.core.outer.removeClass('lg-zoom-drag-transition lg-zoom-dragging');
630 _this.setPageCords(e);
631 _this.resetImageTranslate(_this.core.index);
632 _this.core.touchAction = 'pinch';
633 startDist = _this.getTouchDistance(e);
634 }
635 });
636 this.core.$inner.on('touchmove.lg', function (e) {
637 if (e.touches.length === 2 &&
638 _this.core.touchAction === 'pinch' &&
639 (_this.$LG(e.target).hasClass('lg-item') ||
640 $item.get().contains(e.target))) {
641 e.preventDefault();
642 var endDist = _this.getTouchDistance(e);
643 var distance = startDist - endDist;
644 if (!pinchStarted && Math.abs(distance) > 5) {
645 pinchStarted = true;
646 }
647 if (pinchStarted) {
648 prevScale = _this.scale;
649 var _scale = Math.max(1, initScale + -distance * 0.02);
650 _this.scale =
651 Math.round((_scale + Number.EPSILON) * 100) / 100;
652 var diff = _this.scale - prevScale;
653 _this.zoomImage(_this.scale, Math.round((diff + Number.EPSILON) * 100) / 100, false, false);
654 }
655 }
656 });
657 this.core.$inner.on('touchend.lg', function (e) {
658 if (_this.core.touchAction === 'pinch' &&
659 (_this.$LG(e.target).hasClass('lg-item') ||
660 $item.get().contains(e.target))) {
661 pinchStarted = false;
662 startDist = 0;
663 if (_this.scale <= 1) {
664 _this.resetZoom();
665 }
666 else {
667 var actualSizeScale = _this.getCurrentImageActualSizeScale();
668 if (_this.scale >= actualSizeScale) {
669 var scaleDiff = actualSizeScale - _this.scale;
670 if (scaleDiff === 0) {
671 scaleDiff = 0.01;
672 }
673 _this.zoomImage(actualSizeScale, scaleDiff, false, true);
674 }
675 _this.manageActualPixelClassNames();
676 _this.core.outer.addClass('lg-zoomed');
677 }
678 _this.core.touchAction = undefined;
679 }
680 });
681 };
682 Zoom.prototype.touchendZoom = function (startCoords, endCoords, allowX, allowY, touchDuration) {
683 var distanceXnew = endCoords.x - startCoords.x;
684 var distanceYnew = endCoords.y - startCoords.y;
685 var speedX = Math.abs(distanceXnew) / touchDuration + 1;
686 var speedY = Math.abs(distanceYnew) / touchDuration + 1;
687 if (speedX > 2) {
688 speedX += 1;
689 }
690 if (speedY > 2) {
691 speedY += 1;
692 }
693 distanceXnew = distanceXnew * speedX;
694 distanceYnew = distanceYnew * speedY;
695 var _LGel = this.core
696 .getSlideItem(this.core.index)
697 .find('.lg-img-wrap')
698 .first();
699 var distance = {};
700 distance.x = this.left + distanceXnew;
701 distance.y = this.top + distanceYnew;
702 var possibleSwipeCords = this.getPossibleSwipeDragCords();
703 if (Math.abs(distanceXnew) > 15 || Math.abs(distanceYnew) > 15) {
704 if (allowY) {
705 if (this.isBeyondPossibleTop(distance.y, possibleSwipeCords.minY)) {
706 distance.y = possibleSwipeCords.minY;
707 }
708 else if (this.isBeyondPossibleBottom(distance.y, possibleSwipeCords.maxY)) {
709 distance.y = possibleSwipeCords.maxY;
710 }
711 }
712 if (allowX) {
713 if (this.isBeyondPossibleLeft(distance.x, possibleSwipeCords.minX)) {
714 distance.x = possibleSwipeCords.minX;
715 }
716 else if (this.isBeyondPossibleRight(distance.x, possibleSwipeCords.maxX)) {
717 distance.x = possibleSwipeCords.maxX;
718 }
719 }
720 if (allowY) {
721 this.top = distance.y;
722 }
723 else {
724 distance.y = this.top;
725 }
726 if (allowX) {
727 this.left = distance.x;
728 }
729 else {
730 distance.x = this.left;
731 }
732 this.setZoomSwipeStyles(_LGel, distance);
733 this.positionChanged = true;
734 }
735 };
736 Zoom.prototype.getZoomSwipeCords = function (startCoords, endCoords, allowX, allowY, possibleSwipeCords) {
737 var distance = {};
738 if (allowY) {
739 distance.y = this.top + (endCoords.y - startCoords.y);
740 if (this.isBeyondPossibleTop(distance.y, possibleSwipeCords.minY)) {
741 var diffMinY = possibleSwipeCords.minY - distance.y;
742 distance.y = possibleSwipeCords.minY - diffMinY / 6;
743 }
744 else if (this.isBeyondPossibleBottom(distance.y, possibleSwipeCords.maxY)) {
745 var diffMaxY = distance.y - possibleSwipeCords.maxY;
746 distance.y = possibleSwipeCords.maxY + diffMaxY / 6;
747 }
748 }
749 else {
750 distance.y = this.top;
751 }
752 if (allowX) {
753 distance.x = this.left + (endCoords.x - startCoords.x);
754 if (this.isBeyondPossibleLeft(distance.x, possibleSwipeCords.minX)) {
755 var diffMinX = possibleSwipeCords.minX - distance.x;
756 distance.x = possibleSwipeCords.minX - diffMinX / 6;
757 }
758 else if (this.isBeyondPossibleRight(distance.x, possibleSwipeCords.maxX)) {
759 var difMaxX = distance.x - possibleSwipeCords.maxX;
760 distance.x = possibleSwipeCords.maxX + difMaxX / 6;
761 }
762 }
763 else {
764 distance.x = this.left;
765 }
766 return distance;
767 };
768 Zoom.prototype.isBeyondPossibleLeft = function (x, minX) {
769 return x >= minX;
770 };
771 Zoom.prototype.isBeyondPossibleRight = function (x, maxX) {
772 return x <= maxX;
773 };
774 Zoom.prototype.isBeyondPossibleTop = function (y, minY) {
775 return y >= minY;
776 };
777 Zoom.prototype.isBeyondPossibleBottom = function (y, maxY) {
778 return y <= maxY;
779 };
780 Zoom.prototype.isImageSlide = function (index) {
781 var currentItem = this.core.galleryItems[index];
782 return this.core.getSlideType(currentItem) === 'image';
783 };
784 Zoom.prototype.getPossibleSwipeDragCords = function (scale) {
785 var $image = this.core
786 .getSlideItem(this.core.index)
787 .find('.lg-image')
788 .first();
789 var bottom = this.core.mediaContainerPosition.bottom;
790 var imgRect = $image.get().getBoundingClientRect();
791 var imageHeight = imgRect.height;
792 var imageWidth = imgRect.width;
793 if (scale) {
794 imageHeight = imageHeight + scale * imageHeight;
795 imageWidth = imageWidth + scale * imageWidth;
796 }
797 var minY = (imageHeight - this.containerRect.height) / 2;
798 var maxY = (this.containerRect.height - imageHeight) / 2 + bottom;
799 var minX = (imageWidth - this.containerRect.width) / 2;
800 var maxX = (this.containerRect.width - imageWidth) / 2;
801 var possibleSwipeCords = {
802 minY: minY,
803 maxY: maxY,
804 minX: minX,
805 maxX: maxX,
806 };
807 return possibleSwipeCords;
808 };
809 Zoom.prototype.setZoomSwipeStyles = function (LGel, distance) {
810 LGel.css('transform', 'translate3d(' + distance.x + 'px, ' + distance.y + 'px, 0)');
811 };
812 Zoom.prototype.zoomSwipe = function () {
813 var _this = this;
814 var startCoords = {};
815 var endCoords = {};
816 var isMoved = false;
817 // Allow x direction drag
818 var allowX = false;
819 // Allow Y direction drag
820 var allowY = false;
821 var startTime = new Date();
822 var endTime = new Date();
823 var possibleSwipeCords;
824 var _LGel;
825 var $item = this.core.getSlideItem(this.core.index);
826 this.core.$inner.on('touchstart.lg', function (e) {
827 // Allow zoom only on image
828 if (!_this.isImageSlide(_this.core.index)) {
829 return;
830 }
831 $item = _this.core.getSlideItem(_this.core.index);
832 if ((_this.$LG(e.target).hasClass('lg-item') ||
833 $item.get().contains(e.target)) &&
834 e.touches.length === 1 &&
835 _this.core.outer.hasClass('lg-zoomed')) {
836 e.preventDefault();
837 startTime = new Date();
838 _this.core.touchAction = 'zoomSwipe';
839 _LGel = _this.core
840 .getSlideItem(_this.core.index)
841 .find('.lg-img-wrap')
842 .first();
843 var dragAllowedAxises = _this.getDragAllowedAxises(0);
844 allowY = dragAllowedAxises.allowY;
845 allowX = dragAllowedAxises.allowX;
846 if (allowX || allowY) {
847 startCoords = _this.getSwipeCords(e);
848 }
849 possibleSwipeCords = _this.getPossibleSwipeDragCords();
850 // reset opacity and transition duration
851 _this.core.outer.addClass('lg-zoom-dragging lg-zoom-drag-transition');
852 }
853 });
854 this.core.$inner.on('touchmove.lg', function (e) {
855 if (e.touches.length === 1 &&
856 _this.core.touchAction === 'zoomSwipe' &&
857 (_this.$LG(e.target).hasClass('lg-item') ||
858 $item.get().contains(e.target))) {
859 e.preventDefault();
860 _this.core.touchAction = 'zoomSwipe';
861 endCoords = _this.getSwipeCords(e);
862 var distance = _this.getZoomSwipeCords(startCoords, endCoords, allowX, allowY, possibleSwipeCords);
863 if (Math.abs(endCoords.x - startCoords.x) > 15 ||
864 Math.abs(endCoords.y - startCoords.y) > 15) {
865 isMoved = true;
866 _this.setZoomSwipeStyles(_LGel, distance);
867 }
868 }
869 });
870 this.core.$inner.on('touchend.lg', function (e) {
871 if (_this.core.touchAction === 'zoomSwipe' &&
872 (_this.$LG(e.target).hasClass('lg-item') ||
873 $item.get().contains(e.target))) {
874 e.preventDefault();
875 _this.core.touchAction = undefined;
876 _this.core.outer.removeClass('lg-zoom-dragging');
877 if (!isMoved) {
878 return;
879 }
880 isMoved = false;
881 endTime = new Date();
882 var touchDuration = endTime.valueOf() - startTime.valueOf();
883 _this.touchendZoom(startCoords, endCoords, allowX, allowY, touchDuration);
884 }
885 });
886 };
887 Zoom.prototype.zoomDrag = function () {
888 var _this = this;
889 var startCoords = {};
890 var endCoords = {};
891 var isDragging = false;
892 var isMoved = false;
893 // Allow x direction drag
894 var allowX = false;
895 // Allow Y direction drag
896 var allowY = false;
897 var startTime;
898 var endTime;
899 var possibleSwipeCords;
900 var _LGel;
901 this.core.outer.on('mousedown.lg.zoom', function (e) {
902 // Allow zoom only on image
903 if (!_this.isImageSlide(_this.core.index)) {
904 return;
905 }
906 var $item = _this.core.getSlideItem(_this.core.index);
907 if (_this.$LG(e.target).hasClass('lg-item') ||
908 $item.get().contains(e.target)) {
909 startTime = new Date();
910 _LGel = _this.core
911 .getSlideItem(_this.core.index)
912 .find('.lg-img-wrap')
913 .first();
914 var dragAllowedAxises = _this.getDragAllowedAxises(0);
915 allowY = dragAllowedAxises.allowY;
916 allowX = dragAllowedAxises.allowX;
917 if (_this.core.outer.hasClass('lg-zoomed')) {
918 if (_this.$LG(e.target).hasClass('lg-object') &&
919 (allowX || allowY)) {
920 e.preventDefault();
921 startCoords = _this.getDragCords(e);
922 possibleSwipeCords = _this.getPossibleSwipeDragCords();
923 isDragging = true;
924 _this.core.outer
925 .removeClass('lg-grab')
926 .addClass('lg-grabbing lg-zoom-drag-transition lg-zoom-dragging');
927 // reset opacity and transition duration
928 }
929 }
930 }
931 });
932 this.$LG(window).on("mousemove.lg.zoom.global" + this.core.lgId, function (e) {
933 if (isDragging) {
934 isMoved = true;
935 endCoords = _this.getDragCords(e);
936 var distance = _this.getZoomSwipeCords(startCoords, endCoords, allowX, allowY, possibleSwipeCords);
937 _this.setZoomSwipeStyles(_LGel, distance);
938 }
939 });
940 this.$LG(window).on("mouseup.lg.zoom.global" + this.core.lgId, function (e) {
941 if (isDragging) {
942 endTime = new Date();
943 isDragging = false;
944 _this.core.outer.removeClass('lg-zoom-dragging');
945 // Fix for chrome mouse move on click
946 if (isMoved &&
947 (startCoords.x !== endCoords.x ||
948 startCoords.y !== endCoords.y)) {
949 endCoords = _this.getDragCords(e);
950 var touchDuration = endTime.valueOf() - startTime.valueOf();
951 _this.touchendZoom(startCoords, endCoords, allowX, allowY, touchDuration);
952 }
953 isMoved = false;
954 }
955 _this.core.outer.removeClass('lg-grabbing').addClass('lg-grab');
956 });
957 };
958 Zoom.prototype.closeGallery = function () {
959 this.resetZoom();
960 this.zoomInProgress = false;
961 };
962 Zoom.prototype.destroy = function () {
963 // Unbind all events added by lightGallery zoom plugin
964 this.$LG(window).off(".lg.zoom.global" + this.core.lgId);
965 this.core.LGel.off('.lg.zoom');
966 this.core.LGel.off('.zoom');
967 clearTimeout(this.zoomableTimeout);
968 this.zoomableTimeout = false;
969 };
970 return Zoom;
971}());
972
973export default Zoom;
974//# sourceMappingURL=lg-zoom.es5.js.map