zhangwei
2024-08-14 9d8994790fe403935ed46ad478f83ae110bb7a01
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:1 6 3,营业执照号:9  1 5    10 131   33   2  006 193K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <!-- #ifndef APP-VUE || MP-WEIXIN || H5  -->
    <view class="fui-drag__wrap" ref="fui_drag" :id="elId"
        :style="{width:wrapWidth+'px',height:(rows * cellHeight)+'px'}"
        :class="[dragging?'fui-drag-item__show':'fui-drag-item__hidden']">
    <!-- #endif -->
        <!-- #ifdef APP-VUE || MP-WEIXIN || H5 -->
        <!-- vue3下编译到小程序data数据与wxs混用中无法获取 $data数据无法直接应用到页面,uni-app官方编译bug-->
        <view class="fui-drag__wrap" :catch:touchmove="wxDrag?true:''"
            :class="[dragging?'fui-drag-item__show':'fui-drag-item__hidden']" ref="fui_drag" :id="elId"
            :style="{width:wrapWidth+'px',height:(rows * cellHeight)+'px'}" :list="list"
            :change:list="handler.listChange" :param="param" :change:param="handler.paramChange">
            <view class="fui-drag-item__wrap" :style="{width:cellWidth+'px',height:cellHeight+'px'}"
                v-for="(item, index) in list" :key="item.id" :data-index="index" @longpress="handler.longPress"
                :data-param="param" :data-isdrag="isDrag && dragging?1:0" :data-app="isApp" @touchstart="handler.touchStart"
                @touchmove="handler.touchMove" @touchend="handler.touchEnd" @mousedown="handler.mousedown"
                @tap="itemClick(index)">
                <template v-if="custom">
                    <slot :model="item.entity" :width="cellWidth" :height="cellHeight" :index="index"></slot>
                </template>
                <template v-else>
                    <f-drag-item @delete="deleteItem" :src="(item.entity && item.entity[srcKey]) || 'true'"
                        :text="item.entity && item.entity[textKey]" :width="cellWidth" :height="cellHeight" :isMenu="isMenu"
                        :styles="styles" :index="index">
                    </f-drag-item>
                </template>
            </view>
            <!-- #endif -->
 
            <!-- #ifdef APP-NVUE -->
            <view fireEventSync="true" class="fui-drag-item__wrap" v-for="(item, index) in list" :key="item.id"
                :style="{width:cellWidth+'px',height:cellHeight+'px'}" @touchstart="touchstart" @touchmove="touchmove"
                @touchend="touchend" @longpress="longPress" :data-index="index" :ref="'f_drag_'+index">
                <template v-if="custom">
                    <slot :model="item.entity" :width="cellWidth" :height="cellHeight" :index="index"></slot>
                </template>
                <template v-else>
                    <f-drag-item @delete="deleteItem" :src="item.entity && item.entity[srcKey]"
                        :text="item.entity && item.entity[textKey]" :width="cellWidth" :height="cellHeight" :isMenu="isMenu"
                        :styles="styles" :index="index">
                    </f-drag-item>
                </template>
            </view>
            <!-- #endif -->
 
            <!-- #ifndef APP-PLUS || MP-WEIXIN || H5-->
            <view class="fui-drag-item__wrap"
                :class="{'fui-drag__cur':current===index,'fui-drag__trans':current!==index}"
                v-for="(item, index) in list" :key="item.id"
                :style="{width:cellWidth+'px',height:cellHeight+'px',transform: `translate3d(${index === current ? transX : item.transX}, ${index === current ? transY: item.transY}, 0px)`}"
                @touchstart="touchstart" @touchmove.stop.prevent="touchmove" @touchend="touchend" @longpress="longPress"
                :data-index="index">
                <template v-if="custom">
                    <slot :model="item.entity" :width="cellWidth" :height="cellHeight" :index="index"></slot>
                </template>
                <template v-else>
                    <f-drag-item @delete="deleteItem" :src="(item.entity && item.entity[srcKey]) || ''"
                        :text="item.entity && item.entity[textKey]" :width="cellWidth" :height="cellHeight" :styles="styles"
                        :isMenu="isMenu" :index="index">
                    </f-drag-item>
                </template>
            </view>
            <!-- #endif -->
        </view>
