king
2020-08-09 ce2b708f61de1855771d78f35309bd77df9d3b15
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
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/mob.js'
import enUS from '@/locales/en-US/mob.js'
import VerifyCard from './verifycard'
import './index.scss'
 
const { confirm } = Modal
 
class DataSource extends Component {
  static propTpyes = {
    config: PropTypes.any,
    updateConfig: PropTypes.func
  }
 
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    sourcelist: [],
    searches: [],
    visible: false,
    loading: false,
    source: null
  }
 
  UNSAFE_componentWillMount () {
    const { config } = this.props
 
    this.setState({sourcelist: config.sourcelist || []})
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
 
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  editDataSource = (item) => {
    // const { config } = this.props
 
    if (!item) {
      item = {
        uuid: Utils.getuuid(),
        setting: {},
        columns: [],
        scripts: []
      }
    }
 
    this.setState({
      visible: true,
      source: item,
      searches: []
    })
  }
 
  closeDataSource = (item) => {
    const { config } = this.props
    let sourcelist = fromJS(this.state.sourcelist).toJS()
    const _this = this
 
    sourcelist = sourcelist.filter(cell => cell.uuid !== item.uuid)
 
    confirm({
      title: '确定删除数据源吗?',
      content: '',
      okText: _this.state.dict['mob.confirm'],
      cancelText: _this.state.dict['mob.cancel'],
      onOk() {
        _this.setState({sourcelist})
        _this.props.updateConfig({...config, sourcelist: fromJS(sourcelist).toJS()})
      },
      onCancel() {}
    })
  }
 
  verifySubmit = () => {
    const { config } = this.props
    let sourcelist = fromJS(this.state.sourcelist).toJS()
 
    this.setState({loading: true})
    this.verifyRef.submitDataSource().then((res) => {
      let isadd = true
      sourcelist = sourcelist.map(item => {
        if (item.uuid === res.uuid) {
          isadd = false
          return res
        } else {
          return item
        }
      })
 
      if (isadd) {
        sourcelist.push(res)
      }
      
      this.setState({loading: false, visible: false, sourcelist})
      this.props.updateConfig({...config, sourcelist: fromJS(sourcelist).toJS()})
    }, () => {
      this.setState({loading: false})
    })
  }
 
  render () {
    const { config } = this.props
    const { sourcelist, visible, source, dict, searches, loading } = this.state
 
    let addable = true
    if (config.components && config.components.length === 1 && config.components[0].type === 'login') {
      addable = false
    }
 
    return (
      <div className="mob-datasource">
        {sourcelist.map(item => (
          <span className="mob-input-group-wrapper" key={item.uuid}>
            <span className="mob-input-wrapper">
              <span className="mob-input-value">{item.setting.name}</span>
              <span className="mob-input-group-addon">
                <Icon type="setting" onClick={() => this.editDataSource(item)} />
                <Icon type="close" onClick={() => this.closeDataSource(item)} />
              </span>
            </span>
          </span>
        ))}
        {addable ? <span className="mob-input-group-wrapper">
          <span className="mob-input-wrapper">
            <span className="mob-input-insert" onClick={() => this.editDataSource()}>
              <Icon type="plus" />
            </span>
          </span>
        </span> : null}
        <Modal
          wrapClassName="mob-datasource-verify-modal popview-modal"
          title={'数据源配置'}
          visible={visible}
          width={'75vw'}
          maskClosable={false}
          style={{minWidth: '900px', maxWidth: '1200px'}}
          okText={dict['mob.submit']}
          cancelText={dict['mob.cancel']}
          onOk={this.verifySubmit}
          confirmLoading={loading}
          onCancel={() => { this.setState({ visible: false }) }}
          destroyOnClose
        >
          <VerifyCard
            dict={dict}
            card={source}
            menuId={this.props.config.uuid}
            searches={searches}
            wrappedComponentRef={(inst) => this.verifyRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
export default DataSource