ImagePreviewItem.js 6.7 KB

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