index.wxs 6.5 KB

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