电速宝
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { __decorate } from "tslib";
  2. import { SuperComponent, wxComponent } from "../common/src/index";
  3. import config from "../common/config";
  4. import props from "./props";
  5. import { getRect } from "../common/utils";
  6. import { getObserver } from "../common/wechat";
  7. let ARRAY = [];
  8. const { prefix } = config;
  9. const name = `${prefix}-swipe-cell`;
  10. const ContainerClass = `.${name}`;
  11. // 补充滑动阈值常量
  12. const THRESHOLD = 0.5;
  13. let SwiperCell = class extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`];
  17. this.options = { multipleSlots: true };
  18. this.properties = props;
  19. this.data = {
  20. prefix: prefix,
  21. wrapperStyle: "",
  22. closed: true,
  23. classPrefix: name,
  24. skipMove: false,
  25. leftWidth: 0,
  26. rightWidth: 0,
  27. // 补充滑动状态管理
  28. nv_state: {
  29. nv_direction: "", // vertical/horizontal
  30. nv_dragging: false,
  31. nv_startOffset: 0,
  32. nv_deltaX: 0,
  33. nv_offset: 0,
  34. nv_leftWidth: 0,
  35. nv_rightWidth: 0,
  36. nv_THRESHOLD: THRESHOLD,
  37. },
  38. };
  39. this.observers = {
  40. "left, right"() {
  41. this.setSwipeWidth();
  42. },
  43. };
  44. this.lifetimes = {
  45. attached() {
  46. ARRAY.push(this);
  47. },
  48. ready() {
  49. this.setSwipeWidth();
  50. },
  51. detached() {
  52. ARRAY = ARRAY.filter((e) => e !== this);
  53. },
  54. };
  55. // 声明事件处理方法(关键:暴露给模板调用)
  56. this.methods = {
  57. // 触摸开始:初始化滑动状态
  58. nv_touchStart(nv_event) {
  59. const touch = nv_event.touches[0];
  60. const { leftWidth, rightWidth } = this.data;
  61. const nv_state = {
  62. ...this.data.nv_state,
  63. nv_direction: "",
  64. nv_dragging: false,
  65. nv_startOffset: this.data.nv_state.nv_offset || 0,
  66. nv_deltaX: 0,
  67. nv_offset: this.data.nv_state.nv_offset || 0,
  68. nv_leftWidth: leftWidth,
  69. nv_rightWidth: rightWidth,
  70. nv_THRESHOLD: THRESHOLD,
  71. };
  72. this.setData({ nv_state });
  73. this.nv_state = nv_state; // 同步到实例属性
  74. },
  75. // 触摸移动:核心滑动逻辑(解决 touchmove 报错)
  76. nv_touchMove(nv_event) {
  77. const nv_ownerInstance = this;
  78. const nv_state = this.nv_state || this.data.nv_state;
  79. // 获取滑动偏移量
  80. const touch = nv_event.touches[0];
  81. nv_state.nv_deltaX = touch.clientX - (this.startX || touch.clientX);
  82. this.startX = touch.clientX;
  83. // 判断滑动方向
  84. if (Math.abs(nv_state.nv_deltaX) > Math.abs(touch.clientY - (this.startY || touch.clientY))) {
  85. nv_state.nv_direction = "horizontal";
  86. } else {
  87. nv_state.nv_direction = "vertical";
  88. nv_ownerInstance.skipMove(); // 垂直滑动跳过
  89. return;
  90. }
  91. // 水平滑动逻辑
  92. if (nv_state.nv_direction !== "horizontal") return;
  93. if (!nv_state.nv_dragging) {
  94. nv_ownerInstance.triggerEvent("dragstart"); // 触发开始拖拽事件
  95. }
  96. nv_state.nv_dragging = true;
  97. // 计算滑动偏移
  98. const newOffset = nv_state.nv_startOffset + nv_state.nv_deltaX;
  99. nv_state.nv_offset = newOffset;
  100. // 限制滑动范围
  101. this.nv_swipeMove(newOffset);
  102. this.setData({ nv_state });
  103. return false;
  104. },
  105. // 触摸结束:处理滑动收尾(解决 touchend 报错)
  106. nv_touchEnd() {
  107. const nv_ownerInstance = this;
  108. const nv_state = this.nv_state || this.data.nv_state;
  109. nv_state.nv_dragging = false;
  110. // 判断是否打开/关闭
  111. if (nv_state.nv_rightWidth > 0 && -nv_state.nv_startOffset < nv_state.nv_rightWidth && -nv_state.nv_offset > nv_state.nv_rightWidth * nv_state.nv_THRESHOLD) {
  112. this.nv_open("right");
  113. } else if (nv_state.nv_leftWidth > 0 && nv_state.nv_startOffset < nv_state.nv_leftWidth && nv_state.nv_offset > nv_state.nv_leftWidth * nv_state.nv_THRESHOLD) {
  114. this.nv_open("left");
  115. } else {
  116. if (nv_state.nv_startOffset !== nv_state.nv_offset) {
  117. this.nv_close();
  118. }
  119. }
  120. nv_ownerInstance.triggerEvent("dragend"); // 触发结束拖拽事件
  121. this.setData({ nv_state });
  122. },
  123. // 滑动偏移处理
  124. nv_swipeMove(offset) {
  125. const { leftWidth, rightWidth } = this.data;
  126. // 限制偏移范围:-rightWidth ≤ offset ≤ leftWidth
  127. const clampedOffset = Math.max(-rightWidth, Math.min(offset, leftWidth));
  128. this.setData({
  129. wrapperStyle: `transform: translateX(${clampedOffset}px); transition: none;`,
  130. });
  131. },
  132. // 打开侧滑
  133. nv_open(direction) {
  134. const offset = direction === "left" ? this.data.leftWidth : -this.data.rightWidth;
  135. this.setData({
  136. wrapperStyle: `transform: translateX(${offset}px); transition: transform 0.3s ease;`,
  137. nv_state: { ...this.data.nv_state, nv_offset: offset },
  138. closed: false,
  139. });
  140. this.triggerEvent("open", { direction });
  141. },
  142. // 关闭侧滑
  143. nv_close() {
  144. this.setData({
  145. wrapperStyle: `transform: translateX(0px); transition: transform 0.3s ease;`,
  146. nv_state: { ...this.data.nv_state, nv_offset: 0 },
  147. closed: true,
  148. });
  149. this.triggerEvent("close");
  150. },
  151. // 原有方法保留
  152. setSwipeWidth() {
  153. Promise.all([
  154. getRect(this, `${ContainerClass}__left`),
  155. getRect(this, `${ContainerClass}__right`),
  156. ]).then(([e, t]) => {
  157. if (e.width === 0 && t.width === 0 && !this._hasObserved) {
  158. this._hasObserved = true;
  159. getObserver(this, `.${name}`).then(() => {
  160. this.setSwipeWidth();
  161. });
  162. }
  163. this.setData({ leftWidth: e.width, rightWidth: t.width });
  164. });
  165. },
  166. skipMove() {
  167. if (!this.data.skipMove) {
  168. this.setData({ skipMove: true });
  169. }
  170. },
  171. catchMove() {
  172. if (this.data.skipMove) {
  173. this.setData({ skipMove: false });
  174. }
  175. },
  176. open() {
  177. this.setData({ opened: true });
  178. },
  179. close() {
  180. this.setData({ opened: false });
  181. },
  182. closeOther() {
  183. ARRAY.filter((e) => e !== this).forEach((e) => e.close());
  184. },
  185. onTap() {
  186. this.close();
  187. },
  188. onActionTap(e) {
  189. const {
  190. currentTarget: {
  191. dataset: { action: t },
  192. },
  193. } = e;
  194. this.triggerEvent("click", t);
  195. },
  196. };
  197. }
  198. };
  199. SwiperCell = __decorate([wxComponent()], SwiperCell);
  200. export default SwiperCell;