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
<template>
    <!--本文件由FirstUI授权予四川政采招投标咨询有限公司(会员ID:16  3,营业执照号:  91510    13133 2  00 6      193 K)专用,请尊重知识产权,勿私下传播,违者追究法律责任。-->
    <view class="fui-waterfall__wrap" :style="{height:height+'px'}">
        <slot></slot>
    </view>
</template>
 
<script>
    export default {
        name: "fui-waterfall",
        emits: ['init', 'end'],
        props: {
            columnGap: {
                type: [Number, String],
                default: 24
            },
            topGap: {
                type: [Number, String],
                default: 24
            },
            leftGap: {
                type: [Number, String],
                default: 0
            },
            rightGap: {
                type: [Number, String],
                default: 0
            },
            isBefore: {
                type: Boolean,
                default: false
            }
        },
        provide() {
            return {
                waterfall: this
            }
        },
        data() {
            return {
                height: 0,
                itemWidth: 0,
                leftHeight: 0,
                rightHeight: 0,
                tGap: 0,
                lGap: 0,
                x2: 0
            };
        },
        created() {
            this.childrenArr = []
            this.loadedArr = []
            this.loadedChild = []
            this.initParam()
        },
        // #ifndef VUE3
        beforeDestroy() {
            this.childrenArr = null
            this.loadedArr = null
            this.loadedChild = null
        },
        // #endif
        // #ifdef VUE3
        beforeUnmount() {
            this.childrenArr = null
            this.loadedArr = null
            this.loadedChild = null
        },
        // #endif
        methods: {
            getPx(value) {
                let val = parseInt(uni.upx2px(Number(value)))
                return val % 2 === 0 ? val : val + 1
            },
            initParam(callback) {
                this.tGap = this.getPx(this.topGap)
                this.lGap = this.getPx(this.leftGap)
                const colGap = this.getPx(this.columnGap)
                const rGap = this.getPx(this.rightGap)
                const sys = uni.getSystemInfoSync()
                const gap = colGap + this.lGap + rGap;
                this.itemWidth = (sys.windowWidth - gap) / 2
                this.x2 = this.lGap + this.itemWidth + colGap
                callback && callback(this.itemWidth)
                this.$emit('init', {
                    itemWidth: this.itemWidth
                })
            },
            //重置加载
            resetLoadmore() {
                this.leftHeight = 0
                this.rightHeight = 0
                this.height = 0
                this.childrenArr = []
                this.loadedArr = []
                this.loadedChild = []
            },
            getWaterfallInfo(itemHeight, callback) {
                if (!itemHeight) return;
                let x = this.lGap;
                let y = 0;
                let itemGap = 0;
                if (this.leftHeight <= this.rightHeight) {
                    y = this.leftHeight;
 
                    if (this.leftHeight == 0) {
                        this.leftHeight += itemHeight;
                    } else {
                        itemGap = this.tGap;
                        y += this.tGap;
                        this.leftHeight += itemHeight + this.tGap;
                    }
                } else {
                    x = this.x2;
                    y = this.rightHeight;
 
                    if (this.rightHeight == 0) {
                        this.rightHeight += itemHeight;
                    } else {
                        itemGap = this.tGap;
                        y += this.tGap;
                        this.rightHeight += itemHeight + this.tGap;
                    }
                }
                callback && callback({
                    x,
                    y,
                    itemGap
                })
            },
            setWaterfallHeight(itemGap) {
                this.height = Math.ceil(Math.max(this.leftHeight, this.rightHeight) + itemGap);
            },
            startSorting() {
                let clen = this.childrenArr.length
                let llen = this.loadedArr.length
                if (clen == llen && llen > 0) {
                    let itemGap = 0
                    if (this.isBefore) {
                        this.leftHeight = 0
                        this.rightHeight = 0
                        this.loadedChild = this.childrenArr.concat(this.loadedChild)
                    } else {
                        this.loadedChild = this.loadedChild.concat(this.childrenArr)
                    }
                    const usageData = this.isBefore ? this.loadedChild : this.childrenArr;
                    usageData.forEach((item, index) => {
                        this.getWaterfallInfo(item.height, (res) => {
                            itemGap = res.itemGap
                            // #ifdef APP-NVUE
                            item.aniTransForm(res.x, res.y)
                            item.transform = `translate(${res.x}px,${res.y}px)`
                            // #endif
                            // #ifndef APP-NVUE
                            item.transform = `translate3d(${res.x}px,${res.y}px,0)`
                            // #endif
                            setTimeout(() => {
                                item.isShow = true
                            }, 20)
                        })
                    })
                    this.setWaterfallHeight(itemGap)
                    this.childrenArr = []
                    this.loadedArr = []
                    this.$emit('end', {})
                }
            }
        }
    }
</script>
 
<style scoped>
    .fui-waterfall__wrap {
        /* #ifndef APP-NVUE */
        width: 100%;
        box-sizing: border-box;
        /* #endif */
        position: relative;
        /* #ifdef APP-NVUE */
        flex: 1;
        /* #endif */
    }
</style>