ImagePreviewItem.js 6.6 KB

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