| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- "use strict";
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
- exports.__esModule = true;
- exports["default"] = void 0;
- var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props"));
- var _utils = require("../utils");
- var _event = require("../utils/event");
- var _popup = require("../mixins/popup");
- var _touch = require("../mixins/touch");
- var _swipe = _interopRequireDefault(require("../swipe"));
- var _swipeItem = _interopRequireDefault(require("../swipe-item"));
- var _use = (0, _utils.use)('image-preview'),
- sfc = _use[0],
- bem = _use[1];
- function getDistance(touches) {
- return Math.sqrt(Math.abs((touches[0].clientX - touches[1].clientX) * (touches[0].clientY - touches[1].clientY)));
- }
- var _default = sfc({
- mixins: [_popup.PopupMixin, _touch.TouchMixin],
- props: {
- images: Array,
- className: null,
- lazyLoad: Boolean,
- asyncClose: Boolean,
- startPosition: Number,
- showIndicators: Boolean,
- loop: {
- type: Boolean,
- "default": true
- },
- overlay: {
- type: Boolean,
- "default": true
- },
- showIndex: {
- type: Boolean,
- "default": true
- },
- minZoom: {
- type: Number,
- "default": 1 / 3
- },
- maxZoom: {
- type: Number,
- "default": 3
- },
- overlayClass: {
- type: String,
- "default": 'van-image-preview__overlay'
- },
- closeOnClickOverlay: {
- type: Boolean,
- "default": true
- }
- },
- data: function data() {
- return {
- scale: 1,
- moveX: 0,
- moveY: 0,
- moving: false,
- zooming: false,
- active: 0
- };
- },
- computed: {
- imageStyle: function imageStyle() {
- var scale = this.scale;
- var style = {
- transition: this.zooming || this.moving ? '' : '.3s all'
- };
- if (scale !== 1) {
- style.transform = "scale3d(" + scale + ", " + scale + ", 1) translate(" + this.moveX / scale + "px, " + this.moveY / scale + "px)";
- }
- return style;
- }
- },
- watch: {
- value: function value() {
- this.active = this.startPosition;
- },
- startPosition: function startPosition(active) {
- this.active = active;
- }
- },
- methods: {
- onWrapperTouchStart: function onWrapperTouchStart() {
- this.touchStartTime = new Date();
- },
- onWrapperTouchEnd: function onWrapperTouchEnd(event) {
- (0, _event.preventDefault)(event);
- var deltaTime = new Date() - this.touchStartTime;
- var _ref = this.$refs.swipe || {},
- _ref$offsetX = _ref.offsetX,
- offsetX = _ref$offsetX === void 0 ? 0 : _ref$offsetX,
- _ref$offsetY = _ref.offsetY,
- offsetY = _ref$offsetY === void 0 ? 0 : _ref$offsetY; // prevent long tap to close component
- if (deltaTime < 300 && offsetX < 10 && offsetY < 10) {
- var index = this.active;
- this.resetScale();
- this.$emit('close', {
- index: index,
- url: this.images[index]
- });
- if (!this.asyncClose) {
- this.$emit('input', false);
- }
- }
- },
- startMove: function startMove(event) {
- var image = event.currentTarget;
- var rect = image.getBoundingClientRect();
- var winWidth = window.innerWidth;
- var winHeight = window.innerHeight;
- this.touchStart(event);
- this.moving = true;
- this.startMoveX = this.moveX;
- this.startMoveY = this.moveY;
- this.maxMoveX = Math.max(0, (rect.width - winWidth) / 2);
- this.maxMoveY = Math.max(0, (rect.height - winHeight) / 2);
- },
- startZoom: function startZoom(event) {
- this.moving = false;
- this.zooming = true;
- this.startScale = this.scale;
- this.startDistance = getDistance(event.touches);
- },
- onTouchStart: function onTouchStart(event) {
- var touches = event.touches;
- var _ref2 = this.$refs.swipe || {},
- _ref2$offsetX = _ref2.offsetX,
- offsetX = _ref2$offsetX === void 0 ? 0 : _ref2$offsetX;
- if (touches.length === 1 && this.scale !== 1) {
- this.startMove(event);
- }
- /* istanbul ignore else */
- else if (touches.length === 2 && !offsetX) {
- this.startZoom(event);
- }
- },
- onTouchMove: function onTouchMove(event) {
- var touches = event.touches;
- if (this.moving || this.zooming) {
- (0, _event.preventDefault)(event, true);
- }
- if (this.moving) {
- this.touchMove(event);
- var moveX = this.deltaX + this.startMoveX;
- var moveY = this.deltaY + this.startMoveY;
- this.moveX = (0, _utils.range)(moveX, -this.maxMoveX, this.maxMoveX);
- this.moveY = (0, _utils.range)(moveY, -this.maxMoveY, this.maxMoveY);
- }
- if (this.zooming && touches.length === 2) {
- var distance = getDistance(touches);
- var scale = this.startScale * distance / this.startDistance;
- this.scale = (0, _utils.range)(scale, this.minZoom, this.maxZoom);
- }
- },
- onTouchEnd: function onTouchEnd(event) {
- /* istanbul ignore else */
- if (this.moving || this.zooming) {
- var stopPropagation = true;
- if (this.moving && this.startMoveX === this.moveX && this.startMoveY === this.moveY) {
- stopPropagation = false;
- }
- if (!event.touches.length) {
- this.moving = false;
- this.zooming = false;
- this.startMoveX = 0;
- this.startMoveY = 0;
- this.startScale = 1;
- if (this.scale < 1) {
- this.resetScale();
- }
- }
- if (stopPropagation) {
- (0, _event.preventDefault)(event, true);
- }
- }
- },
- onChange: function onChange(active) {
- this.resetScale();
- this.active = active;
- this.$emit('change', active);
- },
- resetScale: function resetScale() {
- this.scale = 1;
- this.moveX = 0;
- this.moveY = 0;
- }
- },
- render: function render(h) {
- var _this = this;
- if (!this.value) {
- return;
- }
- var active = this.active,
- images = this.images;
- var Index = this.showIndex && h("div", {
- "class": bem('index')
- }, [this.slots('index') || active + 1 + "/" + images.length]);
- var Images = h(_swipe["default"], {
- "ref": "swipe",
- "attrs": {
- "loop": this.loop,
- "indicatorColor": "white",
- "initialSwipe": this.startPosition,
- "showIndicators": this.showIndicators
- },
- "on": {
- "change": this.onChange
- }
- }, [images.map(function (image, index) {
- var props = {
- "class": bem('image'),
- style: index === active ? _this.imageStyle : null,
- on: {
- touchstart: _this.onTouchStart,
- touchmove: _this.onTouchMove,
- touchend: _this.onTouchEnd,
- touchcancel: _this.onTouchEnd
- }
- };
- return h(_swipeItem["default"], [_this.lazyLoad ? h("img", (0, _babelHelperVueJsxMergeProps["default"])([{
- "directives": [{
- name: "lazy",
- value: image
- }]
- }, props])) : h("img", (0, _babelHelperVueJsxMergeProps["default"])([{
- "attrs": {
- "src": image
- }
- }, props]))]);
- })]);
- return h("transition", {
- "attrs": {
- "name": "van-fade"
- }
- }, [h("div", {
- "class": [bem(), this.className],
- "on": {
- "touchstart": this.onWrapperTouchStart,
- "touchend": this.onWrapperTouchEnd,
- "touchcancel": this.onWrapperTouchEnd
- }
- }, [Index, Images])]);
- }
- });
- exports["default"] = _default;
|