-
Notifications
You must be signed in to change notification settings - Fork 2
/
WaterfallContainer.vue
77 lines (67 loc) · 1.91 KB
/
WaterfallContainer.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
<script setup lang="ts">
import { watch, onMounted, onUnmounted } from 'vue'
import { mediatorRects } from "./stores/useWaterStore";
import { getLayoutStrategy } from "./utils/calculate";
import { adjustCells, setRects } from './utils/render'
//due to this... https://github.com/vuejs/core/issues/4294
//I have to copy the interface here, and also no able to combine them into an object..fuck...
interface WaterfallConfig {
interval: number
lineGap: number
minLineGap: number
maxLineGap: number
singleMaxWidth: number
}
const props = withDefaults(
defineProps<WaterfallConfig>(),
{
interval: 200,
lineGap: 250,
minLineGap: 100,
maxLineGap: 320,
singleMaxWidth: 300,
}
)
const emit = defineEmits<{
(event: 'scrollToBottom', columnCount: number): void
(event: 'allThingDone'): void
}>()
const onResize = () => {
let { columnCount, columnWidth } = getLayoutStrategy(document.documentElement.clientWidth, props)
let columnHeights: Array<number> = new Array(columnCount).fill(0)
setRects(columnWidth)
adjustCells(mediatorRects, columnHeights, columnWidth, true)
emit('allThingDone')
}
const onScroll = () => {
let scrollTop = document.documentElement.scrollTop
let windowHeight = document.documentElement.clientHeight
let scrollHeight = document.documentElement.scrollHeight
if (scrollTop + windowHeight + 3 >= scrollHeight) {
let { columnCount, columnWidth } = getLayoutStrategy(document.documentElement.clientWidth, props)
emit('scrollToBottom', columnCount)
}
}
watch(() => mediatorRects.length, () => {
onResize()
})
onMounted(() => {
addEventListener('resize', onResize)
addEventListener('scroll', onScroll)
onResize()
})
onUnmounted(() => {
removeEventListener('resize', onResize)
removeEventListener('scroll', onScroll)
})
</script>
<template>
<div class="biu">
<slot></slot>
</div>
</template>
<style scoped>
/* .biu {
position: relative
} */
</style>