UNPKG

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