king
2020-06-08 46fa548514ba2a438908586e2b54ed742777e9b0
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Icon, Modal } from 'antd'
 
import Utils from '@/utils/utils.js'
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import { getChartViewForm } from '@/templates/zshare/formconfig'
 
import ChartForm from './chartform'
import DragChartView from './dragchartview'
import './index.scss'
 
const { confirm } = Modal
 
class ChartGroupComponent extends Component {
  static propTpyes = {
    config: PropTypes.object,         // 菜单配置信息
    sysRoles: PropTypes.array,        // 角色列表,黑名单使用
    updatechartgroup: PropTypes.func  // 图表更新
  }
 
  state = {
    dict: (!localStorage.getItem('lang') || localStorage.getItem('lang') === 'zh-CN') ? zhCN : enUS,
    chartlist: null,      // 图表集
    card: null,           // 编辑中元素
    formlist: null,       // 表单信息
    modaltype: '',        // 模态框控制
    chartview: null       // 活动图表
  }
 
  /**
   * @description 图表初始化
   */
  UNSAFE_componentWillMount () {
    this.setState({
      chartlist: fromJS(this.props.config.charts).toJS(),
      chartview: this.props.config.charts[0].uuid
    })
  }
 
  /**
   * @description 图表信息修改时更新 chartlist
   */
  UNSAFE_componentWillReceiveProps (nextProps) {
    const { chartlist } = this.state
 
    if (nextProps.config && nextProps.config.charts && !is(fromJS(nextProps.config.charts), fromJS(chartlist))) {
      this.setState({
        chartlist: fromJS(nextProps.config.charts).toJS()
      })
    }
  }
 
  /**
   * @description 图表顺序调整,或拖拽添加
   */
  handleList = (list) => {
    const { config } = this.props
 
    this.setState({chartlist: list})
    this.props.updatechartgroup({...config, charts: list}, this.state.chartview)
  }
 
  /**
   * @description 取消保存,关闭模态框
   */
  editModalCancel = () => {
    this.setState({
      card: null,
      modaltype: ''
    })
  }
 
  /**
   * @description 添加或修改图表
   */
  handleChart = (item) => {
    const { config } = this.props
 
    let _type = 'editChart'
    if (!item) {
      _type = 'addChart'
      item = {
        uuid: Utils.getuuid(),
        label: '',
        title: '',
        chartType: 'line',
        icon: 'line-chart',
        Hide: 'false',
        blacklist: [],
        correction: 7
      }
    }
 
    let _columns = [{
      uuid: 'x',
      value: '',
      text: '空',
      background: ''
    }]
    config.columns.forEach(col => {
      if (!['text', 'number'].includes(col.type)) return
      if (!col.marks) return
 
      let _marks = col.marks.filter(_mark => _mark.signType === 'card')
 
      if (_marks.length === 0) return
 
      _columns.push({
        uuid: col.uuid,
        value: col.field,
        text: col.label,
        background: _marks[0].color ? _marks[0].color[1] : ''
      })
    })
 
    if (item.chartType === 'card') { // 检验已添加背景是否修改
      if (item.bgfield) {
        let _column = _columns.filter(col => col.value === item.bgfield)[0]
        if (_column) {
          item.background = _column.background
        } else {
          item.bgfield = ''
          item.background = ''
        }
      } else {
        item.background = ''
      }
    }
 
    this.setState({
      card: item,
      modaltype: _type,
      formlist: getChartViewForm(item, this.props.sysRoles, _columns)
    })
  }
 
  /**
   * @description 图表信息修改后,提交保存
   */
  submitChart = () => {
    const { config } = this.props
    const { modaltype } = this.state
    let _chartlist = fromJS(this.state.chartlist).toJS()
    let _chartview = this.state.chartview
 
    this.chartFormRef.handleConfirm().then(res => {
      if (modaltype === 'addChart') {
        _chartlist.push(res)
        _chartview = res.uuid
      } else {
        _chartlist = _chartlist.map(item => {
          if (item.uuid === res.uuid) {
            if (!is(fromJS(item), fromJS(res))) {
              let _element = document.getElementById(res.uuid)
              if (_element) {
                _element.innerHTML = ''
              }
            }
            return res
          }
          return item
        })
      }
 
      this.setState({
        card: null,
        chartview: _chartview,
        modaltype: '',
        chartlist: _chartlist
      })
 
      this.props.updatechartgroup({...config, charts: _chartlist}, _chartview)
    })
  }
 
  /**
   * @description 图表展开收起切换
   */
  onChartChange = (e) => {
    const { config } = this.props
    e.stopPropagation()
 
    this.props.updatechartgroup({...config, expand: !config.expand}, this.state.chartview)
  }
 
  /**
   * @description 图表删除
   */
  deletechart = (plot) => {
    const { config } = this.props
    const { dict } = this.state
    let _this = this
 
    confirm({
      content: dict['model.confirm'] + dict['model.delete'] + ` ${plot.title} ?`,
      okText: dict['model.confirm'],
      cancelText: dict['header.cancel'],
      onOk() {
        let _chartlist = fromJS(_this.state.chartlist).toJS()
        let _chartview = _this.state.chartview
 
        _chartlist = _chartlist.filter(item => item.uuid !== plot.uuid)
 
        if (_chartview === plot.uuid) {
          _chartview = _chartlist[0].uuid
        }
 
        _this.setState({
          chartlist: _chartlist,
          chartview: _chartview
        })
        _this.props.updatechartgroup({...config, charts: _chartlist}, _chartview)
      },
      onCancel() {}
    })
  }
 
  changetabview = (id) => {
    const { config } = this.props
 
    this.setState({
      chartview: id
    })
 
    this.props.updatechartgroup(config, id)
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
  }
 
  render() {
    const { config } = this.props
    const { dict, chartlist, modaltype, card, chartview } = this.state
 
    return (
      <div className="model-table-chartview-list">
        <Icon type="plus" onClick={() => this.handleChart()} />
        {chartlist.length > 1 ? <Icon type={config.expand ? 'up' : 'down'} onClick={this.onChartChange} /> : null}
        {chartlist.length > 1 ? <DragChartView
          activeKey={chartview}
          list={chartlist}
          expand={config.expand}
          handleList={this.handleList}
          changetabview={this.changetabview}
          handleMenu={this.handleChart}
          deleteMenu={this.deletechart}
        /> : null}
        {/* 图表信息编辑 */}
        <Modal
          title={modaltype === 'addChart' ? '图表-添加' : '图标-编辑'}
          visible={modaltype === 'addChart' || modaltype === 'editChart'}
          width={800}
          maskClosable={false}
          onOk={this.submitChart}
          onCancel={this.editModalCancel}
          destroyOnClose
        >
          <ChartForm
            dict={dict}
            card={card}
            formlist={this.state.formlist}
            inputSubmit={this.submitChart}
            wrappedComponentRef={(inst) => this.chartFormRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
export default ChartGroupComponent