爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
7.0 KiB

2 weeks ago
  1. const MIN_DISTANCE = 10;
  2. export default {
  3. showWatch(newVal, oldVal, ownerInstance, instance, self) {
  4. var state = self.state || {}
  5. var $el = ownerInstance.$el || ownerInstance.$vm && ownerInstance.$vm.$el
  6. if (!$el) return
  7. this.getDom(instance, ownerInstance, self)
  8. if (newVal && newVal !== 'none') {
  9. this.openState(newVal, instance, ownerInstance, self)
  10. return
  11. }
  12. if (state.left) {
  13. this.openState('none', instance, ownerInstance, self)
  14. }
  15. this.resetTouchStatus(instance, self)
  16. },
  17. /**
  18. * 开始触摸操作
  19. * @param {Object} e
  20. * @param {Object} ins
  21. */
  22. touchstart(e, ownerInstance, self) {
  23. let instance = e.instance;
  24. let disabled = instance.getDataset().disabled
  25. let state = self.state || {};
  26. this.getDom(instance, ownerInstance, self)
  27. // fix by mehaotian, TODO 兼容 app-vue 获取dataset为字符串 , h5 获取 为 undefined 的问题,待框架修复
  28. disabled = this.getDisabledType(disabled)
  29. if (disabled) return
  30. // 开始触摸时移除动画类
  31. instance.requestAnimationFrame(function() {
  32. instance.removeClass('ani');
  33. ownerInstance.callMethod('closeSwipe');
  34. })
  35. // 记录上次的位置
  36. state.x = state.left || 0
  37. // 计算滑动开始位置
  38. this.stopTouchStart(e, ownerInstance, self)
  39. },
  40. /**
  41. * 开始滑动操作
  42. * @param {Object} e
  43. * @param {Object} ownerInstance
  44. */
  45. touchmove(e, ownerInstance, self) {
  46. let instance = e.instance;
  47. // 删除之后已经那不到实例了
  48. if (!instance) return;
  49. let disabled = instance.getDataset().disabled
  50. let state = self.state || {}
  51. // fix by mehaotian, TODO 兼容 app-vue 获取dataset为字符串 , h5 获取 为 undefined 的问题,待框架修复
  52. disabled = this.getDisabledType(disabled)
  53. if (disabled) return
  54. // 是否可以滑动页面
  55. this.stopTouchMove(e, self);
  56. if (state.direction !== 'horizontal') {
  57. return;
  58. }
  59. if (e.preventDefault) {
  60. // 阻止页面滚动
  61. e.preventDefault()
  62. }
  63. let x = state.x + state.deltaX
  64. this.move(x, instance, ownerInstance, self)
  65. },
  66. /**
  67. * 结束触摸操作
  68. * @param {Object} e
  69. * @param {Object} ownerInstance
  70. */
  71. touchend(e, ownerInstance, self) {
  72. let instance = e.instance;
  73. let disabled = instance.getDataset().disabled
  74. let state = self.state || {}
  75. // fix by mehaotian, TODO 兼容 app-vue 获取dataset为字符串 , h5 获取 为 undefined 的问题,待框架修复
  76. disabled = this.getDisabledType(disabled)
  77. if (disabled) return
  78. // 滑动过程中触摸结束,通过阙值判断是开启还是关闭
  79. // fixed by mehaotian 定时器解决点击按钮,touchend 触发比 click 事件时机早的问题 ,主要是 ios13
  80. this.moveDirection(state.left, instance, ownerInstance, self)
  81. },
  82. /**
  83. * 设置移动距离
  84. * @param {Object} value
  85. * @param {Object} instance
  86. * @param {Object} ownerInstance
  87. */
  88. move(value, instance, ownerInstance, self) {
  89. value = value || 0
  90. let state = self.state || {}
  91. let leftWidth = state.leftWidth
  92. let rightWidth = state.rightWidth
  93. // 获取可滑动范围
  94. state.left = this.range(value, -rightWidth, leftWidth);
  95. instance.requestAnimationFrame(function() {
  96. instance.setStyle({
  97. transform: 'translateX(' + state.left + 'px)',
  98. '-webkit-transform': 'translateX(' + state.left + 'px)'
  99. })
  100. })
  101. },
  102. /**
  103. * 获取元素信息
  104. * @param {Object} instance
  105. * @param {Object} ownerInstance
  106. */
  107. getDom(instance, ownerInstance, self) {
  108. var state = self.state || {}
  109. var $el = ownerInstance.$el || ownerInstance.$vm && ownerInstance.$vm.$el
  110. var leftDom = $el.querySelector('.button-group--left')
  111. var rightDom = $el.querySelector('.button-group--right')
  112. if (leftDom && leftDom.offsetWidth) {
  113. state.leftWidth = leftDom.offsetWidth || 0
  114. } else {
  115. state.leftWidth = 0
  116. }
  117. if (rightDom && rightDom.offsetWidth) {
  118. state.rightWidth = rightDom.offsetWidth || 0
  119. } else {
  120. state.rightWidth = 0
  121. }
  122. state.threshold = instance.getDataset().threshold
  123. },
  124. getDisabledType(value) {
  125. return (typeof(value) === 'string' ? JSON.parse(value) : value) || false;
  126. },
  127. /**
  128. * 获取范围
  129. * @param {Object} num
  130. * @param {Object} min
  131. * @param {Object} max
  132. */
  133. range(num, min, max) {
  134. return Math.min(Math.max(num, min), max);
  135. },
  136. /**
  137. * 移动方向判断
  138. * @param {Object} left
  139. * @param {Object} value
  140. * @param {Object} ownerInstance
  141. * @param {Object} ins
  142. */
  143. moveDirection(left, ins, ownerInstance, self) {
  144. var state = self.state || {}
  145. var threshold = state.threshold
  146. var position = state.position
  147. var isopen = state.isopen || 'none'
  148. var leftWidth = state.leftWidth
  149. var rightWidth = state.rightWidth
  150. if (state.deltaX === 0) {
  151. this.openState('none', ins, ownerInstance, self)
  152. return
  153. }
  154. if ((isopen === 'none' && rightWidth > 0 && -left > threshold) || (isopen !== 'none' && rightWidth > 0 &&
  155. rightWidth +
  156. left < threshold)) {
  157. // right
  158. this.openState('right', ins, ownerInstance, self)
  159. } else if ((isopen === 'none' && leftWidth > 0 && left > threshold) || (isopen !== 'none' && leftWidth > 0 &&
  160. leftWidth - left < threshold)) {
  161. // left
  162. this.openState('left', ins, ownerInstance, self)
  163. } else {
  164. // default
  165. this.openState('none', ins, ownerInstance, self)
  166. }
  167. },
  168. /**
  169. * 开启状态
  170. * @param {Boolean} type
  171. * @param {Object} ins
  172. * @param {Object} ownerInstance
  173. */
  174. openState(type, ins, ownerInstance, self) {
  175. let state = self.state || {}
  176. let leftWidth = state.leftWidth
  177. let rightWidth = state.rightWidth
  178. let left = ''
  179. state.isopen = state.isopen ? state.isopen : 'none'
  180. switch (type) {
  181. case "left":
  182. left = leftWidth
  183. break
  184. case "right":
  185. left = -rightWidth
  186. break
  187. default:
  188. left = 0
  189. }
  190. // && !state.throttle
  191. if (state.isopen !== type) {
  192. state.throttle = true
  193. ownerInstance.callMethod('change', {
  194. open: type
  195. })
  196. }
  197. state.isopen = type
  198. // 添加动画类
  199. ins.requestAnimationFrame(() => {
  200. ins.addClass('ani');
  201. this.move(left, ins, ownerInstance, self)
  202. })
  203. },
  204. getDirection(x, y) {
  205. if (x > y && x > MIN_DISTANCE) {
  206. return 'horizontal';
  207. }
  208. if (y > x && y > MIN_DISTANCE) {
  209. return 'vertical';
  210. }
  211. return '';
  212. },
  213. /**
  214. * 重置滑动状态
  215. * @param {Object} event
  216. */
  217. resetTouchStatus(instance, self) {
  218. let state = self.state || {};
  219. state.direction = '';
  220. state.deltaX = 0;
  221. state.deltaY = 0;
  222. state.offsetX = 0;
  223. state.offsetY = 0;
  224. },
  225. /**
  226. * 设置滑动开始位置
  227. * @param {Object} event
  228. */
  229. stopTouchStart(event, ownerInstance, self) {
  230. let instance = event.instance;
  231. let state = self.state || {}
  232. this.resetTouchStatus(instance, self);
  233. var touch = event.touches[0];
  234. state.startX = touch.clientX;
  235. state.startY = touch.clientY;
  236. },
  237. /**
  238. * 滑动中是否禁止打开
  239. * @param {Object} event
  240. */
  241. stopTouchMove(event, self) {
  242. let instance = event.instance;
  243. let state = self.state || {};
  244. let touch = event.touches[0];
  245. state.deltaX = touch.clientX - state.startX;
  246. state.deltaY = touch.clientY - state.startY;
  247. state.offsetY = Math.abs(state.deltaY);
  248. state.offsetX = Math.abs(state.deltaX);
  249. state.direction = state.direction || this.getDirection(state.offsetX, state.offsetY);
  250. }
  251. }