File size: 20,605 Bytes
8ede856 | 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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | <template>
<div class="folder-item-selector">
<!-- 触发按钮区域 -->
<div class="d-flex align-center justify-space-between">
<span v-if="!modelValue" style="color: rgb(var(--v-theme-primaryText));">
{{ labels.notSelected || '未选择' }}
</span>
<span v-else>
{{ displayValue }}
</span>
<v-btn size="small" color="primary" variant="tonal" @click="openDialog">
{{ labels.buttonText || '选择...' }}
</v-btn>
</div>
<!-- 选择对话框 -->
<v-dialog v-model="dialog" max-width="1000px" min-width="800px">
<v-card class="selector-dialog-card">
<v-card-title class="dialog-title d-flex align-center py-4 px-5">
<v-icon class="mr-3" color="primary">mdi-account-circle</v-icon>
<span>{{ labels.dialogTitle || '选择项目' }}</span>
</v-card-title>
<v-divider />
<v-card-text class="pa-0" style="height: 600px; max-height: 80vh; overflow: hidden;">
<div class="selector-layout">
<!-- 左侧文件夹树 -->
<div class="folder-sidebar">
<div class="sidebar-header pa-3 pb-2">
<span class="text-caption text-medium-emphasis font-weight-medium">
<v-icon size="small" class="mr-1">mdi-folder-multiple</v-icon>
文件夹
</span>
</div>
<v-list density="compact" nav class="tree-list pa-2" bg-color="transparent">
<!-- 根目录 -->
<v-list-item :active="currentFolderId === null" @click="navigateToFolder(null)"
rounded="lg" class="mb-1 root-item">
<template v-slot:prepend>
<v-icon size="20" :color="currentFolderId === null ? 'primary' : ''">mdi-home</v-icon>
</template>
<v-list-item-title class="text-body-2">{{ labels.rootFolder || '根目录' }}</v-list-item-title>
</v-list-item>
<!-- 文件夹树 -->
<template v-if="!treeLoading">
<BaseMoveTargetNode v-for="folder in folderTree" :key="folder.folder_id"
:folder="folder" :depth="0" :selected-folder-id="currentFolderId"
:disabled-folder-ids="[]" @select="navigateToFolder" />
</template>
<div v-if="treeLoading" class="text-center pa-4">
<v-progress-circular indeterminate size="20" color="primary" />
</div>
</v-list>
</div>
<!-- 右侧项目列表 -->
<div class="items-panel">
<!-- 面包屑导航 -->
<div class="breadcrumb-bar px-4 py-3">
<v-breadcrumbs :items="breadcrumbItems" density="compact" class="pa-0">
<template v-slot:item="{ item }">
<v-breadcrumbs-item :disabled="(item as any).disabled"
@click="!(item as any).disabled && navigateToFolder((item as any).folderId)"
:class="{ 'breadcrumb-link': !(item as any).disabled }">
<v-icon v-if="(item as any).isRoot" size="small"
class="mr-1">mdi-home</v-icon>
{{ item.title }}
</v-breadcrumbs-item>
</template>
<template v-slot:divider>
<v-icon size="small" color="grey">mdi-chevron-right</v-icon>
</template>
</v-breadcrumbs>
</div>
<v-divider />
<!-- 项目列表 -->
<div class="items-list">
<v-progress-linear v-if="itemsLoading" indeterminate
color="primary" height="2"></v-progress-linear>
<!-- 子文件夹 -->
<v-list v-if="!itemsLoading" lines="two" class="pa-3 items-content">
<template v-if="currentSubFolders.length > 0">
<div class="section-label text-caption text-medium-emphasis mb-2 px-2">子文件夹</div>
<v-list-item v-for="folder in currentSubFolders" :key="'folder-' + folder.folder_id"
@click="navigateToFolder(folder.folder_id)" rounded="lg" class="mb-1 folder-item">
<template v-slot:prepend>
<v-avatar size="36" color="amber-lighten-4" class="mr-3">
<v-icon color="amber-darken-2" size="20">mdi-folder</v-icon>
</v-avatar>
</template>
<v-list-item-title class="font-weight-medium">{{ folder.name }}</v-list-item-title>
<template v-slot:append>
<v-icon size="20" color="grey">mdi-chevron-right</v-icon>
</template>
</v-list-item>
</template>
<!-- 项目列表 -->
<template v-if="currentItems.length > 0">
<div class="section-label text-caption text-medium-emphasis mb-2 px-2" :class="{ 'mt-4': currentSubFolders.length > 0 }">可选项目</div>
<v-list-item v-for="item in currentItems" :key="'item-' + getItemId(item)"
:value="getItemId(item)" @click="selectItem(item)"
:active="selectedItemId === getItemId(item)" rounded="lg" class="mb-1 persona-item"
:class="{ 'selected-item': selectedItemId === getItemId(item) }">
<template v-slot:prepend>
<v-avatar size="36" :color="selectedItemId === getItemId(item) ? 'primary-lighten-4' : 'grey-lighten-3'" class="mr-3">
<v-icon :color="selectedItemId === getItemId(item) ? 'primary' : 'grey-darken-1'" size="20">mdi-account</v-icon>
</v-avatar>
</template>
<v-list-item-title class="font-weight-medium">{{ getItemName(item) }}</v-list-item-title>
<v-list-item-subtitle v-if="getItemDescription(item)" class="text-truncate">
{{ truncateText(getItemDescription(item), 80) }}
</v-list-item-subtitle>
<template v-slot:append>
<div class="d-flex align-center ga-1">
<v-btn v-if="showEditButton && !isDefaultItem(item)"
icon="mdi-pencil"
size="small"
variant="text"
@click.stop="handleEditItem(item)"
:title="labels.editButton || 'Edit'"
/>
<v-icon v-if="selectedItemId === getItemId(item)"
color="primary" size="22">mdi-check-circle</v-icon>
</div>
</template>
</v-list-item>
</template>
<!-- 空状态 -->
<div v-if="currentSubFolders.length === 0 && currentItems.length === 0"
class="empty-state text-center py-12">
<v-icon size="64" color="grey-lighten-2">mdi-folder-open-outline</v-icon>
<p class="text-grey mt-4 text-body-2">{{ labels.emptyFolder || labels.noItems || '此文件夹为空' }}</p>
</div>
</v-list>
</div>
</div>
</div>
</v-card-text>
<v-card-actions class="pa-4">
<v-btn v-if="showCreateButton" variant="text" color="primary" prepend-icon="mdi-plus"
@click="$emit('create')">
{{ labels.createButton || '新建' }}
</v-btn>
<v-spacer></v-spacer>
<v-btn variant="text" @click="cancelSelection">{{ labels.cancelButton || '取消' }}</v-btn>
<v-btn color="primary" @click="confirmSelection" :disabled="!selectedItemId">
{{ labels.confirmButton || '确认' }}
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</div>
</template>
<script lang="ts">
import { defineComponent, type PropType } from 'vue';
import BaseMoveTargetNode from './BaseMoveTargetNode.vue';
import type { FolderTreeNode, FolderItemSelectorLabels, SelectableItem } from './types';
export default defineComponent({
name: 'BaseFolderItemSelector',
components: {
BaseMoveTargetNode
},
props: {
modelValue: {
type: String,
default: ''
},
// 文件夹树数据
folderTree: {
type: Array as PropType<FolderTreeNode[]>,
default: () => []
},
// 当前项目列表
items: {
type: Array as PropType<SelectableItem[]>,
default: () => []
},
// 加载状态
treeLoading: {
type: Boolean,
default: false
},
itemsLoading: {
type: Boolean,
default: false
},
// 标签配置
labels: {
type: Object as PropType<Partial<FolderItemSelectorLabels>>,
default: () => ({})
},
// 是否显示创建按钮
showCreateButton: {
type: Boolean,
default: false
},
// 是否显示编辑按钮
showEditButton: {
type: Boolean,
default: false
},
// 默认项(如 "默认人格")
defaultItem: {
type: Object as PropType<SelectableItem | null>,
default: null
},
// 项目字段映射
itemIdField: {
type: String,
default: 'id'
},
itemNameField: {
type: String,
default: 'name'
},
itemDescriptionField: {
type: String,
default: 'description'
},
// 显示值的格式化函数(用于显示选中项的名称)
displayValueFormatter: {
type: Function as unknown as PropType<((value: string) => string) | null>,
default: null
}
},
emits: ['update:modelValue', 'navigate', 'create', 'edit'],
data() {
return {
dialog: false,
selectedItemId: '' as string,
currentFolderId: null as string | null,
breadcrumbPath: [] as FolderTreeNode[]
};
},
computed: {
displayValue(): string {
if (this.displayValueFormatter) {
return this.displayValueFormatter(this.modelValue);
}
// 如果是默认项
if (this.defaultItem && this.modelValue === this.getItemId(this.defaultItem)) {
return this.labels.defaultItem || this.getItemName(this.defaultItem);
}
return this.modelValue;
},
currentItems(): SelectableItem[] {
const items: SelectableItem[] = [];
// 如果在根目录且有默认项,添加到列表开头
if (this.currentFolderId === null && this.defaultItem) {
items.push(this.defaultItem);
}
// 添加当前文件夹的项目
items.push(...this.items);
return items;
},
currentSubFolders(): FolderTreeNode[] {
if (this.currentFolderId === null) {
return this.folderTree;
}
const folder = this.findFolderInTree(this.currentFolderId);
return folder?.children || [];
},
breadcrumbItems(): any[] {
const items: any[] = [
{
title: this.labels.rootFolder || '根目录',
folderId: null,
disabled: this.currentFolderId === null,
isRoot: true
}
];
this.breadcrumbPath.forEach((folder, index) => {
items.push({
title: folder.name,
folderId: folder.folder_id,
disabled: index === this.breadcrumbPath.length - 1,
isRoot: false
});
});
return items;
}
},
methods: {
getItemId(item: SelectableItem): string {
return String(item[this.itemIdField] || item.id || '');
},
getItemName(item: SelectableItem): string {
return String(item[this.itemNameField] || item.name || '');
},
getItemDescription(item: SelectableItem): string {
return String(item[this.itemDescriptionField] || item.description || '');
},
truncateText(text: string, maxLength: number): string {
if (!text) return '';
return text.length > maxLength ? text.substring(0, maxLength) + '...' : text;
},
openDialog() {
this.selectedItemId = this.modelValue || '';
this.currentFolderId = null;
this.breadcrumbPath = [];
this.dialog = true;
this.$emit('navigate', null);
},
navigateToFolder(folderId: string | null) {
this.currentFolderId = folderId;
this.updateBreadcrumb(folderId);
this.$emit('navigate', folderId);
},
findFolderInTree(folderId: string): FolderTreeNode | null {
const findNode = (nodes: FolderTreeNode[]): FolderTreeNode | null => {
for (const node of nodes) {
if (node.folder_id === folderId) {
return node;
}
if (node.children && node.children.length > 0) {
const found = findNode(node.children);
if (found) return found;
}
}
return null;
};
return findNode(this.folderTree);
},
findPathToFolder(folderId: string): FolderTreeNode[] {
const findPath = (nodes: FolderTreeNode[], path: FolderTreeNode[]): FolderTreeNode[] | null => {
for (const node of nodes) {
if (node.folder_id === folderId) {
return [...path, node];
}
if (node.children && node.children.length > 0) {
const result = findPath(node.children, [...path, node]);
if (result) return result;
}
}
return null;
};
return findPath(this.folderTree, []) || [];
},
updateBreadcrumb(folderId: string | null) {
if (folderId === null) {
this.breadcrumbPath = [];
} else {
this.breadcrumbPath = this.findPathToFolder(folderId);
}
},
selectItem(item: SelectableItem) {
this.selectedItemId = this.getItemId(item);
},
confirmSelection() {
this.$emit('update:modelValue', this.selectedItemId);
this.dialog = false;
},
cancelSelection() {
this.selectedItemId = this.modelValue || '';
this.dialog = false;
},
isDefaultItem(item: SelectableItem): boolean {
if (this.defaultItem === null) {
return false;
}
return this.getItemId(item) === this.getItemId(this.defaultItem);
},
handleEditItem(item: SelectableItem) {
this.$emit('edit', item);
}
}
});
</script>
<style scoped>
.selector-dialog-card {
border-radius: 12px;
overflow: hidden;
}
.dialog-title {
font-size: 1.25rem;
font-weight: 500;
}
.selector-layout {
display: flex;
height: 100%;
}
.folder-sidebar {
width: 280px;
border-right: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
overflow-y: auto;
flex-shrink: 0;
background-color: transparent;
}
.sidebar-header {
border-bottom: 1px solid rgba(var(--v-border-color), 0.5);
}
.items-panel {
flex: 1;
display: flex;
flex-direction: column;
min-width: 0;
background-color: rgb(var(--v-theme-surface));
}
.breadcrumb-bar {
background-color: transparent;
min-height: 56px;
display: flex;
align-items: center;
}
.items-list {
flex: 1;
overflow-y: auto;
}
.items-content {
background-color: transparent;
}
.tree-list {
padding: 0;
}
.section-label {
text-transform: uppercase;
letter-spacing: 0.5px;
font-size: 0.7rem;
}
.breadcrumb-link {
cursor: pointer;
transition: color 0.2s;
}
.breadcrumb-link:hover {
color: rgb(var(--v-theme-primary));
}
.root-item {
margin-bottom: 4px;
}
.folder-item {
transition: all 0.15s ease;
}
.folder-item:hover {
background-color: rgba(var(--v-theme-primary), 0.06);
}
.persona-item {
transition: all 0.15s ease;
border: 1px solid transparent;
}
.persona-item:hover {
background-color: rgba(var(--v-theme-primary), 0.04);
}
.persona-item.selected-item {
background-color: rgba(var(--v-theme-primary), 0.08);
border-color: rgba(var(--v-theme-primary), 0.3);
}
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 200px;
}
.v-list-item {
transition: all 0.15s ease;
}
.v-list-item:hover {
background-color: rgba(var(--v-theme-primary), 0.04);
}
.v-list-item.v-list-item--active {
background-color: rgba(var(--v-theme-primary), 0.08);
}
@media (max-width: 600px) {
.selector-layout {
flex-direction: column;
height: auto;
max-height: 500px;
}
.folder-sidebar {
width: 100%;
border-right: none;
border-bottom: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
max-height: 150px;
}
.items-list {
max-height: 300px;
}
}
</style>
|