import React, { Component } from 'react'
|
import PropTypes from 'prop-types'
|
import { connect } from 'react-redux'
|
import { DndProvider } from 'react-dnd'
|
import { is, fromJS } from 'immutable'
|
import moment from 'moment'
|
import HTML5Backend from 'react-dnd-html5-backend'
|
import { notification, Modal, Collapse, Card, Switch, Button } from 'antd'
|
|
import Api from '@/api'
|
import Utils from '@/utils/utils.js'
|
import zhCN from '@/locales/zh-CN/mob.js'
|
import enUS from '@/locales/en-US/mob.js'
|
import MKEmitter from '@/utils/events.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import { modifyCustomMenu } from '@/store/action'
|
|
import './index.scss'
|
|
const { Panel } = Collapse
|
const { confirm } = Modal
|
|
const MenuForm = asyncComponent(() => import('./menuform'))
|
const SourceWrap = asyncComponent(() => import('@/menu/modelsource'))
|
const MenuShell = asyncComponent(() => import('@/menu/menushell'))
|
const BgController = asyncComponent(() => import('@/menu/bgcontroller'))
|
const PasteController = asyncComponent(() => import('@/menu/pastecontroller'))
|
const PaddingController = asyncComponent(() => import('@/menu/padcontroller'))
|
const StyleCombControlButton = asyncComponent(() => import('@/menu/stylecombcontrolbutton'))
|
const TableComponent = asyncComponent(() => import('@/templates/sharecomponent/tablecomponent'))
|
|
sessionStorage.setItem('isEditState', 'true')
|
|
class MenuDesign extends Component {
|
static propTpyes = {
|
btn: PropTypes.object,
|
handleBack: PropTypes.func
|
}
|
|
state = {
|
dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
MenuType: '',
|
MenuId: '',
|
MenuNo: '',
|
tableFields: [],
|
delButtons: [],
|
activeKey: 'basedata',
|
menuloading: false,
|
oriConfig: null,
|
openEdition: '',
|
config: null,
|
}
|
|
UNSAFE_componentWillMount() {
|
const { btn } = this.props
|
|
this.setState({
|
MenuId: btn.uuid,
|
}, () => {
|
this.getMenuParam()
|
})
|
}
|
|
componentDidMount () {
|
MKEmitter.addListener('delButtons', this.delButtons)
|
MKEmitter.addListener('submitComponentStyle', this.updateComponentStyle)
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @description 组件销毁,清除state更新
|
*/
|
componentWillUnmount () {
|
this.setState = () => {
|
return
|
}
|
MKEmitter.removeListener('delButtons', this.delButtons)
|
MKEmitter.removeListener('submitComponentStyle', this.updateComponentStyle)
|
}
|
|
updateComponentStyle = (parentId, keys, style) => {
|
const { config } = this.state
|
|
if (config.uuid !== parentId) return
|
|
let components = config.components.map(item => {
|
if (keys.includes(item.uuid)) {
|
item.style = {...item.style, ...style}
|
}
|
return item
|
})
|
|
this.setState({
|
config: {...config, components: []}
|
}, () => {
|
this.setState({
|
config: {...config, components: components}
|
})
|
})
|
}
|
|
delButtons = (items) => {
|
this.setState({delButtons: [...this.state.delButtons, ...items]})
|
}
|
|
closeView = () => {
|
const { oriConfig, config } = this.state
|
|
if (!config) {
|
this.props.handleBack()
|
return
|
}
|
|
let _config = fromJS(config).toJS()
|
delete _config.tableFields
|
const _this = this
|
|
if (!is(fromJS(oriConfig), fromJS(_config))) {
|
confirm({
|
title: '配置已修改,放弃保存吗?',
|
content: '',
|
onOk() {
|
_this.props.handleBack()
|
},
|
onCancel() {}
|
})
|
} else {
|
this.props.handleBack()
|
}
|
}
|
|
getMenuParam = () => {
|
const { btn } = this.props
|
const { MenuId } = this.state
|
|
let param = {
|
func: 'sPC_Get_LongParam',
|
MenuID: MenuId
|
}
|
|
Api.getSystemConfig(param).then(result => {
|
if (result.status) {
|
let config = null
|
|
try {
|
config = result.LongParam ? JSON.parse(window.decodeURIComponent(window.atob(result.LongParam))) : null
|
} catch (e) {
|
console.warn('Parse Failure')
|
config = null
|
}
|
|
if (!config) {
|
config = {
|
version: 1.0,
|
uuid: MenuId,
|
MenuID: MenuId,
|
Template: 'CustomPage',
|
enabled: false,
|
MenuName: btn.config.MenuName + '-' + btn.label,
|
MenuNo: '',
|
tables: [],
|
components: [],
|
viewType: 'popview',
|
style: {
|
backgroundColor: '#ffffff', backgroundImage: '',
|
paddingTop: '16px', paddingBottom: '80px', paddingLeft: '16px', paddingRight: '16px'
|
},
|
}
|
} else {
|
config.uuid = MenuId
|
config.MenuID = MenuId
|
}
|
|
this.setState({
|
oriConfig: config,
|
config: fromJS(config).toJS(),
|
openEdition: result.open_edition || '',
|
})
|
|
this.props.modifyCustomMenu(config)
|
} else {
|
notification.warning({
|
top: 92,
|
message: result.message,
|
duration: 5
|
})
|
}
|
})
|
}
|
|
getMenuMessage = () => {
|
const { config } = this.state
|
let buttons = []
|
let _sort = 1
|
|
let traversal = (components) => {
|
components.forEach(item => {
|
if (item.type === 'tabs') {
|
item.subtabs.forEach(tab => {
|
traversal(tab.components)
|
})
|
} else if (item.type === 'group') {
|
traversal(item.components)
|
} else if (item.type === 'card' || (item.type === 'table' && item.subtype === 'tablecard')) {
|
item.action && item.action.forEach(btn => {
|
buttons.push(`select '${btn.uuid}' as menuid, '${item.name + '-' + btn.label}' as menuname, '${_sort * 10}' as Sort`)
|
_sort++
|
})
|
item.subcards.forEach(card => {
|
card.elements && card.elements.forEach(cell => {
|
if (cell.eleType !== 'button') return
|
|
buttons.push(`select '${cell.uuid}' as menuid, '${item.name + '-' + cell.label}' as menuname, '${_sort * 10}' as Sort`)
|
_sort++
|
})
|
card.backElements && card.backElements.forEach(cell => {
|
if (cell.eleType !== 'button') return
|
|
buttons.push(`select '${cell.uuid}' as menuid, '${item.name + '-' + cell.label}' as menuname, '${_sort * 10}' as Sort`)
|
_sort++
|
})
|
})
|
} else if (item.type === 'line' || item.type === 'bar') {
|
item.action && item.action.forEach(btn => {
|
buttons.push(`select '${btn.uuid}' as menuid, '${item.name + '-' + btn.label}' as menuname, '${_sort * 10}' as Sort`)
|
_sort++
|
})
|
} else if (item.type === 'table' && item.subtype === 'normaltable') {
|
item.action && item.action.forEach(btn => {
|
if (btn.origin) return
|
buttons.push(`select '${btn.uuid}' as menuid, '${item.name + '-' + btn.label}' as menuname, '${_sort * 10}' as Sort`)
|
_sort++
|
})
|
item.cols && item.cols.forEach(col => {
|
if (col.type !== 'action') return
|
col.elements.forEach(btn => {
|
buttons.push(`select '${btn.uuid}' as menuid, '${item.name + '-' + btn.label}' as menuname, '${_sort * 10}' as Sort`)
|
_sort++
|
})
|
})
|
}
|
})
|
}
|
|
traversal(config.components)
|
|
return buttons
|
}
|
|
filterConfig = (components) => {
|
return components.map(item => {
|
if (item.type === 'tabs') {
|
item.subtabs.forEach(tab => {
|
tab.components = this.filterConfig(tab.components)
|
})
|
} else if (item.type === 'group') {
|
item.components = this.filterConfig(item.components)
|
} else if (item.type === 'table' && item.subtype === 'normaltable') {
|
item.search = item.search.filter(a => !a.origin)
|
item.action = item.action.filter(a => !a.origin)
|
item.cols = item.cols.filter(a => !a.origin)
|
}
|
return item
|
})
|
}
|
|
submitConfig = () => {
|
const { btn } = this.props
|
const { openEdition, delButtons } = this.state
|
let config = fromJS(this.state.config).toJS()
|
|
if ((config.cacheUseful === 'true' && !config.cacheTime) || !config.MenuNo || !config.MenuName) {
|
notification.warning({
|
top: 92,
|
message: '请完善菜单基本信息!',
|
duration: 5
|
})
|
return
|
}
|
|
config.components = this.filterConfig(config.components)
|
|
if (config.enabled && this.verifyConfig()) {
|
config.enabled = false
|
}
|
|
let _config = fromJS(config).toJS()
|
delete _config.tableFields
|
|
let _name = (btn.component.name ? btn.component.name + '-' : '') + btn.label
|
|
let param = {
|
func: 'sPC_ButtonParam_AddUpt',
|
ParentID: btn.config.uuid,
|
MenuID: _config.uuid,
|
MenuNo: _config.MenuNo || '',
|
Template: 'CustomPage',
|
MenuName: _name,
|
PageParam: JSON.stringify({Template: 'CustomPage'}),
|
LongParam: window.btoa(window.encodeURIComponent(JSON.stringify(_config)))
|
}
|
|
if (openEdition) { // 版本管理
|
param.open_edition = openEdition
|
}
|
|
let btnParam = { // 添加菜单按钮
|
func: 'sPC_Button_AddUpt',
|
Type: 60, // 添加菜单下的按钮type为40,按钮下的按钮type为60
|
ParentID: _config.uuid,
|
MenuNo: _config.MenuNo,
|
Template: 'CustomPage',
|
PageParam: '',
|
LongParam: '',
|
LText: []
|
}
|
|
btnParam.LText = this.getMenuMessage()
|
btnParam.LText = btnParam.LText.join(' union all ')
|
btnParam.LText = Utils.formatOptions(btnParam.LText)
|
btnParam.timestamp = moment().format('YYYY-MM-DD HH:mm:ss')
|
btnParam.secretkey = Utils.encrypt(btnParam.LText, btnParam.timestamp)
|
|
this.setState({
|
menuloading: true
|
}, () => {
|
new Promise(resolve => {
|
if (delButtons.length === 0) {
|
resolve({
|
status: true
|
})
|
} else {
|
let _param = {
|
func: 'sPC_MainMenu_Del',
|
MenuID: delButtons.join(',')
|
}
|
Api.getSystemConfig(_param).then(res => {
|
resolve(res)
|
})
|
}
|
}).then(res => {
|
if (res.status) {
|
this.setState({
|
delButtons: []
|
})
|
return Api.getSystemConfig(param)
|
} else {
|
this.setState({
|
menuloading: false
|
})
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return false
|
}
|
}).then(res => {
|
if (!res) return
|
|
if (res.status) {
|
this.setState({
|
oriConfig: fromJS(_config).toJS(),
|
openEdition: res.open_edition || ''
|
})
|
|
if (btnParam.LText) {
|
return Api.getSystemConfig(btnParam)
|
} else {
|
return {
|
status: true
|
}
|
}
|
} else {
|
this.setState({
|
menuloading: false
|
})
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
return false
|
}
|
}).then(res => {
|
if (!res) return
|
|
if (res.status) {
|
this.setState({
|
menuloading: false,
|
config: {...config, components: []}
|
}, () => {
|
this.setState({
|
config: {...this.state.config, components: this.state.oriConfig.components}
|
})
|
})
|
notification.success({
|
top: 92,
|
message: '保存成功',
|
duration: 2
|
})
|
} else {
|
this.setState({
|
menuloading: false
|
})
|
notification.warning({
|
top: 92,
|
message: res.message,
|
duration: 5
|
})
|
}
|
})
|
})
|
}
|
|
onEnabledChange = () => {
|
const { config } = this.state
|
|
if (!config.enabled && this.verifyConfig(true)) {
|
return
|
}
|
|
this.setState({
|
config: {...config, enabled: !config.enabled}
|
})
|
}
|
|
verifyConfig = (show) => {
|
const { config } = this.state
|
let error = ''
|
|
config.components.forEach(item => {
|
if (error) return
|
if (item.subtype === 'propcard' && item.wrap.datatype === 'static') return
|
|
if (item.setting) {
|
if (item.setting.interType === 'system' && item.setting.execute !== 'false' && !item.setting.dataresource) {
|
error = `组件《${item.name}》未设置数据源!`
|
} else if (item.setting.interType === 'system' && item.setting.execute === 'false' && item.scripts.length === 0) {
|
error = `组件《${item.name}》未设置数据源!`
|
} else if (item.setting.interType && !item.setting.primaryKey) {
|
error = `组件《${item.name}》未设置主键!`
|
}
|
}
|
if (item.type === 'bar' || item.type === 'line' || item.type === 'pie') {
|
if (!item.plot.Xaxis) {
|
error = `组件《${item.name}》图表字段尚未设置!`
|
}
|
}
|
})
|
|
if (show && error) {
|
notification.warning({
|
top: 92,
|
message: error,
|
duration: 5
|
})
|
}
|
|
return error
|
}
|
|
// 更新配置信息
|
updateConfig = (config) => {
|
this.setState({
|
config: config
|
})
|
|
this.props.modifyCustomMenu(config)
|
}
|
|
/**
|
* @description 更新常用表信息,快捷添加后更新配置信息
|
*/
|
updatetable = (config, fields) => {
|
const { tableFields } = this.state
|
|
config.tableFields = fields ? fields : tableFields
|
|
this.setState({
|
tableFields: fields ? fields : tableFields,
|
config
|
})
|
|
this.props.modifyCustomMenu(config)
|
}
|
|
insert = (item) => {
|
let config = fromJS(this.state.config).toJS()
|
|
config.components.push(item)
|
|
this.setState({config})
|
this.props.modifyCustomMenu(config)
|
}
|
|
render () {
|
const { btn } = this.props
|
const { activeKey, MenuType, dict, config, menuloading } = this.state
|
|
return (
|
<DndProvider backend={HTML5Backend}>
|
<div className="menu-body">
|
<div className="menu-setting">
|
<Collapse accordion activeKey={activeKey} bordered={false} onChange={(key) => this.setState({activeKey: key})}>
|
{/* 基本信息 */}
|
<Panel header={dict['mob.basemsg']} key="basedata">
|
{/* 菜单信息 */}
|
{config ? <MenuForm dict={dict} config={config} btn={btn} updateConfig={this.updateConfig}/> : null}
|
{/* 表名添加 */}
|
{config ? <TableComponent config={config} updatetable={this.updatetable}/> : null}
|
</Panel>
|
{/* 组件添加 */}
|
<Panel header={dict['mob.component']} key="component">
|
<SourceWrap MenuType={MenuType} />
|
</Panel>
|
<Panel header={'背景'} key="background">
|
{config ? <BgController config={config} updateConfig={this.updateConfig} /> : null}
|
</Panel>
|
<Panel header={'内边距'} key="padding">
|
{config ? <PaddingController config={config} updateConfig={this.updateConfig} /> : null}
|
</Panel>
|
</Collapse>
|
</div>
|
<div className={'menu-view ' + (menuloading ? 'saving' : '')}>
|
<Card title={
|
<div> {config && config.MenuName} </div>
|
} bordered={false} extra={
|
<div>
|
<StyleCombControlButton menu={config} />
|
<PasteController type="menu" Tab={null} insert={this.insert} />
|
{config ? <Switch className="big" checkedChildren={dict['mob.enable']} unCheckedChildren={dict['mob.disable']} checked={config.enabled} onChange={this.onEnabledChange} /> : null}
|
<Button type="primary" onClick={this.submitConfig} loading={menuloading}>{dict['mob.save']}</Button>
|
<Button type="default" onClick={this.closeView}>{dict['mob.return']}</Button>
|
</div>
|
} style={{ width: '100%' }}>
|
{config && config.components ? <MenuShell menu={config} handleList={this.updateConfig} /> : null}
|
</Card>
|
</div>
|
</div>
|
</DndProvider>
|
)
|
}
|
}
|
|
const mapStateToProps = () => {
|
return {}
|
}
|
|
const mapDispatchToProps = (dispatch) => {
|
return {
|
modifyCustomMenu: (customMenu) => dispatch(modifyCustomMenu(customMenu))
|
}
|
}
|
|
export default connect(mapStateToProps, mapDispatchToProps)(MenuDesign)
|