ImagePreviewItem.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports.default = void 0;
  5. var _shared = require("./shared");
  6. var _number = require("../utils/format/number");
  7. var _event = require("../utils/dom/event");
  8. var _touch = require("../mixins/touch");
  9. var _image = _interopRequireDefault(require("../image"));
  10. var _loading = _interopRequireDefault(require("../loading"));
  11. var _swipeItem = _interopRequireDefault(require("../swipe-item"));
  12. // Utils
  13. // Mixins
  14. // Component
  15. function getDistance(touches) {
  16. return Math.sqrt(Math.pow(touches[0].clientX - touches[1].clientX, 2) + Math.pow(touches[0].clientY - touches[1].clientY, 2));
  17. }
  18. var _default = {
  19. mixins: [_touch.TouchMixin],
  20. props: {
  21. src: String,
  22. show: Boolean,
  23. active: Number,
  24. minZoom: [Number, String],
  25. maxZoom: [Number, String],
  26. rootWidth: Number,
  27. rootHeight: Number
  28. },
  29. data: function data() {
  30. return {
  31. scale: 1,
  32. moveX: 0,
  33. moveY: 0,
  34. moving: false,
  35. zooming: false,
  36. imageRatio: 0,
  37. displayWidth: 0,
  38. displayHeight: 0
  39. };
  40. },
  41. computed: {
  42. vertical: function vertical() {
  43. var rootWidth = this.rootWidth,
  44. rootHeight = this.rootHeight;
  45. var rootRatio = rootHeight / rootWidth;
  46. return this.imageRatio > rootRatio;
  47. },
  48. imageStyle: function imageStyle() {
  49. var scale = this.scale;
  50. var style = {
  51. transitionDuration: this.zooming || this.moving ? '0s' : '.3s'
  52. };
  53. if (scale !== 1) {
  54. var offsetX = this.moveX / scale;
  55. var offsetY = this.moveY / scale;
  56. style.transform = "scale(" + scale + ", " + scale + ") translate(" + offsetX + "px, " + offsetY + "px)";
  57. }
  58. return style;
  59. },
  60. maxMoveX: function maxMoveX() {
  61. if (this.imageRatio) {
  62. var displayWidth = this.vertical ? this.rootHeight / this.imageRatio : this.rootWidth;
  63. return Math.max(0, (this.scale * displayWidth - this.rootWidth) / 2);
  64. }
  65. return 0;
  66. },
  67. maxMoveY: function maxMoveY() {
  68. if (this.imageRatio) {
  69. var displayHeight = this.vertical ? this.rootHeight : this.rootWidth * this.imageRatio;
  70. return Math.max(0, (this.scale * displayHeight - this.rootHeight) / 2);
  71. }
  72. return 0;
  73. }
  74. },
  75. watch: {
  76. active: 'resetScale',
  77. show: function show(val) {
  78. if (!val) {
  79. this.resetScale();
  80. }
  81. }
  82. },
  83. mounted: function mounted() {
  84. this.bindTouchEvent(this.$el);
  85. },
  86. methods: {
  87. resetScale: function resetScale() {
  88. this.setScale(1);
  89. this.moveX = 0;
  90. this.moveY = 0;
  91. },
  92. setScale: function setScale(scale) {
  93. scale = (0, _number.range)(scale, +this.minZoom, +this.maxZoom);
  94. if (scale !== this.scale) {
  95. this.scale = scale;
  96. this.$emit('scale', {
  97. scale: this.scale,
  98. index: this.active
  99. });
  100. }
  101. },
  102. toggleScale: function toggleScale() {
  103. var scale = this.scale > 1 ? 1 : 2;
  104. this.setScale(scale);
  105. this.moveX = 0;
  106. this.moveY = 0;
  107. },
  108. onTouchStart: function onTouchStart(event) {
  109. var touches = event.touches;
  110. var _this$offsetX = this.offsetX,
  111. offsetX = _this$offsetX === void 0 ? 0 : _this$offsetX;
  112. this.touchStart(event);
  113. this.touchStartTime = new Date();
  114. this.startMoveX = this.moveX;
  115. this.startMoveY = this.moveY;
  116. this.moving = touches.length === 1 && this.scale !== 1;
  117. this.zooming = touches.length === 2 && !offsetX;
  118. if (this.zooming) {
  119. this.startScale = this.scale;
  120. this.startDistance = getDistance(event.touches);
  121. }
  122. },
  123. onTouchMove: function onTouchMove(event) {
  124. var touches = event.touches;
  125. this.touchMove(event);
  126. if (this.moving || this.zooming) {
  127. (0, _event.preventDefault)(event, true);
  128. }
  129. if (this.moving) {
  130. var moveX = this.deltaX + this.startMoveX;
  131. var moveY = this.deltaY + this.startMoveY;
  132. this.moveX = (0, _number.range)(moveX, -this.maxMoveX, this.maxMoveX);
  133. this.moveY = (0, _number.range)(moveY, -this.maxMoveY, this.maxMoveY);
  134. }
  135. if (this.zooming && touches.length === 2) {
  136. var distance = getDistance(touches);
  137. var scale = this.startScale * distance / this.startDistance;
  138. this.setScale(scale);
  139. }
  140. },
  141. onTouchEnd: function onTouchEnd(event) {
  142. var stopPropagation = false;
  143. /* istanbul ignore else */
  144. if (this.moving || this.zooming) {
  145. stopPropagation = true;
  146. if (this.moving && this.startMoveX === this.moveX && this.startMoveY === this.moveY) {
  147. stopPropagation = false;
  148. }
  149. if (!event.touches.length) {
  150. if (this.zooming) {
  151. this.moveX = (0, _number.range)(this.moveX, -this.maxMoveX, this.maxMoveX);
  152. this.moveY = (0, _number.range)(this.moveY, -this.maxMoveY, this.maxMoveY);
  153. this.zooming = false;
  154. }
  155. this.moving = false;
  156. this.startMoveX = 0;
  157. this.startMoveY = 0;
  158. this.startScale = 1;
  159. if (this.scale < 1) {
  160. this.resetScale();
  161. }
  162. }
  163. } // eliminate tap delay on safari
  164. (0, _event.preventDefault)(event, stopPropagation);
  165. this.checkTap();
  166. this.resetTouchStatus();
  167. },
  168. checkTap: function checkTap() {
  169. var _this = this;
  170. var _this$offsetX2 = this.offsetX,
  171. offsetX = _this$offsetX2 === void 0 ? 0 : _this$offsetX2,
  172. _this$offsetY = this.offsetY,
  173. offsetY = _this$offsetY === void 0 ? 0 : _this$offsetY;
  174. var deltaTime = new Date() - this.touchStartTime;
  175. var TAP_TIME = 250;
  176. var TAP_OFFSET = 10;
  177. if (offsetX < TAP_OFFSET && offsetY < TAP_OFFSET && deltaTime < TAP_TIME) {
  178. if (this.doubleTapTimer) {
  179. clearTimeout(this.doubleTapTimer);
  180. this.doubleTapTimer = null;
  181. this.toggleScale();
  182. } else {
  183. this.doubleTapTimer = setTimeout(function () {
  184. _this.$emit('close');
  185. _this.doubleTapTimer = null;
  186. }, TAP_TIME);
  187. }
  188. }
  189. },
  190. onLoad: function onLoad(event) {
  191. var _event$target = event.target,
  192. naturalWidth = _event$target.naturalWidth,
  193. naturalHeight = _event$target.naturalHeight;
  194. this.imageRatio = naturalHeight / naturalWidth;
  195. }
  196. },
  197. render: function render() {
  198. var h = arguments[0];
  199. var imageSlots = {
  200. loading: function loading() {
  201. return h(_loading.default, {
  202. "attrs": {
  203. "type": "spinner"
  204. }
  205. });
  206. }
  207. };
  208. return h(_swipeItem.default, {
  209. "class": (0, _shared.bem)('swipe-item')
  210. }, [h(_image.default, {
  211. "attrs": {
  212. "src": this.src,
  213. "fit": "contain"
  214. },
  215. "class": (0, _shared.bem)('image', {
  216. vertical: this.vertical
  217. }),
  218. "style": this.imageStyle,
  219. "scopedSlots": imageSlots,
  220. "on": {
  221. "load": this.onLoad
  222. }
  223. })]);
  224. }
  225. };
  226. exports.default = _default;