index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * touch事件判断方式
  3. * https://github.com/madrobby/zepto/blob/master/src/touch.js#files
  4. */
  5. function swipeDirection(x1, x2, y1, y2) {
  6. return Math.abs(x1 - x2) >=
  7. Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'Left' : 'Right') : (y1 - y2 > 0 ? 'Up' : 'Down')
  8. }
  9. Component({
  10. externalClasses: ['i-class'],
  11. properties: {
  12. actions: {
  13. value: [],
  14. type: Array,
  15. observer : '_updateButtonSize'
  16. },
  17. unclosable : {
  18. value : false,
  19. type : Boolean
  20. },
  21. toggle : {
  22. value : false,
  23. type : Boolean,
  24. observer : 'closeButtonGroup'
  25. },
  26. operateWidth : {
  27. type : Number,
  28. value : 160
  29. }
  30. },
  31. options: {
  32. // 在组件定义时的选项中启用多slot支持
  33. multipleSlots: true
  34. },
  35. data : {
  36. //touch start position
  37. tStart : {
  38. pageX : 0,
  39. pageY : 0
  40. },
  41. //限制滑动距离
  42. limitMove : 0,
  43. //element move position
  44. position : {
  45. pageX : 0,
  46. pageY : 0
  47. }
  48. },
  49. methods : {
  50. //阻止事件冒泡
  51. loop(){},
  52. _updateButtonSize(){
  53. const actions = this.data.actions;
  54. if( actions.length > 0 ){
  55. const query = wx.createSelectorQuery().in(this);
  56. let limitMovePosition = 0;
  57. actions.forEach(item => {
  58. limitMovePosition += item.width || 0;
  59. });
  60. this.data.limitMove = limitMovePosition;
  61. /*
  62. * 动态获取每个传进值的按钮尺寸不能正确获取,在安卓上少了6px
  63. * 暂时实现需要在actions里面传递宽度
  64. * 需要后期调研
  65. */
  66. //query.selectAll('.i-swipeout-button-right-item').boundingClientRect((rects)=>{
  67. // if( rects ){
  68. // console.log(rects,1111111)
  69. // rects.forEach(item => {
  70. // limitMovePosition += item.width;
  71. // });
  72. // this.data.limitMove = limitMovePosition;
  73. // console.log(limitMovePosition,111111111)
  74. // }
  75. // }).exec()
  76. }else{
  77. this.data.limitMove = this.data.operateWidth;
  78. }
  79. },
  80. handlerTouchstart(event){
  81. const touches = event.touches ? event.touches[0] : {};
  82. const tStart = this.data.tStart;
  83. if( touches ){
  84. for( let i in tStart ){
  85. if( touches[i] ){
  86. tStart[i] = touches[i];
  87. }
  88. }
  89. }
  90. },
  91. swipper(touches){
  92. const data = this.data;
  93. const start = data.tStart;
  94. const spacing = {
  95. pageX : touches.pageX - start.pageX,
  96. pageY : touches.pageY - start.pageY
  97. }
  98. if( data.limitMove < Math.abs( spacing.pageX ) ){
  99. spacing.pageX = -data.limitMove;
  100. }
  101. this.setData({
  102. 'position' : spacing
  103. })
  104. },
  105. handlerTouchmove(event){
  106. const start = this.data.tStart;
  107. const touches = event.touches ? event.touches[0] : {};
  108. if( touches ){
  109. const direction = swipeDirection( start.pageX,touches.pageX,start.pageY,touches.pageY );
  110. if( direction === 'Left' ){
  111. this.swipper( touches );
  112. }
  113. }
  114. },
  115. handlerTouchend(event){
  116. const start = this.data.tStart;
  117. const touches = event.changedTouches ? event.changedTouches[0] : {};
  118. if( touches ){
  119. const direction = swipeDirection( start.pageX,touches.pageX,start.pageY,touches.pageY );
  120. const spacing = {
  121. pageX : touches.pageX - start.pageX,
  122. pageY : touches.pageY - start.pageY
  123. }
  124. if( Math.abs( spacing.pageX ) >= 40 && direction === "Left" ){
  125. spacing.pageX = spacing.pageX < 0 ? - this.data.limitMove : this.data.limitMove;
  126. }else{
  127. spacing.pageX = 0;
  128. }
  129. this.setData({
  130. 'position' : spacing
  131. })
  132. }
  133. },
  134. handlerButton(event){
  135. if( !this.data.unclosable ){
  136. this.closeButtonGroup();
  137. }
  138. const dataset = event.currentTarget.dataset;
  139. this.triggerEvent('change',{
  140. index : dataset.index
  141. })
  142. },
  143. closeButtonGroup(){
  144. this.setData({
  145. 'position' : {pageX : 0,pageY : 0}
  146. })
  147. },
  148. //控制自定义组件
  149. handlerParentButton(event){
  150. if( !this.data.unclosable ){
  151. this.closeButtonGroup();
  152. }
  153. }
  154. },
  155. ready(){
  156. this._updateButtonSize();
  157. }
  158. });