index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { VantComponent } from '../common/component';
  2. import { touch } from '../mixins/touch';
  3. import { range } from '../common/utils';
  4. const THRESHOLD = 0.3;
  5. let ARRAY = [];
  6. VantComponent({
  7. props: {
  8. disabled: Boolean,
  9. leftWidth: {
  10. type: Number,
  11. value: 0
  12. },
  13. rightWidth: {
  14. type: Number,
  15. value: 0
  16. },
  17. asyncClose: Boolean,
  18. name: {
  19. type: [Number, String],
  20. value: ''
  21. }
  22. },
  23. mixins: [touch],
  24. data: {
  25. catchMove: false
  26. },
  27. created() {
  28. this.offset = 0;
  29. ARRAY.push(this);
  30. },
  31. destroyed() {
  32. ARRAY = ARRAY.filter(item => item !== this);
  33. },
  34. methods: {
  35. open(position) {
  36. const { leftWidth, rightWidth } = this.data;
  37. const offset = position === 'left' ? leftWidth : -rightWidth;
  38. this.swipeMove(offset);
  39. this.$emit('open', {
  40. position,
  41. name: this.data.name
  42. });
  43. },
  44. close() {
  45. this.swipeMove(0);
  46. },
  47. swipeMove(offset = 0) {
  48. this.offset = range(offset, -this.data.rightWidth, this.data.leftWidth);
  49. const transform = `translate3d(${this.offset}px, 0, 0)`;
  50. const transition = this.dragging
  51. ? 'none'
  52. : 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  53. this.setData({
  54. wrapperStyle: `
  55. -webkit-transform: ${transform};
  56. -webkit-transition: ${transition};
  57. transform: ${transform};
  58. transition: ${transition};
  59. `
  60. });
  61. },
  62. swipeLeaveTransition() {
  63. const { leftWidth, rightWidth } = this.data;
  64. const { offset } = this;
  65. if (rightWidth > 0 && -offset > rightWidth * THRESHOLD) {
  66. this.open('right');
  67. }
  68. else if (leftWidth > 0 && offset > leftWidth * THRESHOLD) {
  69. this.open('left');
  70. }
  71. else {
  72. this.swipeMove(0);
  73. }
  74. this.setData({ catchMove: false });
  75. },
  76. startDrag(event) {
  77. if (this.data.disabled) {
  78. return;
  79. }
  80. this.startOffset = this.offset;
  81. this.touchStart(event);
  82. },
  83. noop() { },
  84. onDrag(event) {
  85. if (this.data.disabled) {
  86. return;
  87. }
  88. this.touchMove(event);
  89. if (this.direction !== 'horizontal') {
  90. return;
  91. }
  92. this.dragging = true;
  93. ARRAY.filter(item => item !== this).forEach(item => item.close());
  94. this.setData({ catchMove: true });
  95. this.swipeMove(this.startOffset + this.deltaX);
  96. },
  97. endDrag() {
  98. if (this.data.disabled) {
  99. return;
  100. }
  101. this.dragging = false;
  102. this.swipeLeaveTransition();
  103. },
  104. onClick(event) {
  105. const { key: position = 'outside' } = event.currentTarget.dataset;
  106. this.$emit('click', position);
  107. if (!this.offset) {
  108. return;
  109. }
  110. if (this.data.asyncClose) {
  111. this.$emit('close', {
  112. position,
  113. instance: this,
  114. name: this.data.name
  115. });
  116. }
  117. else {
  118. this.swipeMove(0);
  119. }
  120. }
  121. }
  122. });