dengjunjie
5 天以前 4f39dcc195f28fa275fc2d065fbf1bf6a46c21b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<template>
  <view
    class="tn-drag-class tn-drag"
    :style="{
      height: wrapHeight + 'rpx'
    }"
    :list="listData"
    :basedata="baseData"
    :change:list="wxs.listObserver"
    :change:basedata="wxs.baseDataObserver"
  >
    <!-- #ifdef MP-WEIXIN -->
    <view
      v-for="(item, index) in listData"
      :key="item.id"
      class="tn-drag__item"
      :style="{
        width: 100 / columns + '%',
        height: itemHeight + 'rpx'
      }"
      :data-index="index"
      :data-basedata="baseData"
      :data-edit="edit"
      @longpress="wxs.longPress"
      @touchstart="wxs.touchStart"
      :catch:touchmove="dragging?wxs.touchMove:''"
      :catch:touchend="dragging?wxs.touchEnd:''"
    >
      <slot :entity="item.data" :fixed="item.fixed" :index="index" :height="itemHeight" :isEdit="edit"></slot>
    </view>
    <!-- #endif -->
 
    <!-- #ifndef MP-WEIXIN -->
    <view
      v-for="(item, index) in listData"
      :key="item.id"
      class="tn-drag__item"
      :style="{
        width: 100 / columns + '%',
        height: itemHeight + 'rpx' 
      }"
      @longpress="wxs.longPress"
      :data-index="index"
      :data-basedata="baseData"
      :data-edit="edit"
      @touchstart="wxs.touchStart"
      @touchmove="wxs.touchMove"
      @touchend="wxs.touchEnd"
    >
      <slot :entity="item.data" :fixed="item.fixed" :index="index" :height="itemHeight" :isEdit="edit"></slot>
    </view>
    <!-- #endif -->
  </view>
</template>
<script src="./index.wxs" lang="wxs" module="wxs"></script>
<script>
  export default {
    name: 'tn-drag',
    props: {
      // 数据源
      // 如果属性中包含fixed,则标识当前数据不允许拖动
      list: {
        type: Array,
        default () {
          return []
        }
      },
      // 是否允许拖动编辑
      edit: {
        type: Boolean,
        default: true
      },
      // 列数
      columns: {
        type: Number,
        default: 3
      },
      // item元素高度, 单位rpx
      itemHeight: {
        type: Number,
        default: 0
      },
      // 当前父元素滚动的高度
      scrollTop: {
        type: Number,
        default: 0
      }
    },
    computed: {
      wrapHeight() {
        return this.rows * this.itemHeight
      }
    },
    data() {
      return {
        // 未渲染前节点数据
        baseData: {},
        // 拖动后的数据
        dragData: [],
        // 行数
        rows: 0,
        // 渲染数据
        listData: [],
        // 标记是否正在拖动
        dragging: false
      }
    },
    watch: {
      list(val) {
        this.listData = []
        this.$nextTick(() => {
          this.init()
        })
      },
      columns(val) {
        this.listData = []
        this.$nextTick(() => {
          this.init()
        })
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.init()
      })
    },
    methods: {
      // 初始化
      init() {
        this.dragging = true
        const initDragItem = item => {
          const obj = {
            ...item
          }
          const fixed = obj?.fixed || false
          delete obj["fixed"]
          return {
            id: this.unique(),
            fixed,
            data: {
              ...obj
            }
          }
        }
        
        let i = 0
        const listData = (this.list || []).map((item, index) => {
          let listItem = initDragItem(item)
          // 真实排序
          listItem.realKey = i++
          // 整体排序
          listItem.sortKey = index
          listItem.translateX = `${(listItem.sortKey % this.columns) * 100}%`
          listItem.translateY = `${Math.floor(listItem.sortKey / this.columns) * 100}%`
          return listItem
        })
        this.rows = Math.ceil(listData.length / this.columns)
        this.listData = listData
        this.dragData = listData
        
        if (listData.length === 0) return
        // console.log(listData);
        
        // 初始化dom元素
        this.$nextTick(() => {
          this.initRect()
        })
      },
      // 初始化dom元素
      initRect() {
        const {
          windowWidth,
          windowHeight
        } = uni.getSystemInfoSync()
        
        let baseData = {}
        baseData.windowHeight = windowHeight
        baseData.realTopSize = 0
        baseData.realBottomSize = 0
        baseData.columns = this.columns
        baseData.rows = this.rows
        
        const query = uni.createSelectorQuery().in(this)
        query.select('.tn-drag').boundingClientRect()
        query.select('.tn-drag__item').boundingClientRect()
        query.exec(res => {
          if (!res) {
            setTimeout(() => {
              this.initRect()
            }, 10)
            return
          }
          
          baseData.itemWidth = res[1].width
          baseData.itemHeight = res[1].height
          baseData.left = res[0].left
          baseData.top = res[0].top + this.scrollTop
          this.dragging = false
          this.baseData = baseData
        })
        
      },
      
      // 触发震动
      vibrate() {
        uni.vibrateShort()
      },
      // 滚动到指定的位置
      pageScroll(e) {
        uni.pageScrollTo({
          scrollTop: e.scrollTop,
          duration: 0
        })
      },
      // 修改拖动状态
      drag(e) {
        this.dragging = e.dragging
      },
      // 拖拽数据发生改变
      listDataChange(e) {
        this.dragData = e.data
      },
      // item被点击
      itemClick(index) {
        const item = this.dragData[index]
        this.$emit('click', {
          key: item.realKey,
          data: item.data
        })
      },
      
      // 拖拽结束事件
      sortEnd(e) {
        this.$emit('end', {
          data: e.data
        })
      },
      // 排序发生改变事件
      change(e) {
        this.$emit('change', {
          data: e.data
        })
      },
      
      // 生成元素唯一id
      unique(n = 6) {
        let id = ''
        for (let i = 0; i < n; i++) id += Math.floor(Math.random() * 10)
        return 'tn_' + new Date().getTime() + id
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  .tn-drag {
    position: relative;
    
    &__item {
      position: absolute;
      z-index: 2;
      top: 0;
      left: 0;
    }
    
    &__transition {
      transition: transform 0.25s !important;
    }
    
    &__current {
      z-index: 10 !important;
    }
    
    &__fixed {
      z-index: 1 !important;
    }
  }
</style>