import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Spin, notification, Modal } from 'antd'
|
|
import asyncComponent from '@/utils/asyncComponent'
|
import Api from '@/api'
|
import UtilsDM from '@/utils/utils-datamanage.js'
|
import MKEmitter from '@/utils/events.js'
|
import './index.scss'
|
|
const BraftContent = asyncComponent(() => import('@/tabviews/custom/components/share/braftContent'))
|
const NormalHeader = asyncComponent(() => import('@/tabviews/custom/components/share/normalheader'))
|
|
class BraftEditorContent extends Component {
|
static propTpyes = {
|
data: PropTypes.array, // 统一查询数据
|
config: PropTypes.object, // 组件配置信息
|
mainSearch: PropTypes.any, // 外层搜索条件
|
}
|
|
state = {
|
BID: '', // 上级ID
|
config: null, // 图表配置信息
|
loading: false, // 数据加载状态
|
sync: false, // 是否统一请求数据
|
data: {} // 数据
|
}
|
|
UNSAFE_componentWillMount () {
|
const { data, initdata } = this.props
|
let _config = fromJS(this.props.config).toJS()
|
|
let _data = { $$empty: true }
|
let _sync = false
|
|
let BID = ''
|
let BData = ''
|
|
if (_config.setting.supModule) {
|
BData = window.GLOB.CacheData.get(_config.setting.supModule)
|
} else {
|
BData = window.GLOB.CacheData.get(_config.$pageId)
|
}
|
if (BData) {
|
BID = BData.$BID || ''
|
}
|
|
if (_config.setting && _config.wrap.datatype === 'dynamic') {
|
_sync = _config.setting.sync === 'true'
|
|
if (_sync && data) {
|
_data = data[_config.dataName] || {$$empty: true}
|
if (_data && Array.isArray(_data)) {
|
_data = _data[0] || {$$empty: true}
|
}
|
_sync = false
|
} else if (_sync && initdata) {
|
_data = initdata
|
if (_data && Array.isArray(_data)) {
|
_data = _data[0] || {$$empty: true}
|
}
|
_sync = false
|
}
|
}
|
|
if (_config.wrap.minHeight) {
|
_config.style.minHeight = _config.wrap.minHeight
|
}
|
|
this.setState({
|
sync: _sync,
|
data: _data,
|
BID: BID || '',
|
config: _config,
|
arr_field: _config.columns.map(col => col.field).join(','),
|
}, () => {
|
if (_config.wrap.datatype === 'dynamic' && _config.setting && _config.setting.sync !== 'true' && _config.setting.onload === 'true') {
|
setTimeout(() => {
|
this.loadData()
|
}, _config.setting.delay || 0)
|
}
|
})
|
}
|
|
componentDidMount () {
|
const { config } = this.state
|
|
MKEmitter.addListener('reloadData', this.reloadData)
|
MKEmitter.addListener('resetSelectLine', this.resetParentParam)
|
|
if (config.wrap.datatype === 'public') {
|
MKEmitter.addListener('mkPublicData', this.mkPublicData)
|
}
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('reloadData', this.reloadData)
|
MKEmitter.removeListener('mkPublicData', this.mkPublicData)
|
MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
|
}
|
|
/**
|
* @description 图表数据更新,刷新内容
|
*/
|
UNSAFE_componentWillReceiveProps (nextProps) {
|
const { sync, config } = this.state
|
|
if (sync && !is(fromJS(this.props.data), fromJS(nextProps.data))) {
|
let _data = {$$empty: true}
|
if (nextProps.data && nextProps.data[config.dataName]) {
|
_data = nextProps.data[config.dataName]
|
if (_data && Array.isArray(_data)) {
|
_data = _data[0] || {$$empty: true}
|
}
|
}
|
|
this.setState({sync: false, data: _data})
|
} else if (config.setting.useMSearch && nextProps.mainSearch && !is(fromJS(this.props.mainSearch), fromJS(nextProps.mainSearch))) {
|
this.setState({}, () => {
|
this.loadData()
|
})
|
}
|
}
|
|
mkPublicData = (publicId, data) => {
|
const { config } = this.state
|
|
if (config.wrap.datatype === 'public' && config.wrap.publicId === publicId) {
|
let _data = fromJS(data).toJS()
|
|
this.setState({data: _data})
|
}
|
}
|
|
resetParentParam = (MenuID, id) => {
|
const { config } = this.state
|
|
if (!config.setting.supModule || config.setting.supModule !== MenuID) return
|
if (id !== this.state.BID || id !== '') {
|
this.setState({ BID: id }, () => {
|
this.loadData()
|
})
|
}
|
}
|
|
reloadData = (menuId) => {
|
const { config } = this.state
|
|
if (menuId !== config.uuid) return
|
|
this.loadData()
|
}
|
|
async loadData () {
|
const { mainSearch } = this.props
|
const { config, arr_field, BID } = this.state
|
|
if (config.wrap.datatype === 'public') {
|
MKEmitter.emit('reloadData', config.wrap.publicId)
|
return
|
}
|
|
if (config.wrap.datatype === 'static') {
|
this.setState({
|
data: {$$empty: true},
|
loading: false
|
})
|
return
|
} else if (config.setting.supModule && !BID) { // BID 不存在时,不做查询
|
this.setState({
|
data: {$$empty: true},
|
loading: false
|
})
|
return
|
}
|
|
let searches = config.setting.useMSearch && mainSearch ? mainSearch : []
|
|
let requireFields = searches.filter(item => item.required && item.value === '')
|
if (requireFields.length > 0) {
|
return
|
}
|
|
this.setState({
|
loading: true
|
})
|
|
let _orderBy = config.setting.order || ''
|
let param = UtilsDM.getQueryDataParams(config.setting, arr_field, searches, _orderBy, 1, 1, BID)
|
|
let result = await Api.genericInterface(param)
|
if (result.status) {
|
let _data = result.data && result.data[0] ? result.data[0] : {$$empty: true}
|
|
this.setState({
|
data: _data,
|
loading: false
|
})
|
|
if (result.message) {
|
if (result.ErrCode === 'Y') {
|
Modal.success({
|
title: result.message
|
})
|
} else if (result.ErrCode === 'S') {
|
notification.success({
|
top: 92,
|
message: result.message,
|
duration: 2
|
})
|
}
|
}
|
} else {
|
this.setState({
|
loading: false
|
})
|
|
if (!result.message) return
|
if (result.ErrCode === 'N') {
|
Modal.error({
|
title: result.message
|
})
|
} else if (result.ErrCode !== '-2') {
|
notification.error({
|
top: 92,
|
message: result.message,
|
duration: 10
|
})
|
}
|
}
|
}
|
|
render() {
|
const { config, loading, data } = this.state
|
|
if (config.wrap.empty === 'hidden' && (!data || data.$$empty)) return null
|
|
return (
|
<div className={'custom-braft-editor-box ' + (config.wrap.firstTr || '')} id={'anchor' + config.uuid} style={config.style}>
|
{loading ?
|
<div className="loading-mask">
|
<div className="ant-spin-blur"></div>
|
<Spin />
|
</div> : null
|
}
|
<NormalHeader config={config}/>
|
<BraftContent
|
value={config.wrap.datatype !== 'static' ? (data[config.wrap.field] || '') : config.html}
|
encryption={config.wrap.datatype !== 'static' ? config.wrap.encryption : 'false'}
|
/>
|
</div>
|
)
|
}
|
}
|
|
export default BraftEditorContent
|