king
2020-12-03 569ccb3c1ff82f30ffefa7d3700571448d742662
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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { DndProvider, DragSource, DropTarget } from 'react-dnd'
import { Table, Form, Popover, Icon, Modal } from 'antd'
 
import asyncComponent from '@/utils/asyncComponent'
import asyncIconComponent from '@/utils/asyncIconComponent'
import Utils from '@/utils/utils.js'
import zhCN from '@/locales/zh-CN/model.js'
import enUS from '@/locales/en-US/model.js'
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
const { confirm } = Modal
const coldict = localStorage.getItem('lang') !== 'en-US' ? zhCN : enUS
const EditColumn = asyncIconComponent(() => import('./editColumn'))
const CardCellComponent = asyncComponent(() => import('@/menu/components/card/cardcellcomponent'))
 
class HeaderCol extends Component {
  deleteCol = () => {
    const _this = this
 
    confirm({
      content: '确定删除显示列吗?',
      onOk() {
        _this.props.deleteCol(_this.props.column)
      },
      onCancel() {}
    })
  }
 
  shouldComponentUpdate (nextProps, nextState) {
 
    if (!nextProps.column) return false
 
    return !is(fromJS(this.props.column), fromJS(nextProps.column)) ||
      !is(fromJS(this.props.fields), fromJS(nextProps.fields)) ||
      this.props.index !== nextProps.index
  }
 
  render() {
    const { connectDragSource, connectDropTarget, moveCol, addElement, editColumn, deleteCol, index, column, align, fields, children, ...restProps } = this.props
 
    if (index !== undefined) {
      return connectDragSource(
        connectDropTarget(<th {...restProps} index={index} style={{ cursor: 'move', textAlign: align }}>
          <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
            <div className="mk-popover-control">
              {column && (column.type === 'custom' || column.type === 'colspan' || column.type === 'action') ?
                <Icon className="plus" title="添加" type="plus" onClick={() => this.props.addElement(column)} /> : null
              }
              <Icon className="edit" title="编辑" type="edit" onClick={() => this.props.editColumn(column)} />
              <Icon className="close" title="删除" type="delete" onClick={this.deleteCol} />
            </div>
          } trigger="hover">
            {children}
          </Popover>
        </th>),
      )
    } else if (column) {
      return (
        <th {...restProps}>
          <Popover overlayClassName="mk-popover-control-wrap" mouseLeaveDelay={0.2} mouseEnterDelay={0.2} content={
            <div className="mk-popover-control">
              {column && column.type === 'custom' ?
                <Icon className="plus" title="添加" type="plus" onClick={() => this.props.addElement(column)} /> : null
              }
              <Icon className="edit" title="编辑" type="edit" onClick={() => this.props.editColumn(column)} />
              <Icon className="close" title="删除" type="delete" onClick={this.deleteCol} />
            </div>
          } trigger="hover">
            {children}
          </Popover>
        </th>
      )
    } else {
      return (<th {...restProps}>{children}</th>)
    }
  }
}
 
const rowSource = {
  beginDrag(props) {
    return {
      index: props.index,
    }
  }
}
 
const ColTarget = {
  drop(props, monitor) {
    const dragIndex = monitor.getItem().index
    const hoverIndex = props.index
 
    if (dragIndex === undefined || hoverIndex === undefined || dragIndex === hoverIndex) {
      return
    }
 
    props.moveCol(dragIndex, hoverIndex)
    monitor.getItem().index = hoverIndex
  },
}
 
const DragableHeaderCol = DropTarget('col', ColTarget, connect => ({
  connectDropTarget: connect.dropTarget()
}))(
  DragSource('col', rowSource, connect => ({
    connectDragSource: connect.dragSource(),
  }))(HeaderCol),
)
 
class EditableCell extends Component {
  updateCard = (vals) => {
    const { column } = this.props
    this.props.upComponent({...column, elements: vals})
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    const { config, column } = this.props
 
    if (!nextProps.column) return true
 
    return !is(fromJS(column), fromJS(nextProps.column)) ||
      !is(fromJS(config.columns), fromJS(nextProps.config.columns)) ||
      !is(fromJS(config.search), fromJS(nextProps.config.search))
  }
 
