-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
TimelineEventList.vue
274 lines (231 loc) · 6.66 KB
/
TimelineEventList.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<script lang="ts">
import EmptyPane from '@front/features/layout/EmptyPane.vue'
import TimelineEventListItem from './TimelineEventListItem.vue'
import { computed, ref, watch, defineComponent } from 'vue'
import { getStorage, setStorage } from '@vue-devtools/shared-utils'
import { useRoute, useRouter } from 'vue-router'
import { onKeyDown } from '@front/util/keyboard'
import { useInspectedEvent, useSelectedEvent, selectEvent, useLayers } from './composable'
const itemHeight = 34
const STORAGE_TAB_ID = 'timeline.event-list.tab-id'
export default defineComponent({
components: {
TimelineEventListItem,
EmptyPane,
},
setup () {
const route = useRoute()
const router = useRouter()
const {
selectedLayer,
} = useLayers()
const {
selectedEvent,
selectedGroupEvents,
} = useSelectedEvent()
const {
inspectedEvent,
} = useInspectedEvent()
// Tabs
const tabId = computed({
get: () => route.query.tabId,
set: value => {
setStorage(STORAGE_TAB_ID, value)
router.push({
query: {
...route.query,
tabId: value,
},
})
},
})
if (!route.query.tabId) {
tabId.value = getStorage(STORAGE_TAB_ID, 'all')
}
watch(selectedEvent, value => {
if (value && !value.group && tabId.value === 'group') {
tabId.value = 'all'
}
})
const displayedEvents = computed(() => {
switch (tabId.value) {
case 'group':
return selectedGroupEvents.value ?? []
case 'all':
default:
return selectedLayer.value?.events ?? []
}
})
// Filter
const filter = ref('')
const filteredEvents = computed(() => {
const rawFilter = filter.value.trim()
if (rawFilter) {
const reg = new RegExp(rawFilter, 'i')
return displayedEvents.value.filter(event =>
(event.title && reg.test(event.title)) ||
(event.subtitle && reg.test(event.subtitle)),
)
} else {
return displayedEvents.value
}
})
// Scrolling
const scroller = ref()
const isAtScrollBottom = ref(false)
function onScroll () {
const scrollerEl = scroller.value.$el
isAtScrollBottom.value = scrollerEl.scrollTop + scrollerEl.clientHeight >= scrollerEl.scrollHeight - 400
}
watch(scroller, value => {
if (value) {
onScroll()
}
}, { immediate: true })
watch(tabId, () => {
checkScrollToInspectedEvent()
}, { immediate: true })
function scrollToInspectedEvent () {
if (!scroller.value) return
const scrollerEl = scroller.value.$el
const index = filteredEvents.value.indexOf(inspectedEvent.value)
if (index !== -1) {
// We need to first disable auto bottom scroll
isAtScrollBottom.value = false
scrollerEl.scrollTop = itemHeight * (index + 0.5) - (scrollerEl.clientHeight) / 2
}
}
watch(inspectedEvent, () => {
checkScrollToInspectedEvent()
}, { immediate: true })
function checkScrollToInspectedEvent () {
requestAnimationFrame(() => {
if (!scroller.value) return
const scrollerEl = scroller.value.$el
const index = filteredEvents.value.indexOf(inspectedEvent.value)
const minPosition = itemHeight * index
const maxPosition = minPosition + itemHeight
if (scrollerEl.scrollTop > minPosition || scrollerEl.scrollTop + scrollerEl.clientHeight < maxPosition) {
scrollToInspectedEvent()
}
})
}
// Auto bottom scroll
function scrollToBottom () {
requestAnimationFrame(() => {
if (!scroller.value) return
const scrollerEl = scroller.value.$el
scrollerEl.scrollTop = scrollerEl.scrollHeight
})
}
// Important: Watch this after the scroll to inspect event watchers
watch(() => filteredEvents.value.length, () => {
if (isAtScrollBottom.value) {
scrollToBottom()
}
}, { immediate: true })
// List interactions
function inspectEvent (event) {
inspectedEvent.value = event
}
// Keyboard
onKeyDown(event => {
const index = filteredEvents.value.indexOf(inspectedEvent.value)
if (event.key === 'ArrowDown') {
if (index < filteredEvents.value.length - 1) {
inspectEvent(filteredEvents.value[index + 1])
}
} else if (event.key === 'ArrowUp') {
if (index > 0) {
inspectEvent(filteredEvents.value[index - 1])
}
} else if (event.key === 'Enter' || event.key === ' ') {
if (inspectedEvent.value) {
selectEvent(inspectedEvent.value)
}
}
})
return {
selectedEvent,
selectedLayer,
tabId,
scroller,
filter,
filteredEvents,
itemHeight,
isAtScrollBottom,
inspectEvent,
selectEvent,
onScroll,
scrollToBottom,
}
},
})
</script>
<template>
<div
v-if="selectedLayer && (filteredEvents.length || filter.length)"
class="h-full flex flex-col relative"
>
<div class="flex-none flex flex-col items-stretch border-gray-200 dark:border-gray-800 border-b">
<VueGroup
v-if="selectedEvent && selectedEvent.group"
v-model="tabId"
indicator
class="accent extend border-gray-200 dark:border-gray-800 border-b"
>
<VueGroupButton
value="all"
label="All"
class="flat"
/>
<VueGroupButton
value="group"
label="Group"
class="flat"
/>
</VueGroup>
<VueInput
v-model="filter"
icon-left="search"
:placeholder="`Filter ${selectedLayer.label}`"
class="search flat"
/>
</div>
<RecycleScroller
:key="tabId"
ref="scroller"
:items="filteredEvents"
:item-size="itemHeight"
class="flex-1"
@scroll.passive="onScroll()"
>
<template #default="{ item: event }">
<TimelineEventListItem
:event="event"
:selected="selectedEvent === event"
@inspect="inspectEvent(event)"
@select="selectEvent(event)"
/>
</template>
</RecycleScroller>
<VueButton
v-if="!isAtScrollBottom"
v-tooltip="'Scroll to bottom'"
icon-left="keyboard_arrow_down"
class="icon-button absolute bottom-1 right-4 rounded-full shadow-md"
@click="scrollToBottom()"
/>
</div>
<EmptyPane
v-else
:icon="!selectedLayer ? 'layers' : 'inbox'"
>
<template v-if="!selectedLayer">
Select a layer to get started
</template>
<template v-else>
No events
</template>
</EmptyPane>
</template>