import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
import { Form } from 'antd'
|
|
import zhCN from '@/locales/zh-CN/mob.js'
|
import enUS from '@/locales/en-US/mob.js'
|
import asyncComponent from '@/utils/asyncComponent'
|
import './index.scss'
|
|
const ColorSketch = asyncComponent(() => import('@/mob/colorsketch'))
|
const SourceComponent = asyncComponent(() => import('@/menu/components/share/sourcecomponent'))
|
|
class MobController extends Component {
|
static propTpyes = {
|
config: PropTypes.any,
|
updateConfig: PropTypes.func,
|
}
|
|
state = {
|
dict: sessionStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
|
backgroundColor: '',
|
backgroundImage: '',
|
}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
let bgImg = config.style.backgroundImage || ''
|
|
if (bgImg && /^url/.test(bgImg)) {
|
bgImg = bgImg.replace('url(', '')
|
bgImg = bgImg.replace(')', '')
|
}
|
|
this.setState({
|
backgroundColor: config.style.backgroundColor,
|
backgroundImage: bgImg
|
})
|
}
|
|
shouldComponentUpdate (nextProps, nextState) {
|
return !is(fromJS(this.state), fromJS(nextState))
|
}
|
|
/**
|
* @description 修改背景颜色 ,颜色控件
|
*/
|
changeBackgroundColor = (val) => {
|
let config = fromJS(this.props.config).toJS()
|
|
this.setState({
|
backgroundColor: val
|
})
|
|
config.style.backgroundColor = val
|
this.props.updateConfig(config)
|
}
|
|
imgChange = (val) => {
|
this.setState({
|
backgroundImage: val
|
})
|
|
let config = fromJS(this.props.config).toJS()
|
|
if (val) {
|
config.style.backgroundImage = `url(${val})`
|
} else {
|
delete config.style.backgroundImage
|
}
|
this.props.updateConfig(config)
|
}
|
|
render () {
|
const { backgroundColor, backgroundImage } = this.state
|
const formItemLayout = {
|
labelCol: {
|
xs: { span: 24 },
|
sm: { span: 4 }
|
},
|
wrapperCol: {
|
xs: { span: 24 },
|
sm: { span: 20 }
|
}
|
}
|
|
return (
|
<div className="menu-background-controller">
|
<Form {...formItemLayout}>
|
<Form.Item className="color-control" colon={false} label="颜色">
|
<ColorSketch value={backgroundColor} onChange={this.changeBackgroundColor} />
|
</Form.Item>
|
<Form.Item colon={false} label="图片">
|
<SourceComponent value={backgroundImage} type="" placement="right" onChange={this.imgChange}/>
|
</Form.Item>
|
</Form>
|
</div>
|
)
|
}
|
}
|
|
export default MobController
|