king
2020-09-26 ab60d53b67f802878662aaa5a5b52580cca421b8
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Modal } from 'antd'
 
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import { getCardCellForm } from './formconfig'
 
import MKEmitter from '@/utils/events.js'
import ElementForm from './elementform'
import DragElement from './dragaction'
import './index.scss'
 
const { confirm } = Modal
 
class CardCellComponent extends Component {
  static propTpyes = {
    config: PropTypes.object,        // 菜单配置信息
    updateElement: PropTypes.func     // 菜单配置更新
  }
 
  state = {
    dict: localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS,
    card: null,          // 编辑中元素
    formlist: null,      // 表单信息
    elements: null,      // 按钮组
    visible: false,      // 模态框控制
  }
 
  /**
   * @description 搜索条件初始化
   */
  UNSAFE_componentWillMount () {
    const { config } = this.props
 
    this.setState({
      elements: fromJS(config.elements).toJS()
    })
  }
 
  componentDidMount () {
    MKEmitter.addListener('cardAddElement', this.cardAddElement)
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props.config), fromJS(nextProps.config)) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  /**
   * @description 组件销毁,清除state更新,清除快捷键设置
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('cardAddElement', this.cardAddElement)
  }
 
  cardAddElement = (cardId, element) => {
    if (cardId !== this.props.config.uuid) return
 
    const { elements } = this.state
 
    this.setState({elements: [...elements, element]})
    this.handleElement(element)
  }
 
  /**
   * @description 按钮顺序调整
   */
  handleList = (list) => {
    const { config } = this.props
 
    this.setState({elements: list}, () => {
      this.props.updateElement({...config, elements: list})
    })
  }
 
  /**
   * @description 按钮编辑,获取按钮表单信息
   */
  handleElement = (card) => {
    this.setState({
      visible: true,
      card: card,
      formlist: getCardCellForm(card)
    })
  }
 
  /**
   * @description 取消保存,如果元素为新添元素,则从序列中删除
   */
  editModalCancel = () => {
    const { card, elements } = this.state
    let _elements = null
 
    if (card.focus) {
      _elements = elements.filter(item => item.uuid !== card.uuid)
    } else {
      _elements = elements
    }
 
    this.setState({
      card: null,
      elements: _elements,
      visible: false
    })
  }
 
  /**
   * @description 搜索修改后提交保存
   * 1、去除系统默认搜索条件
   * 2、字段及提示文字重复校验
   * 3、更新下拉菜单可选集合
   * 4、下拉菜单数据源语法验证
   */
  handleSubmit = () => {
    const { config } = this.props
    const { elements } = this.state
 
    this.elementFormRef.handleConfirm().then(ele => {
      let _elements = elements.map(cell => {
        if (cell.uuid === ele.uuid) return ele
        return cell
      })
 
      this.setState({
        elements: _elements,
        visible: false
      }, () => {
        this.props.updateElement({...config, elements: _elements})
      })
    })
  }
 
  /**
   * @description 按钮删除
   */
  deleteElement = (card) => {
    const { config } = this.props
    const { dict, elements } = this.state
    let _this = this
 
    confirm({
      content: dict['model.confirm'] + dict['model.delete'] + '元素吗?',
      onOk() {
        let _elements = elements.filter(item => item.uuid !== card.uuid)
 
        _this.setState({
          elements: _elements
        }, () => {
          _this.props.updateElement({...config, elements: _elements})
        })
      },
      onCancel() {}
    })
  }
 
  render() {
    const { config } = this.props
    const { elements, visible, card, dict } = this.state
 
    return (
      <div className="model-menu-card-cell-list">
        <DragElement
          list={elements}
          handleList={this.handleList}
          handleMenu={this.handleElement}
          deleteMenu={this.deleteElement}
        />
        {/* 编辑按钮:复制、编辑 */}
        <Modal
          title={'编辑元素'}
          visible={visible}
          width={800}
          maskClosable={false}
          onCancel={this.editModalCancel}
          onOk={this.handleSubmit}
          destroyOnClose
        >
          <ElementForm
            dict={dict}
            card={card}
            formlist={this.state.formlist}
            inputSubmit={this.handleSubmit}
            config={config}
            wrappedComponentRef={(inst) => this.elementFormRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
 
export default CardCellComponent