-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFilePickerBreadcrumbs.vue
More file actions
154 lines (141 loc) · 3.93 KB
/
FilePickerBreadcrumbs.vue
File metadata and controls
154 lines (141 loc) · 3.93 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
<!--
- SPDX-FileCopyrightText: 2023-2024 Nextcloud GmbH and Nextcloud contributors
- SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
<NcBreadcrumbs class="file-picker__breadcrumbs">
<template #default>
<NcBreadcrumb :name="t('All files')"
:title="t('Home')"
@click="emit('update:path', '/')">
<template #icon>
<IconHome :size="20" />
</template>
</NcBreadcrumb>
<NcBreadcrumb v-for="dir in pathElements"
:key="dir.path"
:name="dir.name"
:title="dir.path"
@click="emit('update:path', dir.path)" />
</template>
<template v-if="showMenu" #actions>
<NcActions :open.sync="actionsOpen"
:aria-label="t('Create directory')"
:force-menu="true"
:force-name="true"
:menu-name="t('New')"
type="secondary"
@close="newNodeName = ''">
<template #icon>
<IconPlus :size="20" />
</template>
<NcActionInput ref="nameInput"
:value.sync="newNodeName"
:label="t('New folder')"
:placeholder="t('New folder name')"
@submit="onSubmit"
@update:model-value="validateInput">
<template #icon>
<IconFolder :size="20" />
</template>
</NcActionInput>
</NcActions>
</template>
</NcBreadcrumbs>
</template>
<script setup lang="ts">
import type Vue from 'vue'
import { InvalidFilenameError, InvalidFilenameErrorReason, validateFilename } from '@nextcloud/files'
import { computed, ref } from 'vue'
import NcActionInput from '@nextcloud/vue/components/NcActionInput'
import NcActions from '@nextcloud/vue/components/NcActions'
import NcBreadcrumb from '@nextcloud/vue/components/NcBreadcrumb'
import NcBreadcrumbs from '@nextcloud/vue/components/NcBreadcrumbs'
import IconFolder from 'vue-material-design-icons/Folder.vue'
import IconHome from 'vue-material-design-icons/Home.vue'
import IconPlus from 'vue-material-design-icons/Plus.vue'
import { t } from '../../utils/l10n'
const props = defineProps<{
/** Current path to display */
path: string
/** Whether to show the "new node" menu or not */
showMenu: boolean
}>()
const emit = defineEmits<{
/**
* Triggered when the path was changed by using the breadcrumbs
*/
(e: 'update:path', path: string): void
/**
* Triggered when a new directory on the current path should be created
*/
(e: 'create-node', name: string): void
}>()
const actionsOpen = ref(false)
/**
* Input on the "new node" menu
*/
const newNodeName = ref('')
const nameInput = ref<Vue>()
/**
* Validate user folder name input
*/
function validateInput() {
const name = newNodeName.value.trim()
const input = nameInput.value?.$el?.querySelector('input')
let validity = ''
try {
validateFilename(name)
} catch (error) {
if (!(error instanceof InvalidFilenameError)) {
throw error
}
switch (error.reason) {
case InvalidFilenameErrorReason.Character:
validity = t('"{char}" is not allowed inside a folder name.', { char: error.segment })
break
case InvalidFilenameErrorReason.ReservedName:
validity = t('"{segment}" is a reserved name and not allowed for folder names.', { segment: error.segment })
break
case InvalidFilenameErrorReason.Extension:
validity = t('Folder names must not end with "{extension}".', { extension: error.segment })
break
default:
validity = t('Invalid folder name.')
}
}
if (input) {
input.setCustomValidity(validity)
}
return validity === ''
}
/**
* Handle creating a new node
*/
const onSubmit = function() {
const name = newNodeName.value.trim()
if (validateInput()) {
actionsOpen.value = false
emit('create-node', name)
newNodeName.value = ''
}
}
/**
* Split current path and provide the path and the basename
*/
const pathElements = computed(() => props.path.split('/')
.filter((v) => v !== '')
.map((v, i, elements) => ({
name: v,
path: '/' + elements.slice(0, i + 1).join('/'),
})),
)
</script>
<style scoped lang="scss">
.file-picker {
&__breadcrumbs {
// ensure the breadcrumbs have a static height
flex-grow: 0 !important;
}
}
</style>