/
cl-waterfall.vue
209 lines (178 loc) · 3.84 KB
/
cl-waterfall.vue
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
<template>
<view
class="cl-waterfall"
:style="{
columnCount,
columnGap,
}"
>
<!-- 纵向 -->
<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></slot>
</template>
</view>
</template>
<script>
import { isEmpty } from "../../utils";
/**
* waterfall 瀑布流
* @description 瀑布流布局,自定义内容
* @tutorial https://docs.cool-js.com/uni/components/layout/waterfall.html
* @property {Array} value 数据列表
* @property {Number} column 列的数量,默认2
* @property {Number} gutter 列间隔,默认20
* @property {String} direction 布局方向 horizontal | vertical,默认horizontal
* @property {String} nodeKey 匹配值,默认 id
* @example 见教程
*/
export default {
name: "cl-waterfall",
componentName: "ClWaterfall",
props: {
value: Array,
column: {
type: Number,
default: 2,
},
gutter: {
type: Number,
default: 20,
},
direction: {
type: String,
default: "horizontal", // horizontal, vertical
},
nodeKey: {
type: String,
default: "id",
},
},
data() {
return {
list: [],
};
},
watch: {
list: {
handler(val) {
this.$emit("input", val);
},
},
},
computed: {
columnCount() {
return this.direction === "vertical" ? this.column : "none";
},
columnGap() {
return this.direction === "vertical" ? `${this.gutter}rpx` : "none";
},
},
mounted() {
this.refresh(this.value);
},
methods: {
// 刷新列表
refresh(list) {
// 清空列表,清空 cl-waterfall-column 的组件遍历
this.clear();
this.$nextTick(() => {
switch (this.direction) {
case "horizontal":
this.list = new Array(this.column).fill(1).map(() => []);
// 等待 cl-waterfall-column 渲染完成后追加数据
this.$nextTick(() => {
this.append(list);
});
break;
case "vertical":
this.list = list;
break;
}
});
},
// 计算高度,一个个往列表追加
async append(list) {
for (let i in list) {
const next = async () => {
const rects = await this.getRect();
// 获取 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;
}
this.list[index].push(list[i]);
return true;
});
};
await next();
}
},
// 更新单条数据,根据nodeKey来匹配
update(id, data) {
const next = (e) => {
let d = e[this.nodeKey] === id;
if (d) {
Object.assign(e, data);
}
return Boolean(d);
};
switch (this.direction) {
case "horizontal":
this.list.find((col) => {
return col.find(next);
});
break;
case "vertical":
this.list.find(next);
break;
}
},
// 清空列表
clear() {
this.list = [];
},
// 获取列
getRect() {
// #ifdef MP || H5
return new Promise((resolve) => {
let timer = null;
const fn = () => {
// #ifdef H5
let children = this.$children[0].$children;
// #endif
// #ifdef MP || APP
let children = this.$children;
// #endif
if (isEmpty(children)) {
timer = setTimeout(() => {
fn();
}, 50);
} else {
clearTimeout(timer);
resolve(children.map((e) => e.getRect()));
}
};
fn();
});
// #endif
// #ifdef APP
return new Promise((resolve) => {
uni.createSelectorQuery()
.selectAll(`.cl-waterfall-column`)
.boundingClientRect(resolve)
.exec();
});
// #endif
},
},
};
</script>