king
2022-01-21 46f79b491173d284a4900d19e7aecf7509481438
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Modal, Form, Tabs } from 'antd'
import { EditOutlined } from '@ant-design/icons'
 
import { getBaseForm } from './formconfig'
import asyncComponent from '@/utils/asyncComponent'
import './index.scss'
 
const { TabPane } = Tabs
const NormalForm = asyncComponent(() => import('@/menu/components/share/normalform'))
const CodeMirror = asyncComponent(() => import('@/templates/zshare/codemirror'))
 
class CustomChartDrawerForm extends Component {
  static propTpyes = {
    dict: PropTypes.object,
    plot: PropTypes.object,
    config: PropTypes.object,
    plotchange: PropTypes.func
  }
 
  state = {
    view: 'base',
    visible: false,
    plot: null,
    baseFormlist: null,
  }
 
  showDrawer = () => {
    const { config } = this.props
 
    this.setState({
      visible: true,
      view: 'base',
      plot: fromJS(config.plot).toJS(),
      baseFormlist: getBaseForm(config.plot),
    })
  }
 
  onSubmit = () => {
    const { config } = this.props
    const { plot, view } = this.state
 
    if (view === 'base') {
      this.baseRef.handleConfirm().then(res => {
        let _plot = {...plot, ...res}
 
        this.setState({
          plot: _plot,
          visible: false
        })
 
        this.props.plotchange({...config, plot: _plot})
      })
    } else {
      this.setState({
        visible: false
      })
      this.props.plotchange({...config, plot})
    }
  }
 
  changeTab = (tab) => {
    const { plot, view } = this.state
 
    if (view === 'base') {
      this.baseRef.handleConfirm().then(res => {
        this.setState({
          plot: {...plot, ...res},
          view: tab
        })
      })
    } else {
      this.setState({
        view: tab
      })
    }
  }
 
  onChange = (val) => {
    const { plot } = this.state
 
    this.setState({
      plot: {...plot, script: val},
    })
  }
 
  render() {
    const { view, visible, baseFormlist, plot } = this.state
 
    return (
      <>
        <EditOutlined style={{color: '#1890ff'}} title="编辑" onClick={this.showDrawer} />
        <Modal
          wrapClassName="popview-modal custom-chart-edit-modal"
          title="自定义图表编辑"
          visible={visible}
          width={950}
          maskClosable={false}
          onOk={this.onSubmit}
          onCancel={() => { this.setState({ visible: false }) }}
          destroyOnClose
        >
          <Tabs activeKey={view} onChange={this.changeTab}>
            <TabPane tab="组件设置" key="base">
              <NormalForm dict={this.props.dict} formlist={baseFormlist} inputSubmit={this.onSubmit} wrappedComponentRef={(inst) => this.baseRef = inst}/>
            </TabPane>
            {plot ? <TabPane tab="JS" key="JS">
              {plot.chartType === 'antv' ? <div>入参:Chart、 DataSet、 wrap(dom节点)、data、 config</div> : <div>入参:echarts、 DataSet、 wrap(dom节点)、 data、 config</div>}
              <CodeMirror mode="text/javascript" theme="cobalt" value={plot.script} onChange={this.onChange} />
            </TabPane> : null}
          </Tabs>
        </Modal>
      </>
    );
  }
}
 
export default Form.create()(CustomChartDrawerForm)