forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateItems.vue
More file actions
108 lines (92 loc) · 2.4 KB
/
GenerateItems.vue
File metadata and controls
108 lines (92 loc) · 2.4 KB
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
<template>
<q-virtual-scroll
style="max-height: 300px; overflow-x: hidden"
:items-size="size"
:items-fn="getItems"
:virtual-scroll-item-size="78"
separator
>
<template v-slot="{ item, index }">
<async-component :key="index" :index="item.index" :sent="item.sent"></async-component>
</template>
</q-virtual-scroll>
</template>
<script>
import { QChatMessage, QSkeleton } from 'quasar'
import { h, defineComponent, ref, onBeforeMount, onBeforeUnmount } from 'vue'
const AsyncComponent = defineComponent({
props: {
index: Number,
sent: Boolean
},
setup (props) {
const asyncContent = ref(null)
let timer
onBeforeMount(() => {
timer = setTimeout(() => {
asyncContent.value = {
sent: props.sent,
name: props.sent === true ? 'me' : 'Someone else',
avatar: props.sent === true ? 'https://cdn.quasar.dev/img/avatar4.jpg' : 'https://cdn.quasar.dev/img/avatar3.jpg',
stamp: `${Math.floor(props.index / 1000)} minutes ago`,
text: [`Message with id ${props.index}`]
}
}, 300 + Math.random() * 2000)
})
onBeforeUnmount(() => {
clearTimeout(timer)
})
return () => {
if (asyncContent.value === Object(asyncContent.value)) {
return h(QChatMessage, {
class: 'q-mx-sm',
key: props.index,
...asyncContent.value
})
}
const content = [
h(QSkeleton, {
class: 'on-left on-right',
animation: 'none',
type: 'text',
width: '150px',
height: '100px'
})
]
content[ props.sent === true ? 'push' : 'unshift' ](
h(QSkeleton, {
animation: 'none',
type: 'QAvatar'
})
)
return h('div', {
class: `row no-wrap items-center q-mx-sm justify-${props.sent === true ? 'end' : 'start'}`,
style: 'height: 78px',
key: props.index
}, content)
}
}
})
const size = ref(100000)
const allItems = Array(size.value).fill(null).map((_, index) => ({
index,
sent: Math.random() > 0.5
}))
export default {
components: {
AsyncComponent
},
setup () {
return {
size,
getItems (from, size) {
const items = []
for (let i = 0; i < size; i++) {
items.push(allItems[ from + i ])
}
return Object.freeze(items)
}
}
}
}
</script>