-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcontextMenu.vue
More file actions
186 lines (161 loc) · 4.58 KB
/
Copy pathcontextMenu.vue
File metadata and controls
186 lines (161 loc) · 4.58 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<script setup lang="ts">
import type { AssetItem, PermedProtocol } from "~/types/index";
import { storeToRefs } from "pinia";
import { useUserInfoStore } from "~/store/modules/userInfo";
interface Props {
asset: AssetItem;
visible: boolean;
x?: number;
y?: number;
}
const props = defineProps<Props>();
const emits = defineEmits<{
(e: "update:visible", visible: boolean): void;
(e: "contextTrigger", asset: AssetItem): void;
(e: "editTrigger", asset: AssetItem): void;
(e: "connectTrigger", asset: AssetItem): void;
(e: "renameTrigger", asset: AssetItem): void;
}>();
const { t } = useI18n();
const { handleAssetConnection, displayUser, handleAssetFavorite, handleAssetUnfavorite } = useAssetAction();
const userInfoStore = useUserInfoStore();
const { currentConnectionInfoMap } = storeToRefs(userInfoStore);
interface MenuItem {
value?: string;
label: string;
icon: string;
onClick: () => void;
children?: MenuItem[];
}
const isFavorited = computed(() => !!props.asset.isFavorite);
const resolveProtocols = (asset: AssetItem) => {
const candidateProtocols: string[] = [];
(asset.permedProtocols || []).forEach((p: PermedProtocol | undefined) => {
if (p?.name) candidateProtocols.push(p.name);
});
const saved = currentConnectionInfoMap.value[asset.id];
(saved?.availableProtocols || []).forEach((name) => {
if (name) candidateProtocols.push(name);
});
if (saved?.protocol) {
candidateProtocols.push(saved.protocol);
}
return Array.from(new Set(candidateProtocols.filter((name) => typeof name === "string" && name.length > 0)));
};
const menuItems = computed((): MenuItem[] => {
const uniqueProtocols = resolveProtocols(props.asset);
const baseItems: MenuItem[] = [
{
value: "connect",
label: t("ContextMenu.Connect"),
icon: "i-lucide-plug",
onClick: () => handleConnect()
},
{
label: t("ContextMenu.Edit"),
icon: "solar:pen-new-square-linear",
onClick: () => handleEdit()
},
{
label: t("ContextMenu.Rename"),
icon: "i-lucide-pencil",
onClick: () => handleRename()
},
{
label: isFavorited.value ? t("ContextMenu.Unfavorite") : t("ContextMenu.Favorite"),
icon: isFavorited.value ? "lucide:star-off" : "lucide:star",
onClick: () => (isFavorited.value ? handleUnfavorite() : handleFavorite())
}
];
// 如果有多个协议,为连接项添加子菜单
if (uniqueProtocols.length > 1) {
const protocolItems: MenuItem[] = uniqueProtocols.map((name: string) => ({
label: `${t("ContextMenu.Use")} ${name.toUpperCase()}`,
icon: "i-lucide-plug",
onClick: () => handleConnect(name)
}));
const item = {
value: "moreConnect",
label: t("ContextMenu.MoreConnect"),
icon: "i-lucide-ellipsis",
onClick: () => void 0,
children: protocolItems
};
baseItems.splice(1, 0, item);
}
return baseItems;
});
// 处理连接
const handleConnect = (protocol?: string) => {
if (protocol) {
// 如果有指定协议,直接连接
handleAssetConnection(
displayUser(props.asset.id, props.asset.permedAccounts!),
props.asset.id,
protocol,
props.asset.permedAccounts!,
undefined,
{
accountMode: "hosted",
manualUsername: "",
manualPassword: "",
dynamicPassword: ""
}
);
} else {
emits("connectTrigger", props.asset);
}
emits("update:visible", false);
};
// 处理编辑
const handleEdit = () => {
emits("editTrigger", props.asset);
nextTick(() => {
emits("update:visible", false);
});
};
// 处理重命名
const handleRename = () => {
emits("renameTrigger", props.asset);
nextTick(() => {
emits("update:visible", false);
});
};
// 处理收藏
const handleFavorite = () => {
handleAssetFavorite(props.asset.id);
try {
useEventBus().emit("favoriteChanged", { assetId: props.asset.id, favorite: true });
} catch {}
emits("update:visible", false);
};
const handleUnfavorite = () => {
handleAssetUnfavorite(props.asset.id);
try {
useEventBus().emit("favoriteChanged", { assetId: props.asset.id, favorite: false });
} catch {}
emits("update:visible", false);
};
</script>
<template>
<UDropdownMenu
:open="visible"
:items="menuItems"
size="sm"
:ui="{
content: 'w-48 p-1'
}"
@update:open="emits('update:visible', $event)"
>
<!-- 使用一个隐藏的触发器 -->
<div
class="fixed pointer-events-none"
:style="{
left: `${x || 0}px`,
top: `${y || 0}px`,
width: '1px',
height: '1px'
}"
/>
</UDropdownMenu>
</template>