viewerjs 1.9.2 → 1.10.3

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.
@@ -1,11 +1,11 @@
1
1
  /*!
2
- * Viewer.js v1.9.2
2
+ * Viewer.js v1.10.3
3
3
  * https://fengyuanchen.github.io/viewerjs
4
4
  *
5
5
  * Copyright 2015-present Chen Fengyuan
6
6
  * Released under the MIT license
7
7
  *
8
- * Date: 2021-05-29T03:50:07.891Z
8
+ * Date: 2022-02-02T05:15:01.702Z
9
9
  */
10
10
 
11
11
  function ownKeys(object, enumerableOnly) {
@@ -13,14 +13,9 @@ function ownKeys(object, enumerableOnly) {
13
13
 
14
14
  if (Object.getOwnPropertySymbols) {
15
15
  var symbols = Object.getOwnPropertySymbols(object);
16
-
17
- if (enumerableOnly) {
18
- symbols = symbols.filter(function (sym) {
19
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
20
- });
21
- }
22
-
23
- keys.push.apply(keys, symbols);
16
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
17
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
18
+ })), keys.push.apply(keys, symbols);
24
19
  }
25
20
 
26
21
  return keys;
@@ -28,19 +23,12 @@ function ownKeys(object, enumerableOnly) {
28
23
 
29
24
  function _objectSpread2(target) {
30
25
  for (var i = 1; i < arguments.length; i++) {
31
- var source = arguments[i] != null ? arguments[i] : {};
32
-
33
- if (i % 2) {
34
- ownKeys(Object(source), true).forEach(function (key) {
35
- _defineProperty(target, key, source[key]);
36
- });
37
- } else if (Object.getOwnPropertyDescriptors) {
38
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
39
- } else {
40
- ownKeys(Object(source)).forEach(function (key) {
41
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
42
- });
43
- }
26
+ var source = null != arguments[i] ? arguments[i] : {};
27
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
28
+ _defineProperty(target, key, source[key]);
29
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
30
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
31
+ });
44
32
  }
45
33
 
46
34
  return target;
@@ -49,17 +37,11 @@ function _objectSpread2(target) {
49
37
  function _typeof(obj) {
50
38
  "@babel/helpers - typeof";
51
39
 
52
- if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
53
- _typeof = function (obj) {
54
- return typeof obj;
55
- };
56
- } else {
57
- _typeof = function (obj) {
58
- return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
59
- };
60
- }
61
-
62
- return _typeof(obj);
40
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
41
+ return typeof obj;
42
+ } : function (obj) {
43
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
44
+ }, _typeof(obj);
63
45
  }
64
46
 