</template>
<!-- #ifdef APP-VUE || MP-WEIXIN || H5-->
<script src="./index.wxs" module="handler" lang="wxs"></script>
<!-- #endif -->
 
<script>
    // #ifdef APP-NVUE
    const dom = uni.requireNativePlugin('dom');
    // #endif
    import fDragItem from './f-drag-item.vue'
    import bindingx from './bindingx.js'
    import mpjs from './mpjs.js'
    export default {
        name: "fui-drag",
        mixins: [mpjs, bindingx],
        emits: ['change', 'end', 'click', 'delete'],
        components: {
            fDragItem
        },
        props: {
            itemList: {
                type: Array,
                default () {
                    return [];
                }
            },
            textKey: {
                type: String,
                default: 'text'
            },
            srcKey: {
                type: String,
                default: 'src'
            },
            width: {
                type: [Number, String],
                default: 0
            },
            // 每行显示个数
            columns: {
                type: [Number, String],
                default: 1
            },
            //必传(square为true时失效)
            itemHeight: {
                type: [Number, String],
                default: 0
            },
            // 是否正方形显示,为true时itemHeight失效
            square: {
                type: Boolean,
                default: false
            },
            styles: {
                type: Object,
                default () {
                    return {}
                }
            },
            isMenu: {
                type: Boolean,
                default: true
            },
            // 页面滚动高度
            scrollTop: {
                type: Number,
                default: 0
            },
            //是否可拖拽
            isDrag: {
                type: Boolean,
                default: true
            },
            custom: {
                type: Boolean,
                default: false
            }
        },
        watch: {
            itemList(val) {
                this.list = []
                setTimeout(() => {
                    this.reset()
                }, 0);
            },
            width(val) {
                this.list = []
                setTimeout(() => {
                    this.reset()
                }, 0);
            },
            columns(val) {
                this.list = []
                setTimeout(() => {
                    this.reset()
                }, 0);
            }
        },
        data() {
            const elId = `fui_${Math.ceil(Math.random() * 10e5).toString(36)}`
            let isApp = 0;
            // #ifdef APP
            isApp = 1;
            // #endif
            return {
                isApp,
                elId,
                // #ifdef APP-VUE || MP-WEIXIN || H5
                param: {},
                // #endif
                windowHeight: 0,
                wrapWidth: 0,
                wrapHeight: 0,
                cellWidth: 0,
                cellHeight: 0,
                changeList: [],
                rows: 3,
                options: [],
                dragging: true,
                wrapTop: 0,
                wrapLeft: 0,
                list: [],
                wxDrag: true
            };
        },
        mounted() {
            this.$nextTick(() => {
                this.reset();
            })
        },
        methods: {
            getId() {
                return `${Math.ceil(Math.random() * 10e5).toString(36)}_${new Date().getTime()}`
            },
            getWidth(fn) {
                let width = Number(this.width)
                let sys = uni.getSystemInfoSync()
                this.windowHeight = sys.windowHeight;
                if (width === 0) {
                    width = sys.windowWidth
                } else {
                    width = uni.upx2px(width)
                }
                this.wrapWidth = width
                //此处小数会导致出现误差
                this.cellWidth = width / Number(this.columns)
                let cellHeight = uni.upx2px(this.itemHeight)
                if (this.square) {
                    cellHeight = this.cellWidth
                }
                this.cellHeight = cellHeight
                fn && fn()
            },
            reset() {
                this.options = []
                // #ifndef APP-NVUE
                this.wxDrag = true;
                this.dragging = false
                // #endif
                this.getWidth(() => {
                    setTimeout(() => {
                        this.init()
                    }, 0);
                })
            },
            getDom(callback, index = 0) {
                // #ifndef APP-NVUE
                uni.createSelectorQuery()
                    // #ifndef MP-ALIPAY
                    .in(this)
                    // #endif
                    .select(`#${this.elId}`)
                    .boundingClientRect()
                    .exec(ret => {
                        if (index >= 10) return
                        if (!ret && !ret[0]) {
                            index++
                            this.getDom(callback, index)
                            return
                        }
                        let wrapTop = ret[0].top + this.scrollTop;
                        let wrapLeft = ret[0].left
                        callback && callback(wrapTop, wrapLeft)
                    })
                // #endif
                // #ifdef APP-NVUE
                dom.getComponentRect(this.$refs['fui_drag'], (ret) => {
                    if (ret && ret.size) {
                        let wrapTop = ret.size.top + this.scrollTop;
                        let wrapLeft = ret.size.left
                        callback && callback(wrapTop, wrapLeft)
                    }
                })
                // #endif
            },
            init(index = 0) {
                let options = JSON.parse(JSON.stringify(this.itemList));
                let columns = Number(this.columns)
                this.rows = Math.ceil(options.length / this.columns);
                if (options.length === 0) return;
                setTimeout(() => {
                    this.getDom((wrapTop, wrapLeft) => {
                        let list = options.map((item, index) => {
                            return {
                                id: this.getId(),
                                realKey: index,
                                sortKey: index,
                                //若有误差,cellWidth、cellHeight取整
                                transX: `${index%columns * this.cellWidth}px`,
                                transY: `${Math.floor(index/columns) * this.cellHeight}px`,
                                entity: item
                            }
                        });
                        this.changeList = list;
                        this.list = list
                        this.wrapTop = wrapTop;
                        this.wrapLeft = wrapLeft;
                        // #ifdef APP-VUE || MP-WEIXIN || H5
                        this.param = {
                            windowHeight: this.windowHeight,
                            columns: Number(this.columns),
                            rows: this.rows,
                            cellWidth: this.cellWidth,
                            cellHeight: this.cellHeight,
                            wrapLeft: this.wrapLeft,
                            wrapTop: this.wrapTop
                        };
                        // #endif
                        this.dragging = true
                    })
                }, 500)
            },
            drag(e) {
                this.wxDrag = e.wxdrag;
            },
            sortend(e) {
                this.$emit('end', {
                    itemList: e.itemList
                });
            },
            change(e) {
                this.$emit('change', {
                    itemList: e.itemList
                });
            },
            itemClick(index) {
                if (!this.changeList || this.changeList.length === 0) return;
                let item = this.changeList[index];
                this.$emit('click', {
                    index: item.realKey,
                    item: item.entity
                });
            },
            deleteItem(e) {
                this.$emit('delete', {
                    index: e.index
                });
            },
            listChange(e) {
                this.changeList = e.itemList;
            },
            pageScroll(e) {
                // #ifndef APP-NVUE
                uni.pageScrollTo({
                    scrollTop: e.scrollTop,
                    duration: 0
                });
                // #endif
            }
        }
    }
</script>
 
<style scoped>
    .fui-drag__wrap {
        /* #ifndef APP-NVUE */
        width: 100%;
        box-sizing: border-box;
        transform: translate3d(0, 0, 0);
        /* #endif */
        position: relative;
    }
 
    .fui-drag-item__wrap {
        position: absolute;
        left: 0;
        top: 0;
        /* #ifndef APP-NVUE */
        display: flex;
        box-sizing: border-box;
        overflow: hidden;
        /* #endif */
        flex-direction: column;
        align-items: center;
        justify-content: center;
        z-index: 1;
        /* #ifdef H5 */
        cursor: grab;
        /* #endif */
    }
 
    .fui-drag-item__hidden {
        opacity: 0;
        transition-property: opacity;
        transition-duration: .35s;
    }
 
    .fui-drag-item__show {
        opacity: 1 !important;
    }
 
    .fui-drag__trans {
        transition-property: transform;
        transition-duration: .35s;
    }
 
    .fui-drag__trans-end {
        transition-property: transform;
        transition-duration: .35s;
    }
 
    .fui-drag__cur {
        z-index: 5 !important;
    }
</style>