import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Popover, Modal } from 'antd'
|
import { ToolOutlined, PlusOutlined, SettingOutlined, EditOutlined, FontColorsOutlined, DeleteOutlined } from '@ant-design/icons'
|
|
import asyncComponent from '@/utils/asyncComponent'
|
import asyncIconComponent from '@/utils/asyncIconComponent'
|
import { resetStyle } from '@/utils/utils-custom.js'
|
import MKEmitter from '@/utils/events.js'
|
import Utils from '@/utils/utils.js'
|
import getWrapForm from './options'
|
import './index.scss'
|
|
const NormalForm = asyncIconComponent(() => import('@/components/normalform'))
|
const MenuComponent = asyncComponent(() => import('./menucomponent'))
|
const CopyComponent = asyncIconComponent(() => import('@/menu/components/share/copycomponent'))
|
const PasteComponent = asyncIconComponent(() => import('@/menu/components/share/pastecomponent'))
|
const UserComponent = asyncIconComponent(() => import('@/menu/components/share/usercomponent'))
|
const NormalHeader = asyncComponent(() => import('@/menu/components/share/normalheader'))
|
const SettingComponent = asyncIconComponent(() => import('@/menu/datasource'))
|
|
const { confirm } = Modal
|
|
class NoramlMenuBar extends Component {
|
static propTpyes = {
|
card: PropTypes.object,
|
deletecomponent: PropTypes.func,
|
updateConfig: PropTypes.func,
|
}
|
|
state = {
|
card: null,
|
back: false
|
}
|
|
UNSAFE_componentWillMount () {
|
const { card } = this.props
|
|
if (card.isNew) {
|
let _card = {
|
uuid: card.uuid,
|
type: card.type,
|
tabId: '',
|
parentId: '',
|
format: 'object', // 组件属性 - 数据格式
|
pageable: false, // 组件属性 - 是否可分页
|
switchable: false, // 组件属性 - 数据是否可切换
|
dataName: card.dataName || '',
|
width: card.width || 24,
|
name: card.name,
|
subtype: card.subtype,
|
setting: { interType: 'system' },
|
wrap: { name: card.name, width: card.width || 24, title: '' },
|
style: { marginLeft: '0px', marginRight: '0px', marginTop: '8px', marginBottom: '8px' },
|
headerStyle: { fontSize: '16px', borderBottomWidth: '1px', borderBottomColor: '#e8e8e8' },
|
subMenus: [{
|
uuid: Utils.getuuid(),
|
setting: { type: 'menu', width: 6, sign: 'icon', icon: 'user', name: '客户', url: '', color: '#ffffff', iconFont: 20, padding: 12, background: '#1890ff', imgWidth: '' },
|
style: {
|
paddingTop: '15px', paddingBottom: '15px'
|
}
|
}]
|
}
|
|
if (card.config) {
|
let config = fromJS(card.config).toJS()
|
|
_card.wrap = config.wrap
|
_card.wrap.name = card.name
|
_card.style = config.style
|
_card.headerStyle = config.headerStyle
|
|
_card.subMenus = config.subMenus.map(item => {
|
item.uuid = Utils.getuuid()
|
|
return item
|
})
|
}
|
this.setState({
|
card: _card
|
})
|
this.props.updateConfig(_card)
|
} else {
|
this.setState({
|
card: fromJS(card).toJS()
|
})
|
}
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('submitStyle', this.getStyle)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @description 组件销毁,清除state更新,清除快捷键设置
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('submitStyle', this.getStyle)
|
}
|
|
/**
|
* @description 卡片行外层信息更新(数据源,样式等)
|
*/
|
updateComponent = (component) => {
|
this.setState({
|
card: component
|
})
|
|
component.width = component.wrap.width
|
component.name = component.wrap.name
|
|
this.props.updateConfig(component)
|
}
|
|
/**
|
* @description 单个卡片信息更新
|
*/
|
updateCard = (cell) => {
|
let card = fromJS(this.state.card).toJS()
|
|
card.subMenus = card.subMenus.map(item => {
|
if (item.uuid === cell.uuid) return cell
|
if (cell.oriuuid && item.uuid === cell.oriuuid) {
|
delete cell.oriuuid
|
|
return cell
|
}
|
return item
|
})
|
|
this.setState({card})
|
|
this.props.updateConfig(card)
|
}
|
|
/**
|
* @description 单个卡片信息更新
|
*/
|
deleteCard = (cell, type) => {
|
let card = fromJS(this.state.card).toJS()
|
let _this = this
|
|
if (type !== 'direct') {
|
confirm({
|
content: '确定删除卡片吗?',
|
onOk() {
|
card.subMenus = card.subMenus.filter(item => item.uuid !== cell.uuid)
|
|
_this.setState({card})
|
_this.props.updateConfig(card)
|
},
|
onCancel() {}
|
})
|
} else {
|
card.subMenus = card.subMenus.filter(item => item.uuid !== cell.uuid)
|
|
this.setState({card})
|
this.props.updateConfig(card)
|
}
|
}
|
|
changeStyle = () => {
|
const { card } = this.state
|
|
MKEmitter.emit('changeStyle', [card.uuid], ['background', 'border', 'padding', 'margin', 'shadow'], card.style)
|
}
|
|
getStyle = (comIds, style) => {
|
const { card } = this.state
|
|
if (comIds.length !== 1 || comIds[0] !== card.uuid) return
|
|
let _card = {...card, style}
|
|
this.setState({
|
card: _card
|
})
|
|
this.props.updateConfig(_card)
|
}
|
|
addMenu = () => {
|
let card = fromJS(this.state.card).toJS()
|
|
let newcard = {
|
uuid: Utils.getuuid(),
|
setting: { type: 'menu', width: 6, sign: 'icon', icon: '', name: '', url: '', color: '#ffffff', iconFont: 20, padding: 12, background: '#1890ff', imgWidth: '' },
|
style: {
|
paddingTop: '15px', paddingBottom: '15px'
|
}
|
}
|
|
if (card.subMenus.length > 0) {
|
newcard = fromJS(card.subMenus.slice(-1)[0]).toJS()
|
newcard.uuid = Utils.getuuid()
|
|
newcard.setting.clearMenu = 'true'
|
newcard.setting.icon = ''
|
newcard.setting.name = ''
|
newcard.setting.MenuNo = ''
|
newcard.setting.type = 'menu'
|
newcard.setting.copyMenuId = ''
|
newcard.setting.linkMenuId = ''
|
newcard.setting.linkurl = ''
|
newcard.setting.tip = ''
|
newcard.setting.url = ''
|
}
|
|
card.subMenus.push(newcard)
|
|
this.setState({card})
|
this.props.updateConfig(card)
|
|
setTimeout(() => {
|
let node = document.getElementById(newcard.uuid)
|
node && node.click()
|
}, 200)
|
}
|
|
move = (item, direction) => {
|
let card = fromJS(this.state.card).toJS()
|
|
let dragIndex = card.subMenus.findIndex(c => c.uuid === item.uuid)
|
let hoverIndex = null
|
|
if (direction === 'left') {
|
hoverIndex = dragIndex - 1
|
} else {
|
hoverIndex = dragIndex + 1
|
}
|
|
if (hoverIndex === -1 || hoverIndex === card.subMenus.length) return
|
|
card.subMenus.splice(hoverIndex, 0, ...card.subMenus.splice(dragIndex, 1))
|
|
this.setState({card})
|
this.props.updateConfig(card)
|
}
|
|
getWrapForms = () => {
|
const { card } = this.state
|
|
return getWrapForm(card.wrap)
|
}
|
|
updateWrap = (res) => {
|
let card = {...this.state.card, wrap: res}
|
if (res.datatype === 'dynamic' && !card.format) {
|
card.format = 'object'
|
card.pageable = false
|
card.switchable = false
|
card.setting = { interType: 'system' }
|
card.columns = []
|
card.scripts = []
|
}
|
this.updateComponent(card)
|
}
|
|
clickComponent = (e) => {
|
if (sessionStorage.getItem('style-control') === 'true' || sessionStorage.getItem('style-control') === 'component') {
|
e.stopPropagation()
|
MKEmitter.emit('clickComponent', this.state.card)
|
}
|
}
|
|
render() {
|
const { card } = this.state
|
|
let offset = 0
|
if (card.wrap.cardFloat && card.wrap.cardFloat !== 'left') {
|
let _width = 0
|
card.subMenus.forEach(card => {
|
_width += card.setting.width
|
})
|
offset = _width < 24 ? 24 - _width : 0
|
if (card.wrap.cardFloat === 'center') {
|
offset = Math.floor(offset / 2)
|
}
|
}
|
|
let _style = resetStyle(card.style)
|
|
return (
|
<div className="menu-menubar-edit-box" style={_style} onClick={this.clickComponent} id={card.uuid}>
|
{card.wrap.title ? <NormalHeader config={card} updateComponent={this.updateComponent}/> : null}
|
<Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
|
<div className="mk-popover-control">
|
<PlusOutlined className="plus" title="添加菜单" onClick={this.addMenu}/>
|
<NormalForm title="菜单设置" width={800} update={this.updateWrap} getForms={this.getWrapForms}>
|
<EditOutlined style={{color: '#1890ff'}} title="编辑"/>
|
</NormalForm>
|
<CopyComponent type="menubar" card={card}/>
|
<PasteComponent config={card} options={['menucell']} updateConfig={this.updateComponent} />
|
<FontColorsOutlined className="style" title="调整样式" onClick={this.changeStyle} />
|
<UserComponent config={card}/>
|
<DeleteOutlined className="close" title="删除组件" onClick={() => this.props.deletecomponent(card.uuid)} />
|
{card.wrap.datatype === 'dynamic' ? <SettingComponent config={card} updateConfig={this.updateComponent} /> : null}
|
{card.wrap.datatype !== 'dynamic' ? <SettingOutlined style={{color: '#eeeeee', cursor: 'not-allowed'}}/> : null}
|
</div>
|
} trigger="hover">
|
<ToolOutlined />
|
</Popover>
|
<div className={(card.wrap.layout || 'grid') + '-layout'}>
|
{card.subMenus.map((menu, index) => (<MenuComponent key={menu.uuid} offset={!index ? offset : 0} cards={card} card={menu} move={this.move} updateElement={this.updateCard} deleteElement={this.deleteCard}/>))}
|
</div>
|
</div>
|
)
|
}
|
}
|
|
export default NoramlMenuBar
|