Files
dvcp_v2_webapp/packages/bigscreen/designer/components/datasourcePicker.vue

122 lines
4.1 KiB
Vue
Raw Normal View History

2023-10-09 16:46:02 +08:00
<template>
<section class="datasourcePicker">
<config-item label="数据类型">
2023-10-09 17:37:22 +08:00
<ai-select v-model="source.dataType" placeholder="请选择数据类型" :select-list="dataTypes"/>
2023-10-09 16:46:02 +08:00
</config-item>
2024-01-22 18:19:56 +08:00
<div class="codeEditor" v-if="['htmlData'].includes(source.dataType)">
2023-10-09 16:46:02 +08:00
<ai-dialog-btn :modal="false" dialog-title="编辑器" :customFooter="false"
2023-10-09 17:37:22 +08:00
@confirm="changeData(JSON.parse(content))" @open="content=contentstr">
2023-10-09 16:46:02 +08:00
<code-editor slot="btn" readonly :value="contentstr" :lang="dataLang" theme="github" width="100%" height="250"/>
<code-editor v-model="content" :lang="dataLang" theme="github" width="100%" height="440" wrap/>
</ai-dialog-btn>
</div>
2024-01-22 18:19:56 +08:00
<template v-else-if="source.dataType === 'staticData'">
2024-01-23 18:17:48 +08:00
<table-editor label="设置数据" v-model="tableData" :configs.sync="colConfigs" @data="changeData"/>
2024-01-22 18:19:56 +08:00
</template>
2023-10-09 17:37:22 +08:00
<config-item v-else-if="source.dataType === 'dynamicData'" label="数据源">
<ai-select v-model="source.sourceDataId" placeholder="请选择数据源" :instance="instance"
2023-10-09 16:46:02 +08:00
:prop="{label:'description'}" @change="changeData"
action="/app/appdiylargescreen/allDatasourceByPage"/>
</config-item>
2023-10-09 17:37:22 +08:00
<config-item label="接口地址" v-else-if="source.dataType === 'apiData'">
<el-input size="small" v-model="source.api" @change="changeData" placeholder="请输入数据接口URL"/>
2023-10-09 16:46:02 +08:00
</config-item>
</section>
</template>
<script>
import AiDialogBtn from "dui/packages/layout/AiDialogBtn.vue";
import ConfigItem from "./configItem.vue";
import {DvCompData} from "../config";
import CodeEditor from 'bin-ace-editor'
import 'brace/mode/json'
import 'brace/snippets/json';
import 'brace/theme/github';
import 'brace/theme/monokai';
2024-01-22 18:19:56 +08:00
import TableEditor from "./tableEditor.vue";
2023-10-09 16:46:02 +08:00
export default {
name: "datasourcePicker",
2024-01-22 18:19:56 +08:00
components: {TableEditor, ConfigItem, AiDialogBtn, CodeEditor},
2023-10-09 16:46:02 +08:00
model: {
event: "input",
prop: "options"
},
props: {
options: Object,
instance: Function
},
data() {
return {
dataTypes: Object.entries(DvCompData.types).map(e => ({id: e[0], label: e[1]})),
content: "",
2024-01-22 18:19:56 +08:00
loading: false,
2024-01-23 18:17:48 +08:00
tableData: [],
2024-01-22 18:19:56 +08:00
colConfigs: []
2023-10-09 16:46:02 +08:00
}
},
computed: {
contentstr: v => JSON.stringify(v.options.staticData),
2023-10-09 17:37:22 +08:00
dataLang: v => v.options.dataType == 'htmlData' ? 'html' : 'json',
source: {
set(v) {
this.$emit("input", v)
},
get() {
return this.options
}
}
2023-10-09 16:46:02 +08:00
},
methods: {
2023-10-09 17:37:22 +08:00
changeData(sdata) {
this.source.dataType == 'staticData' ? this.source.staticData = sdata :
new DvCompData(this.source.dataType, this.source, this.instance).getData().then(data => {
this.source[this.source.dataType] = data
})
2024-01-23 18:17:48 +08:00
},
initStaticDataProps() {
const columnProp = "name"
2024-01-29 10:32:11 +08:00
if (Array.isArray(this.options.staticData)) {
2024-02-07 15:27:16 +08:00
const columns = []
2024-01-29 10:32:11 +08:00
this.options.staticData.map((row, i) => {
const prop = `c${i || ""}`
this.colConfigs.push({label: row[columnProp], prop})
Object.entries(row).map(([k, v]) => {
if (/^v/.test(k)) {
const item = this.tableData[k.substring(1) || 0] || {}
item[prop] = v
this.tableData[k.substring(1) || 0] = item
2024-02-07 15:27:16 +08:00
} else if (k != columnProp) {
const index = columns.findIndex(e => k == e)
if (index > -1) {
const item = this.tableData[index] || {}
item[prop] = v
this.tableData[index] = item
} else {
columns.push(k)
const newIndex = columns.length - 1
const item = this.tableData[newIndex] || {}
item[prop] = v
this.tableData[newIndex] = item
}
2024-01-29 10:32:11 +08:00
}
})
2024-01-23 18:17:48 +08:00
})
2024-01-29 10:32:11 +08:00
this.tableData = this.tableData.map(e => ({...e, $cellEdit: false}))
}
2023-10-09 16:46:02 +08:00
}
},
2024-01-23 18:17:48 +08:00
created() {
//新版静态数据
this.initStaticDataProps()
}
2023-10-09 16:46:02 +08:00
}
</script>
<style scoped lang="scss">
.datasourcePicker {
.codeEditor {
position: relative;
padding-left: 10px;
}
}
</style>