UNPKG

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