import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Spin } 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 = {
|
config: PropTypes.object
|
}
|
|
state = {
|
BID: '', // 上级ID
|
config: null, // 图表配置信息
|
loading: false, // 数据加载状态
|
data: [] // 数据
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
let _config = fromJS(config).toJS()
|
let _data = []
|
|
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.wrap.datatype === 'dynamic') {
|
_config.setting.onload = _config.setting.sync === 'true' ? 'false' : _config.setting.onload || 'true'
|
|
if (_config.setting.supModule && !BID) {
|
_config.setting.onload = 'false'
|
}
|
|
if (_config.setting.sync === 'true' && window.GLOB.SyncData.has(_config.dataName)) {
|
_data = window.GLOB.SyncData.get(_config.dataName) || []
|
_config.setting.sync = 'false'
|
|
window.GLOB.SyncData.delete(_config.dataName)
|
}
|
} else if (_config.wrap.datatype === 'public' && window.GLOB.CacheData.has(_config.wrap.publicId)) {
|
_data = window.GLOB.CacheData.get(_config.wrap.publicId)
|
_data = fromJS(_data).toJS()
|
if (_data.$$empty) {
|
_data = []
|
} else {
|
_data = [_data]
|
}
|
} else if (_config.html) {
|
if (_config.wrap.prefunc) {
|
let _html = ''
|
try {
|
// eslint-disable-next-line
|
let func = new Function('html', 'data', _config.wrap.prefunc)
|
_html = func(_config.html, {})
|
} catch (e) {
|
_html = ''
|
console.warn(e)
|
}
|
|
_config.html = _html || _config.html
|
}
|
}
|
|
if (_config.wrap.minHeight) {
|
_config.style.minHeight = _config.wrap.minHeight
|
}
|
if (_config.wrap.firstTr === 'light') {
|
_config.wrap.tbStyle = 'th-light'
|
}
|
|
this.setState({
|
data: this.decodeHtml(_data, _config.wrap),
|
BID: BID || '',
|
config: _config,
|
})
|
}
|
|
componentDidMount () {
|
const { config } = this.state
|
|
MKEmitter.addListener('reloadData', this.reloadData)
|
MKEmitter.addListener('resetSelectLine', this.resetParentParam)
|
|
if (config.setting.sync === 'true') {
|
MKEmitter.addListener('transferSyncData', this.transferSyncData)
|
}
|
|
if (config.wrap.datatype === 'public') {
|
MKEmitter.addListener('mkPublicData', this.mkPublicData)
|
} else if (config.setting.useMSearch) {
|
MKEmitter.addListener('searchRefresh', this.searchRefresh)
|
}
|
|
this.initExec()
|
}
|
|
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('searchRefresh', this.searchRefresh)
|
MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
|
MKEmitter.removeListener('transferSyncData', this.transferSyncData)
|
}
|
|
initExec = () => {
|
const { config } = this.state
|
|
if (config.wrap.datatype === 'dynamic' && config.setting.onload === 'true') {
|
setTimeout(() => {
|
this.loadData()
|
}, config.setting.delay || 0)
|
}
|
}
|
|
transferSyncData = (syncId) => {
|
const { config } = this.state
|
|
if (config.$syncId !== syncId) return
|
|
let _data = window.GLOB.SyncData.get(config.dataName) || []
|
|
this.setState({data: this.decodeHtml(_data, config.wrap)})
|
|
window.GLOB.SyncData.delete(config.dataName)
|
|
MKEmitter.removeListener('transferSyncData', this.transferSyncData)
|
}
|
|
searchRefresh = (searchId) => {
|
const { config } = this.state
|
|
if (config.$searchId !== searchId) return
|
|
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()
|
if (_data.$$empty) {
|
_data = []
|
} else {
|
_data = [_data]
|
}
|
|
this.setState({data: this.decodeHtml(_data, config.wrap)})
|
}
|
}
|
|
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 }, () => {
|
if (config.wrap.datatype !== 'public') {
|
this.loadData()
|
}
|
})
|
}
|
}
|
|
reloadData = (menuId) => {
|
const { config } = this.state
|
|
if (menuId !== config.uuid) return
|
|
this.loadData()
|
}
|
|
async loadData () {
|
const { config, BID } = this.state
|
|
if (config.wrap.datatype === 'public') {
|
MKEmitter.emit('reloadData', config.wrap.publicId)
|
return
|
}
|
|
if (config.wrap.datatype === 'static') {
|
this.setState({
|
data: [],
|
loading: false
|
})
|
return
|
} else if (config.setting.supModule && !BID) { // BID 不存在时,不做查询
|
this.setState({
|
data: [],
|
loading: false
|
})
|
return
|
}
|
|
let searches = []
|
if (config.setting.useMSearch) { // 主表搜索条件
|
searches = window.GLOB.SearchBox.get(config.$searchId) || []
|
}
|
|
if (config.$s_req && searches.filter(item => item.required && item.value === '').length > 0) {
|
return
|
}
|
|
this.setState({
|
loading: true
|
})
|
|
let _orderBy = config.setting.order || ''
|
let param = UtilsDM.getQueryDataParams(config.setting, searches, _orderBy, 1, 1, BID)
|
|
let result = await Api.genericInterface(param)
|
if (result.status) {
|
this.setState({
|
data: this.decodeHtml(result.data, config.wrap),
|
loading: false
|
})
|
|
UtilsDM.querySuccess(result)
|
} else {
|
this.setState({
|
loading: false
|
})
|
|
UtilsDM.queryFail(result)
|
}
|
}
|
|
decodeHtml = (data, wrap) => {
|
if (!data || data.length === 0) return []
|
|
data.forEach(item => {
|
item.$html = item[wrap.field] || ''
|
if (item.$html) {
|
if (wrap.encryption === 'true') {
|
try {
|
item.$html = window.decodeURIComponent(window.atob(item.$html))
|
} catch (e) {
|
item.$html = item[wrap.field] || ''
|
}
|
}
|
|
delete item[wrap.field]
|
|
if (/\$[\s\S]+\$/.test(item.$html)) {
|
Object.keys(item).forEach(key => {
|
if (/^\$/.test(key)) return
|
let reg = new RegExp('\\$' + key + '\\$', 'ig')
|
item.$html = item.$html.replace(reg, item[key])
|
})
|
}
|
|
if (wrap.prefunc) {
|
let _html = ''
|
try {
|
// eslint-disable-next-line
|
let func = new Function('html', 'data', wrap.prefunc)
|
_html = func(item.$html, item)
|
} catch (e) {
|
_html = ''
|
console.warn(e)
|
}
|
|
item.$html = _html || item.$html
|
}
|
// if (/\$blank_space_\d+\$/ig.test(item.$html)) {
|
// item.$html = item.$html.replace(/\$blank_space_\d+\$/ig, (w) => {
|
// let n = +w.replace(/blank_space_|\$/ig, '')
|
// if (n) {
|
// return new Array(n).fill(' ').join('')
|
// }
|
|
// return w
|
// })
|
// }
|
}
|
})
|
|
return data
|
}
|
|
render() {
|
const { config, loading, data } = this.state
|
|
if (config.wrap.empty === 'hidden' && (!data || data.length === 0)) return null
|
|
return (
|
<div className={'custom-braft-editor-box ' + (config.wrap.tbStyle || '')} id={'anchor' + config.uuid} style={config.style}>
|
{loading ?
|
<div className="loading-mask">
|
<div className="ant-spin-blur"></div>
|
<Spin />
|
</div> : null
|
}
|
<NormalHeader config={config}/>
|
{config.wrap.datatype === 'static' ? <BraftContent
|
value={config.html}
|
/> : data.map((item, index) => <BraftContent
|
key={index}
|
value={item.$html}
|
script={config.wrap.loadedfunc || ''}
|
/>)}
|
</div>
|
)
|
}
|
}
|
|
export default BraftEditorContent
|