DataBindModal.vue
4.15 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
<script lang="ts" setup>
import { Tabs } from 'ant-design-vue';
import BasicModal from '/@/components/Modal/src/BasicModal.vue';
import BasicConfiguration from './BasicConfiguration.vue';
import VisualConfiguration from './VisualConfiguration.vue';
import { computed, ref, unref } from 'vue';
import { RouteParams, useRoute } from 'vue-router';
import { addDataComponent, updateDataComponent } from '/@/api/dataBoard';
import { useModalInner } from '/@/components/Modal';
import { DataBoardLayoutInfo } from '../../types/type';
import { useMessage } from '/@/hooks/web/useMessage';
import { decode } from '../../config/config';
import { ComponentInfo } from '/@/api/dataBoard/model';
interface DataComponentRouteParams extends RouteParams {
id: string;
}
const emit = defineEmits(['update', 'create', 'register']);
const ROUTE = useRoute();
const { createMessage } = useMessage();
const boardId = computed(() => {
return decode((ROUTE.params as DataComponentRouteParams).boardId as string);
});
const frontId = ref('');
const isEdit = ref(false);
const componentRecord = ref<DataBoardLayoutInfo>({} as unknown as DataBoardLayoutInfo);
const componentDefaultConfig = ref<Partial<ComponentInfo>>({});
const [register, { closeModal, changeOkLoading }] = useModalInner(
(data: { isEdit: boolean; record?: DataBoardLayoutInfo }) => {
componentRecord.value = data.record || ({} as unknown as DataBoardLayoutInfo);
frontId.value = data.record?.record?.frontId || '';
isEdit.value = data.isEdit || false;
}
);
const basicConfigurationEl = ref<{
getAllDataSourceFieldValue: Fn<any, Recordable>;
validate: Fn;
}>();
const resetForm = () => {
isEdit.value = false;
frontId.value = '';
componentRecord.value = {} as unknown as DataBoardLayoutInfo;
componentDefaultConfig.value = {};
};
const handleSubmit = async () => {
const { getAllDataSourceFieldValue, validate } = unref(basicConfigurationEl)!;
await validate();
const value = getAllDataSourceFieldValue();
unref(isEdit) ? handleUpdateComponent(value) : handleAddComponent(value);
resetForm();
};
const handleAddComponent = async (value: Recordable) => {
try {
if (!unref(frontId)) {
createMessage.warning('请选择可视化组件');
return;
}
changeOkLoading(true);
await addDataComponent({
boardId: unref(boardId),
record: { dataBoardId: unref(boardId), frontId: unref(frontId), ...value },
});
createMessage.success('创建成功');
closeModal();
emit('create');
} catch (error) {
// createMessage.error('创建失败');
} finally {
changeOkLoading(false);
}
};
const handleUpdateComponent = async (value: Recordable) => {
try {
changeOkLoading(true);
const res = await updateDataComponent({
boardId: unref(boardId),
record: {
id: unref(componentRecord).i,
dataBoardId: unref(boardId),
frontId: unref(frontId),
...value,
},
});
createMessage.success('修改成功');
closeModal();
// emit('submit');
emit('update', res.data.id);
} catch (error) {
// createMessage.error('修改失败');
} finally {
changeOkLoading(false);
}
};
const handleComponentCheckedChange = (record: ComponentInfo) => {
componentDefaultConfig.value = record;
};
</script>
<template>
<BasicModal
v-bind="$attrs"
@register="register"
title="自定义组件"
width="70%"
:destroy-on-close="true"
@ok="handleSubmit"
@cancel="resetForm"
>
<section>
<Tabs type="card">
<Tabs.TabPane key="1" tab="基础配置">
<BasicConfiguration
ref="basicConfigurationEl"
:front-id="frontId"
:record="componentRecord"
:defaultConfig="componentDefaultConfig"
/>
</Tabs.TabPane>
<Tabs.TabPane key="2" tab="可视化配置">
<VisualConfiguration v-model:value="frontId" @change="handleComponentCheckedChange" />
</Tabs.TabPane>
</Tabs>
</section>
</BasicModal>
</template>