UNPKG

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