mixins.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { deepCopy } from '@/utils/lib.js'
  2. export default {
  3. data() {
  4. return {
  5. allBoundary: [],
  6. moveItemData: null,
  7. activeIndex: -1,
  8. moveTop: 0,
  9. moveLeft: 0,
  10. itemWidth: 0,
  11. itemHeight: 0,
  12. showMoveItem: false,
  13. dealyTimer: null,
  14. dealy: 50
  15. }
  16. },
  17. computed: {
  18. moveStyle() {
  19. return {
  20. left: this.moveLeft + 'px',
  21. top: this.moveTop + 'px',
  22. width: this.itemWidth + 'px',
  23. height: this.itemHeight + 'px',
  24. visibility: this.showMoveItem ? 'unset' : 'hidden'
  25. }
  26. }
  27. },
  28. watch: {
  29. sortList: {
  30. handler() {
  31. this.moveItemData = this.sortList[0]
  32. this.$nextTick(function() {
  33. this.getAllBoundary()
  34. })
  35. },
  36. deep: true,
  37. immediate: true
  38. }
  39. },
  40. methods: {
  41. getAllBoundary() {
  42. const that = this
  43. uni.createSelectorQuery()
  44. .in(this)
  45. .selectAll('.wk-drag-sort-item')
  46. .boundingClientRect(data => {
  47. console.log('getAllBoundary: ', data)
  48. that.allBoundary = data.map(item => {
  49. return {
  50. ...item,
  51. beginX: item.left,
  52. endX: item.left + item.width,
  53. beginY: item.top,
  54. endY: item.top + item.height,
  55. }
  56. })
  57. })
  58. .exec();
  59. },
  60. handleTouchstart(evt, index) {
  61. if (this.dealyTimer) {
  62. clearTimeout(this.dealyTimer)
  63. this.dealyTimer = null
  64. }
  65. this.dealyTimer = setTimeout(() => {
  66. const query = uni.createSelectorQuery().in(this);
  67. query.select(`#wk-drag-sort-item-${index}`).boundingClientRect(data => {
  68. this.moveTop = data.top
  69. this.moveLeft = data.left
  70. this.moveItemData = this.sortList[index]
  71. this.activeIndex = index
  72. this.itemWidth = data.width
  73. this.itemHeight = data.height
  74. this.showMoveItem = true; // 悬浮开始
  75. console.log('start move')
  76. clearTimeout(this.dealyTimer)
  77. this.dealyTimer = null
  78. }).exec();
  79. }, this.dealy)
  80. },
  81. handleTouchmove(evt, index) {
  82. const touch = evt.touches[0];
  83. this.moveLeft = touch.clientX - this.itemWidth / 2
  84. this.moveTop = touch.clientY - this.itemHeight / 2
  85. },
  86. handleTouchend(evt, index) {
  87. if (this.dealyTimer) {
  88. clearTimeout(this.dealyTimer)
  89. this.dealyTimer = null
  90. }
  91. const overIndex = this.findOverIndex()
  92. if (overIndex !== -1) {
  93. const arr = deepCopy(this.sortList)
  94. arr[overIndex] = arr.splice(this.activeIndex, 1, arr[overIndex])[0]
  95. this.handleSort(arr)
  96. }
  97. this.clearMoveItem()
  98. },
  99. /**
  100. * 悬浮结束
  101. */
  102. clearMoveItem() {
  103. this.showMoveItem = false
  104. this.activeIndex = -1
  105. this.moveLeft = 0
  106. this.moveTop = 0
  107. },
  108. // 找到停下的元素的下标
  109. findOverIndex() {
  110. for (let i = 0; i < this.allBoundary.length; i++) {
  111. const item = this.allBoundary[i]
  112. if (
  113. this.moveLeft > item.beginX &&
  114. this.moveLeft < item.endX &&
  115. this.moveTop > item.beginY &&
  116. this.moveTop < item.endY
  117. ) {
  118. return i
  119. }
  120. }
  121. return -1
  122. }
  123. }
  124. }