index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <uni-shadow-root class="weapp-lib-swipe-cell-index"><view class="van-swipe-cell custom-class" data-key="cell" @click.stop.prevent="onClick" @touchstart="startDrag" @touchmove.stop.prevent="_$self[(catchMove ? 'noop' : '')||'_$noop']($event)" @touchmove.capture="onDrag" @touchend="endDrag" @touchcancel="endDrag">
  3. <view :style="wrapperStyle">
  4. <view v-if="leftWidth" class="van-swipe-cell__left" data-key="left" @click.stop.prevent="onClick">
  5. <slot name="left"></slot>
  6. </view>
  7. <slot></slot>
  8. <view v-if="rightWidth" class="van-swipe-cell__right" data-key="right" @click.stop.prevent="onClick">
  9. <slot name="right"></slot>
  10. </view>
  11. </view>
  12. </view></uni-shadow-root>
  13. </template>
  14. <script>
  15. global['__wxRoute'] = 'weapp/lib/swipe-cell/index'
  16. "use strict";
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. var component_1 = require("../common/component");
  19. var touch_1 = require("../mixins/touch");
  20. var utils_1 = require("../common/utils");
  21. var THRESHOLD = 0.3;
  22. var ARRAY = [];
  23. (0, component_1.VantComponent)({
  24. props: {
  25. disabled: Boolean,
  26. leftWidth: {
  27. type: Number,
  28. value: 0,
  29. observer: function (leftWidth) {
  30. if (leftWidth === void 0) { leftWidth = 0; }
  31. if (this.offset > 0) {
  32. this.swipeMove(leftWidth);
  33. }
  34. },
  35. },
  36. rightWidth: {
  37. type: Number,
  38. value: 0,
  39. observer: function (rightWidth) {
  40. if (rightWidth === void 0) { rightWidth = 0; }
  41. if (this.offset < 0) {
  42. this.swipeMove(-rightWidth);
  43. }
  44. },
  45. },
  46. asyncClose: Boolean,
  47. name: {
  48. type: null,
  49. value: '',
  50. },
  51. },
  52. mixins: [touch_1.touch],
  53. data: {
  54. catchMove: false,
  55. wrapperStyle: '',
  56. },
  57. created: function () {
  58. this.offset = 0;
  59. ARRAY.push(this);
  60. },
  61. destroyed: function () {
  62. var _this = this;
  63. ARRAY = ARRAY.filter(function (item) { return item !== _this; });
  64. },
  65. methods: {
  66. open: function (position) {
  67. var _a = this.data, leftWidth = _a.leftWidth, rightWidth = _a.rightWidth;
  68. var offset = position === 'left' ? leftWidth : -rightWidth;
  69. this.swipeMove(offset);
  70. this.$emit('open', {
  71. position: position,
  72. name: this.data.name,
  73. });
  74. },
  75. close: function () {
  76. this.swipeMove(0);
  77. },
  78. swipeMove: function (offset) {
  79. if (offset === void 0) { offset = 0; }
  80. this.offset = (0, utils_1.range)(offset, -this.data.rightWidth, this.data.leftWidth);
  81. var transform = "translate3d(".concat(this.offset, "px, 0, 0)");
  82. var transition = this.dragging
  83. ? 'none'
  84. : 'transform .6s cubic-bezier(0.18, 0.89, 0.32, 1)';
  85. this.setData({
  86. wrapperStyle: "\n -webkit-transform: ".concat(transform, ";\n -webkit-transition: ").concat(transition, ";\n transform: ").concat(transform, ";\n transition: ").concat(transition, ";\n "),
  87. });
  88. },
  89. swipeLeaveTransition: function () {
  90. var _a = this.data, leftWidth = _a.leftWidth, rightWidth = _a.rightWidth;
  91. var offset = this.offset;
  92. if (rightWidth > 0 && -offset > rightWidth * THRESHOLD) {
  93. this.open('right');
  94. }
  95. else if (leftWidth > 0 && offset > leftWidth * THRESHOLD) {
  96. this.open('left');
  97. }
  98. else {
  99. this.swipeMove(0);
  100. }
  101. this.setData({ catchMove: false });
  102. },
  103. startDrag: function (event) {
  104. if (this.data.disabled) {
  105. return;
  106. }
  107. this.startOffset = this.offset;
  108. this.touchStart(event);
  109. },
  110. noop: function () { },
  111. onDrag: function (event) {
  112. var _this = this;
  113. if (this.data.disabled) {
  114. return;
  115. }
  116. this.touchMove(event);
  117. if (this.direction !== 'horizontal') {
  118. return;
  119. }
  120. this.dragging = true;
  121. ARRAY.filter(function (item) { return item !== _this && item.offset !== 0; }).forEach(function (item) { return item.close(); });
  122. this.setData({ catchMove: true });
  123. this.swipeMove(this.startOffset + this.deltaX);
  124. },
  125. endDrag: function () {
  126. if (this.data.disabled) {
  127. return;
  128. }
  129. this.dragging = false;
  130. this.swipeLeaveTransition();
  131. },
  132. onClick: function (event) {
  133. var _a = event.currentTarget.dataset.key, position = _a === void 0 ? 'outside' : _a;
  134. this.$emit('click', position);
  135. if (!this.offset) {
  136. return;
  137. }
  138. if (this.data.asyncClose) {
  139. this.$emit('close', {
  140. position: position,
  141. instance: this,
  142. name: this.data.name,
  143. });
  144. }
  145. else {
  146. this.swipeMove(0);
  147. }
  148. },
  149. },
  150. });
  151. export default global['__wxComponents']['weapp/lib/swipe-cell/index']
  152. </script>
  153. <style platform="mp-weixin">
  154. @import '../common/index.css';.van-swipe-cell{overflow:hidden;position:relative}.van-swipe-cell__left,.van-swipe-cell__right{height:100%;position:absolute;top:0}.van-swipe-cell__left{left:0;transform:translate3d(-100%,0,0)}.van-swipe-cell__right{right:0;transform:translate3d(100%,0,0)}
  155. </style>