-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathFilesNavigationListItem.vue
More file actions
167 lines (148 loc) · 4.59 KB
/
FilesNavigationListItem.vue
File metadata and controls
167 lines (148 loc) · 4.59 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
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
<!--
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<script setup lang="ts">
import type { IView } from '@nextcloud/files'
import { getCanonicalLocale, getLanguage } from '@nextcloud/l10n'
import { computed, onMounted, ref } from 'vue'
import NcAppNavigationItem from '@nextcloud/vue/components/NcAppNavigationItem'
import NcIconSvgWrapper from '@nextcloud/vue/components/NcIconSvgWrapper'
import { useVisibleViews } from '../composables/useViews.ts'
import { folderTreeId } from '../services/FolderTree.ts'
import { useViewConfigStore } from '../store/viewConfig.ts'
const props = withDefaults(defineProps<{
view: IView
level?: number
}>(), {
level: 0,
})
const maxLevel = 6 // Limit nesting to not exceed max call stack size
const viewConfigStore = useViewConfigStore()
const viewConfig = computed(() => viewConfigStore.viewConfigs[props.view.id])
const isExpanded = computed(() => viewConfig.value
? (viewConfig.value.expanded === true)
: (props.view.expanded === true))
const views = useVisibleViews()
const childViews = computed(() => {
if (props.level < maxLevel) {
return views.value.filter((v) => v.parent === props.view.id)
} else {
return views.value.filter((v) => isDescendant(v, props.view.id))
}
/**
* Check if a view is a descendant of another view by recursively traversing up the parent chain.
*
* @param view - The view to check
* @param parent - The parent view id to check against
*/
function isDescendant(view: IView, parent: string): boolean {
if (!view.parent) {
return false
} else if (view.parent === parent) {
return true
}
const parentView = views.value.find((v) => v.id === view.parent)
return !!parentView && isDescendant(parentView, parent)
}
})
const sortedChildViews = computed(() => childViews.value.slice().sort((a, b) => {
if (a.order !== undefined && b.order === undefined) {
return -1
} else if (a.order === undefined && b.order !== undefined) {
return 1
}
return collator.compare(a.name, b.name)
}))
const hasChildViews = computed(() => childViews.value.length > 0)
const navigationRoute = computed(() => {
if (props.view.params) {
const { dir } = props.view.params
return { name: 'filelist', params: { ...props.view.params }, query: { dir } }
}
return { name: 'filelist', params: { view: props.view.id } }
})
const isLoading = ref(false)
const childViewsLoaded = ref(false)
/**
* Load child views on mount if the view is expanded by default
* but has no child views loaded yet.
*/
onMounted(() => {
if (isExpanded.value && !hasChildViews.value) {
loadChildViews()
}
})
/**
* Handle expanding/collapsing the navigation item.
*
* @param expanded - The expanded state
*/
async function onExpandCollapse(expanded: boolean) {
if (viewConfig.value) {
viewConfig.value.expanded = expanded
} else if (expanded) {
viewConfigStore.viewConfigs[props.view.id] = { expanded: true }
}
// folder tree should only show current directory by default,
// so we don't want to persist the expanded state in the store for its views
if (!props.view.id.startsWith(`${folderTreeId}::`)) {
viewConfigStore.update(props.view.id, 'expanded', expanded)
}
if (expanded) {
await loadChildViews()
}
}
/**
* Load child views if a loader function is provided and child views haven't been loaded yet.
*/
async function loadChildViews() {
if (props.view.loadChildViews && !childViewsLoaded.value) {
isLoading.value = true
try {
await props.view.loadChildViews(props.view)
childViewsLoaded.value = true
} finally {
isLoading.value = false
}
}
}
</script>
<script lang="ts">
const collator = Intl.Collator(
[getLanguage(), getCanonicalLocale()],
{ numeric: true, usage: 'sort' },
)
// TODO: Remove this with Vue 3 - the name is inferred by the filename!
export default {
name: 'FilesNavigationListItem',
}
</script>
<template>
<NcAppNavigationItem
class="files-navigation__item"
allow-collapse
:loading="isLoading"
:data-cy-files-navigation-item="view.id"
:exact="hasChildViews"
:name="view.name"
:open="isExpanded"
:pinned="view.sticky"
:to="navigationRoute"
@update:open="onExpandCollapse">
<template v-if="view.icon" #icon>
<NcIconSvgWrapper :svg="view.icon" />
</template>
<!-- Hack to force the collapse icon to be displayed -->
<li
v-if="!hasChildViews && !childViewsLoaded && view.loadChildViews"
v-show="false"
role="presentation" />
<!-- Recursively nest child views -->
<FilesNavigationListItem
v-for="childView in sortedChildViews"
:key="childView.id"
:level="level + 1"
:view="childView" />
</NcAppNavigationItem>
</template>