-
Notifications
You must be signed in to change notification settings - Fork 1
/
FlexCardLayout.svelte
195 lines (184 loc) · 5.96 KB
/
FlexCardLayout.svelte
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
<script context="module">
export const CARDLAYOUT = {}
</script>
<script>
import { onMount, onDestroy, tick, setContext } from 'svelte'
import { writable } from 'svelte/store'
import ResizeObserver from 'resize-observer-polyfill'
export let maxwidth = 500
export let preserveorder = false
export let gutter = 10
export let className = ''
const blocks = []
const gutterstore = writable(gutter)
setContext(CARDLAYOUT, {
registerBlock: block => {
blocks.push(block)
hardrecalc()
onDestroy(() => {
blocks.splice(blocks.indexOf(block), 1)
hardrecalc()
})
block.order = writable(0)
block.linebreak = writable(false)
block.width = writable(savecolumns ? `calc(${100.0 / savecolumns}% - ${gutter * (savecolumns-1) / savecolumns}px` : '0px')
return block
},
recalculate: () => {
hardrecalc()
},
gutter: gutterstore
})
$: gutterstore.set(gutter)
let layoutelement
let cycle1 = 0
let cycle2 = 0
function detectcycle (w) {
if (cycle1 === 0 && Math.abs(w - cycle2) > 5) {
cycle1 = w
} else if (cycle2 === 0 && Math.abs(w - cycle1) > 5) {
cycle2 = w
} else if (w === cycle1 || w === cycle2) {
return true
} else {
cycle1 = 0
cycle2 = 0
}
return false
}
let savecolumns = 0
let optimal
let fullheight = 0
let hardrequired = false
async function recalculate (realw) {
let columns = Math.ceil(realw / maxwidth)
let guttereach = gutter * (columns-1) / columns
const cycling = detectcycle(realw)
if (columns !== savecolumns) {
for (const block of blocks) block.width.set(`calc(${100.0 / columns}% - ${guttereach}px`)
await tick()
}
// collect all the card heights at this new column width
for (const block of blocks) block.height = block.element.offsetHeight
if (columns !== savecolumns || hardrequired) { // only do real work if number of columns has changed, recalculate triggers on resize
if (preserveorder) {
optimal = [blocks]
if (columns > 1) {
const totalheight = blocks.reduce((totalheight, block) => totalheight + block.height + gutter, 0)
const minheight = Math.max(totalheight / columns, ...blocks.map(b => b.height))
let shortestoverall = totalheight
// 2d packing problem is NP-hard so this is an O(n) heuristic
// optimal configuration would be if each column is `minheight` so start there
// then relax it in a few increments and see if the overall height shrinks
for (let colmaxheight = minheight; colmaxheight < 1.5*minheight; colmaxheight += 0.1 * minheight) {
let colheight = 0
let colidx = 0
let tallestcol = 0
const arrangement = []
for (const block of blocks) {
if (colheight + block.height > colmaxheight && colidx < columns - 1) {
colidx++
if (colheight > tallestcol) tallestcol = colheight
colheight = 0
}
if (!arrangement[colidx]) arrangement[colidx] = []
arrangement[colidx].push(block)
colheight += block.height + gutter
}
if (colheight > tallestcol) tallestcol = colheight
if (tallestcol < shortestoverall) {
shortestoverall = tallestcol
optimal = arrangement
}
}
}
} else {
optimal = []
const heights = Array.apply(null, Array(columns)).map(h => 0) // initializes heights to an array of zeroes
// begin sorting blocks into columns
for (const block of blocks) {
// find the column with the smallest current height
const colidx = heights.reduce((acc, curr, curridx) => curr < heights[acc] ? curridx : acc, 0)
// record the height we are adding to the chosen column
heights[colidx] += block.height + gutter
// move the current card to the chosen column
if (!optimal[colidx]) optimal[colidx] = []
optimal[colidx].push(block)
}
}
// we created the optimal arrangement, now move the block elements to
// reflect it
let order = 0
for (let i = 0; i < optimal.length; i++) {
for(let j = 0; j < optimal[i].length; j++) {
const block = optimal[i][j]
block.order.set(order++)
block.linebreak.set(j === optimal[i].length - 1 && i < optimal.length - 1)
}
}
savecolumns = columns
hardrequired = false
}
// re-adjust container height
const saveheight = fullheight
fullheight = 0
for (let i = 0; i < optimal.length; i++) {
let top = 0
for(const block of optimal[i]) {
top += block.height + gutter
}
fullheight = Math.max(fullheight, top)
}
fullheight += 0
if (cycling && saveheight < fullheight) fullheight = saveheight
}
let timer
let savewidth = 0
function triggerrecalc (width) {
if (!layoutelement) return
if (!width) width = layoutelement.clientWidth
if (width === savewidth && !hardrequired) return
cancelAnimationFrame(timer)
timer = requestAnimationFrame(() => {
recalculate(width)
savewidth = width
})
}
function hardrecalc () {
hardrequired = true
cycle1 = 0
cycle2 = 0
triggerrecalc()
}
const ro = new ResizeObserver((entries, observer) => {
triggerrecalc()
})
onMount(() => ro.observe(layoutelement))
onDestroy(() => ro.disconnect())
</script>
<style>
.cardlayout {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 150%;
list-style: none;
padding: 0;
margin: 0;
overflow: hidden;
display: flex;
flex-wrap: wrap;
flex-direction: column;
align-content: space-between;
}
div {
position: relative;
overflow: hidden;
}
</style>
<div style="height: {fullheight}px;">
<ul class="cardlayout {className}" bind:this={layoutelement}>
<slot></slot>
</ul>
</div>