dataease/frontend/src/views/dataset/form.vue

288 lines
7.0 KiB
Vue
Raw Normal View History

<template>
2022-09-19 16:13:39 +08:00
<div class="de-dataset-form">
2022-09-27 17:46:51 +08:00
<div class="top">
2022-09-19 16:13:39 +08:00
<span class="name">
<i @click="back" class="el-icon-arrow-left"></i>
<svg-icon
style="margin: 0 9.5px 0 16.2px"
:icon-class="`de-${datasetType}-new`"
/>
<template v-if="showInput">
<el-input @blur="nameBlur" v-model="table.name"></el-input>
2022-09-19 17:51:06 +08:00
<div v-if="nameExsit" style="left: 55px" class="el-form-item__error">
{{ $t('deDataset.already_exists') }}
</div>
2022-09-19 16:13:39 +08:00
</template>
<span
:class="[{ 'show-point': ['sql', 'union'].includes(datasetType) }]"
v-else
@click="handleClick"
>{{ datasetName }}</span
>
</span>
<span class="oprate">
<span
class="table-num"
v-if="['db', 'excel', 'api'].includes(datasetType)"
2022-09-19 17:51:06 +08:00
>{{ $t('deDataset.selected') }} {{ tableNum }}
2022-09-28 13:04:06 +08:00
{{ ['excel'].includes(datasetType) ? $t('deDataset.table') : '项' }}</span
2022-09-19 16:13:39 +08:00
>
2022-09-19 17:51:06 +08:00
<deBtn @click="datasetSave" type="primary">{{
$t('commons.save')
}}</deBtn>
2022-09-19 16:13:39 +08:00
</span>
</div>
<div class="container">
<component
@setTableNum="(val) => (tableNum = val)"
:param="table"
:is="component"
ref="addDataset"
2022-09-27 17:46:51 +08:00
:originName="originName"
2022-09-19 16:13:39 +08:00
:nameList="nameList"
/>
</div>
</div>
</template>
<script>
2022-09-19 17:51:06 +08:00
import AddDB from './add/AddDB'
import AddApi from './add/AddApi'
import AddSQL from './add/AddSQL'
import AddExcel from './add/AddExcel'
import AddUnion from '@/views/dataset/add/AddUnion'
import { post } from '@/api/dataset/dataset'
2022-09-27 17:46:51 +08:00
import { datasetTypeMap } from './group/options'
export default {
2022-09-19 17:51:06 +08:00
name: 'DatasetForm',
2022-09-19 16:13:39 +08:00
components: { AddDB, AddSQL, AddExcel, AddApi, AddUnion },
data() {
return {
2022-09-19 17:51:06 +08:00
originName: '',
2022-09-19 16:13:39 +08:00
tableNum: 0,
showInput: false,
2022-09-19 17:51:06 +08:00
editType: '',
2022-09-19 16:13:39 +08:00
loading: false,
selectDatasets: [],
tData: [],
2022-09-19 17:51:06 +08:00
datasetType: '',
component: '',
2022-09-19 16:13:39 +08:00
table: {},
nameExsit: false,
nameList: [],
datasetForm: {
2022-09-19 17:51:06 +08:00
id: '',
name: '',
2022-09-19 16:13:39 +08:00
},
datasetFormRules: {
name: [
{
required: true,
2022-09-19 17:51:06 +08:00
message: this.$t('commons.input_content'),
trigger: 'change'
2022-09-19 16:13:39 +08:00
},
{
max: 50,
2022-09-19 17:51:06 +08:00
message: this.$t('commons.char_can_not_more_50'),
trigger: 'change'
2022-09-19 16:13:39 +08:00
},
2022-09-19 17:51:06 +08:00
{ required: true, trigger: 'blur', validator: this.nameValidator }
],
id: [
{
required: true,
message: this.$t('fu.search_bar.please_select'),
trigger: 'blur'
},
2022-09-19 17:51:06 +08:00
]
}
}
2022-09-19 16:13:39 +08:00
},
computed: {
datasetName() {
if (+this.editType === 0) {
2022-09-19 17:51:06 +08:00
return this.$t('dataset.excel_replace') + this.$t('chart.chart_data')
2022-09-19 16:13:39 +08:00
}
2022-09-19 16:13:39 +08:00
if (+this.editType === 1) {
2022-09-19 17:51:06 +08:00
return this.$t('dataset.excel_add') + this.$t('chart.chart_data')
2022-09-19 16:13:39 +08:00
}
2022-09-19 17:51:06 +08:00
return this.table.name || this.dialogTitle
}
2022-09-19 16:13:39 +08:00
},
created() {
2022-09-27 17:46:51 +08:00
const fromGroup = this.$route.params.fromGroup
const routeInfo = fromGroup ? this.$route.params : this.$route.query
const { datasetType, sceneId, id, editType, name } = routeInfo
2022-09-19 17:51:06 +08:00
this.datasetType = datasetType
this.editType = editType
2022-09-19 16:13:39 +08:00
if (id) {
2022-09-19 17:51:06 +08:00
this.initTable(id)
2022-09-19 16:13:39 +08:00
} else {
2022-09-27 17:46:51 +08:00
const name = name || this.$t('commons.create') + this.$t(datasetTypeMap[datasetType]) + this.$t('auth.datasetAuth')
this.table = {
name,
id: sceneId
}
this.getDatasetNameFromGroup(sceneId, name)
}
2022-09-19 17:51:06 +08:00
this.switchComponent(datasetType)
},
methods: {
2022-09-19 16:13:39 +08:00
back() {
this.$router.push('/dataset/index')
2022-09-19 16:13:39 +08:00
},
nameBlur() {
2022-09-19 17:51:06 +08:00
this.nameExsitValidator()
this.showInput = this.nameExsit
2022-09-19 16:13:39 +08:00
},
2022-09-27 17:46:51 +08:00
getDatasetNameFromGroup(sceneId, name) {
2022-09-19 16:13:39 +08:00
post(`/dataset/table/getDatasetNameFromGroup/${sceneId}`, null).then(
(res) => {
2022-09-19 17:51:06 +08:00
this.nameList = res.data
2022-09-27 17:46:51 +08:00
if (name && ['sql', 'union'].includes(this.datasetType)) {
this.nameBlur()
}
2022-09-19 16:13:39 +08:00
}
2022-09-19 17:51:06 +08:00
)
2022-09-19 16:13:39 +08:00
},
datasetSave() {
2022-09-19 17:51:06 +08:00
if (['sql', 'union'].includes(this.datasetType)) {
this.nameExsitValidator()
2022-09-19 16:13:39 +08:00
if (this.nameExsit) {
2022-09-19 17:51:06 +08:00
return
2022-09-19 16:13:39 +08:00
}
}
2022-09-19 17:51:06 +08:00
this.$refs.addDataset.save()
2022-09-19 16:13:39 +08:00
},
handleClick() {
2022-09-19 17:51:06 +08:00
if (['sql', 'union'].includes(this.datasetType)) {
this.showInput = true
2022-09-19 16:13:39 +08:00
}
},
nameRepeat(value) {
if (!this.nameList || this.nameList.length === 0) {
2022-09-19 17:51:06 +08:00
return false
2022-09-19 16:13:39 +08:00
}
2022-09-19 17:51:06 +08:00
return this.nameList.some((name) => name === value)
2022-09-19 16:13:39 +08:00
},
nameValidator(rule, value, callback) {
if (this.nameRepeat(value)) {
2022-09-19 17:51:06 +08:00
callback(new Error(this.$t('deDataset.already_exists')))
2022-09-19 16:13:39 +08:00
} else {
2022-09-19 17:51:06 +08:00
callback()
2022-09-19 16:13:39 +08:00
}
},
nameExsitValidator() {
if (!this.nameList || this.nameList.length === 0) {
2022-09-19 17:51:06 +08:00
this.nameExsit = false
return
2022-09-19 16:13:39 +08:00
}
2022-09-19 17:51:06 +08:00
this.nameExsit = this.nameList.some(
(name) => name === this.table.name && name !== this.originName
)
2022-09-19 16:13:39 +08:00
},
initTable(id) {
2022-09-19 17:51:06 +08:00
post('/dataset/table/getWithPermission/' + id, null)
2022-09-19 16:13:39 +08:00
.then((response) => {
2022-09-19 17:51:06 +08:00
const { sceneId: id, id: tableId, name } = response.data || {}
2022-09-19 16:13:39 +08:00
this.table = {
id,
tableId,
table: response.data,
2022-09-19 17:51:06 +08:00
name
}
this.getDatasetNameFromGroup(id)
this.originName = name
if (this.datasetType === 'excel') {
this.table.editType = +this.editType
2022-09-19 16:13:39 +08:00
}
})
2022-09-19 17:51:06 +08:00
.catch(() => {})
2022-09-19 16:13:39 +08:00
},
switchComponent(c) {
switch (c) {
2022-09-19 17:51:06 +08:00
case 'db':
this.component = AddDB
break
case 'sql':
this.component = AddSQL
break
case 'excel':
this.component = AddExcel
break
case 'union':
this.component = AddUnion
break
case 'api':
this.component = AddApi
break
2022-09-19 16:13:39 +08:00
default:
2022-09-19 17:51:06 +08:00
break
2022-09-19 16:13:39 +08:00
}
2022-09-19 17:51:06 +08:00
}
}
}
2022-09-19 16:13:39 +08:00
</script>
<style lang="scss">
.de-dataset-form {
.top {
height: 56px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24px;
box-shadow: 0 2px 2px 0 rgb(0 0 0 / 10%);
.show-point {
cursor: pointer;
}
.name {
font-family: PingFang SC;
font-size: 16px;
font-weight: 500;
display: flex;
align-items: center;
width: 50%;
position: relative;
2022-09-19 16:13:39 +08:00
.el-input {
min-width: 96px;
.el-input__inner {
line-height: 24px;
height: 24px;
}
}
i {
cursor: pointer;
}
}
.oprate {
.table-num {
font-family: PingFang SC;
font-size: 14px;
font-weight: 400;
margin-right: 16px;
color: var(--deTextPrimary, #1f2329);
}
}
}
.container {
width: 100%;
height: calc(100vh - 56px);
}
}
2022-09-19 16:13:39 +08:00
.dataset-filed {
height: 400px;
overflow-y: auto;
}
.tree-select-dataset {
display: none;
}
</style>