index.wxs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * 监听页面内值的变化,主要用于动态开关swipe-action
  3. * @param {Object} newValue
  4. * @param {Object} oldValue
  5. * @param {Object} ownerInstance
  6. * @param {Object} instance
  7. */
  8. function sizeReady(newValue, oldValue, ownerInstance, instance) {
  9. var state = instance.getState()
  10. state.position = JSON.parse(newValue)
  11. if (!state.position || state.position.length === 0) return
  12. var show = state.position[0].show
  13. state.left = state.left || state.position[0].left;
  14. // 通过用户变量,开启或关闭
  15. if (show) {
  16. openState(true, instance, ownerInstance)
  17. } else {
  18. openState(false, instance, ownerInstance)
  19. }
  20. }
  21. /**
  22. * 开始触摸操作
  23. * @param {Object} e
  24. * @param {Object} ins
  25. */
  26. function touchstart(e, ins) {
  27. var instance = e.instance;
  28. var state = instance.getState();
  29. var pageX = e.touches[0].pageX;
  30. // 开始触摸时移除动画类
  31. instance.removeClass('ani');
  32. var owner = ins.selectAllComponents('.button-hock')
  33. for (var i = 0; i < owner.length; i++) {
  34. owner[i].removeClass('ani');
  35. }
  36. // state.position = JSON.parse(instance.getDataset().position);
  37. state.left = state.left || state.position[0].left;
  38. // 获取最终按钮组的宽度
  39. state.width = pageX - state.left;
  40. ins.callMethod('closeSwipe')
  41. }
  42. /**
  43. * 开始滑动操作
  44. * @param {Object} e
  45. * @param {Object} ownerInstance
  46. */
  47. function touchmove(e, ownerInstance) {
  48. var instance = e.instance;
  49. var disabled = instance.getDataset().disabled
  50. var state = instance.getState()
  51. // fix by mehaotian, TODO 兼容 app-vue 获取dataset为字符串 , h5 获取 为 undefined 的问题,待框架修复
  52. disabled = (typeof(disabled) === 'string' ? JSON.parse(disabled) : disabled) || false;
  53. if (disabled) return
  54. var pageX = e.touches[0].pageX;
  55. move(pageX - state.width, instance, ownerInstance)
  56. }
  57. /**
  58. * 结束触摸操作
  59. * @param {Object} e
  60. * @param {Object} ownerInstance
  61. */
  62. function touchend(e, ownerInstance) {
  63. var instance = e.instance;
  64. var disabled = instance.getDataset().disabled
  65. var state = instance.getState()
  66. // fix by mehaotian, TODO 兼容 app-vue 获取dataset为字符串 , h5 获取 为 undefined 的问题,待框架修复
  67. disabled = (typeof(disabled) === 'string' ? JSON.parse(disabled) : disabled) || false;
  68. if (disabled){
  69. return
  70. }
  71. // 滑动过程中触摸结束,通过阙值判断是开启还是关闭
  72. // fixed by mehaotian 定时器解决点击按钮,touchend 触发比 click 事件时机早的问题 ,主要是 ios13
  73. // setTimeout(()=>{
  74. // },50)
  75. moveDirection(state.left, -40, instance, ownerInstance)
  76. }
  77. /**
  78. * 设置移动距离
  79. * @param {Object} value
  80. * @param {Object} instance
  81. * @param {Object} ownerInstance
  82. */
  83. function move(value, instance, ownerInstance) {
  84. var state = instance.getState()
  85. // 获取可滑动范围
  86. var x = Math.max(-state.position[1].width, Math.min((value), 0));
  87. state.left = x;
  88. instance.setStyle({
  89. transform: 'translateX(' + x + 'px)',
  90. '-webkit-transform': 'translateX(' + x + 'px)'
  91. })
  92. // 折叠按钮动画
  93. buttonFold(x, instance, ownerInstance)
  94. }
  95. /**
  96. * 移动方向判断
  97. * @param {Object} left
  98. * @param {Object} value
  99. * @param {Object} ownerInstance
  100. * @param {Object} ins
  101. */
  102. function moveDirection(left, value, ins, ownerInstance) {
  103. var state = ins.getState()
  104. var position = state.position
  105. var isopen = state.isopen
  106. if (!position[1].width) {
  107. openState(false, ins, ownerInstance)
  108. return
  109. }
  110. // 如果已经是打开状态,进行判断是否关闭,还是保留打开状态
  111. if (isopen) {
  112. if (-left <= position[1].width) {
  113. openState(false, ins, ownerInstance)
  114. } else {
  115. openState(true, ins, ownerInstance)
  116. }
  117. return
  118. }
  119. // 如果是关闭状态,进行判断是否打开,还是保留关闭状态
  120. if (left <= value) {
  121. openState(true, ins, ownerInstance)
  122. } else {
  123. openState(false, ins, ownerInstance)
  124. }
  125. }
  126. /**
  127. * 设置按钮移动距离
  128. * @param {Object} value
  129. * @param {Object} instance
  130. * @param {Object} ownerInstance
  131. */
  132. function buttonFold(value, instance, ownerInstance) {
  133. var ins = ownerInstance.selectAllComponents('.button-hock');
  134. var state = instance.getState();
  135. var position = state.position;
  136. var arr = [];
  137. var w = 0;
  138. for (var i = 0; i < ins.length; i++) {
  139. if (!ins[i].getDataset().button) return
  140. var btnData = JSON.parse(ins[i].getDataset().button)
  141. // fix by mehaotian TODO 在 app-vue 中,字符串转对象,需要转两次,这里先这么兼容
  142. if (typeof(btnData) === 'string') {
  143. btnData = JSON.parse(btnData)
  144. }
  145. var button = btnData[i] && btnData[i].width || 0
  146. w += button
  147. arr.push(-w)
  148. // 动态计算按钮组每个按钮的折叠动画移动距离
  149. var distance = arr[i - 1] + value * (arr[i - 1] / position[1].width)
  150. if (i != 0) {
  151. ins[i].setStyle({
  152. transform: 'translateX(' + distance + 'px)',
  153. })
  154. }
  155. }
  156. }
  157. /**
  158. * 开启状态
  159. * @param {Boolean} type
  160. * @param {Object} ins
  161. * @param {Object} ownerInstance
  162. */
  163. function openState(type, ins, ownerInstance) {
  164. var state = ins.getState()
  165. var position = state.position
  166. if (state.isopen === undefined) {
  167. state.isopen = false
  168. }
  169. // 只有状态有改变才会通知页面改变状态
  170. if (state.isopen !== type) {
  171. // 通知页面,已经打开
  172. ownerInstance.callMethod('change', {
  173. open: type
  174. })
  175. }
  176. // 设置打开和移动状态
  177. state.isopen = type
  178. // 添加动画类
  179. ins.addClass('ani');
  180. var owner = ownerInstance.selectAllComponents('.button-hock')
  181. for (var i = 0; i < owner.length; i++) {
  182. owner[i].addClass('ani');
  183. }
  184. // 设置最终移动位置
  185. move(type ? -position[1].width : 0, ins, ownerInstance)
  186. }
  187. module.exports = {
  188. sizeReady: sizeReady,
  189. touchstart: touchstart,
  190. touchmove: touchmove,
  191. touchend: touchend
  192. }