king
2023-06-14 08cce3334a2dc81d690b518136b0aaea64e48b0b
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { fromJS } from 'immutable'
import { Modal, notification, Button } from 'antd'
import { SnippetsOutlined, QuestionCircleOutlined } from '@ant-design/icons'
 
import Utils from '@/utils/utils.js'
import asyncComponent from '@/utils/asyncComponent'
import './index.scss'
 
const PasteForm = asyncComponent(() => import('@/templates/zshare/pasteform'))
 
class PasteForms extends Component {
  static propTpyes = {
    config: PropTypes.object,        // 组件配置
    update: PropTypes.func
  }
 
  state = {
    visible: false,
    choVisible: false,
    result: null
  }
 
  pasteSubmit = () => {
    const { config } = this.props
 
    this.pasteFormRef.handleConfirm().then(res => {
      if (res.copyType === 'search' && ['text', 'select', 'multiselect', 'link', 'checkcard', 'date', 'datemonth'].includes(res.type)) {
        res.copyType = 'form'
      }
 
      if (!['form', 'forms', 'formgroup', 'simpleform'].includes(res.copyType)) {
        notification.warning({ top: 92, message: '配置信息格式错误!', duration: 5 })
        return
      }
 
      if (res.copyType === 'form') {
        delete res.copyType
        res = {fields: [res]}
      }
 
      res.fields = res.fields || []
      res.fields = res.fields.map(item => {
        item.uuid = Utils.getuuid()
        return item
      })
 
      let fields = res.fields.map(item => item.field ? item.field.toLowerCase() : '')
 
      let repeat = false
      let forms = []
      if (config.fields) {
        forms = fromJS(config.fields).toJS()
        forms.forEach(item => {
          if (item.field && fields.includes(item.field.toLowerCase())) {
            repeat = true
          }
        })
      }
 
      if (repeat) {
        this.setState({result: res, choVisible: true, visible: false})
        return
      } else {
        forms.push(...res.fields)
      }
 
      this.props.update(forms, res)
 
      this.setState({visible: false})
 
      notification.success({
        top: 92,
        message: '粘贴成功!',
        duration: 2
      })
    })
  }
 
  replaceForms = () => {
    const { config } = this.props
    const { result } = this.state
 
    let forms = fromJS(config.fields).toJS()
    let fields = fromJS(result.fields).toJS()
    let repeats = []
 
    forms = forms.map(item => {
      if (!item.field) return item
 
      let cell = fields.filter(m => m.field && m.field.toLowerCase() === item.field.toLowerCase())[0]
      if (cell) {
        repeats.push(cell.field)
        return cell
      }
 
      return item
    })
    
    fields = fields.filter(m => !m.field || !repeats.includes(m.field))
 
    forms.push(...fields)
 
    this.props.update(forms, result)
 
    this.setState({choVisible: false})
  }
  
  jumpForms = () => {
    const { config } = this.props
    const { result } = this.state
 
    let forms = fromJS(config.fields).toJS()
    let fields = fromJS(result.fields).toJS()
    let repeats = []
 
    forms = forms.map(item => {
      if (!item.field) return item
 
      let cell = fields.filter(m => m.field && m.field.toLowerCase() === item.field.toLowerCase())[0]
      if (cell) {
        repeats.push(cell.field)
      }
 
      return item
    })
    
    fields = fields.filter(m => !m.field || !repeats.includes(m.field))
 
    forms.push(...fields)
 
    this.props.update(forms, result)
 
    this.setState({choVisible: false})
  }
 
  render() {
    const { type } = this.props
    const { visible, choVisible } = this.state
 
    return (
      <div style={{display: 'inline-block'}}>
        {type === 'toolbar' ? <Button icon="snippets" style={{color: 'purple'}} onClick={() => {this.setState({visible: true})}} >粘贴</Button> :
        <SnippetsOutlined style={{color: 'purple'}} onClick={() => {this.setState({visible: true})}} />}
        <Modal
          title="粘贴"
          visible={visible}
          width={600}
          maskClosable={false}
          onOk={this.pasteSubmit}
          onCancel={() => {this.setState({visible: false})}}
          destroyOnClose
        >
          <PasteForm wrappedComponentRef={(inst) => this.pasteFormRef = inst} inputSubmit={this.pasteSubmit}/>
        </Modal>
        <Modal
          title=""
          visible={choVisible}
          width={450}
          closable={false}
          maskClosable={false}
          onCancel={() => {this.setState({choVisible: false, result: null})}}
          footer={[
            <Button key="cancel" onClick={() => this.setState({choVisible: false, result: null})}>取消</Button>,
            <Button key="replace" className="mk-border-purple" style={{color: 'purple'}} onClick={this.replaceForms}>替换</Button>,
            <Button key="confirm" type="primary" onClick={this.jumpForms}>跳过</Button>
          ]}
          destroyOnClose
        >
          <QuestionCircleOutlined style={{color: 'orange', fontSize: '24px', margin: '15px 10px', position: 'relative', top: '2px'}}/>存在重复表单,请选择处理方式。
        </Modal>
      </div>
    )
  }
}
 
export default PasteForms