wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
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
<template>
    <view class="cl-waterfall" v-if="visible">
        <!-- 纵向 -->
        <template v-if="direction === 'vertical'">
            <view class="cl-waterfall__item" v-for="(item, index) in list" :key="index">
                <slot :item="item" :index="index"></slot>
            </view>
        </template>
 
        <!-- 横向 -->
        <template v-else-if="direction === 'horizontal'">
            <slot :list="list"></slot>
        </template>
 
        <!-- 空态 -->
        <slot name="empty" v-if="showEmpty">
            <cl-empty />
        </slot>
    </view>
</template>
 
<script lang="ts">
import { type PropType, computed, defineComponent, ref, nextTick, getCurrentInstance } from "vue";
import { flatMap, isEmpty } from "lodash-es";
import { parseRpx } from "/@/cool/utils";
import { onShow } from "@dcloudio/uni-app";
 
export default defineComponent({
    name: "cl-waterfall",
 
    props: {
        // 列的数量
        column: {
            type: Number,
            default: 2,
        },
        // 列间隔
        gutter: {
            type: Number,
            default: 20,
        },
        // 布局方向
        direction: {
            type: String as PropType<"horizontal" | "vertical">,
            default: "horizontal",
        },
        // 匹配值
        nodeKey: {
            type: String,
            default: "id",
        },
    },
 
    setup(props) {
        const { proxy }: any = getCurrentInstance();
 
        const visible = ref(true);
 
        // 列表
        const list = ref<any[]>([]);
 
        // 每次追加的数据
        let data: any[] = [];
 
        // 列数量
        const columnCount = computed(() => (props.direction == "vertical" ? props.column : "none"));
 
        // 列间距
        const columnGap = computed(() =>
            props.direction == "vertical" ? parseRpx(props.gutter) : "none",
        );
 
        // 是否空
        const showEmpty = computed(() => {
            return list.value.filter((e) => !isEmpty(e)).length == 0;
        });
 
        // 刷新列表
        function refresh(data: any[]) {
            visible.value = false;
 
            nextTick(() => {
                visible.value = true;
 
                switch (props.direction) {
                    case "horizontal":
                        list.value = new Array(props.column).fill(1).map(() => []);
 
                        // 等待 cl-waterfall-column 渲染完成后追加数据
                        nextTick(() => {
                            append(data);
                        });
                        break;
                    case "vertical":
                        list.value = data;
                        break;
                }
            });
        }
 
        // 计算高度,一个个往列表追加
        async function append(arr: any[]) {
            data = arr;
 
            for (let i = 0; i < arr.length; i++) {
                const next = async () => {
                    const rects = await getRect();
 
                    // 获取不到的时候阻断掉
                    if (isEmpty(rects) && i !== 0) {
                        return false;
                    }
 
                    // 获取 cl-waterfall-column 的高度,比较后在最小的列中塞入
                    return Promise.all(rects).then((res) => {
                        let colsHeight = res.map((e) => e.height);
 
                        let minH = Math.min(...colsHeight);
                        let index = colsHeight.indexOf(minH);
 
                        if (index < 0) {
                            index = 0;
                        }
 
                        list.value[index]?.push(arr[i]);
 
                        return true;
                    });
                };
 
                await next();
            }
        }
 
        // 更新单条数据,根据nodeKey来匹配
        function update(id: string | number, data: any) {
            const next = (e: any) => {
                const d = e[props.nodeKey] === id;
 
                if (d) {
                    Object.assign(e, data);
                }
 
                return Boolean(d);
            };
 
            switch (props.direction) {
                case "horizontal":
                    list.value.find((col) => {
                        return col.find(next);
                    });
                    break;
                case "vertical":
                    list.value.find(next);
                    break;
            }
        }
 
        // 清空列表
        function clear() {
            list.value = [];
        }
 
        // 获取列
        function getRect(): Promise<any> {
            return new Promise((resolve) => {
                // #ifdef MP
                let timer: any = null;
 
                function fn() {
                    const children: any[] = proxy.$children;
 
                    if (isEmpty(children)) {
                        timer = setTimeout(() => {
                            fn();
                        }, 50);
                    } else {
                        clearTimeout(timer);
                        const arr = children.filter((e) => e.getRect).map((e) => e.getRect());
                        resolve(arr);
                    }
                }
 
                fn();
                // #endif
 
                // #ifndef MP
                nextTick(() => {
                    uni.createSelectorQuery()
                        .in(proxy.$root)
                        .selectAll(`.cl-waterfall-column`)
                        .boundingClientRect(resolve)
                        .exec();
                });
                // #endif
            });
        }
 
        // 重新布局,在加载中切换页面导致计算错误,返回页面时 onShow 重新计算
        function doLayout() {
            // 列空的时候 重新追加数据
            if (isEmpty(flatMap(list.value)) && !isEmpty(data)) {
                append(data);
            }
        }
 
        onShow(() => {
            doLayout();
        });
 
        return {
            visible,
            list,
            columnCount,
            columnGap,
            refresh,
            append,
            update,
            clear,
            showEmpty,
            doLayout,
        };
    },
});
</script>