king
2024-08-25 326aa6b3effaccc71cfe0775d47b0f29eb3695a6
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { DndProvider } from 'react-dnd'
import HTML5Backend from 'react-dnd-html5-backend'
import { Button, Modal, Collapse, Switch, message } from 'antd'
import { LeftOutlined } from '@ant-design/icons'
 
import { getModalForm } from '@/templates/zshare/formconfig'
import SourceElement from '@/templates/modalconfig/dragelement/source'
import SettingForm from '@/templates/modalconfig/settingform'
import asyncComponent from '@/utils/asyncComponent'
import MKEmitter from '@/utils/events.js'
import { SearchItems } from './source'
import './index.scss'
 
const { Panel } = Collapse
const { confirm } = Modal
const TableComponent = asyncComponent(() => import('@/menu/modalconfig/tablecomponent'))
const ModalForm = asyncComponent(() => import('@/templates/zshare/modalform'))
const PasteForms = asyncComponent(() => import('@/menu/components/share/pasteforms'))
const DragElement = asyncComponent(() => import('@/mob/components/formdragelement'))
const FieldsComponent = asyncComponent(() => import('@/templates/sharecomponent/fieldscomponent'))
 
class ComModalConfig extends Component {
  static propTpyes = {
    btn: PropTypes.object,
    handleSave: PropTypes.func,
    handleBack: PropTypes.func
  }
 
  state = {
    config: null,          // 页面配置,包括模板类型、模态框设置、添加表名、表单列表
    visible: false,        // 表单编辑模态框,显示控制
    formlist: null,        // 表单编辑模态框,可编辑字段
    card: null,            // 编辑元素
    settingVisible: false, // 全局配置模态框
    originConfig: null,    // 原始菜单
    sqlVerifing: false,    // sql验证
    showField: false,      // 显示表单字段值
    saving: false
  }
 
  /**
   * @description 数据预处理
   */
  UNSAFE_componentWillMount () {
    const { btn } = this.props
 
    let _config = btn.modal
    _config.version = '1.0'
 
    this.setState({
      config: _config,
      originConfig: fromJS(_config).toJS()
    })
  }
 
  componentDidMount () {
    window.GLOB.formId = ''
    MKEmitter.addListener('completeSave', this.completeSave)
  }
 
  /**
   * @description 组件销毁,清除state更新
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('completeSave', this.completeSave)
  }
 
  completeSave = () => {
    this.setState({saving: false})
  }
 
  /**
   * @description 表单变化
   * 1、表单拖拽添加时,检查是否存在示例表单,如存在则去除示例
   * 2、表单移动后,保存移动后的顺序
   * 3、新增表单时,直接打开编辑框
   */
  handleList = (list, newcard) => {
    let _config = fromJS(this.state.config).toJS()
 
    if (list.length > _config.fields.length) {
      _config.fields = list
      this.setState({
        config: _config
      }, () => {
        this.handleForm(newcard)
      })
    } else {
      _config.fields = list
      this.setState({config: _config})
    }
  }
 
  /**
   * @description 表单编辑
   * 1、显示编辑弹窗-visible
   * 2、保存编辑项-card
   * 3、设置编辑参数项-formlist
   */
  handleForm = (_card) => {
    const { componentConfig, btn } = this.props
    const { config } = this.state
    
    let card = fromJS(_card).toJS()
 
    let columns = componentConfig.columns
    if (btn.$sub) {
      columns = componentConfig.subColumns || []
    }
 
    this.setState({
      visible: true,
      card: card,
      formlist: getModalForm(card, config.fields, columns)
    })
  }
 
  /**
   * @description 编辑后提交
   * 1、获取编辑后的表单信息
   * 2、去除可能存在的示例表单
   * 3、通过loading刷新
   */
  handleSubmit = () => {
    let _config = fromJS(this.state.config).toJS()
 
    this.formRef.handleConfirm(_config.fields).then(res => {
      _config.fields = _config.fields.map(item => {
        delete item.focus
 
        if (item.uuid === res.values.uuid) {
          return res.values
        } else {
          return item
        }
      })
 
      if (res.loading) {
        this.setState({
          sqlVerifing: true
        })
 
        res.promise().then(() => {
          this.setState({
            sqlVerifing: false,
            config: _config,
            card: null,
            visible: false
          })
        }, () => {
          this.setState({sqlVerifing: false})
        })
      } else {
        this.setState({
          config: _config,
          card: null,
          visible: false
        })
      }
    })
  }
 
  /**
   * @description 表单删除并刷新
   */
  closeForm = (card) => {
    let that = this
 
    confirm({
      content: `确定删除${card.label ? `<<${card.label}>>` : ''}吗?`,
      onOk() {
        let _config = fromJS(that.state.config).toJS()
        _config.fields = _config.fields.filter(item => !(item.uuid === card.uuid))
 
        that.setState({
          config: _config,
        })
      },
      onCancel() {}
    })
  }
 
  submitConfig = () => {
    const { config } = this.state
 
    this.setState({originConfig: fromJS(config).toJS(), saving: true})
    this.props.handleSave(config)
 
    setTimeout(() => {
      MKEmitter.emit('triggerMenuSave')
    }, 100)
  }
 
  cancelConfig = () => {
    const { config, originConfig } = this.state
 
    if (!is(fromJS(config), fromJS(originConfig))) {
      let that = this
      confirm({
        content: '配置信息未保存,确定返回吗?',
        onOk() {
          that.props.handleBack()
        },
        onCancel() {}
      })
    } else {
      this.props.handleBack()
    }
  }
 