  render() {
    const { column, config, children, className, style } = this.props
 
    if (column && column.type === 'custom') {
      return (
        <td style={{padding: 0, verticalAlign: 'top', minWidth: column.Width || 100}} className={className}>
          <CardCellComponent cards={config} cardCell={column} elements={column.elements} updateElement={this.updateCard}/>
        </td>
      )
    } else if (column && column.type === 'action') {
      return (
        <td style={{padding: '0 5px', textAlign: column.Align, minWidth: column.Width || 100}} className={className}>
          <CardCellComponent cards={config} cardCell={column} elements={column.elements} updateElement={this.updateCard}/>
        </td>
      )
    } else if (column) {
      return (
        <td style={{...style, minWidth: column.Width || 100}} className={className}>
          {column.field}
        </td>
      )
    } else {
      return (
        <td style={style} className={className}>
          {children}
        </td>
      )
    }
  }
}
 
class EditTable extends Component {
  static propTpyes = {
    config: PropTypes.object,       // 配置信息
    updatecolumn: PropTypes.func    // 数据变化
  }
 
  state = {
    data: [{uuid: Utils.getuuid()}],
    columns: [],
    fields: []
  }
 
  UNSAFE_componentWillMount () {
    const { config } = this.props
 
    this.setState({
      columns: fromJS(config.cols).toJS(),
      fields: fromJS(config.columns).toJS()
    })
  }
 
  UNSAFE_componentWillReceiveProps (nextProps) {
    if (!is(fromJS(this.state.columns), fromJS(nextProps.config.cols))) {
      let _columns = fromJS(nextProps.config.cols).toJS()
      this.setState({columns: _columns})
      if (_columns[_columns.length - 1] && _columns[_columns.length - 1].focus) {
        this.editColumn(_columns[_columns.length - 1])
      }
    } else if (!is(fromJS(this.state.fields), fromJS(nextProps.config.columns))) {
      this.setState({fields: fromJS(nextProps.config.columns).toJS()})
    }
  }
  shouldComponentUpdate (nextProps, nextState) {
    const { config } = this.props
 
    return !is(fromJS(this.state), fromJS(nextState)) ||
      !is(fromJS(config.wrap), fromJS(nextProps.config.wrap)) ||
      !is(fromJS(config.search), fromJS(nextProps.config.search)) ||
      config.setting.laypage !== nextProps.config.setting.laypage
  }
 
  moveCol = (dragIndex, hoverIndex) => {
    let _columns = fromJS(this.state.columns).toJS()
 
    _columns.splice(hoverIndex, 0, ..._columns.splice(dragIndex, 1))
 
    this.setState({
      columns: _columns
    }, () => {
      this.props.updatecolumn({...this.props.config, cols: _columns})
    })
  }
 
  updateCol = (col) => {
    let _columns = fromJS(this.state.columns).toJS()
 
    if (col.isSub) {
      _columns = _columns.map(column => {
        if (column.type === 'colspan') {
          column.subcols = column.subcols.map(item => {
            if (item.uuid === col.uuid) {
              return col
            }
            return item
          })
        }
        return column
      })
    } else {
      _columns = _columns.map(column => {
        if (column.uuid === col.uuid) {
          return col
        }
        return column
      })
    }
 
    this.setState({
      columns: _columns,
    }, () => {
      this.props.updatecolumn({...this.props.config, cols: _columns})
    })
  }
 
  editColumn = (col) => {
    this.setState({
      card: fromJS(col).toJS()
    })
  }
 
