123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import { VantComponent } from '../common/component';
- import { touch } from '../mixins/touch';
- const THRESHOLD = 0.15;
- VantComponent({
- props: {
- disabled: Boolean,
- leftWidth: {
- type: Number,
- value: 0
- },
- rightWidth: {
- type: Number,
- value: 0
- },
- asyncClose: Boolean
- },
- mixins: [touch],
- data: {
- offset: 0,
- draging: false
- },
- computed: {
- wrapperStyle() {
- const { offset, draging } = this.data;
- const transform = `translate3d(${offset}px, 0, 0)`;
- const transition = draging ? 'none' : '.6s cubic-bezier(0.18, 0.89, 0.32, 1)';
- return `
- -webkit-transform: ${transform};
- -webkit-transition: ${transition};
- transform: ${transform};
- transition: ${transition};
- `;
- }
- },
- methods: {
- onTransitionend() {
- this.swipe = false;
- },
- open(position) {
- const { leftWidth, rightWidth } = this.data;
- const offset = position === 'left' ? leftWidth : -rightWidth;
- this.swipeMove(offset);
- this.resetSwipeStatus();
- },
- close() {
- this.set({ offset: 0 });
- },
- resetSwipeStatus() {
- this.swiping = false;
- this.opened = true;
- },
- swipeMove(offset = 0) {
- this.set({ offset });
- offset && (this.swiping = true);
- !offset && (this.opened = false);
- },
- swipeLeaveTransition(direction) {
- const { offset, leftWidth, rightWidth } = this.data;
- const threshold = this.opened ? 1 - THRESHOLD : THRESHOLD;
- // right
- if (direction > 0 && -offset > rightWidth * threshold && rightWidth > 0) {
- this.open('right');
- // left
- }
- else if (direction < 0 && offset > leftWidth * threshold && leftWidth > 0) {
- this.open('left');
- }
- else {
- this.swipeMove();
- }
- },
- startDrag(event) {
- if (this.data.disabled) {
- return;
- }
- this.set({ draging: true });
- this.touchStart(event);
- if (this.opened) {
- this.startX -= this.data.offset;
- }
- },
- onDrag(event) {
- if (this.data.disabled) {
- return;
- }
- this.touchMove(event);
- const { deltaX } = this;
- const { leftWidth, rightWidth } = this.data;
- if ((deltaX < 0 && (-deltaX > rightWidth || !rightWidth)) ||
- (deltaX > 0 && (deltaX > leftWidth || (deltaX > 0 && !leftWidth)))) {
- return;
- }
- if (this.direction === 'horizontal') {
- this.swipeMove(deltaX);
- }
- },
- endDrag() {
- if (this.data.disabled) {
- return;
- }
- this.set({ draging: false });
- if (this.swiping) {
- this.swipeLeaveTransition(this.data.offset > 0 ? -1 : 1);
- }
- },
- onClick(event) {
- const { key: position = 'outside' } = event.currentTarget.dataset;
- this.$emit('click', position);
- if (!this.data.offset) {
- return;
- }
- if (this.data.asyncClose) {
- this.$emit('close', { position, instance: this });
- }
- else {
- this.swipeMove(0);
- }
- }
- }
- });
|