65
47
  function _classCallCheck(instance, Constructor) {
@@ -81,6 +63,9 @@ function _defineProperties(target, props) {
81
63
  function _createClass(Constructor, protoProps, staticProps) {
82
64
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
83
65
  if (staticProps) _defineProperties(Constructor, staticProps);
66
+ Object.defineProperty(Constructor, "prototype", {
67
+ writable: false
68
+ });
84
69
  return Constructor;
85
70
  }
86
71
 
@@ -151,7 +136,8 @@ var DEFAULTS = {
151
136
 
152
137
  /**
153
138
  * Enable to request fullscreen when play.
154
- * @type {boolean}
139
+ * {@link https://developer.mozilla.org/en-US/docs/Web/API/FullscreenOptions}
140
+ * @type {boolean|FullscreenOptions}
155
141
  */
156
142
  fullscreen: true,
157
143
 
@@ -371,6 +357,7 @@ var EVENT_DRAG_START = 'dragstart';
371
357
  var EVENT_FOCUSIN = 'focusin';
372
358
  var EVENT_KEY_DOWN = 'keydown';
373
359
  var EVENT_LOAD = 'load';
360
+ var EVENT_ERROR = 'error';
374
361
  var EVENT_TOUCH_END = IS_TOUCH_DEVICE ? 'touchend touchcancel' : 'mouseup';
375
362
  var EVENT_TOUCH_MOVE = IS_TOUCH_DEVICE ? 'touchmove' : 'mousemove';
376
363
  var EVENT_TOUCH_START = IS_TOUCH_DEVICE ? 'touchstart' : 'mousedown';
@@ -487,15 +474,15 @@ function forEach(data, callback) {
487
474
  if (Array.isArray(data) || isNumber(data.length)
488
475
  /* array-like */
489
476
  ) {
490
- var length = data.length;
491
- var i;
477
+ var length = data.length;
478
+ var i;
492
479
 
493
- for (i = 0; i < length; i += 1) {
494
- if (callback.call(data, data[i], i, data) === false) {
495
- break;
496
- }
480
+ for (i = 0; i < length; i += 1) {
481
+ if (callback.call(data, data[i], i, data) === false) {
482
+ break;
497
483
  }
498
- } else if (isObject(data)) {
484
+ }
485
+ } else if (isObject(data)) {
499
486
  Object.keys(data).forEach(function (key) {
500
487
  callback.call(data, data[key], key, data);
501
488
  });
@@ -1120,13 +1107,17 @@ var render = {
1120
1107
  this.items = items;
1121
1108
  forEach(items, function (item) {
1122
1109
  var image = item.firstElementChild;
1110
+ var onLoad;
1111
+ var onError;
1123
1112
  setData(image, 'filled', true);
1124
1113
 
1125
1114
  if (options.loading) {
1126
1115
  addClass(item, CLASS_LOADING);
1127
1116
  }
1128
1117
 
1129
- addListener(image, EVENT_LOAD, function (event) {
1118
+ addListener(image, EVENT_LOAD, onLoad = function onLoad(event) {
1119
+ removeListener(image, EVENT_ERROR, onError);
1120
+
1130
1121
  if (options.loading) {
1131
1122
  removeClass(item, CLASS_LOADING);
1132
1123
  }
@@ -1135,6 +1126,15 @@ var render = {
1135
1126
  }, {
1136
1127
  once: true
1137
1128
  });
1129
+ addListener(image, EVENT_ERROR, onError = function onError() {
1130
+ removeListener(image, EVENT_LOAD, onLoad);
1131
+
1132
+ if (options.loading) {
1133
+ removeClass(item, CLASS_LOADING);
1134
+ }
1135
+ }, {
1136
+ once: true
1137
+ });
1138
1138
  });
1139
1139
 
1140
1140
  if (options.transition) {
@@ -1145,16 +1145,23 @@ var render = {
1145
1145
  });
1146
1146
  }
1147
1147
  },
1148
- renderList: function renderList(index) {
1149
- var i = index || this.index;
1150
- var width = this.items[i].offsetWidth || 30;
1151
- var outerWidth = width + 1; // 1 pixel of `margin-left` width
1152
- // Place the active item in the center of the screen
1148
+ renderList: function renderList() {
1149
+ var index = this.index;
1150
+ var item = this.items[index];
1151
+
1152
+ if (!item) {
1153
+ return;
1154
+ }
1155
+
1156
+ var next = item.nextElementSibling;
1157
+ var gutter = parseInt(window.getComputedStyle(next || item).marginLeft, 10);
1158
+ var offsetWidth = item.offsetWidth;
1159
+ var outerWidth = offsetWidth + gutter; // Place the active item in the center of the screen
1153
1160
 
1154
1161
  setStyle(this.list, assign({
1155
- width: outerWidth * this.length
1162
+ width: outerWidth * this.length - gutter
1156
1163
  }, getTransforms({
1157
- translateX: (this.viewerData.width - width) / 2 - outerWidth * i
1164
+ translateX: (this.viewerData.width - offsetWidth) / 2 - outerWidth * index
1158
1165
  })));
1159
1166
  },
1160
1167
  resetList: function resetList() {
@@ -1204,6 +1211,7 @@ var render = {
1204
1211
  y: top,
1205
1212
  width: width,
1206
1213
  height: height,
1214
+ oldRatio: 1,
1207
1215
  ratio: width / naturalWidth,
1208
1216
  aspectRatio: aspectRatio,
1209
1217
  naturalWidth: naturalWidth,
@@ -1429,9 +1437,10 @@ var handlers = {
1429
1437
  // Cancel the emulated double click when the native dblclick event was triggered.
1430
1438
  if (IS_TOUCH_DEVICE && event.isTrusted) {
1431
1439
  clearTimeout(this.doubleClickImageTimeout);
1432
- }
1440
+ } // XXX: No pageX/Y properties in custom event, fallback to the original event.
1433
1441
 
1434
- this.toggle();
1442
+
1443
+ this.toggle(event.isTrusted ? event : event.detail && event.detail.originalEvent);
1435
1444
  }
1436
1445
  },
1437
1446
  load: function load() {
@@ -1453,7 +1462,7 @@ var handlers = {
1453
1462
  removeClass(this.canvas, CLASS_LOADING);
1454
1463
  }
1455
1464
 
1456
- image.style.cssText = 'height:0;' + "margin-left:".concat(viewerData.width / 2, "px;") + "margin-top:".concat(viewerData.height / 2, "px;") + 'max-width:none!important;' + 'position:absolute;' + 'width:0;';
1465
+ image.style.cssText = 'height:0;' + "margin-left:".concat(viewerData.width / 2, "px;") + "margin-top:".concat(viewerData.height / 2, "px;") + 'max-width:none!important;' + 'position:relative;' + 'width:0;';
1457
1466
  this.initImage(function () {
1458
1467
  toggleClass(image, CLASS_MOVE, options.movable);
1459
1468
  toggleClass(image, CLASS_TRANSITION, options.transition);
@@ -1698,7 +1707,9 @@ var handlers = {
1698
1707
  this.imageClicked = false; // This timeout will be cleared later when a native dblclick event is triggering
1699
1708
 
1700
1709
  this.doubleClickImageTimeout = setTimeout(function () {
1701
- dispatchEvent(_this2.image, EVENT_DBLCLICK);
1710
+ dispatchEvent(_this2.image, EVENT_DBLCLICK, {
1711
+ originalEvent: event
1712
+ });
1702
1713
  }, 50);
1703
1714
  } else {
1704
1715
  this.imageClicked = true; // The default timing of a double click in Windows is 500 ms
@@ -1713,7 +1724,9 @@ var handlers = {
1713
1724
  if (options.backdrop && options.backdrop !== 'static' && event.target === this.canvas) {
1714
1725
  // This timeout will be cleared later when a native click event is triggering
1715
1726
  this.clickCanvasTimeout = setTimeout(function () {
1716
- dispatchEvent(_this2.canvas, EVENT_CLICK);
1727
+ dispatchEvent(_this2.canvas, EVENT_CLICK, {
1728
+ originalEvent: event
1729
+ });
1717
1730
  }, 50);
1718
1731
  }
1719
1732
  }
@@ -1941,7 +1954,7 @@ var methods = {
1941
1954
  addListener(image, EVENT_TRANSITION_END, onImageTransitionEnd, {
1942
1955
  once: true
1943
1956
  });
1944
- this.zoomTo(0, false, false, true);
1957
+ this.zoomTo(0, false, null, true);
1945
1958
  } else {
1946
1959
  onImageTransitionEnd();
1947
1960
  }
@@ -2010,8 +2023,12 @@ var methods = {
2010
2023
  }
2011
2024
 
2012
2025
  var activeItem = this.items[this.index];
2013
- removeClass(activeItem, CLASS_ACTIVE);
2014
- activeItem.removeAttribute('aria-selected');
2026
+
2027
+ if (activeItem) {
2028
+ removeClass(activeItem, CLASS_ACTIVE);
2029
+ activeItem.removeAttribute('aria-selected');
2030
+ }
2031
+
2015
2032
  addClass(item, CLASS_ACTIVE);
2016
2033
  item.setAttribute('aria-selected', true);
2017
2034
 
@@ -2043,6 +2060,7 @@ var methods = {
2043
2060
  };
2044
2061
 
2045
2062
  var onLoad;
2063
+ var onError;
2046
2064
  addListener(element, EVENT_VIEWED, onViewed, {
2047
2065
  once: true
2048
2066
  });
@@ -2071,7 +2089,27 @@ var methods = {
2071
2089
  if (image.complete) {
2072
2090
  this.load();
2073
2091
  } else {
2074
- addListener(image, EVENT_LOAD, onLoad = this.load.bind(this), {
2092
+ addListener(image, EVENT_LOAD, onLoad = function onLoad() {
2093
+ removeListener(image, EVENT_ERROR, onError);
2094
+
2095
+ _this2.load();
2096
+ }, {
2097
+ once: true
2098
+ });
2099
+ addListener(image, EVENT_ERROR, onError = function onError() {
2100
+ removeListener(image, EVENT_LOAD, onLoad);
2101
+
2102
+ if (_this2.timeout) {
2103
+ clearTimeout(_this2.timeout);
2104
+ _this2.timeout = false;
2105
+ }
2106
+
2107
+ removeClass(image, CLASS_INVISIBLE);
2108
+
2109
+ if (options.loading) {
2110
+ removeClass(_this2.canvas, CLASS_LOADING);
2111
+ }
2112
+ }, {
2075
2113
  once: true
2076
2114
  });
2077
2115
 
@@ -2442,15 +2480,31 @@ var methods = {
2442
2480
  ratio = Math.min(Math.max(ratio, minZoomRatio), maxZoomRatio);
2443
2481
  }
2444
2482
 
2445
- if (_originalEvent && options.zoomRatio >= 0.055 && ratio > 0.95 && ratio < 1.05) {
2446
- ratio = 1;
2483
+ if (_originalEvent) {
2484
+ switch (_originalEvent.type) {
2485
+ case 'wheel':
2486
+ if (options.zoomRatio >= 0.055 && ratio > 0.95 && ratio < 1.05) {
2487
+ ratio = 1;
2488
+ }
2489
+
2490
+ break;
2491
+
2492
+ case 'pointermove':
2493
+ case 'touchmove':
2494
+ case 'mousemove':
2495
+ if (ratio > 0.99 && ratio < 1.01) {
2496
+ ratio = 1;
2497
+ }
2498
+
2499
+ break;
2500
+ }
2447
2501
  }
2448
2502
 
2449
2503
  var newWidth = naturalWidth * ratio;
2450
2504
  var newHeight = naturalHeight * ratio;
2451
2505
  var offsetWidth = newWidth - width;
2452
2506
  var offsetHeight = newHeight - height;
2453
- var oldRatio = width / naturalWidth;
2507
+ var oldRatio = imageData.ratio;
2454
2508
 
2455
2509
  if (isFunction(options.zoom)) {
2456
2510
  addListener(element, EVENT_ZOOM, options.zoom, {
@@ -2470,7 +2524,7 @@ var methods = {
2470
2524
 
2471
2525
  if (_originalEvent) {
2472
2526
  var offset = getOffset(this.viewer);
2473
- var center = pointers && Object.keys(pointers).length ? getPointersCenter(pointers) : {
2527
+ var center = pointers && Object.keys(pointers).length > 0 ? getPointersCenter(pointers) : {
2474
2528
  pageX: _originalEvent.pageX,
2475
2529
  pageY: _originalEvent.pageY
2476
2530
  }; // Zoom from the triggering point of the event
@@ -2487,6 +2541,7 @@ var methods = {
2487
2541
  imageData.top = imageData.y;
2488
2542
  imageData.width = newWidth;
2489
2543
  imageData.height = newHeight;
2544
+ imageData.oldRatio = oldRatio;
2490
2545
  imageData.ratio = ratio;
2491
2546
  this.renderImage(function () {
2492
2547
  _this6.zooming = false;
@@ -2516,7 +2571,7 @@ var methods = {
2516
2571
 
2517
2572
  /**
2518
2573
  * Play the images
2519
- * @param {boolean} [fullscreen=false] - Indicate if request fullscreen or not.
2574
+ * @param {boolean|FullscreenOptions} [fullscreen=false] - Indicate if request fullscreen or not.
2520
2575
  * @returns {Viewer} this
2521
2576
  */
2522
2577
  play: function play() {
@@ -2550,7 +2605,7 @@ var methods = {
2550
2605
  this.onLoadWhenPlay = onLoad;
2551
2606
 
2552
2607
  if (fullscreen) {
2553
- this.requestFullscreen();
2608
+ this.requestFullscreen(fullscreen);
2554
2609
  }
2555
2610
 
2556
2611
  addClass(player, CLASS_SHOW);
@@ -2795,12 +2850,19 @@ var methods = {
2795
2850
  }, 1000);
2796
2851
  return this;
2797
2852
  },
2798
- // Toggle the image size between its natural size and initial size
2853
+
2854
+ /**
2855
+ * Toggle the image size between its current size and natural size
2856
+ * @param {Event} [_originalEvent=null] - The original event if any.
2857
+ * @returns {Viewer} this
2858
+ */
2799
2859
  toggle: function toggle() {
2860
+ var _originalEvent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
2861
+
2800
2862
  if (this.imageData.ratio === 1) {
2801
- this.zoomTo(this.initialImageData.ratio, true);
2863
+ this.zoomTo(this.imageData.oldRatio, true, _originalEvent);
2802
2864
  } else {
2803
- this.zoomTo(1, true);
2865
+ this.zoomTo(1, true, _originalEvent);
2804
2866
  }
2805
2867
 
2806
2868
  return this;
@@ -3062,14 +3124,19 @@ var others = {
3062
3124
  });
3063
3125
  }
3064
3126
  },
3065
- requestFullscreen: function requestFullscreen() {
3127
+ requestFullscreen: function requestFullscreen(options) {
3066
3128
  var document = this.element.ownerDocument;
3067
3129
 
3068
3130
  if (this.fulled && !(document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement)) {
3069
3131
  var documentElement = document.documentElement; // Element.requestFullscreen()
3070
3132
 
3071
3133
  if (documentElement.requestFullscreen) {
3072
- documentElement.requestFullscreen();
3134
+ // Avoid TypeError when convert `options` to dictionary
3135
+ if (isPlainObject(options)) {
3136
+ documentElement.requestFullscreen(options);
3137
+ } else {
3138
+ documentElement.requestFullscreen();
3139
+ }
3073
3140
  } else if (documentElement.webkitRequestFullscreen) {
3074
3141
  documentElement.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
3075
3142
  } else if (documentElement.mozRequestFullScreen) {
@@ -3497,4 +3564,4 @@ var Viewer = /*#__PURE__*/function () {
3497
3564
 
3498
3565
  assign(Viewer.prototype, render, events, handlers, methods, others);
3499
3566
 
3500
- export default Viewer;
3567
+ export { Viewer as default };