  addElement = (col) => {
    const { config } = this.props
    let column = fromJS(col).toJS()
 
    if (column.type === 'colspan') {
      column.subcols = column.subcols || []
      let subcol = { isSub: true, focus: true, uuid: Utils.getuuid(), label: 'label', field: '', type: 'text' }
      column.subcols.push(subcol)
 
      this.setState({
        card: subcol
      })
      this.updateCol(column)
    } else if (column.type === 'custom') {
      let newcard = {uuid: Utils.getuuid(), focus: true, eleType: 'text', datatype: 'dynamic'}
  
      // 注册事件-添加元素
      MKEmitter.emit('cardAddElement', [config.uuid, column.uuid], newcard)
    } else if (column.type === 'action') {
      let newcard = {
        uuid: Utils.getuuid(),
        focus: true,
        eleType: 'button',
        label: 'button',
        OpenType: 'prompt',
        class: 'primary',
        intertype: 'system',
        execSuccess: 'grid',
        execError: 'never',
        show: 'link',
        $type: 'tableButton'
      }
 
      // 注册事件-添加元素
      MKEmitter.emit('cardAddElement', [config.uuid, column.uuid], newcard)
    }
  }
 
  submitCol = (col) => {
    const { card } = this.state
 
    col.uuid = card.uuid
    col.isSub = card.isSub === true
    col.marks = card.marks || []
    
    if (col.type === 'colspan') {
      col.subcols = card.subcols || []
    } else if (col.type === 'custom') {
      col.elements = card.type === 'custom' ? (card.elements || []) : []
    } else if (col.type === 'action') {
      col.elements = card.type === 'action' ? (card.elements || []) : []
    }
 
    this.setState({card: null})
    this.updateCol(col)
  }
 
  cancelCol = () => {
    const { card } = this.state
 
    if (card.focus) {
      this.deleteCol(card)
    }
 
    this.setState({card: null})
  }
 
  deleteCol = (col) => {
    let _columns = fromJS(this.state.columns).toJS()
 
    if (col.isSub) {
      _columns = _columns.map(column => {
        if (column.type !== 'colspan') return column
        if (column.subcols && column.subcols.length > 0) {
          column.subcols = column.subcols.filter(item => item.uuid !== col.uuid)
        }
        return column
      })
    } else {
      _columns = _columns.filter(column => column.uuid !== col.uuid)
    }
 
    this.setState({
      columns: _columns
    }, () => {
      this.props.updatecolumn({...this.props.config, cols: _columns})
    })
  }
 
  render() {
    const { config } = this.props
    const { fields, card } = this.state
    const components = {
      header: {
        cell: DragableHeaderCol
      },
      body: {
        cell: EditableCell
      }
    }
    
    const columns = this.state.columns.map((col, index) => {
      return {
        title: col.label,
        dataIndex: col.field,
        align: col.Align,
        sorter: col.IsSort === 'true',
        onCell: () => ({
          column: col,
          width: col.Width,
          config: config,
          upComponent: this.updateCol
        }),
        children: col.subcols && col.subcols.length > 0 ? col.subcols.map(cell => ({
          align: col.Align,
          title: cell.label,
          key: cell.uuid,
          onCell: () => ({
            column: cell,
            width: cell.Width,
            config: config,
            upComponent: this.updateCol
          }),
          onHeaderCell: () => ({
            column: cell,
            align: cell.Align,
            addElement: this.addElement,
            editColumn: this.editColumn,
            deleteCol: this.deleteCol,
          })
        })) : null,
        onHeaderCell: () => ({
          index,
          column: col,
          align: col.Align,
          moveCol: this.moveCol,
          addElement: this.addElement,
          editColumn: this.editColumn,
          deleteCol: this.deleteCol,
        })
      }
    })
 
    return (
      <div className={`normal-table-columns ${config.setting.laypage} ${config.wrap.tableType}`}>
        <DndProvider>
          <Table
            rowKey="uuid"
            bordered={config.wrap.border !== 'false'}
            components={components}
            dataSource={this.state.data}
            rowSelection={config.wrap.tableType ? { type: 'radio' } : null}
            columns={columns}
            rowClassName="editable-row"
            pagination={{
              current: 1,
              pageSize: 10,
              pageSizeOptions: ['10', '25', '50', '100', '500', '1000'],
              showSizeChanger: true,
              total: 58,
              showTotal: (total, range) => `${range[0]}-${range[1]} 共 ${total} 条`
            }}
          />
        </DndProvider>
        <EditColumn column={card} dict={coldict} fields={fields} submitCol={this.submitCol} cancelCol={this.cancelCol}/>
      </div>
    )
  }
}
 
export default Form.create()(EditTable)