import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import {connect} from 'react-redux'
|
import { is, fromJS } from 'immutable'
|
import { Icon, Modal } from 'antd'
|
|
import zhCN from '@/locales/zh-CN/model.js'
|
import enUS from '@/locales/en-US/model.js'
|
import VerifyCard from './verifycard'
|
import './index.scss'
|
|
class DataSource extends Component {
|
static propTpyes = {
|
config: PropTypes.any,
|
updateConfig: PropTypes.func
|
}
|
|
state = {
|
dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
sourcelist: [],
|
mainSearch: [],
|
visible: false,
|
loading: false,
|
setting: null
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
this.setState({setting: fromJS(config.setting).toJS()})
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
editDataSource = () => {
|
const { config, menu } = this.props
|
|
let search = []
|
let parents = []
|
let _conf = config
|
let getParents = (box) => {
|
box.components.forEach(item => {
|
if (item.type !== 'tabs') return
|
|
item.subtabs.forEach(tab => {
|
if (_conf.parentId === tab.parentId && _conf.tabId === tab.uuid) {
|
parents.unshift(tab)
|
_conf = item
|
|
if (_conf.parentId && _conf.tabId) {
|
getParents(menu)
|
}
|
} else {
|
getParents(tab)
|
}
|
})
|
})
|
}
|
|
if (config.parentId && config.tabId) {
|
getParents(menu)
|
}
|
|
parents.unshift(menu)
|
|
parents.forEach(parent => {
|
parent.components.forEach(item => {
|
if (item.type === 'search') {
|
search = item.search
|
}
|
})
|
})
|
|
this.setState({
|
visible: true,
|
mainSearch: search
|
})
|
}
|
|
verifySubmit = () => {
|
const { config } = this.props
|
|
this.setState({loading: true})
|
this.verifyRef.submitDataSource().then(res => {
|
|
if (res.columns) {
|
res.columns = res.columns.map(item => {
|
|
if (/int/ig.test(item.datatype)) {
|
item.type = 'number'
|
item.decimal = 0
|
} else if (/Decimal/ig.test(item.datatype)) {
|
item.type = 'number'
|
item.decimal = +item.datatype.replace(/^Decimal\(18,/ig, '').replace(/\)/ig, '')
|
} else {
|
item.type = 'text'
|
item.fieldlength = +item.datatype.replace(/^Nvarchar\(/ig, '').replace(/\)/ig, '')
|
}
|
return item
|
})
|
}
|
|
this.setState({loading: false, visible: false})
|
this.props.updateConfig({...config, ...res})
|
}, () => {
|
this.setState({loading: false})
|
})
|
}
|
|
render () {
|
const { config, menu } = this.props
|
const { visible, dict, loading, mainSearch } = this.state
|
|
return (
|
<div className="model-datasource">
|
<Icon type="setting" onClick={() => this.editDataSource()} />
|
<Modal
|
wrapClassName="popview-modal"
|
title={'数据源配置'}
|
visible={visible}
|
width={'75vw'}
|
maskClosable={false}
|
style={{minWidth: '900px', maxWidth: '1200px'}}
|
okText={dict['model.submit']}
|
onOk={this.verifySubmit}
|
confirmLoading={loading}
|
onCancel={() => { this.setState({ visible: false }) }}
|
destroyOnClose
|
>
|
<VerifyCard
|
dict={dict}
|
menu={menu}
|
mainSearch={mainSearch}
|
config={config}
|
wrappedComponentRef={(inst) => this.verifyRef = inst}
|
/>
|
</Modal>
|
</div>
|
)
|
}
|
}
|
|
const mapStateToProps = (state) => {
|
return {
|
menu: state.customMenu
|
}
|
}
|
|
const mapDispatchToProps = () => {
|
return {}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(DataSource)
|