feat: 移动主机分组.

This commit is contained in:
lijiahang
2023-11-13 18:32:14 +08:00
parent 9f7e0c945f
commit faa34a3600
27 changed files with 810 additions and 252 deletions

View File

@@ -9,10 +9,20 @@ export interface HostGroupCreateRequest {
}
/**
* 主机分组更新请求
* 主机分组重命名请求
*/
export interface HostGroupUpdateRequest extends HostGroupCreateRequest {
export interface HostGroupRenameRequest {
id?: number;
name?: string;
}
/**
* 主机分组移动请求
*/
export interface HostGroupMoveRequest {
id?: number;
targetId?: number;
position?: number;
}
/**
@@ -31,10 +41,8 @@ export interface HostGroupQueryRequest {
* 主机分组查询响应
*/
export interface HostGroupQueryResponse {
id: number;
parentId: number;
name: string;
sort: number;
key: number;
title: string;
children: Array<HostGroupQueryResponse>;
}
@@ -42,14 +50,21 @@ export interface HostGroupQueryResponse {
* 创建主机分组
*/
export function createHostGroup(request: HostGroupCreateRequest) {
return axios.post('/asset/host-group/create', request);
return axios.post<number>('/asset/host-group/create', request);
}
/**
* 更新主机分组
* 更新主机分组名称
*/
export function updateHostGroup(request: HostGroupUpdateRequest) {
return axios.put('/asset/host-group/update', request);
export function updateHostGroupName(request: HostGroupRenameRequest) {
return axios.put('/asset/host-group/rename', request);
}
/**
* 移动主机分组
*/
export function moveHostGroup(request: HostGroupMoveRequest) {
return axios.put('/asset/host-group/move', request);
}
/**

View File

@@ -114,3 +114,40 @@ export const flatNodes = <T extends NodeData>(nodes: Array<T>,
flatNodes(s.children, result);
});
};
// 移动节点
export const moveNode = (nodes: Array<NodeData>,
dragNode: NodeData,
dropNode: NodeData,
dropPosition: number) => {
// dropPosition === -1 将 dragNode 拖拽到 dropNode 上
// dropPosition === 0 将 dragNode 拖拽到 dropNode 中
// dropPosition === 1 将 dragNode 拖拽到 dropNode 下
const loop = (data: NodeData, key: number, callback: any) => {
data.some((item: NodeData, index: any, arr: NodeData[]) => {
if (item.key === key) {
callback(item, index, arr);
return true;
}
if (item.children) {
return loop(item.children, key, callback);
}
return false;
});
};
loop(nodes, dragNode.key as number, (_: NodeData, index: number, arr: NodeData[]) => {
arr.splice(index, 1);
});
if (dropPosition === 0) {
loop(nodes, dropNode.key as number, (item: NodeData) => {
item.children = item.children || [];
item.children?.push(dragNode);
});
} else {
loop(nodes, dropNode.key as number, (_: NodeData, index: number, arr: NodeData[]) => {
arr.splice(dropPosition < 0 ? index : index + 1, 0, dragNode);
});
}
};

View File

@@ -1,14 +1,22 @@
<template>
<!-- 分组树 -->
<a-tree
v-if="treeData.length"
ref="tree"
class="tree-container"
:blockNode="true"
:draggable="true"
:data="treeData">
:data="treeData"
@drop="moveGroup">
<!-- 标题 -->
<template #title="node">
<!-- 修改名称输入框 -->
<template v-if="node.editable">
<a-input size="mini"
ref="renameInput"
v-model="currName"
placeholder="名称"
autofocus
:max-length="32"
:disabled="node.loading"
@blur="() => saveNode(node.key)"
@@ -21,30 +29,52 @@
<icon-check v-else
class="pointer"
title="保存"
@click="saveNode(node.key)" />
@click="() => saveNode(node.key)" />
</template>
</a-input>
</template>
<span class="node-title" v-else>
{{ node.title }}
</span>
<!-- 名称 -->
<span v-else
class="node-title-wrapper"
@click="() => emits('selectKey', node.key)">
{{ node.title }}
</span>
</template>
<!-- 操作图标 -->
<template #drag-icon="{ node }">
<a-space v-if="!node.editable">
<icon-edit class="tree-icon"
title="重命名"
@click="rename(node.title, node.key)" />
<icon-delete class="tree-icon"
title="删除"
@click="rename(node.title, node.key)" />
<icon-plus class="tree-icon"
title="新增"
@click="rename(node.title, node.key)" />
<!-- 重命名 -->
<span v-permission="['asset:host-group:update']"
class="tree-icon"
title="重命名"
@click="rename(node.title, node.key)">
<icon-edit />
</span>
<!-- 删除 -->
<a-popconfirm content="确认删除这条记录吗?"
position="left"
type="warning"
@ok="deleteNode(node.key)">
<span v-permission="['asset:host-group:delete']"
class="tree-icon" title="删除">
<icon-delete />
</span>
</a-popconfirm>
<!-- 新增 -->
<span v-permission="['asset:host-group:create']"
class="tree-icon"
title="新增"
@click="addChildren(node)">
<icon-plus />
</span>
</a-space>
</template>
</a-tree>
<!-- 无数据 -->
<div v-else-if="!loading" class="empty-container">
<span>暂无数据</span>
<span>点击上方 '<icon-plus />' 添加一个分组吧~</span>
</div>
</template>
<script lang="ts">
@@ -55,51 +85,25 @@
<script lang="ts" setup>
import type { TreeNodeData } from '@arco-design/web-vue';
import type { NodeData } from '@/types/global';
import { nextTick, ref } from 'vue';
import { nextTick, onMounted, ref } from 'vue';
import { createGroupGroupPrefix, rootId } from '../types/const';
import { findNode, findParentNode, moveNode } from '@/utils/tree';
import { createHostGroup, deleteHostGroup, getHostGroupTree, updateHostGroupName, moveHostGroup } from '@/api/asset/host-group';
import { isString } from '@/utils/is';
const props = defineProps({
loading: Boolean
});
const emits = defineEmits(['loading', 'selectKey']);
const tree = ref();
const modCount = ref(0);
const renameInput = ref();
const currName = ref();
// 提为工具 utils tree.js
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 保存节点
const saveNode = async (key: string) => {
// 寻找节点
const node = findNode<TreeNodeData>(key, treeData.value);
if (!node) {
return;
}
if (currName.value) {
node.loading = true;
try {
if (key.startsWith('create')) {
// 调用创建 api
await sleep(340);
node.key = 'await id';
} else {
// 调用重命名 api
await sleep(340);
}
node.title = currName.value;
} catch (e) {
} finally {
node.loading = false;
}
} else {
if (key.startsWith('create')) {
// 寻找父节点
// 移除子节点
}
}
node.editable = false;
};
const treeData = ref<Array<TreeNodeData>>([]);
// 重命名
const rename = (title: string, key: string) => {
const rename = (title: number, key: number) => {
const node = findNode<TreeNodeData>(key, treeData.value);
if (!node) {
return;
@@ -111,132 +115,181 @@
});
};
// 寻找当前节点
const findNode = <T extends NodeData>(id: string, arr: Array<T>): T | undefined => {
for (let node of arr) {
if (node.key === id) {
return node;
// 删除节点
const deleteNode = async (key: number) => {
try {
emits('loading', true);
// 删除
await deleteHostGroup(key);
// 页面删除
const parentNode = findParentNode<TreeNodeData>(key, treeData.value);
if (!parentNode) {
return;
}
}
// 寻找子级
for (let node of arr) {
if (node?.children?.length) {
const inChildNode = findNode(id, node.children);
if (inChildNode) {
return inChildNode as T;
const children = parentNode.root ? treeData.value : parentNode.children;
if (children) {
// 删除
for (let i = 0; i < children.length; i++) {
if (children[i].key === key) {
children.splice(i, 1);
break;
}
}
}
} catch (e) {
} finally {
emits('loading', false);
}
return undefined;
};
function onIconClick(node: any) {
const children = node.children || [];
children.push({
title: 'new tree node',
key: node.key + '-' + (children.length + 1)
// 新增根节点
const addRootNode = () => {
const newKey = `${createGroupGroupPrefix}${Date.now()}`;
treeData.value.push({
title: 'new',
key: newKey
});
node.children = children;
// 编辑子节点
const newNode = findNode<TreeNodeData>(newKey, treeData.value);
if (newNode) {
newNode.editable = true;
currName.value = '';
nextTick(() => {
renameInput.value?.focus();
});
}
};
// 新增子节点
const addChildren = (parentNode: TreeNodeData) => {
const newKey = `${createGroupGroupPrefix}${Date.now()}`;
const children = parentNode.children || [];
children.push({
title: 'new',
key: newKey
});
parentNode.children = children;
treeData.value = [...treeData.value];
}
nextTick(() => {
// 展开
tree.value.expandNode(parentNode.key);
// 编辑子节点
const newNode = findNode<TreeNodeData>(newKey, treeData.value);
if (newNode) {
newNode.editable = true;
currName.value = '';
nextTick(() => {
renameInput.value?.focus();
});
}
});
};
// 保存节点
const saveNode = async (key: string | number) => {
modCount.value++;
if (modCount.value !== 1) {
return;
}
// 寻找节点
const node = findNode<TreeNodeData>(key, treeData.value);
if (!node) {
return;
}
if (currName.value) {
node.loading = true;
try {
// 创建节点
if (isString(key) && key.startsWith(createGroupGroupPrefix)) {
const parent = findParentNode<TreeNodeData>(key, treeData.value);
if (parent.root) {
parent.key = rootId;
}
// 创建
const { data } = await createHostGroup({
parentId: parent.key as number,
name: currName.value
});
node.key = data;
} else {
// 重命名节点
await updateHostGroupName({
id: key as unknown as number,
name: currName.value
});
}
node.title = currName.value;
node.editable = false;
} catch (e) {
// 重复 重新聚焦
setTimeout(() => {
renameInput.value?.focus();
}, 100);
} finally {
node.loading = false;
}
} else {
// 未输入数据 并且为创建则移除节点
if (isString(key) && key.startsWith(createGroupGroupPrefix)) {
// 寻找父节点
const parent = findParentNode(key, treeData.value);
if (parent && parent.children) {
// 移除子节点
for (let i = 0; i < parent.children.length; i++) {
if (parent.children[i].key === key) {
parent.children.splice(i, 1);
}
}
}
}
node.editable = false;
}
modCount.value = 0;
};
// 移动分组
const moveGroup = async (
{
dragNode, dropNode, dropPosition
}: {
dragNode: TreeNodeData,
dropNode: TreeNodeData,
dropPosition: number
}) => {
try {
emits('loading', true);
// 移动
await moveHostGroup({
id: dragNode.key as number,
targetId: dropNode.key as number,
position: dropPosition
});
// 移动分组
moveNode(treeData.value, dragNode, dropNode, dropPosition);
} catch (e) {
} finally {
emits('loading', false);
}
};
// 加载数据
const fetchTreeData = async () => {
try {
emits('loading', true);
const { data } = await getHostGroupTree();
treeData.value = data;
} catch {
} finally {
emits('loading', false);
}
};
defineExpose({ addRootNode, fetchTreeData });
onMounted(() => {
fetchTreeData();
});
const treeData = ref(
[
{
title: 'Trunk',
key: '0-0',
children: [
{
title: 'Leaf',
key: '0-0-1',
},
{
title: 'Branch',
key: '0-0-2',
children: [
{
title: 'Leaf',
key: '0-0-2-1'
}
]
},
],
},
{
title: 'Trunk',
key: '0-1',
children: [
{
title: 'Branch',
key: '0-1-1',
children: [
{
title: 'Leaf',
key: '0-1-1-11',
},
{
title: 'Leaf',
key: '0-1-1-12',
},
{
title: 'Leaf',
key: '0-1-1-13',
},
{
title: 'Leaf',
key: '0-1-1-41',
},
{
title: 'Leaf',
key: '0-1-1-51',
},
{
title: 'Leaf',
key: '0-1-1-61',
},
{
title: 'Leaf',
key: '0-1-17-1',
},
{
title: 'Leaf',
key: '0-1-81-1',
},
{
title: 'Leaf',
key: '0-19-1-1',
},
{
title: 'Leaf',
key: '0-10-1-1',
},
{
title: 'Leaf',
key: '0-1-111-1',
},
{
title: 'Leaf',
key: '0-21-1-1',
},
{
title: 'Leaf',
key: '0-31-1-1',
},
{
title: 'Leaf',
key: '40-1-1-2',
},
]
},
{
title: 'Leaf',
key: '0-1-2',
},
],
},
]
);
</script>
<style lang="less" scoped>
@@ -246,6 +299,15 @@
user-select: none;
}
.empty-container {
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
padding-top: 25px;
color: var(--color-text-3);
}
:deep(.arco-tree-node-selected) {
.arco-tree-node-title {
&:hover {
@@ -268,25 +330,17 @@
}
}
.node-title {
}
.node-handler {
}
:deep(.arco-tree-node-selected) {
background-color: var(--color-fill-2);
}
.node-title-wrapper {
width: 100%;
}
.tree-icon {
font-size: 12px;
color: rgb(var(--primary-6));
}
.drag-icon {
padding-left: -8px;
}
</style>

View File

@@ -1,18 +1,36 @@
<template>
<!-- 左侧菜单 -->
<div class="simple-card tree-card">
<a-spin class="simple-card tree-card"
:loading="treeLoading">
<!-- 主机分组头部 -->
<div class="tree-card-header">
<!-- 标题 -->
<div class="tree-card-title">
主机菜单
</div>
<!-- 操作 -->
<div class="tree-card-handler">
<div v-permission="['asset:host-group:create']"
class="click-icon-wrapper handler-icon-wrapper"
title="根节点添加"
@click="addRootNode">
<icon-plus />
</div>
<div class="click-icon-wrapper handler-icon-wrapper"
title="刷新"
@click="refreshTree">
<icon-refresh />
</div>
</div>
</div>
<!-- 主机分组树 -->
<div class="tree-card-main">
<host-group-tree ref="tree" />
<host-group-tree ref="tree"
:loading="treeLoading"
@loading="setTreeLoading"
@select-key="selectGroup" />
</div>
</div>
</a-spin>
<!-- 身体部分 -->
<div class="simple-card view-body">
右侧数据
@@ -26,8 +44,30 @@
</script>
<script lang="ts" setup>
import { ref } from 'vue';
import useLoading from '@/hooks/loading';
import HostGroupTree from './host-group-tree.vue';
const { loading: treeLoading, setLoading: setTreeLoading } = useLoading();
const { loading: dataLoading, setLoading: setDataLoading } = useLoading();
const tree = ref();
// 添加根节点
const addRootNode = () => {
tree.value.addRootNode();
};
// 刷新树
const refreshTree = () => {
tree.value.fetchTreeData();
};
// 选中分组
const selectGroup = (key: number) => {
console.log(key);
};
</script>
<style lang="less" scoped>
@@ -44,11 +84,12 @@
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 16px;
padding: 0 8px 0 16px;
position: relative;
width: 100%;
height: 44px;
border-bottom: 1px var(--color-border-2) solid;
user-select: none;
}
&-title {
@@ -60,6 +101,17 @@
text-overflow: ellipsis;
}
&-handler {
display: flex;
.handler-icon-wrapper {
margin-left: 8px;
color: rgb(var(--primary-6));
padding: 4px;
font-size: 16px;
}
}
&-main {
padding: 8px 8px 8px 16px;
position: relative;

View File

@@ -51,7 +51,6 @@
// 加载用户列表
// 加载主机列表
render.value = true;
});

View File

@@ -10,7 +10,7 @@ export const tabItems = [{
key: tabItemKeys.SETTING,
text: '分组配置',
icon: 'icon-unordered-list',
permission: []
permission: ['asset:host-group:query']
}, {
key: tabItemKeys.ROLE_GRANT,
text: '角色授权',
@@ -22,3 +22,9 @@ export const tabItems = [{
icon: 'icon-user',
permission: []
}];
// 创建前缀
export const createGroupGroupPrefix = 'create-';
// 根id
export const rootId = 0;

View File

@@ -231,6 +231,7 @@
for (let i = 0; i < children.length; i++) {
if (children[i].id === id) {
children.splice(i, 1);
break;
}
}
}