  /**
   * @description 全局设置模态框
   */
  changeSetting = () => {
    this.setState({
      settingVisible: true
    })
  }
 
  /**
   * @description 保存全局设置
   */
  settingSave = () => {
    const {config} = this.state
    this.settingRef.handleConfirm().then(res => {
      this.setState({
        config: {...config, setting: res},
        settingVisible: false
      })
    })
  }
 
  editModalCancel = () => {
    const { config, card } = this.state
 
    if (card.focus) {
      let _fields = config.fields.filter(item => item.uuid !== card.uuid)
      let _config = {...config, fields: _fields}
 
      this.setState({
        card: null,
        config: _config,
        visible: false
      })
    } else {
      this.setState({
        card: null,
        visible: false
      })
    }
  }
 
  /**
   * @description 更新
   */
  plusFields = (items) => {
    let _config = fromJS(this.state.config).toJS()
 
    _config.fields.push(...items)
 
    this.setState({
      config: _config
    }, () => {
      if (items.length === 1 && items[0].focus) {
        this.handleForm(items[0])
      }
    })
  }
 
  pasteFields = (items) => {
    let _config = fromJS(this.state.config).toJS()
    _config.fields = items
 
    this.setState({
      config: _config
    })
  }
 
  triggerCopy = () => {
    const { config } = this.state
 
    let val = {
      copyType: 'forms',
      fields: config.fields || []
    }
 
    if (val.fields.length === 0) {
      message.warning('表单元素不可为空!')
      return
    }
 
    try {
      val = window.btoa(window.encodeURIComponent(JSON.stringify(val)))
    } catch (e) {
      console.warn(e)
      message.warning('复制失败,请重试!')
      val = ''
    }
 
    if (val) {
      let oInput = document.createElement('input')
      oInput.value = val
      document.body.appendChild(oInput)
      oInput.select()
      document.execCommand('Copy')
      document.body.removeChild(oInput)
 
      message.success('复制成功。')
    }
  }
 
  clearConfig = () => {
    const that = this
    let _config = {...this.state.config, fields: []}
 
    confirm({
      content: '确定清空表单吗?',
      onOk() {
        that.setState({ config: _config })
      },
      onCancel() {}
    })
  }
 
  render () {
    const { btn } = this.props
    const { config, saving, card } = this.state
 
    return (
      <div className="mob-form-board">
        <DndProvider backend={HTML5Backend}>
          <div className="tools">
            <Collapse accordion defaultActiveKey="1" bordered={false}>
              <Panel header="基本信息" key="0">
                <TableComponent />
              </Panel>
              <Panel header="表单" key="1">
                <div className="search-element">
                  {SearchItems.map((item, index) => {
                    return (<SourceElement key={index} content={item}/>)
                  })}
                </div>
                <FieldsComponent config={config} type="form" plusFields={this.plusFields}/>
              </Panel>
            </Collapse>
          </div>
          <div className="modal-control">
            <Button icon="setting" onClick={this.changeSetting}>设置</Button>
            <Button type="primary" id="save-modal-config" loading={saving} onClick={this.submitConfig}>保存</Button>
            <Button onClick={this.cancelConfig}>返回</Button>
            <Button type="primary" style={{background: '#26C281', border: 'none'}} onClick={this.triggerCopy}>复制</Button>
            <PasteForms type="toolbar" config={config} update={this.pasteFields}/>
            <Button type="danger" onClick={this.clearConfig}>清空</Button>
            <Switch checkedChildren="开" unCheckedChildren="关" defaultChecked={this.state.showField} onChange={(val) => this.setState({showField: val})} />
          </div>
          <div className="setting">
            <div className="mob-shell" style={{width: window.GLOB.shellWidth, height: window.GLOB.shellHeight}}>
              <div className="mob-shell-inner">
                <div className="am-navbar">
                  <LeftOutlined />
                  <div className="am-navbar-title">{btn.label}</div>
                  {config.setting.btnPosition === 'top' ? <Button className="modal-submit-top">{config.setting.btnName || '确定'}</Button> : null}
                </div>
                <DragElement
                  list={config.fields}
                  setting={config.setting}
                  showField={this.state.showField}
                  handleList={this.handleList}
                  handleForm={this.handleForm}
                  closeForm={this.closeForm}
                />
                {config.setting.btnPosition !== 'top' ? 
                  <Button className="modal-submit" type="primary">{config.setting.btnName || '确定'}</Button> : null}
              </div>
            </div>
          </div>
        </DndProvider>
        <Modal
          title={card && card.$copy ? '复制' : '编辑'}
          wrapClassName="mk-scroll-modal"
          visible={this.state.visible}
          width={950}
          maskClosable={false}
          onCancel={this.editModalCancel}
          onOk={this.handleSubmit}
          confirmLoading={this.state.sqlVerifing}
          destroyOnClose
        >
          <ModalForm
            card={card}
            formlist={this.state.formlist}
            inputSubmit={this.handleSubmit}
            fields={config.fields}
            wrappedComponentRef={(inst) => this.formRef = inst}
          />
        </Modal>
        <Modal
          title="编辑"
          visible={this.state.settingVisible}
          width={900}
          maskClosable={false}
          onOk={this.settingSave}
          onCancel={() => { this.setState({ settingVisible: false }) }}
          destroyOnClose
        >
          <SettingForm
            config={config}
            isSubTab={!!this.props.editTab}
            inputSubmit={this.settingSave}
            wrappedComponentRef={(inst) => this.settingRef = inst}
          />
        </Modal>
      </div>
    )
  }
}
 
export default ComModalConfig