ddf10R23">23
highlight-current
draggable
:default-expanded-keys="currentNodes"
:default-expand-all="defaultExpandAll"
node-key="uuid"
@node-click="getCurrentNode"
@node-contextmenu="cancelCurrent"
@ -39,6 +47,7 @@
v-model="data.alias"
class="tree-int"
size="small"
@click.stop
></el-input>
<common-icon
name="common-shanchu"
@ -51,6 +60,16 @@
</div>
<div class="__down">
<h5>后台jsonpath</h5>
<div class="attr-content">
<JsonEditor
v-model="jsonpathData"
class="editor"
current-mode="code"
:mode-list="['code']"
@blur="validate"
></JsonEditor>
</div>
<h5 class="title">属性内容</h5>
<div class="attr-content">
<JsonEditor
@ -65,7 +84,12 @@
</div>
<!--右列表-->
<div class="__right">
<h5 class="title">属性</h5>
<h5 class="title">
<span>数据总线</span>
<el-button size="small" type="primary" @click.stop="quickCreate">
快速创建
</el-button>
</h5>
<ul class="tag-list">
<li v-for="(item, index) in tagList" :key="index">
<h5 class="tag-title">
@ -82,12 +106,21 @@
{{ tag.name }}
</span>
</template>
<!-- <el-button size="small" @click.stop="">快速创建</el-button> -->
</div>
</li>
</ul>
</div>
</div>
<common-dialog
v-if="showAttrDialog"
title="新增自定义属性"
@close-dialog="closeDialog"
@confirm-dialog="confirmDialog"
>
<!-- 自定义属性 -->
<CustomDetail ref="customDetail" :attr-info="attrInfo"></CustomDetail>
</common-dialog>
</template>
<script setup lang="ts">
@ -103,21 +136,24 @@ import JsonEditor from 'json-editor-vue3';
import { v4 as uuidv4 } from 'uuid';
import { useRoute } from 'vue-router';
import CustomDetail from '../../panel/global/CustomDetail.vue';
const { proxy } = getCurrentInstance();
const route = useRoute();
const systemId = route.query.id;
const dataBus = JSON.parse(localStorage.getItem('panelGlobal'))?.[
`${systemId}`
];
const customAttrs = dataBus?.customAttrs?.data || [];
console.log(customAttrs);
const panelGlobal = JSON.parse(localStorage.getItem('panelGlobal'));
const dataBus = panelGlobal?.[`${systemId}`];
const customAttrs = ref<any>(dataBus?.customAttrs?.data || []);
const props = defineProps({
dataSource: {
type: Array,
default: () => []
},
isJsonPath: {
type: Boolean,
default: () => false
}
});
// 源数据
@ -134,6 +170,7 @@ const tagList = ref<any>([
]);
const treeData = ref<any>([...props.dataSource] || []);
const jsonData = ref<any>({});
const jsonpathData = ref<any>({});
const currentNode = ref<any>({});
const myTree = ref<any>(null);
@ -144,14 +181,16 @@ function formateSourceData(data: any) {
data.forEach((tag: any) => {
if (tag.type === item.type) {
tag.alias = '';
item.tags.push(tag);
if (!item.tags.some((el: any) => el.id === tag.id)) {
item.tags.push(tag);
}
}
});
return item;
});
}
formateSourceData(customAttrs);
formateSourceData(customAttrs.value);
/**
* el-tree拖拽start
*/
@ -175,7 +214,7 @@ const allowDrag = (draggingNode: any) => {
console.log(draggingNode);
return true;
};
const defaultExpandAll = ref(false);
function handleDragStart() {}
function handleDragEnter() {}
function handleDragLeave() {}
@ -183,6 +222,15 @@ function handleDragOver() {}
function handleDragEnd() {}
function handleDrop() {}
// 展开全部
function expandAll() {
defaultExpandAll.value = true;
}
// 清除树组件数据
function clearAll() {
treeData.value = [];
}
// 获取当前节点
function getCurrentNode(node: any) {
if (currentNode.value === node) {
@ -207,10 +255,25 @@ function handleClickTag(tag: any) {
type: tag.type,
children: []
};
if (tag.type === 'array') {
proxy
.$confirm('需要创建有迭代器的数组吗?', '提示', {
confirmButtonText: '是的',
cancelButtonText: '不是',
type: 'warning'
})
.then(() => {
newChild.type = 'array-interator';
})
.catch(() => {
newChild.type = 'array-null';
});
}
if (currentNode.value?.type) {
if (
currentNode.value?.type === 'object' ||
currentNode.value?.type === 'array'
currentNode.value?.type.indexOf('array') > -1
) {
if (
!currentNode.value.children.some((el: any) => el.name === newChild.name)
@ -228,11 +291,12 @@ function handleClickTag(tag: any) {
} else {
treeData.value.push(newChild);
}
console.log(JSON.stringify(treeData.value));
nextTick(() => {
myTree.value.setCurrentKey(currentNode.value.uuid);
});
// myTree.value.updateKeyChildren(treeData.value[0]);
// console.log(myTree.value.getCurrentKey());
}
function cancelCurrent() {
@ -266,6 +330,8 @@ const defaultvalueMap = <any>{
int: 0,
boolean: false,
array: [],
'array-interator': [],
'array-null': [],
long: 0,
date: '',
datetime: '',
@ -273,19 +339,103 @@ const defaultvalueMap = <any>{
};
// 数据处理
function tree2json(data: any[], type: 'array' | 'object'): any {
function tree2json(
data: any[],
type: 'array-interator' | 'array-null' | 'object'
): any {
const obj: any = {};
data.forEach((item) => {
const { children } = item;
if (children && children.length) {
const childType = item.type;
obj[item.id] = tree2json(children, childType as 'array' | 'object');
} else {
// obj[item.name] = assignment(item.type);
obj[item.id] = defaultvalueMap[item.type];
data.map(({ children = [], type, alias, id }) => {
const key = alias || id;
obj[key] = children.length
? tree2json(children, type)
: defaultvalueMap[type];
return null;
});
return type.indexOf('array') > -1 ? [obj] : obj;
}
/**
*
* @param data 数据源
* @param type 数据类型
* 方法说明:
* 1. 正常取值转换,通过对databus中的基础数据进行直接赋值的操作;
* 2. 对象型数据转换,从模型中取数进行数据的赋值;
* 3. 数组数据转换;1)需要迭代器的数据格式;2)不需要迭代器的数据格式;
*/
// tree to jsonpath
function arrayNullFunc(item: any) {
const { children, id } = item;
let values;
if (children.length) {
values = children.map((child: any) => {
if (child.type === 'array-null' && child.children.length) {
const childKey = child.alias || child.id;
return {
[childKey]: {
_$$_isInterator: false,
_$$_interator: '',
values: arrayNullFunc(child)
}
};
}
if (
(child.type === 'object' || child.type === 'array-interator') &&
child.children.length
) {
// eslint-disable-next-line no-use-before-define
return tree2Jsonpath(child.children);
}
return `$.${child.id}`;
});
} else {
values = `$.${id}`;
}
return values;
}
function tree2Jsonpath(data: any) {
const result = {};
const typeMap = {
'array-null': (item: any) => {
const { id, alias } = item;
const key = alias || id;
const values = arrayNullFunc(item);
result[key] = {
_$$_isInterator: false,
_$$_interator: '',
values
};
},
'array-interator': (item: any) => {
const { children, id, alias } = item;
const key = alias || id;
result[key] = {
_$$_isInterator: true,
_$$_interator: `$.${id}`,
values: [tree2Jsonpath(children)]
};
},
object: (item: any) => {
const { children, id, alias } = item;
const key = alias || id;
result[key] = tree2Jsonpath(children);
},
default: (item: any) => {
const { id, alias } = item;
const key = alias || id;
result[key] = `$.${id}`;
}
};
data.forEach((item: any) => {
const { type } = item;
const fn = typeMap[type] || typeMap.default;
fn(item);
});
return type === 'array' ? [obj] : obj;
return result;
}
function validate() {}
@ -294,14 +444,48 @@ watch(
() => {
if (treeData.value?.length) {
jsonData.value = tree2json(treeData.value, 'object');
// myTree.value.setCurrentKey(currentNode.value.uuid);
jsonpathData.value = tree2Jsonpath(treeData.value);
} else {
currentNode.value = {};
jsonData.value = {};
jsonpathData.value = {};
}
},
{ deep: true }
);
// 快速创建databus数据
const attrInfo = ref<any>([]);
const showAttrDialog = ref<any>(false);
const customDetail = ref<any>(null);
function quickCreate() {
showAttrDialog.value = true;
}
// 关闭弹窗
function closeDialog() {
showAttrDialog.value = false;
}
function confirmEvent() {}
const btns = [
// { label: '编辑', name: 'edit', onClick: editFn },
{ label: '删除', name: 'delete', onClick: confirmEvent }
];
// 确认弹窗
function confirmDialog() {
const data = customDetail.value.attrList;
data.forEach((item: any) => {
if (!customAttrs.value.some((el: any) => el.id === item.id)) {
item.actions = btns;
customAttrs.value.push(item);
}
});
formateSourceData(customAttrs.value);
dataBus.customAttrs.data = customAttrs.value;
panelGlobal[`${systemId}`] = dataBus;
localStorage.setItem('panelGlobal', JSON.stringify(panelGlobal));
closeDialog();
}
const currentNodes = ref<any>([]);
watch(
@ -314,12 +498,14 @@ watch(
defineExpose({
treeData,
jsonData
jsonData,
jsonpathData
});
onMounted(() => {
if (treeData.value?.length) {
jsonData.value = tree2json(treeData.value, 'object');
jsonpathData.value = tree2Jsonpath(treeData.value);
} else {
currentNode.value = {};
}
@ -337,7 +523,11 @@ onMounted(() => {
width: calc(60% - 7px);
.__up {
height: calc(50% - 20px);
.title {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
}
.tree-content {
background: #ffffff;
border: 1px solid rgba(0, 0, 0, 0.15);
@ -371,7 +561,12 @@ onMounted(() => {
.__right {
width: calc(40% - 8px);
height: 100%;
.title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 5px;
}
.tag-list {
background-color: #ffffff;
border: 1px solid rgba(0, 0, 0, 0.15);
|
||
25 | 25 |
|
26 | 26 |
|
27 | 27 |
|
28 |
|
|
28 | 29 |
|
29 | 30 |
|
30 | 31 |
|
31 | 32 |
|
32 | 33 |
|
33 |
|
|
34 |
|
|
34 | 35 |
|
35 | 36 |
|
36 | 37 |
|
|
||
69 | 70 |
|
70 | 71 |
|
71 | 72 |
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
72 | 78 |
|
73 | 79 |
|
74 | 80 |
|