king
2020-10-11 5902ba5c3ff85efc78c95364196cd6ab5d2d1601
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 { is, fromJS } from 'immutable'
import update from 'immutability-helper'
import { Icon } from 'antd'
 
import Utils from '@/utils/utils.js'
import Card from './card'
import './index.scss'
 
const Container = ({plus, list, handleList, handleMenu, deleteMenu, profileMenu, doubleClickCard }) => {
  const [cards, setCards] = useState(list)
  const moveCard = (id, atIndex) => {
    const { card, index } = findCard(id)
    const _cards = update(cards, { $splice: [[index, 1], [atIndex, 0, card]] })
    handleList(_cards)
  }
 
  if (!is(fromJS(cards), fromJS(list))) {
    setCards(list)
  }
  
  const findCard = id => {
    const card = cards.filter(c => `${c.uuid}` === id)[0]
    return {
      card,
      index: cards.indexOf(card),
    }
  }
 
  const doubleClickBtn = id => {
    const { card } = findCard(id)
    doubleClickCard(card)
  }
 
  const editCard = id => {
    const { card } = findCard(id)
    handleMenu(card)
  }
  
  const profileCard = id => {
    const { card } = findCard(id)
    profileMenu(card)
  }
 
  const delCard = id => {
    const { card } = findCard(id)
    deleteMenu(card)
  }
 
  const copyCard = id => {
    const { card } = findCard(id)
    let copycard = fromJS(card).toJS()
 
    copycard.uuid = Utils.getuuid()
    copycard.origin = false
    copycard.copyType = 'action'
    copycard.focus = true
 
    copycard.originCard = card
 
    let _val = fromJS(copycard).toJS()
 
    try {
      _val.uuid = Utils.getuuid()
      _val = window.btoa(window.encodeURIComponent(JSON.stringify(_val)))
    } catch {
      console.warn('Stringify Failure')
      _val = ''
    }
 
    if (_val) {
      let oInput = document.createElement('input')
      oInput.value = _val
      document.body.appendChild(oInput)
      oInput.select()
      document.execCommand('Copy')
      document.body.removeChild(oInput)
    }
 
    const { index: overIndex } = findCard(id)
 
    const _cards = update(cards, { $splice: [[overIndex + 1, 0, copycard]] })
 
    handleList(_cards, copycard)
  }
 
  let cardIds = cards.map(card => card.uuid)
 
  const [, drop] = useDrop({
    accept: 'action',
    drop() {}
  })
 
  const addaction = () => {
    let newcard = {}
    newcard.uuid = Utils.getuuid()
    newcard.focus = true
    
    newcard.label = 'button'
    newcard.sqlType = ''
    newcard.Ot = 'requiredSgl'
    newcard.OpenType = 'prompt'
    newcard.icon = ''
    newcard.class = 'primary'
    newcard.intertype = 'system'
    newcard.method = 'POST'
    newcard.execSuccess = 'grid'
    newcard.execError = 'never'
    newcard.popClose = 'never'
    newcard.errorTime = 10
    newcard.verify = null
    newcard.show = 'icon'
    
    let targetId = cards.length > 0 ? cards[cards.length - 1].uuid : 0
 
    const { index: overIndex } = findCard(`${targetId}`)
    let targetIndex = overIndex
 
    targetIndex++
 
    const _cards = update(cards, { $splice: [[targetIndex, 0, newcard]] })
 
    handleList(_cards, newcard)
  }
 
  return (
    <div ref={drop} className="ant-row">
      {cards.map(card => (
        <Card
          id={card.uuid}
          key={card.uuid}
          cardIds={cardIds}
          card={card}
          moveCard={moveCard}
          copyCard={copyCard}
          editCard={editCard}
          delCard={delCard}
          findCard={findCard}
          profileCard={profileCard}
          doubleClickCard={doubleClickBtn}
        />
      ))}
      {plus !== 'false' ? <Icon type="plus" title="添加按钮" onClick={addaction}/> : null}
    </div>
  )
}
export default Container