@hyper-proto/iv-viewer 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1432 @@
1
+ /**
2
+ * @hyper-proto/iv-viewer - 2.3.0
3
+ * Author : HyperProto
4
+ * Copyright (c) 2019, 2026 to HyperProto, released under the MIT license.
5
+ * git+https://github.com/hyperproto-gh/iv-viewer.git
6
+ */
7
+
8
+ function _arrayLikeToArray(r, a) {
9
+ (null == a || a > r.length) && (a = r.length);
10
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
11
+ return n;
12
+ }
13
+ function _arrayWithHoles(r) {
14
+ if (Array.isArray(r)) return r;
15
+ }
16
+ function _assertThisInitialized(e) {
17
+ if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
18
+ return e;
19
+ }
20
+ function _callSuper(t, o, e) {
21
+ return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
22
+ }
23
+ function _classCallCheck(a, n) {
24
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
25
+ }
26
+ function _defineProperties(e, r) {
27
+ for (var t = 0; t < r.length; t++) {
28
+ var o = r[t];
29
+ o.enumerable = o.enumerable || false, o.configurable = true, "value" in o && (o.writable = true), Object.defineProperty(e, _toPropertyKey(o.key), o);
30
+ }
31
+ }
32
+ function _createClass(e, r, t) {
33
+ return r && _defineProperties(e.prototype, r), Object.defineProperty(e, "prototype", {
34
+ writable: false
35
+ }), e;
36
+ }
37
+ function _defineProperty(e, r, t) {
38
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
39
+ value: t,
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true
43
+ }) : e[r] = t, e;
44
+ }
45
+ function _get() {
46
+ return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) {
47
+ var p = _superPropBase(e, t);
48
+ if (p) {
49
+ var n = Object.getOwnPropertyDescriptor(p, t);
50
+ return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value;
51
+ }
52
+ }, _get.apply(null, arguments);
53
+ }
54
+ function _getPrototypeOf(t) {
55
+ return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
56
+ return t.__proto__ || Object.getPrototypeOf(t);
57
+ }, _getPrototypeOf(t);
58
+ }
59
+ function _inherits(t, e) {
60
+ if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
61
+ t.prototype = Object.create(e && e.prototype, {
62
+ constructor: {
63
+ value: t,
64
+ writable: true,
65
+ configurable: true
66
+ }
67
+ }), Object.defineProperty(t, "prototype", {
68
+ writable: false
69
+ }), e && _setPrototypeOf(t, e);
70
+ }
71
+ function _isNativeReflectConstruct() {
72
+ try {
73
+ var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
74
+ } catch (t) {}
75
+ return (_isNativeReflectConstruct = function () {
76
+ return !!t;
77
+ })();
78
+ }
79
+ function _iterableToArrayLimit(r, l) {
80
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
81
+ if (null != t) {
82
+ var e,
83
+ n,
84
+ i,
85
+ u,
86
+ a = [],
87
+ f = true,
88
+ o = false;
89
+ try {
90
+ if (i = (t = t.call(r)).next, 0 === l) ; else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
91
+ } catch (r) {
92
+ o = true, n = r;
93
+ } finally {
94
+ try {
95
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
96
+ } finally {
97
+ if (o) throw n;
98
+ }
99
+ }
100
+ return a;
101
+ }
102
+ }
103
+ function _nonIterableRest() {
104
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
105
+ }
106
+ function ownKeys(e, r) {
107
+ var t = Object.keys(e);
108
+ if (Object.getOwnPropertySymbols) {
109
+ var o = Object.getOwnPropertySymbols(e);
110
+ r && (o = o.filter(function (r) {
111
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
112
+ })), t.push.apply(t, o);
113
+ }
114
+ return t;
115
+ }
116
+ function _objectSpread2(e) {
117
+ for (var r = 1; r < arguments.length; r++) {
118
+ var t = null != arguments[r] ? arguments[r] : {};
119
+ r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
120
+ _defineProperty(e, r, t[r]);
121
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
122
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
123
+ });
124
+ }
125
+ return e;
126
+ }
127
+ function _possibleConstructorReturn(t, e) {
128
+ if (e && ("object" == typeof e || "function" == typeof e)) return e;
129
+ if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
130
+ return _assertThisInitialized(t);
131
+ }
132
+ function _setPrototypeOf(t, e) {
133
+ return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
134
+ return t.__proto__ = e, t;
135
+ }, _setPrototypeOf(t, e);
136
+ }
137
+ function _slicedToArray(r, e) {
138
+ return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
139
+ }
140
+ function _superPropBase(t, o) {
141
+ for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t)););
142
+ return t;
143
+ }
144
+ function _superPropGet(t, o, e, r) {
145
+ var p = _get(_getPrototypeOf(t.prototype ), o, e);
146
+ return "function" == typeof p ? function (t) {
147
+ return p.apply(e, t);
148
+ } : p;
149
+ }
150
+ function _toPrimitive(t, r) {
151
+ if ("object" != typeof t || !t) return t;
152
+ var e = t[Symbol.toPrimitive];
153
+ if (void 0 !== e) {
154
+ var i = e.call(t, r);
155
+ if ("object" != typeof i) return i;
156
+ throw new TypeError("@@toPrimitive must return a primitive value.");
157
+ }
158
+ return (String )(t);
159
+ }
160
+ function _toPropertyKey(t) {
161
+ var i = _toPrimitive(t, "string");
162
+ return "symbol" == typeof i ? i : i + "";
163
+ }
164
+ function _unsupportedIterableToArray(r, a) {
165
+ if (r) {
166
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
167
+ var t = {}.toString.call(r).slice(8, -1);
168
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
169
+ }
170
+ }
171
+
172
+ // constants
173
+ var ZOOM_CONSTANT = 15; // increase or decrease value for zoom on mouse wheel
174
+ var MOUSE_WHEEL_COUNT = 5; // A mouse delta after which it should stop preventing default behaviour of mouse wheel
175
+
176
+ function noop() {}
177
+ function preventDefault(e) {
178
+ e.preventDefault();
179
+ }
180
+
181
+ // ease out method
182
+ /*
183
+ t : current time,
184
+ b : initial value,
185
+ c : changed value,
186
+ d : duration
187
+ */
188
+ function easeOutQuart(t, b, c, d) {
189
+ t /= d;
190
+ t -= 1;
191
+ return -c * (t * t * t * t - 1) + b;
192
+ }
193
+ function createElement(options) {
194
+ var elem = document.createElement(options.tagName);
195
+ if (options.id) elem.id = options.id;
196
+ if (options.html) elem.innerHTML = options.html;
197
+ if (options.className) elem.className = options.className;
198
+ if (options.src) elem.src = options.src;
199
+ if (options.style) elem.style.cssText = options.style;
200
+ if (options.child) elem.appendChild(options.child);
201
+
202
+ // Insert before
203
+ if (options.insertBefore) {
204
+ options.parent.insertBefore(elem, options.insertBefore);
205
+
206
+ // Standard append
207
+ } else {
208
+ options.parent.appendChild(elem);
209
+ }
210
+ return elem;
211
+ }
212
+
213
+ // method to add class
214
+ function addClass(el, className) {
215
+ var classNameAry = className.split(' ');
216
+ if (classNameAry.length > 1) {
217
+ classNameAry.forEach(function (classItem) {
218
+ return addClass(el, classItem);
219
+ });
220
+ } else if (el.classList) {
221
+ el.classList.add(className);
222
+ } else {
223
+ el.className += " ".concat(className);
224
+ }
225
+ }
226
+
227
+ // method to remove class
228
+ function removeClass(el, className) {
229
+ var classNameAry = className.split(' ');
230
+ if (classNameAry.length > 1) {
231
+ classNameAry.forEach(function (classItem) {
232
+ return removeClass(el, classItem);
233
+ });
234
+ } else if (el.classList) {
235
+ el.classList.remove(className);
236
+ } else {
237
+ el.className = el.className.replace(new RegExp("(^|\\b)".concat(className.split(' ').join('|'), "(\\b|$)"), 'gi'), ' ');
238
+ }
239
+ }
240
+
241
+ // function to check if image is loaded
242
+ function imageLoaded(img) {
243
+ return img.complete && (typeof img.naturalWidth === 'undefined' || img.naturalWidth !== 0);
244
+ }
245
+ function toArray(list) {
246
+ if (!(list instanceof NodeList || list instanceof HTMLCollection)) return [list];
247
+ return Array.prototype.slice.call(list);
248
+ }
249
+ function css(elements, properties) {
250
+ var elmArray = toArray(elements);
251
+ if (typeof properties === 'string') {
252
+ return window.getComputedStyle(elmArray[0])[properties];
253
+ }
254
+ elmArray.forEach(function (element) {
255
+ Object.keys(properties).forEach(function (key) {
256
+ var value = properties[key];
257
+ element.style[key] = value;
258
+ });
259
+ });
260
+ return undefined;
261
+ }
262
+ function removeCss(element, property) {
263
+ element.style.removeProperty(property);
264
+ }
265
+ function wrap(element, _ref) {
266
+ var _ref$tag = _ref.tag,
267
+ tag = _ref$tag === void 0 ? 'div' : _ref$tag,
268
+ className = _ref.className,
269
+ id = _ref.id,
270
+ style = _ref.style;
271
+ var wrapper = document.createElement(tag);
272
+ if (className) wrapper.className = className;
273
+ if (id) wrapper.id = id;
274
+ if (style) wrapper.style = style;
275
+ element.parentNode.insertBefore(wrapper, element);
276
+ element.parentNode.removeChild(element);
277
+ wrapper.appendChild(element);
278
+ return wrapper;
279
+ }
280
+ function unwrap(element) {
281
+ var parent = element.parentNode;
282
+ if (parent !== document.body) {
283
+ parent.parentNode.insertBefore(element, parent);
284
+ parent.parentNode.removeChild(parent);
285
+ }
286
+ }
287
+ function remove(elements) {
288
+ var elmArray = toArray(elements);
289
+ elmArray.forEach(function (element) {
290
+ element.parentNode.removeChild(element);
291
+ });
292
+ }
293
+ function clamp(num, min, max) {
294
+ return Math.min(Math.max(num, min), max);
295
+ }
296
+ function assignEvent(element, events, handler) {
297
+ if (typeof events === 'string') events = [events];
298
+ events.forEach(function (event) {
299
+ element.addEventListener(event, handler);
300
+ });
301
+ return function () {
302
+ events.forEach(function (event) {
303
+ element.removeEventListener(event, handler);
304
+ });
305
+ };
306
+ }
307
+ function getTouchPointsDistance(touches) {
308
+ var touch0 = touches[0];
309
+ var touch1 = touches[1];
310
+ return Math.sqrt(Math.pow(touch1.pageX - touch0.pageX, 2) + Math.pow(touch1.pageY - touch0.pageY, 2));
311
+ }
312
+
313
+ var Slider = /*#__PURE__*/function () {
314
+ function Slider(container, _ref) {
315
+ var _this = this;
316
+ var _onStart = _ref.onStart,
317
+ _onMove = _ref.onMove,
318
+ onEnd = _ref.onEnd,
319
+ isSliderEnabled = _ref.isSliderEnabled;
320
+ _classCallCheck(this, Slider);
321
+ _defineProperty(this, "startHandler", function (eStart) {
322
+ if (!_this.isSliderEnabled()) return;
323
+ _this.removeListeners();
324
+ eStart.preventDefault();
325
+ var moveHandler = _this.moveHandler,
326
+ endHandler = _this.endHandler,
327
+ onStart = _this.onStart;
328
+ var isTouchEvent = eStart.type === 'touchstart' || eStart.type === 'touchend';
329
+ _this.touchMoveEvent = isTouchEvent ? 'touchmove' : 'mousemove';
330
+ _this.touchEndEvent = isTouchEvent ? 'touchend' : 'mouseup';
331
+ _this.sx = isTouchEvent ? eStart.touches[0].clientX : eStart.clientX;
332
+ _this.sy = isTouchEvent ? eStart.touches[0].clientY : eStart.clientY;
333
+ onStart(eStart, {
334
+ x: _this.sx,
335
+ y: _this.sy
336
+ });
337
+
338
+ // add listeners
339
+ document.addEventListener(_this.touchMoveEvent, moveHandler);
340
+ document.addEventListener(_this.touchEndEvent, endHandler);
341
+ /*
342
+ add end handler in context menu as well.
343
+ As mouseup event is not trigger on context menu open
344
+ https://bugs.chromium.org/p/chromium/issues/detail?id=506801
345
+ */
346
+ document.addEventListener('contextmenu', endHandler);
347
+ });
348
+ _defineProperty(this, "moveHandler", function (eMove) {
349
+ if (!_this.isSliderEnabled()) return;
350
+ eMove.preventDefault();
351
+ var sx = _this.sx,
352
+ sy = _this.sy,
353
+ onMove = _this.onMove;
354
+ var isTouchEvent = _this.touchMoveEvent === 'touchmove';
355
+
356
+ // get the coordinates
357
+ var mx = isTouchEvent ? eMove.touches[0].clientX : eMove.clientX;
358
+ var my = isTouchEvent ? eMove.touches[0].clientY : eMove.clientY;
359
+ onMove(eMove, {
360
+ dx: mx - sx,
361
+ dy: my - sy,
362
+ mx: mx,
363
+ my: my
364
+ });
365
+ });
366
+ _defineProperty(this, "endHandler", function () {
367
+ if (!_this.isSliderEnabled()) return;
368
+ _this.removeListeners();
369
+ _this.onEnd();
370
+ });
371
+ this.container = container;
372
+ this.isSliderEnabled = isSliderEnabled;
373
+ this.onStart = _onStart || noop;
374
+ this.onMove = _onMove || noop;
375
+ this.onEnd = onEnd || noop;
376
+ }
377
+ return _createClass(Slider, [{
378
+ key: "removeListeners",
379
+ value:
380
+ // remove previous events if it's not removed
381
+ // - Case when while sliding mouse moved out of document and released there
382
+ function removeListeners() {
383
+ if (!this.touchMoveEvent) return;
384
+ document.removeEventListener(this.touchMoveEvent, this.moveHandler);
385
+ document.removeEventListener(this.touchEndEvent, this.endHandler);
386
+ document.removeEventListener('contextmenu', this.endHandler);
387
+ }
388
+ }, {
389
+ key: "init",
390
+ value: function init() {
391
+ var _this2 = this;
392
+ ['touchstart', 'mousedown'].forEach(function (evt) {
393
+ _this2.container.addEventListener(evt, _this2.startHandler);
394
+ });
395
+ }
396
+ }, {
397
+ key: "destroy",
398
+ value: function destroy() {
399
+ var _this3 = this;
400
+ ['touchstart', 'mousedown'].forEach(function (evt) {
401
+ _this3.container.removeEventListener(evt, _this3.startHandler);
402
+ });
403
+ this.removeListeners();
404
+ }
405
+ }]);
406
+ }();
407
+
408
+ var ImageViewer = /*#__PURE__*/function () {
409
+ function ImageViewer(element) {
410
+ var _this = this;
411
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
412
+ _classCallCheck(this, ImageViewer);
413
+ _defineProperty(this, "zoom", function (perc, point) {
414
+ var _options = _this._options,
415
+ _elements = _this._elements,
416
+ _state = _this._state;
417
+ var curPerc = _state.zoomValue,
418
+ imageDim = _state.imageDim,
419
+ containerDim = _state.containerDim,
420
+ zoomSliderLength = _state.zoomSliderLength;
421
+ var image = _elements.image,
422
+ zoomHandle = _elements.zoomHandle;
423
+ var maxZoom = _options.maxZoom;
424
+ perc = Math.round(Math.max(100, perc));
425
+ perc = Math.min(maxZoom, perc);
426
+ point = point || {
427
+ x: containerDim.w / 2,
428
+ y: containerDim.h / 2
429
+ };
430
+ var curLeft = parseFloat(css(image, 'left'));
431
+ var curTop = parseFloat(css(image, 'top'));
432
+
433
+ // clear any panning frames
434
+ _this._clearFrames();
435
+ var step = 0;
436
+ var baseLeft = (containerDim.w - imageDim.w) / 2;
437
+ var baseTop = (containerDim.h - imageDim.h) / 2;
438
+ var baseRight = containerDim.w - baseLeft;
439
+ var baseBottom = containerDim.h - baseTop;
440
+ var _zoom = function zoom() {
441
+ step++;
442
+ if (step < 16) {
443
+ _this._frames.zoomFrame = requestAnimationFrame(_zoom);
444
+ }
445
+ var tickZoom = easeOutQuart(step, curPerc, perc - curPerc, 16);
446
+ // snap in at the last percent to more often land at the exact value
447
+ // only do that at the target percent value to make the animation as smooth as possible
448
+ if (Math.abs(perc - tickZoom) < 1) {
449
+ tickZoom = perc;
450
+ }
451
+ var ratio = tickZoom / curPerc;
452
+ var imgWidth = imageDim.w * tickZoom / 100;
453
+ var imgHeight = imageDim.h * tickZoom / 100;
454
+ var newLeft = -((point.x - curLeft) * ratio - point.x);
455
+ var newTop = -((point.y - curTop) * ratio - point.y);
456
+
457
+ // fix for left and top
458
+ newLeft = Math.min(newLeft, baseLeft);
459
+ newTop = Math.min(newTop, baseTop);
460
+
461
+ // fix for right and bottom
462
+ if (newLeft + imgWidth < baseRight) {
463
+ newLeft = baseRight - imgWidth; // newLeft - (newLeft + imgWidth - baseRight)
464
+ }
465
+ if (newTop + imgHeight < baseBottom) {
466
+ newTop = baseBottom - imgHeight; // newTop + (newTop + imgHeight - baseBottom)
467
+ }
468
+ css(image, {
469
+ height: "".concat(imgHeight, "px"),
470
+ width: "".concat(imgWidth, "px"),
471
+ left: "".concat(newLeft, "px"),
472
+ top: "".concat(newTop, "px")
473
+ });
474
+ _this._state.zoomValue = tickZoom;
475
+ _this._resizeSnapHandle(imgWidth, imgHeight, newLeft, newTop);
476
+
477
+ // update zoom handle position
478
+ css(zoomHandle, {
479
+ left: "".concat((tickZoom - 100) * zoomSliderLength / (maxZoom - 100), "px")
480
+ });
481
+
482
+ // dispatch zoom changed event
483
+ if (_this._listeners.onZoomChange) {
484
+ _this._listeners.onZoomChange(_this._callbackData);
485
+ }
486
+ };
487
+ _zoom();
488
+ });
489
+ _defineProperty(this, "_clearFrames", function () {
490
+ var _this$_frames = _this._frames,
491
+ slideMomentumCheck = _this$_frames.slideMomentumCheck,
492
+ sliderMomentumFrame = _this$_frames.sliderMomentumFrame,
493
+ zoomFrame = _this$_frames.zoomFrame;
494
+ clearInterval(slideMomentumCheck);
495
+ cancelAnimationFrame(sliderMomentumFrame);
496
+ cancelAnimationFrame(zoomFrame);
497
+ });
498
+ _defineProperty(this, "_resizeSnapHandle", function (imgWidth, imgHeight, imgLeft, imgTop) {
499
+ var _elements = _this._elements,
500
+ _state = _this._state;
501
+ var snapHandle = _elements.snapHandle,
502
+ image = _elements.image;
503
+ var imageDim = _state.imageDim,
504
+ containerDim = _state.containerDim,
505
+ zoomValue = _state.zoomValue,
506
+ snapImageDim = _state.snapImageDim;
507
+ var imageWidth = imgWidth || imageDim.w * zoomValue / 100;
508
+ var imageHeight = imgHeight || imageDim.h * zoomValue / 100;
509
+ var imageLeft = imgLeft || parseFloat(css(image, 'left'));
510
+ var imageTop = imgTop || parseFloat(css(image, 'top'));
511
+ var left = -imageLeft * snapImageDim.w / imageWidth;
512
+ var top = -imageTop * snapImageDim.h / imageHeight;
513
+ var handleWidth = containerDim.w * snapImageDim.w / imageWidth;
514
+ var handleHeight = containerDim.h * snapImageDim.h / imageHeight;
515
+ css(snapHandle, {
516
+ top: "".concat(top, "px"),
517
+ left: "".concat(left, "px"),
518
+ width: "".concat(handleWidth, "px"),
519
+ height: "".concat(handleHeight, "px")
520
+ });
521
+ _this._state.snapHandleDim = {
522
+ w: handleWidth,
523
+ h: handleHeight
524
+ };
525
+ });
526
+ _defineProperty(this, "showSnapView", function (noTimeout) {
527
+ var _this$_state = _this._state,
528
+ snapViewVisible = _this$_state.snapViewVisible,
529
+ zoomValue = _this$_state.zoomValue,
530
+ loaded = _this$_state.loaded;
531
+ var snapView = _this._elements.snapView;
532
+ if (!_this._options.snapView) return;
533
+ if (snapViewVisible || zoomValue <= 100 || !loaded) return;
534
+ clearTimeout(_this._frames.snapViewTimeout);
535
+ _this._state.snapViewVisible = true;
536
+ css(snapView, {
537
+ opacity: 1,
538
+ pointerEvents: 'inherit'
539
+ });
540
+ if (!noTimeout) {
541
+ _this._frames.snapViewTimeout = setTimeout(_this.hideSnapView, 1500);
542
+ }
543
+ });
544
+ _defineProperty(this, "hideSnapView", function () {
545
+ var snapView = _this._elements.snapView;
546
+ css(snapView, {
547
+ opacity: 0,
548
+ pointerEvents: 'none'
549
+ });
550
+ _this._state.snapViewVisible = false;
551
+ });
552
+ _defineProperty(this, "refresh", function () {
553
+ var animate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
554
+ _this._calculateDimensions();
555
+ _this.resetZoom(animate);
556
+ });
557
+ var _this$_findContainerA = this._findContainerAndImageSrc(element, options),
558
+ container = _this$_findContainerA.container,
559
+ domElement = _this$_findContainerA.domElement,
560
+ imageSrc = _this$_findContainerA.imageSrc,
561
+ hiResImageSrc = _this$_findContainerA.hiResImageSrc;
562
+
563
+ // containers for elements
564
+ this._elements = {
565
+ container: container,
566
+ domElement: domElement
567
+ };
568
+ this._options = _objectSpread2(_objectSpread2({}, ImageViewer.defaults), options);
569
+
570
+ // container for all events
571
+ this._events = {};
572
+ this._listeners = this._options.listeners || {};
573
+
574
+ // container for all timeout and frames
575
+ this._frames = {};
576
+
577
+ // container for all sliders
578
+ this._sliders = {};
579
+
580
+ // maintain current state
581
+ this._state = {
582
+ zoomValue: this._options.zoomValue
583
+ };
584
+ this._images = {
585
+ imageSrc: imageSrc,
586
+ hiResImageSrc: hiResImageSrc
587
+ };
588
+ this._init();
589
+ if (imageSrc) {
590
+ this._loadImages();
591
+ }
592
+
593
+ // store reference of imageViewer in domElement
594
+ domElement._imageViewer = this;
595
+ }
596
+ return _createClass(ImageViewer, [{
597
+ key: "zoomInButton",
598
+ get: function get() {
599
+ return this._options.hasZoomButtons ? '<div class="iv-button-zoom--in" role="button"></div>' : '';
600
+ }
601
+ }, {
602
+ key: "zoomOutButton",
603
+ get: function get() {
604
+ return this._options.hasZoomButtons ? '<div class="iv-button-zoom--out" role="button"></div>' : '';
605
+ }
606
+ }, {
607
+ key: "imageViewHtml",
608
+ get: function get() {
609
+ return "\n <div class=\"iv-loader\"></div>\n <div class=\"iv-snap-view\">\n <div class=\"iv-snap-image-wrap\">\n <div class=\"iv-snap-handle\"></div>\n </div>\n <div class=\"iv-zoom-actions ".concat(this._options.hasZoomButtons ? 'iv-zoom-actions--has-buttons' : '', "\">\n ").concat(this.zoomInButton, "\n <div class=\"iv-zoom-slider\">\n <div class=\"iv-zoom-handle\"></div>\n </div>\n ").concat(this.zoomOutButton, "\n </div>\n </div>\n <div class=\"iv-image-view\" >\n <div class=\"iv-image-wrap\" ></div>\n </div>\n ");
610
+ }
611
+ }, {
612
+ key: "_findContainerAndImageSrc",
613
+ value: function _findContainerAndImageSrc(element) {
614
+ var domElement = element;
615
+ var imageSrc, hiResImageSrc;
616
+ if (typeof element === 'string') {
617
+ domElement = document.querySelector(element);
618
+ }
619
+
620
+ // throw error if imageViewer is already assigned
621
+ if (domElement._imageViewer) {
622
+ throw new Error('An image viewer is already being initiated on the element.');
623
+ }
624
+ var container = element;
625
+ if (domElement.tagName === 'IMG') {
626
+ imageSrc = domElement.src;
627
+ hiResImageSrc = domElement.getAttribute('high-res-src') || domElement.getAttribute('data-high-res-src');
628
+
629
+ // wrap the image with iv-container div
630
+ container = wrap(domElement, {
631
+ className: 'iv-container iv-image-mode',
632
+ style: {
633
+ display: 'inline-block',
634
+ overflow: 'hidden'
635
+ }
636
+ });
637
+
638
+ // hide the image and add iv-original-img class
639
+ css(domElement, {
640
+ opacity: 0,
641
+ position: 'relative',
642
+ zIndex: -1
643
+ });
644
+ } else {
645
+ imageSrc = domElement.getAttribute('src') || domElement.getAttribute('data-src');
646
+ hiResImageSrc = domElement.getAttribute('high-res-src') || domElement.getAttribute('data-high-res-src');
647
+ }
648
+ return {
649
+ container: container,
650
+ domElement: domElement,
651
+ imageSrc: imageSrc,
652
+ hiResImageSrc: hiResImageSrc
653
+ };
654
+ }
655
+ }, {
656
+ key: "_init",
657
+ value: function _init() {
658
+ // initialize the dom elements
659
+ this._initDom();
660
+
661
+ // initialize slider
662
+ this._initImageSlider();
663
+ this._initSnapSlider();
664
+ this._initZoomSlider();
665
+
666
+ // enable pinch and zoom feature for touch screens
667
+ this._pinchAndZoom();
668
+
669
+ // enable scroll zoom interaction
670
+ this._scrollZoom();
671
+
672
+ // enable double tap to zoom interaction
673
+ this._doubleTapToZoom();
674
+
675
+ // initialize events
676
+ this._initEvents();
677
+ }
678
+ }, {
679
+ key: "_initDom",
680
+ value: function _initDom() {
681
+ var container = this._elements.container;
682
+
683
+ // add image-viewer layout elements
684
+ createElement({
685
+ tagName: 'div',
686
+ className: 'iv-wrap',
687
+ html: this.imageViewHtml,
688
+ parent: container
689
+ });
690
+
691
+ // add container class on the container
692
+ addClass(container, 'iv-container');
693
+
694
+ // if the element is static position, position it relatively
695
+ if (css(container, 'position') === 'static') {
696
+ css(container, {
697
+ position: 'relative'
698
+ });
699
+ }
700
+
701
+ // save references for later use
702
+ this._elements = _objectSpread2(_objectSpread2({}, this._elements), {}, {
703
+ snapView: container.querySelector('.iv-snap-view'),
704
+ snapImageWrap: container.querySelector('.iv-snap-image-wrap'),
705
+ imageWrap: container.querySelector('.iv-image-wrap'),
706
+ snapHandle: container.querySelector('.iv-snap-handle'),
707
+ zoomHandle: container.querySelector('.iv-zoom-handle'),
708
+ zoomIn: container.querySelector('.iv-button-zoom--in'),
709
+ zoomOut: container.querySelector('.iv-button-zoom--out')
710
+ });
711
+ if (this._listeners.onInit) {
712
+ this._listeners.onInit(this._callbackData);
713
+ }
714
+ }
715
+ }, {
716
+ key: "_initImageSlider",
717
+ value: function _initImageSlider() {
718
+ var _this2 = this;
719
+ var _elements = this._elements;
720
+ var imageWrap = _elements.imageWrap;
721
+ var positions, currentPos;
722
+
723
+ /* Add slide interaction to image */
724
+ var imageSlider = new Slider(imageWrap, {
725
+ isSliderEnabled: function isSliderEnabled() {
726
+ var _this2$_state = _this2._state,
727
+ loaded = _this2$_state.loaded,
728
+ zooming = _this2$_state.zooming,
729
+ zoomValue = _this2$_state.zoomValue;
730
+ return loaded && !zooming && zoomValue > 100;
731
+ },
732
+ onStart: function onStart(e, position) {
733
+ var snapSlider = _this2._sliders.snapSlider;
734
+
735
+ // clear all animation frame and interval
736
+ _this2._clearFrames();
737
+ snapSlider.onStart();
738
+
739
+ // reset positions
740
+ positions = [position, position];
741
+ currentPos = undefined;
742
+ _this2._frames.slideMomentumCheck = setInterval(function () {
743
+ if (!currentPos) return;
744
+ positions.shift();
745
+ positions.push({
746
+ x: currentPos.mx,
747
+ y: currentPos.my
748
+ });
749
+ }, 50);
750
+ },
751
+ onMove: function onMove(e, position) {
752
+ var snapImageDim = _this2._state.snapImageDim;
753
+ var snapSlider = _this2._sliders.snapSlider;
754
+ var imageCurrentDim = _this2._getImageCurrentDim();
755
+ currentPos = position;
756
+ snapSlider.onMove(e, {
757
+ dx: -position.dx * snapImageDim.w / imageCurrentDim.w,
758
+ dy: -position.dy * snapImageDim.h / imageCurrentDim.h
759
+ });
760
+ },
761
+ onEnd: function onEnd() {
762
+ var snapImageDim = _this2._state.snapImageDim;
763
+ var snapSlider = _this2._sliders.snapSlider;
764
+ var imageCurrentDim = _this2._getImageCurrentDim();
765
+
766
+ // clear all animation frame and interval
767
+ _this2._clearFrames();
768
+ var step, positionX, positionY;
769
+ var xDiff = positions[1].x - positions[0].x;
770
+ var yDiff = positions[1].y - positions[0].y;
771
+ var _momentum = function momentum() {
772
+ if (step <= 60) {
773
+ _this2._frames.sliderMomentumFrame = requestAnimationFrame(_momentum);
774
+ }
775
+ positionX += easeOutQuart(step, xDiff / 3, -xDiff / 3, 60);
776
+ positionY += easeOutQuart(step, yDiff / 3, -yDiff / 3, 60);
777
+ snapSlider.onMove(null, {
778
+ dx: -(positionX * snapImageDim.w / imageCurrentDim.w),
779
+ dy: -(positionY * snapImageDim.h / imageCurrentDim.h)
780
+ });
781
+ step++;
782
+ };
783
+ if (Math.abs(xDiff) > 30 || Math.abs(yDiff) > 30) {
784
+ step = 1;
785
+ positionX = currentPos.dx;
786
+ positionY = currentPos.dy;
787
+ _momentum();
788
+ }
789
+ }
790
+ });
791
+ imageSlider.init();
792
+ this._sliders.imageSlider = imageSlider;
793
+ }
794
+ }, {
795
+ key: "_initSnapSlider",
796
+ value: function _initSnapSlider() {
797
+ var _this3 = this;
798
+ var snapHandle = this._elements.snapHandle;
799
+ var startHandleTop, startHandleLeft;
800
+ var snapSlider = new Slider(snapHandle, {
801
+ isSliderEnabled: function isSliderEnabled() {
802
+ return _this3._state.loaded;
803
+ },
804
+ onStart: function onStart() {
805
+ var _this3$_frames = _this3._frames,
806
+ slideMomentumCheck = _this3$_frames.slideMomentumCheck,
807
+ sliderMomentumFrame = _this3$_frames.sliderMomentumFrame;
808
+ startHandleTop = parseFloat(css(snapHandle, 'top'));
809
+ startHandleLeft = parseFloat(css(snapHandle, 'left'));
810
+
811
+ // stop momentum on image
812
+ clearInterval(slideMomentumCheck);
813
+ cancelAnimationFrame(sliderMomentumFrame);
814
+ },
815
+ onMove: function onMove(e, position) {
816
+ var _this3$_state = _this3._state,
817
+ snapHandleDim = _this3$_state.snapHandleDim,
818
+ snapImageDim = _this3$_state.snapImageDim;
819
+ var image = _this3._elements.image;
820
+ var imageCurrentDim = _this3._getImageCurrentDim();
821
+
822
+ // find handle left and top and make sure they lay between the snap image
823
+ var maxLeft = Math.max(snapImageDim.w - snapHandleDim.w, startHandleLeft);
824
+ var maxTop = Math.max(snapImageDim.h - snapHandleDim.h, startHandleTop);
825
+ var minLeft = Math.min(0, startHandleLeft);
826
+ var minTop = Math.min(0, startHandleTop);
827
+ var left = clamp(startHandleLeft + position.dx, minLeft, maxLeft);
828
+ var top = clamp(startHandleTop + position.dy, minTop, maxTop);
829
+ var imgLeft = -left * imageCurrentDim.w / snapImageDim.w;
830
+ var imgTop = -top * imageCurrentDim.h / snapImageDim.h;
831
+ css(snapHandle, {
832
+ left: "".concat(left, "px"),
833
+ top: "".concat(top, "px")
834
+ });
835
+ css(image, {
836
+ left: "".concat(imgLeft, "px"),
837
+ top: "".concat(imgTop, "px")
838
+ });
839
+ }
840
+ });
841
+ snapSlider.init();
842
+ this._sliders.snapSlider = snapSlider;
843
+ }
844
+ }, {
845
+ key: "_initZoomSlider",
846
+ value: function _initZoomSlider() {
847
+ var _this4 = this;
848
+ var _this$_elements = this._elements,
849
+ snapView = _this$_elements.snapView,
850
+ zoomHandle = _this$_elements.zoomHandle;
851
+
852
+ // zoom in zoom out using zoom handle
853
+ var sliderElm = snapView.querySelector('.iv-zoom-slider');
854
+ var leftOffset, handleWidth;
855
+
856
+ // on zoom slider we have to follow the mouse and set the handle to its position.
857
+ var zoomSlider = new Slider(sliderElm, {
858
+ isSliderEnabled: function isSliderEnabled() {
859
+ return _this4._state.loaded;
860
+ },
861
+ onStart: function onStart(eStart) {
862
+ var slider = _this4._sliders.zoomSlider;
863
+ leftOffset = sliderElm.getBoundingClientRect().left;
864
+ handleWidth = parseInt(css(zoomHandle, 'width'), 10);
865
+
866
+ // move the handle to current mouse position
867
+ slider.onMove(eStart);
868
+ },
869
+ onMove: function onMove(e) {
870
+ var maxZoom = _this4._options.maxZoom;
871
+ var zoomSliderLength = _this4._state.zoomSliderLength;
872
+ var clientX = e.clientX !== undefined ? e.clientX : e.touches[0].clientX;
873
+ var newLeft = clamp(clientX - leftOffset - handleWidth / 2, 0, zoomSliderLength);
874
+ var zoomValue = 100 + (maxZoom - 100) * newLeft / zoomSliderLength;
875
+ _this4.zoom(zoomValue);
876
+ }
877
+ });
878
+ zoomSlider.init();
879
+ this._sliders.zoomSlider = zoomSlider;
880
+ }
881
+ }, {
882
+ key: "_initEvents",
883
+ value: function _initEvents() {
884
+ this._snapViewEvents();
885
+
886
+ // handle window resize
887
+ if (this._options.refreshOnResize) {
888
+ this._events.onWindowResize = assignEvent(window, 'resize', this.refresh);
889
+ }
890
+ this._events.onDragStart = assignEvent(this._elements.container, 'dragstart', preventDefault);
891
+ }
892
+ }, {
893
+ key: "_snapViewEvents",
894
+ value: function _snapViewEvents() {
895
+ var _this5 = this;
896
+ var _this$_elements2 = this._elements,
897
+ imageWrap = _this$_elements2.imageWrap,
898
+ snapView = _this$_elements2.snapView;
899
+
900
+ // show snapView on mouse move
901
+ this._events.snapViewOnMouseMove = assignEvent(imageWrap, ['touchmove', 'mousemove'], function () {
902
+ _this5.showSnapView();
903
+ });
904
+
905
+ // keep showing snapView if on hover over it without any timeout
906
+ this._events.mouseEnterSnapView = assignEvent(snapView, ['mouseenter', 'touchstart'], function () {
907
+ _this5._state.snapViewVisible = false;
908
+ _this5.showSnapView(true);
909
+ });
910
+
911
+ // on mouse leave set timeout to hide snapView
912
+ this._events.mouseLeaveSnapView = assignEvent(snapView, ['mouseleave', 'touchend'], function () {
913
+ _this5._state.snapViewVisible = false;
914
+ _this5.showSnapView();
915
+ });
916
+ if (!this._options.hasZoomButtons) {
917
+ return;
918
+ }
919
+ var _this$_elements3 = this._elements,
920
+ zoomOut = _this$_elements3.zoomOut,
921
+ zoomIn = _this$_elements3.zoomIn;
922
+ this._events.zoomInClick = assignEvent(zoomIn, ['click'], function () {
923
+ _this5.zoom(_this5._state.zoomValue + _this5._options.zoomStep || 50);
924
+ });
925
+ this._events.zoomOutClick = assignEvent(zoomOut, ['click'], function () {
926
+ _this5.zoom(_this5._state.zoomValue - _this5._options.zoomStep || 50);
927
+ });
928
+ }
929
+ }, {
930
+ key: "_pinchAndZoom",
931
+ value: function _pinchAndZoom() {
932
+ var _this6 = this;
933
+ var _this$_elements4 = this._elements,
934
+ imageWrap = _this$_elements4.imageWrap,
935
+ container = _this$_elements4.container;
936
+
937
+ // apply pinch and zoom feature
938
+ var onPinchStart = function onPinchStart(eStart) {
939
+ var _this6$_state = _this6._state,
940
+ loaded = _this6$_state.loaded,
941
+ startZoomValue = _this6$_state.zoomValue;
942
+ var events = _this6._events;
943
+ if (!loaded) return;
944
+ var touch0 = eStart.touches[0];
945
+ var touch1 = eStart.touches[1];
946
+ if (!(touch0 && touch1)) {
947
+ return;
948
+ }
949
+ _this6._state.zooming = true;
950
+ var contOffset = container.getBoundingClientRect();
951
+
952
+ // find distance between two touch points
953
+ var startDist = getTouchPointsDistance(eStart.touches);
954
+
955
+ // find the center for the zoom
956
+ var center = {
957
+ x: (touch1.clientX + touch0.clientX) / 2 - contOffset.left,
958
+ y: (touch1.clientY + touch0.clientY) / 2 - contOffset.top
959
+ };
960
+ var moveListener = function moveListener(eMove) {
961
+ // eMove.preventDefault();
962
+
963
+ var newDist = getTouchPointsDistance(eMove.touches);
964
+ var zoomValue = startZoomValue + (newDist - startDist) / 2;
965
+ _this6.zoom(zoomValue, center);
966
+ };
967
+ var endListener = function endListener(eEnd) {
968
+ // unbind events
969
+ events.pinchMove();
970
+ events.pinchEnd();
971
+ _this6._state.zooming = false;
972
+ // properly resume move event if one finger remains
973
+ if (eEnd.touches.length === 1) {
974
+ _this6._sliders.imageSlider.startHandler(eEnd);
975
+ }
976
+ };
977
+
978
+ // remove events if already assigned
979
+ if (events.pinchMove) events.pinchMove();
980
+ if (events.pinchEnd) events.pinchEnd();
981
+
982
+ // assign events
983
+ events.pinchMove = assignEvent(document, 'touchmove', moveListener);
984
+ events.pinchEnd = assignEvent(document, 'touchend', endListener);
985
+ };
986
+ this._events.pinchStart = assignEvent(imageWrap, 'touchstart', onPinchStart);
987
+ }
988
+ }, {
989
+ key: "_scrollZoom",
990
+ value: function _scrollZoom() {
991
+ var _this7 = this;
992
+ /* Add zoom interaction in mouse wheel */
993
+ var _options = this._options;
994
+ var _this$_elements5 = this._elements,
995
+ container = _this$_elements5.container,
996
+ imageWrap = _this$_elements5.imageWrap;
997
+ var changedDelta = 0;
998
+ var onMouseWheel = function onMouseWheel(e) {
999
+ var _this7$_state = _this7._state,
1000
+ loaded = _this7$_state.loaded,
1001
+ zoomValue = _this7$_state.zoomValue;
1002
+ if (!_options.zoomOnMouseWheel || !loaded) return;
1003
+
1004
+ // clear all animation frame and interval
1005
+ _this7._clearFrames();
1006
+
1007
+ // cross-browser wheel delta
1008
+ var delta = Math.max(-1, Math.min(1, e.wheelDelta || -e.detail || -e.deltaY));
1009
+ var newZoomValue = zoomValue * (100 + delta * ZOOM_CONSTANT) / 100;
1010
+ if (!(newZoomValue >= 100 && newZoomValue <= _options.maxZoom)) {
1011
+ changedDelta += Math.abs(delta);
1012
+ } else {
1013
+ changedDelta = 0;
1014
+ }
1015
+ e.preventDefault();
1016
+ if (changedDelta > MOUSE_WHEEL_COUNT) return;
1017
+ var contOffset = container.getBoundingClientRect();
1018
+ var x = e.clientX - contOffset.left;
1019
+ var y = e.clientY - contOffset.top;
1020
+ _this7.zoom(newZoomValue, {
1021
+ x: x,
1022
+ y: y
1023
+ });
1024
+
1025
+ // show the snap viewer
1026
+ _this7.showSnapView();
1027
+ };
1028
+ this._events.scrollZoom = assignEvent(imageWrap, 'wheel', onMouseWheel);
1029
+ }
1030
+ }, {
1031
+ key: "_doubleTapToZoom",
1032
+ value: function _doubleTapToZoom() {
1033
+ var _this8 = this;
1034
+ var imageWrap = this._elements.imageWrap;
1035
+ // handle double tap for zoom in and zoom out
1036
+
1037
+ var touchTime = 0;
1038
+ var point;
1039
+ var onDoubleTap = function onDoubleTap(e) {
1040
+ if (touchTime === 0) {
1041
+ touchTime = Date.now();
1042
+ point = {
1043
+ x: e.clientX,
1044
+ y: e.clientY
1045
+ };
1046
+ } else if (Date.now() - touchTime < 500 && Math.abs(e.clientX - point.x) < 50 && Math.abs(e.clientY - point.y) < 50) {
1047
+ if (_this8._state.zoomValue === _this8._options.zoomValue) {
1048
+ _this8.zoom(200);
1049
+ } else {
1050
+ _this8.resetZoom();
1051
+ }
1052
+ touchTime = 0;
1053
+ } else {
1054
+ touchTime = 0;
1055
+ }
1056
+ };
1057
+ this._events.doubleTapToZoom = assignEvent(imageWrap, 'click', onDoubleTap);
1058
+ }
1059
+ }, {
1060
+ key: "_getImageCurrentDim",
1061
+ value: function _getImageCurrentDim() {
1062
+ var _this$_state2 = this._state,
1063
+ zoomValue = _this$_state2.zoomValue,
1064
+ imageDim = _this$_state2.imageDim;
1065
+ return {
1066
+ w: imageDim.w * (zoomValue / 100),
1067
+ h: imageDim.h * (zoomValue / 100)
1068
+ };
1069
+ }
1070
+ }, {
1071
+ key: "_loadImages",
1072
+ value: function _loadImages() {
1073
+ var _this9 = this;
1074
+ var _images = this._images,
1075
+ _elements = this._elements;
1076
+ var imageSrc = _images.imageSrc,
1077
+ hiResImageSrc = _images.hiResImageSrc;
1078
+ var container = _elements.container,
1079
+ snapImageWrap = _elements.snapImageWrap,
1080
+ imageWrap = _elements.imageWrap;
1081
+ var ivLoader = container.querySelector('.iv-loader');
1082
+
1083
+ // remove old images
1084
+ remove(container.querySelectorAll('.iv-snap-image, .iv-image'));
1085
+
1086
+ // add snapView image
1087
+ var snapImage = createElement({
1088
+ tagName: 'img',
1089
+ className: 'iv-snap-image',
1090
+ src: imageSrc,
1091
+ insertBefore: snapImageWrap.firstChild,
1092
+ parent: snapImageWrap
1093
+ });
1094
+
1095
+ // add image
1096
+ var image = createElement({
1097
+ tagName: 'img',
1098
+ className: 'iv-image iv-small-image',
1099
+ src: imageSrc,
1100
+ parent: imageWrap
1101
+ });
1102
+ this._state.loaded = false;
1103
+
1104
+ // store image reference in _elements
1105
+ this._elements.image = image;
1106
+ this._elements.snapImage = snapImage;
1107
+ css(ivLoader, {
1108
+ display: 'block'
1109
+ });
1110
+
1111
+ // keep visibility hidden until image is loaded
1112
+ css(image, {
1113
+ visibility: 'hidden'
1114
+ });
1115
+
1116
+ // hide snap view if open
1117
+ this.hideSnapView();
1118
+ var onImageLoad = function onImageLoad() {
1119
+ // hide the iv loader
1120
+ css(ivLoader, {
1121
+ display: 'none'
1122
+ });
1123
+
1124
+ // show the image
1125
+ css(image, {
1126
+ visibility: 'visible'
1127
+ });
1128
+
1129
+ // load high resolution image if provided
1130
+ if (hiResImageSrc) {
1131
+ _this9._loadHighResImage(hiResImageSrc);
1132
+ }
1133
+
1134
+ // set loaded flag to true
1135
+ _this9._state.loaded = true;
1136
+
1137
+ // calculate the dimension
1138
+ _this9._calculateDimensions();
1139
+
1140
+ // dispatch image load event
1141
+ if (_this9._listeners.onImageLoaded) {
1142
+ _this9._listeners.onImageLoaded(_this9._callbackData);
1143
+ }
1144
+
1145
+ // reset the zoom
1146
+ _this9.resetZoom();
1147
+ };
1148
+ if (imageLoaded(image)) {
1149
+ onImageLoad();
1150
+ } else {
1151
+ if (typeof this._events.imageLoad === 'function') {
1152
+ this._events.imageLoad();
1153
+ }
1154
+ this._events.imageLoad = assignEvent(image, 'load', onImageLoad);
1155
+ }
1156
+ }
1157
+ }, {
1158
+ key: "_loadHighResImage",
1159
+ value: function _loadHighResImage(hiResImageSrc) {
1160
+ var _this0 = this;
1161
+ var _this$_elements6 = this._elements,
1162
+ imageWrap = _this$_elements6.imageWrap,
1163
+ container = _this$_elements6.container;
1164
+ var lowResImg = this._elements.image;
1165
+ var hiResImage = createElement({
1166
+ tagName: 'img',
1167
+ className: 'iv-image iv-large-image',
1168
+ src: hiResImageSrc,
1169
+ parent: imageWrap,
1170
+ style: lowResImg.style.cssText
1171
+ });
1172
+
1173
+ // add all the style attributes from lowResImg to highResImg
1174
+ hiResImage.style.cssText = lowResImg.style.cssText;
1175
+ this._elements.image = container.querySelectorAll('.iv-image');
1176
+ var onHighResImageLoad = function onHighResImageLoad() {
1177
+ // remove the low size image and set this image as default image
1178
+ remove(lowResImg);
1179
+ _this0._elements.image = hiResImage;
1180
+ // this._calculateDimensions();
1181
+ };
1182
+ if (imageLoaded(hiResImage)) {
1183
+ onHighResImageLoad();
1184
+ } else {
1185
+ if (typeof this._events.hiResImageLoad === 'function') {
1186
+ this._events.hiResImageLoad();
1187
+ }
1188
+ this._events.hiResImageLoad = assignEvent(hiResImage, 'load', onHighResImageLoad);
1189
+ }
1190
+ }
1191
+ }, {
1192
+ key: "_calculateDimensions",
1193
+ value: function _calculateDimensions() {
1194
+ var _this$_elements7 = this._elements,
1195
+ image = _this$_elements7.image,
1196
+ container = _this$_elements7.container,
1197
+ snapView = _this$_elements7.snapView,
1198
+ snapImage = _this$_elements7.snapImage,
1199
+ zoomHandle = _this$_elements7.zoomHandle;
1200
+
1201
+ // calculate content width of image and snap image
1202
+ var imageWidth = parseInt(css(image, 'width'), 10);
1203
+ var imageHeight = parseInt(css(image, 'height'), 10);
1204
+ var contWidth = parseInt(css(container, 'width'), 10);
1205
+ var contHeight = parseInt(css(container, 'height'), 10);
1206
+ var snapViewWidth = snapView.clientWidth;
1207
+ var snapViewHeight = snapView.clientHeight;
1208
+
1209
+ // set the container dimension
1210
+ this._state.containerDim = {
1211
+ w: contWidth,
1212
+ h: contHeight
1213
+ };
1214
+
1215
+ // set the image dimension
1216
+ var ratio = imageWidth / imageHeight;
1217
+ var imgWidth = imageWidth > imageHeight && contHeight >= contWidth || ratio * contHeight > contWidth ? contWidth : ratio * contHeight;
1218
+ var imgHeight = imgWidth / ratio;
1219
+ this._state.imageDim = {
1220
+ w: imgWidth,
1221
+ h: imgHeight
1222
+ };
1223
+
1224
+ // reset image position and zoom
1225
+ css(image, {
1226
+ width: "".concat(imgWidth, "px"),
1227
+ height: "".concat(imgHeight, "px"),
1228
+ left: "".concat((contWidth - imgWidth) / 2, "px"),
1229
+ top: "".concat((contHeight - imgHeight) / 2, "px"),
1230
+ maxWidth: 'none',
1231
+ maxHeight: 'none'
1232
+ });
1233
+
1234
+ // set the snap Image dimension
1235
+ var snapWidth = imgWidth > imgHeight ? snapViewWidth : imgWidth * snapViewHeight / imgHeight;
1236
+ var snapHeight = imgHeight > imgWidth ? snapViewHeight : imgHeight * snapViewWidth / imgWidth;
1237
+ this._state.snapImageDim = {
1238
+ w: snapWidth,
1239
+ h: snapHeight
1240
+ };
1241
+ css(snapImage, {
1242
+ width: "".concat(snapWidth, "px"),
1243
+ height: "".concat(snapHeight, "px")
1244
+ });
1245
+ var zoomSlider = snapView.querySelector('.iv-zoom-slider').clientWidth;
1246
+ // calculate zoom slider area
1247
+ this._state.zoomSliderLength = zoomSlider - zoomHandle.offsetWidth;
1248
+ }
1249
+ }, {
1250
+ key: "resetZoom",
1251
+ value: function resetZoom() {
1252
+ var animate = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
1253
+ var zoomValue = this._options.zoomValue;
1254
+ if (!animate) {
1255
+ this._state.zoomValue = zoomValue;
1256
+ }
1257
+ this.zoom(zoomValue);
1258
+ }
1259
+ }, {
1260
+ key: "load",
1261
+ value: function load(imageSrc, hiResImageSrc) {
1262
+ this._images = {
1263
+ imageSrc: imageSrc,
1264
+ hiResImageSrc: hiResImageSrc
1265
+ };
1266
+ this._loadImages();
1267
+ }
1268
+ }, {
1269
+ key: "destroy",
1270
+ value: function destroy() {
1271
+ var _this$_elements8 = this._elements,
1272
+ container = _this$_elements8.container,
1273
+ domElement = _this$_elements8.domElement;
1274
+ // destroy all the sliders
1275
+ Object.entries(this._sliders).forEach(function (_ref) {
1276
+ var _ref2 = _slicedToArray(_ref, 2),
1277
+ slider = _ref2[1];
1278
+ slider.destroy();
1279
+ });
1280
+
1281
+ // unbind all events
1282
+ Object.entries(this._events).forEach(function (_ref3) {
1283
+ var _ref4 = _slicedToArray(_ref3, 2),
1284
+ unbindEvent = _ref4[1];
1285
+ unbindEvent();
1286
+ });
1287
+
1288
+ // clear all the frames
1289
+ this._clearFrames();
1290
+
1291
+ // remove html from the container
1292
+ remove(container.querySelector('.iv-wrap'));
1293
+
1294
+ // remove iv-container class from container
1295
+ removeClass(container, 'iv-container');
1296
+
1297
+ // remove added style from container
1298
+ removeCss(document.querySelector('html'), 'relative');
1299
+
1300
+ // if container has original image, unwrap the image and remove the class
1301
+ // which will happen when domElement is not the container
1302
+ if (domElement !== container) {
1303
+ unwrap(domElement);
1304
+ }
1305
+
1306
+ // remove imageViewer reference from dom element
1307
+ domElement._imageViewer = null;
1308
+ if (this._listeners.onDestroy) {
1309
+ this._listeners.onDestroy();
1310
+ }
1311
+ }
1312
+
1313
+ /**
1314
+ * Data will be passed to the callback registered with each new instance
1315
+ */
1316
+ }, {
1317
+ key: "_callbackData",
1318
+ get: function get() {
1319
+ return {
1320
+ container: this._elements.container,
1321
+ snapView: this._elements.snapView,
1322
+ zoomValue: this._state.zoomValue,
1323
+ reachedMin: Math.abs(this._state.zoomValue - 100) < 1,
1324
+ reachedMax: Math.abs(this._state.zoomValue - this._options.maxZoom) < 1,
1325
+ instance: this
1326
+ };
1327
+ }
1328
+ }]);
1329
+ }();
1330
+ ImageViewer.defaults = {
1331
+ zoomValue: 100,
1332
+ snapView: true,
1333
+ maxZoom: 500,
1334
+ refreshOnResize: true,
1335
+ zoomOnMouseWheel: true,
1336
+ hasZoomButtons: false,
1337
+ zoomStep: 50,
1338
+ listeners: {
1339
+ onInit: null,
1340
+ onDestroy: null,
1341
+ onImageLoaded: null,
1342
+ onZoomChange: null
1343
+ }
1344
+ };
1345
+
1346
+ var fullScreenHtml = "\n <div class=\"iv-fullscreen-container\"></div>\n <div class=\"iv-fullscreen-close\"></div>\n";
1347
+ var FullScreenViewer = /*#__PURE__*/function (_ImageViewer) {
1348
+ function FullScreenViewer() {
1349
+ var _this;
1350
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
1351
+ _classCallCheck(this, FullScreenViewer);
1352
+ var fullScreenElem = createElement({
1353
+ tagName: 'div',
1354
+ className: 'iv-fullscreen',
1355
+ html: fullScreenHtml,
1356
+ parent: document.body
1357
+ });
1358
+ var container = fullScreenElem.querySelector('.iv-fullscreen-container');
1359
+
1360
+ // call the ImageViewer constructor
1361
+ _this = _callSuper(this, FullScreenViewer, [container, _objectSpread2(_objectSpread2({}, options), {}, {
1362
+ refreshOnResize: false
1363
+ })]);
1364
+
1365
+ // add fullScreenElem on element list
1366
+ _defineProperty(_this, "hide", function () {
1367
+ // hide the fullscreen
1368
+ css(_this._elements.fullScreen, {
1369
+ display: 'none'
1370
+ });
1371
+
1372
+ // enable scroll
1373
+ removeCss(document.querySelector('html'), 'overflow');
1374
+
1375
+ // remove window event
1376
+ if (_this._events.onWindowResize) {
1377
+ _this._events.onWindowResize();
1378
+ }
1379
+ });
1380
+ _this._elements.fullScreen = fullScreenElem;
1381
+ _this._initFullScreenEvents();
1382
+ return _this;
1383
+ }
1384
+ _inherits(FullScreenViewer, _ImageViewer);
1385
+ return _createClass(FullScreenViewer, [{
1386
+ key: "_initFullScreenEvents",
1387
+ value: function _initFullScreenEvents() {
1388
+ var fullScreen = this._elements.fullScreen;
1389
+ var closeBtn = fullScreen.querySelector('.iv-fullscreen-close');
1390
+
1391
+ // add close button event
1392
+ this._events.onCloseBtnClick = assignEvent(closeBtn, 'click', this.hide);
1393
+ }
1394
+ }, {
1395
+ key: "show",
1396
+ value: function show(imageSrc, hiResImageSrc) {
1397
+ // show the element
1398
+ css(this._elements.fullScreen, {
1399
+ display: 'block'
1400
+ });
1401
+
1402
+ // if image source is provide load image source
1403
+ if (imageSrc) {
1404
+ this.load(imageSrc, hiResImageSrc);
1405
+ }
1406
+
1407
+ // handle window resize
1408
+ this._events.onWindowResize = assignEvent(window, 'resize', this.refresh);
1409
+
1410
+ // disable scroll on html
1411
+ css(document.querySelector('html'), {
1412
+ overflow: 'hidden'
1413
+ });
1414
+ }
1415
+ }, {
1416
+ key: "destroy",
1417
+ value: function destroy() {
1418
+ var fullScreen = this._elements.fullScreen;
1419
+
1420
+ // restore scroll before removing elements
1421
+ this.hide();
1422
+
1423
+ // destroy image viewer
1424
+ _superPropGet(FullScreenViewer, "destroy", this)([]);
1425
+
1426
+ // remove the element
1427
+ remove(fullScreen);
1428
+ }
1429
+ }]);
1430
+ }(ImageViewer);
1431
+
1432
+ export { FullScreenViewer, ImageViewer, ImageViewer as default };