import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { fromJS } from 'immutable'
|
import { Icon, Modal, notification } from 'antd'
|
|
import Utils from '@/utils/utils.js'
|
import MKEmitter from '@/utils/events.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import './index.scss'
|
|
const PasteForm = asyncComponent(() => import('@/templates/zshare/pasteform'))
|
|
class PasteController extends Component {
|
static propTpyes = {
|
config: PropTypes.object, // 组件配置
|
updateConfig: PropTypes.func
|
}
|
|
state = {
|
visible: false
|
}
|
|
handleMenuClick = () => {
|
this.setState({visible: true})
|
}
|
|
resetconfig = (item, copyBtns, config) => {
|
let _uuid = Utils.getuuid()
|
|
if (item.OpenType === 'popview') {
|
let _cell = fromJS(item).toJS()
|
_cell.$originUuid = _cell.uuid
|
_cell.uuid = _uuid
|
copyBtns.set(_uuid, _cell)
|
}
|
|
if (item.uuid) {
|
item.uuid = _uuid
|
}
|
|
if (item.copyType === 'cardcell' && config.subtype === 'datacard') {
|
item.setting = item.setting || {}
|
item.$cardType = 'extendCard'
|
item.setting.width = item.setting.width || 6
|
|
if (item.elements) {
|
item.elements = item.elements.map(cell => {
|
if (cell.datatype === 'dynamic') {
|
cell.datatype = 'static'
|
}
|
cell.uuid = Utils.getuuid()
|
return cell
|
})
|
}
|
if (item.backElements) {
|
item.backElements = item.backElements.map(cell => {
|
if (cell.datatype === 'dynamic') {
|
cell.datatype = 'static'
|
}
|
cell.uuid = Utils.getuuid()
|
return cell
|
})
|
}
|
} else if (item.copyType === 'cardcell') {
|
item.setting = item.setting || {}
|
item.setting.width = item.setting.width || 6
|
delete item.$cardType
|
|
if (item.elements) {
|
item.elements = item.elements.map(cell => {
|
cell.uuid = Utils.getuuid()
|
return cell
|
})
|
}
|
if (item.backElements) {
|
item.backElements = item.backElements.map(cell => {
|
cell.uuid = Utils.getuuid()
|
return cell
|
})
|
}
|
} else if (item.copyType === 'cols') {
|
let loopCol = (col) => {
|
col.subcols = col.subcols.map(c => {
|
c.uuid = Utils.getuuid()
|
|
if (c.type === 'colspan' && c.subcols) {
|
c = loopCol(c)
|
} else if (c.type === 'custom' && c.elements) {
|
c.elements = c.elements.map(cell => {
|
cell.uuid = Utils.getuuid()
|
return cell
|
})
|
}
|
return c
|
})
|
|
return col
|
}
|
|
item.cols = item.cols.map(_item => {
|
_item.uuid = Utils.getuuid()
|
if (_item.type === 'colspan' && _item.subcols) {
|
_item = loopCol(_item)
|
} else if (_item.type === 'custom' && _item.elements) {
|
_item.elements = _item.elements.map(cell => {
|
cell.uuid = Utils.getuuid()
|
return cell
|
})
|
} else if (_item.type === 'action' && _item.elements) {
|
_item.elements = _item.elements.map(cell => {
|
let _uuid = Utils.getuuid()
|
if (cell.OpenType === 'popview') {
|
let _cell = fromJS(cell).toJS()
|
_cell.$originUuid = _cell.uuid
|
_cell.uuid = _uuid
|
copyBtns.set(_uuid, _cell)
|
}
|
cell.uuid = _uuid
|
return cell
|
})
|
}
|
return _item
|
})
|
}
|
|
return item
|
}
|
|
pasteSubmit = () => {
|
const { options } = this.props
|
this.pasteFormRef.handleConfirm().then(res => {
|
|
if (!options.includes(res.copyType)) {
|
notification.warning({ top: 92, message: '配置信息格式错误!', duration: 5 })
|
return
|
}
|
|
let type = res.copyType
|
let config = fromJS(this.props.config).toJS()
|
let copyBtns = new Map()
|
|
res = this.resetconfig(res, copyBtns, config)
|
delete res.copyType
|
|
copyBtns = [...copyBtns.values()]
|
|
if (copyBtns.length > 0) {
|
MKEmitter.emit('copyButtons', copyBtns)
|
}
|
|
if (config.type === 'form' && config.subtype === 'stepform') {
|
this.props.updateConfig(res)
|
this.setState({visible: false})
|
return
|
} else if (type === 'action') {
|
config.action = config.action || []
|
config.action = config.action.filter(item => !item.origin)
|
|
if (['line', 'bar', 'scatter'].includes(config.type) && !['excelOut', 'excelIn'].includes(res.OpenType)) {
|
notification.warning({ top: 92, message: '图表中不支持此类按钮!', duration: 5 })
|
return
|
}
|
MKEmitter.emit('addButton', config.uuid, res)
|
} else if (type === 'search' || type === 'form') {
|
config.search = config.search || []
|
config.search = config.search.filter(item => !item.origin)
|
|
let keys = config.search.map(item => item.field.toLowerCase())
|
|
if (type === 'form') {
|
if (['number', 'switch', 'textarea', 'fileupload', 'hint', 'color', 'funcvar'].includes(res.type)) {
|
res.type = 'text'
|
} else if (res.type === 'radio') {
|
res.type = 'select'
|
} else if (res.type === 'checkbox') {
|
res.type = 'multiselect'
|
} else if (res.type === 'datetime') {
|
res.type = 'date'
|
}
|
}
|
|
if (res.field && keys.includes(res.field.toLowerCase())) {
|
notification.warning({
|
top: 92,
|
message: '搜索字段已存在!',
|
duration: 5
|
})
|
return
|
}
|
|
config.search.push(res)
|
} else if (type === 'cardcell') {
|
config.subcards.push(res)
|
} else if (type === 'menucell') {
|
config.subMenus.push(res)
|
} else if (type === 'cols') {
|
config.cols = config.cols.filter(col => !col.origin)
|
|
let keys = config.cols.map(col => (col.field || '$empty'))
|
|
res.cols.forEach(col => {
|
if (!keys.includes(col.field)) {
|
config.cols.push(col)
|
}
|
})
|
}
|
|
this.props.updateConfig(config)
|
this.setState({visible: false})
|
|
notification.success({
|
top: 92,
|
message: '粘贴成功!',
|
duration: 2
|
})
|
})
|
}
|
|
render() {
|
const { visible } = this.state
|
|
return (
|
<div style={{display: 'inline-block'}}>
|
<Icon type="snippets" style={{color: 'purple'}} onClick={() => {this.setState({visible: true})}} />
|
<Modal
|
title="粘贴"
|
visible={visible}
|
width={600}
|
maskClosable={false}
|
onOk={this.pasteSubmit}
|
onCancel={() => {this.setState({visible: false})}}
|
destroyOnClose
|
>
|
<PasteForm wrappedComponentRef={(inst) => this.pasteFormRef = inst} inputSubmit={this.pasteSubmit}/>
|
</Modal>
|
</div>
|
)
|
}
|
}
|
|
export default PasteController
|