king
2019-11-13 8b9effb98612ee8a00d76d639a5733d12e9ecce6
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
import React, { useState } from 'react'
import { useDrop } from 'react-dnd'
import update from 'immutability-helper'
import { Col } from 'antd'
import Utils from '@/utils/utils.js'
import Card from './card'
import ItemTypes from './itemtypes'
import './index.scss'
 
const Container = ({list, type, handleList, handleMenu }) => {
  let target = null
  const [cards, setCards] = useState(list)
  const moveCard = (id, atIndex) => {
    const { card, index } = findCard(id)
    const _cards = update(cards, { $splice: [[index, 1], [atIndex, 0, card]] })
    setCards(_cards)
    handleList({[type]: _cards})
  }
 
  const findCard = id => {
    const card = cards.filter(c => `${c.id}` === id)[0]
    return {
      card,
      index: cards.indexOf(card),
    }
  }
 
  const editCard = id => {
    const { card } = findCard(id)
    handleMenu(card)
  }
 
  const hasDrop = (item) => {
    target = item
  }
 
  const [, drop] = useDrop({
    accept: ItemTypes[type],
    drop(item) {
      if (item.hasOwnProperty('originalIndex')) {
        return
      }
      let newcard = {}
      if (item.type === 'search') {
        newcard.uuid = Utils.getuuid()
        newcard.label = 'fieldName'
        newcard.field = 'field'
        newcard.initval = ''
        newcard.type = item.subType
        if (item.subType === 'select') {
          newcard.resourceType = 0
          newcard.options = []
          newcard.dataSource = ''
        }
      } else if (item.type === 'action') {
        newcard.uuid = Utils.getuuid()
        newcard.label = 'button'
        newcard.func = ''
        newcard.Ot = 'notRequired'
        newcard.OpenType = item.subType
        newcard.icon = 'plus'
        newcard.class = 'green'
      } else if (item.type === 'columns') {
        newcard.uuid = Utils.getuuid()
        newcard.Align = 'left'
        newcard.label = 'fieldName'
        newcard.field = 'field'
        newcard.Hide = 'false'
        newcard.IsSort = item.subType
        newcard.Width = 120
      }
      
      let indexes = cards.map(car => {return car.id})
      let newid = 0
      while (indexes.includes(newid)) {
        newid++
      }
      newcard.id = newid
 
      let targetId = indexes.length > 0 ? indexes[indexes.length - 1] : 0
      if (target) {
        targetId = target.id
      }
 
      const { index: overIndex } = findCard(`${targetId}`)
      let targetIndex = overIndex
      if (!target) {
        targetIndex++
      }
      if (targetIndex < 0) {
        targetIndex = 0
      }
 
      const _cards = update(cards, { $splice: [[targetIndex, 0, newcard]] })
      setCards(_cards)
      handleList({[type]: _cards})
      target = null
    }
  })
  // const [, drop] = useDrop({ accept: ItemTypes[type] })
  return (
    <div ref={drop} className="ant-row" style={type === 'columns' ? {display: 'flex'} : {}}>
      {type === 'action' && cards.map(card => (
        <Card
          key={card.uuid}
          id={`${card.id}`}
          type={type}
          card={card}
          moveCard={moveCard}
          editCard={editCard}
          findCard={findCard}
          hasDrop={hasDrop}
        />
      ))}
      {type === 'search' && cards.map(card => (
        <Col key={card.uuid} span={6}>
          <Card
            key={card.uuid}
            id={`${card.id}`}
            type={type}
            card={card}
            moveCard={moveCard}
            editCard={editCard}
            findCard={findCard}
            hasDrop={hasDrop}
          />
        </Col>
      ))}
      {type === 'columns' && cards.length > 0 &&
      <div className="page-card" style={{flex: 60}}>
        <span className="ant-checkbox-inner"></span>
      </div>}
      {type === 'columns' && cards.map(card => (
        <Card
          key={card.uuid}
          id={`${card.id}`}
          type={type}
          card={card}
          moveCard={moveCard}
          editCard={editCard}
          findCard={findCard}
          hasDrop={hasDrop}
        />
      ))}
    </div>
  )
}
export default Container