import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Icon, Popover, Modal } from 'antd'
|
|
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 zhCN from '@/locales/zh-CN/model.js'
|
import enUS from '@/locales/en-US/model.js'
|
import './index.scss'
|
|
const WrapComponent = asyncIconComponent(() => import('./wrapsetting'))
|
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 { confirm } = Modal
|
|
class NoramlMenuBar extends Component {
|
static propTpyes = {
|
card: PropTypes.object,
|
deletecomponent: PropTypes.func,
|
updateConfig: PropTypes.func,
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
card: null,
|
back: false
|
}
|
|
UNSAFE_componentWillMount () {
|
const { card } = this.props
|
|
if (card.isNew) {
|
let _card = {
|
uuid: card.uuid,
|
type: card.type,
|
floor: card.floor,
|
tabId: '',
|
parentId: '',
|
dataName: card.dataName || '',
|
width: card.width || 24,
|
name: card.name,
|
subtype: card.subtype,
|
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) => {
|
let card = fromJS(this.state.card).toJS()
|
let _this = this
|
|
confirm({
|
content: '确定删除卡片吗?',
|
onOk() {
|
card.subMenus = card.subMenus.filter(item => item.uuid !== cell.uuid)
|
|
_this.setState({card})
|
_this.props.updateConfig(card)
|
},
|
onCancel() {}
|
})
|
}
|
|
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: 'user', name: '客户', url: '', color: '#ffffff', iconFont: 20, padding: 12, background: '#1890ff', imgWidth: '' },
|
style: {
|
paddingTop: '15px', paddingBottom: '15px'
|
},
|
isnew: true
|
}
|
|
if (card.subMenus.length > 0) {
|
newcard = fromJS(card.subMenus.slice(-1)[0]).toJS()
|
newcard.uuid = Utils.getuuid()
|
newcard.isnew = true
|
}
|
|
card.subMenus.push(newcard)
|
|
this.setState({card})
|
this.props.updateConfig(card)
|
}
|
|
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)
|
}
|
|
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">
|
<Icon className="plus" title="添加菜单" onClick={this.addMenu} type="plus" />
|
<WrapComponent config={card} updateConfig={this.updateComponent} />
|
<CopyComponent type="menubar" card={card}/>
|
<PasteComponent config={card} options={['menucell']} updateConfig={this.updateComponent} />
|
<Icon className="style" title="调整样式" onClick={this.changeStyle} type="font-colors" />
|
<UserComponent config={card}/>
|
<Icon className="close" title="删除组件" type="delete" onClick={() => this.props.deletecomponent(card.uuid)} />
|
</div>
|
} trigger="hover">
|
<Icon type="tool" />
|
</Popover>
|
{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>
|
)
|
}
|
}
|
|
export default NoramlMenuBar
|