ImagePreviewItem.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.fingerNum = touches.length;
  115. this.startMoveX = this.moveX;
  116. this.startMoveY = this.moveY;
  117. this.moving = this.fingerNum === 1 && this.scale !== 1;
  118. this.zooming = this.fingerNum === 2 && !offsetX;
  119. if (this.zooming) {
  120. this.startScale = this.scale;
  121. this.startDistance = getDistance(event.touches);
  122. }
  123. },
  124. onTouchMove: function onTouchMove(event) {
  125. var touches = event.touches;
  126. this.touchMove(event);
  127. if (this.moving || this.zooming) {
  128. (0, _event.preventDefault)(event, true);
  129. }
  130. if (this.moving) {
  131. var moveX = this.deltaX + this.startMoveX;
  132. var moveY = this.deltaY + this.startMoveY;
  133. this.moveX = (0, _number.range)(moveX, -this.maxMoveX, this.maxMoveX);
  134. this.moveY = (0, _number.range)(moveY, -this.maxMoveY, this.maxMoveY);
  135. }
  136. if (this.zooming && touches.length === 2) {
  137. var distance = getDistance(touches);
  138. var scale = this.startScale * distance / this.startDistance;
  139. this.setScale(scale);
  140. }
  141. },
  142. onTouchEnd: function onTouchEnd(event) {
  143. var stopPropagation = false;
  144. /* istanbul ignore else */
  145. if (this.moving || this.zooming) {
  146. stopPropagation = true;
  147. if (this.moving && this.startMoveX === this.moveX && this.startMoveY === this.moveY) {
  148. stopPropagation = false;
  149. }
  150. if (!event.touches.length) {
  151. if (this.zooming) {
  152. this.moveX = (0, _number.range)(this.moveX, -this.maxMoveX, this.maxMoveX);
  153. this.moveY = (0, _number.range)(this.moveY, -this.maxMoveY, this.maxMoveY);
  154. this.zooming = false;
  155. }
  156. this.moving = false;
  157. this.startMoveX = 0;
  158. this.startMoveY = 0;
  159. this.startScale = 1;
  160. if (this.scale < 1) {
  161. this.resetScale();
  162. }
  163. }
  164. } // eliminate tap delay on safari
  165. (0, _event.preventDefault)(event, stopPropagation);
  166. this.checkTap();
  167. this.resetTouchStatus();
  168. },
  169. checkTap: function checkTap() {
  170. var _this = this;
  171. if (this.fingerNum > 1) {
  172. return;
  173. }
  174. var _this$offsetX2 = this.offsetX,
  175. offsetX = _this$offsetX2 === void 0 ? 0 : _this$offsetX2,
  176. _this$offsetY = this.offsetY,
  177. offsetY = _this$offsetY === void 0 ? 0 : _this$offsetY;
  178. var deltaTime = new Date() - this.touchStartTime;
  179. var TAP_TIME = 250;
  180. var TAP_OFFSET = 5;
  181. if (offsetX < TAP_OFFSET && offsetY < TAP_OFFSET && deltaTime < TAP_TIME) {
  182. if (this.doubleTapTimer) {
  183. clearTimeout(this.doubleTapTimer);
  184. this.doubleTapTimer = null;
  185. this.toggleScale();
  186. } else {
  187. this.doubleTapTimer = setTimeout(function () {
  188. _this.$emit('close');
  189. _this.doubleTapTimer = null;
  190. }, TAP_TIME);
  191. }
  192. }
  193. },
  194. onLoad: function onLoad(event) {
  195. var _event$target = event.target,
  196. naturalWidth = _event$target.naturalWidth,
  197. naturalHeight = _event$target.naturalHeight;
  198. this.imageRatio = naturalHeight / naturalWidth;
  199. }
  200. },
  201. render: function render() {
  202. var h = arguments[0];
  203. var imageSlots = {
  204. loading: function loading() {
  205. return h(_loading.default, {
  206. "attrs": {
  207. "type": "spinner"
  208. }
  209. });
  210. }
  211. };
  212. return h(_swipeItem.default, {
  213. "class": (0, _shared.bem)('swipe-item')
  214. }, [h(_image.default, {
  215. "attrs": {
  216. "src": this.src,
  217. "fit": "contain"
  218. },
  219. "class": (0, _shared.bem)('image', {
  220. vertical: this.vertical
  221. }),
  222. "style": this.imageStyle,
  223. "scopedSlots": imageSlots,
  224. "on": {
  225. "load": this.onLoad
  226. }
  227. })]);
  228. }
  229. };
  230. exports.default = _default;