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
|
<template>
<div class="waterfall">
<WaterfallItem v-for="(item, index) in items" :key="item.id" :ref="`item-${item.id}`" :width="itemWidth">
<slot :item="item" />
</WaterfallItem>
</div>
</template>
<script>
import WaterfallItem from './WaterfallItem.vue';
const quickSort = (arr, type) => {
const left = [];
const right = [];
if (arr.length <= 1) {
return arr;
}
const povis = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i][type] < povis[type]) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left, type).concat(povis, quickSort(right, type));
};
const getMinIndex = (arr) => {
let pos = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[pos] > arr[i]) {
pos = i;
}
}
return pos;
};
const _ = {
on(el, type, func, capture = false) {
el.addEventListener(type, func, capture);
},
off(el, type, func, capture = false) {
el.removeEventListener(type, func, capture);
},
};
const sum = (arr) => arr.reduce((acc, val) => acc + val, 0);
export default {
name: 'Waterfall',
components: {
WaterfallItem,
},
props: {
gutterWidth: {
type: Number,
default: 0,
},
gutterHeight: {
type: Number,
default: 0,
},
resizable: {
type: Boolean,
default: true,
},
align: {
type: String,
default: 'center',
},
fixWidth: {
type: Number,
default: 0,
},
minCol: {
type: Number,
default: 1,
},
maxCol: {
type: Number,
default: 0,
},
percent: {
type: Array,
default: null,
},
itemWidth: {
type: Number,
default: 150,
},
items: {
type: Array,
default: () => [],
},
},
data() {
return {
timer: null,
colNum: 0,
lastWidth: 0,
percentWidthArr: [],
readyChildCount: 0,
};
},
watch: {
items() {
this.$nextTick(() => this.render('watch'));
},
},
created() {
this.$on('itemRender', () => {
if (this.timer) {
clearTimeout(this.timer);
}
this.timer = setTimeout(() => {
this.render('created');
}, 0);
});
},
mounted() {
this.resizeHandle();
this.$watch('resizable', this.resizeHandle);
},
beforeDestroy() {
this.$off('itemRender');
_.off(window, 'resize', this.render);
},
methods: {
calulate(arr) {
const pageWidth = this.fixWidth ? this.fixWidth : this.$el.offsetWidth;
// 百分比布局计算
if (this.percent) {
this.colNum = this.percent.length;
const total = sum(this.percent);
this.percentWidthArr = this.percent.map((value) => (value / total) * pageWidth);
this.lastWidth = 0;
// 正常布局计算
} else {
this.colNum = parseInt(pageWidth / (arr.width + this.gutterWidth), 10);
if (this.minCol && this.colNum < this.minCol) {
this.colNum = this.minCol;
this.lastWidth = 0;
} else if (this.maxCol && this.colNum > this.maxCol) {
this.colNum = this.maxCol;
this.lastWidth = pageWidth - (arr.width + this.gutterWidth) * this.colNum + this.gutterWidth;
} else {
this.lastWidth = pageWidth - (arr.width + this.gutterWidth) * this.colNum + this.gutterWidth;
}
}
},
resizeHandle() {
if (this.resizable) {
_.on(window, 'resize', this.render, false);
} else {
_.off(window, 'resize', this.render, false);
}
},
render(context) {
console.log(context);
if (!this.items) return;
// 重新排序
let childArr = [];
childArr = this.items.map(({ id }) => this.$refs[`item-${id}`][0].getMeta());
childArr = quickSort(childArr, 'order');
// 计算列数
this.calulate(childArr[0]);
const offsetArr = Array(this.colNum).fill(0);
// 渲染
childArr.forEach((child) => {
const position = getMinIndex(offsetArr);
// 百分比布局渲染
if (this.percent) {
let left = 0;
child.el.style.width = `${this.percentWidthArr[position]}px`;
if (position === 0) {
left = 0;
} else {
for (let i = 0; i < position; i++) {
left += this.percentWidthArr[i];
}
}
child.el.style.left = `${left}px`;
// 正常布局渲染
} else {
if (this.align === 'left') { // eslint-disable-line no-lonely-if
child.el.style.left = `${position * (child.width + this.gutterWidth)}px`;
} else if (this.align === 'right') {
child.el.style.left = `${position * (child.width + this.gutterWidth) + this.lastWidth}px`;
} else {
child.el.style.left = `${position * (child.width + this.gutterWidth) + this.lastWidth / 2}px`;
}
}
if (child.height === 0) {
return;
}
child.el.style.top = `${offsetArr[position]}px`;
offsetArr[position] += child.height + this.gutterHeight;
this.$el.style.height = `${Math.max(...offsetArr)}px`;
});
this.$emit('rendered', this);
},
},
};
</script>
<style>
.waterfall {
position: relative;
}
</style>
|