import React, { useState } from 'react'
|
import { useDrop } from 'react-dnd'
|
import { is, fromJS } from 'immutable'
|
import update from 'immutability-helper'
|
import Utils from '@/utils/utils.js'
|
import Card from './card'
|
import './index.scss'
|
|
const Container = ({list, setting, placeholder, 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
|
|
if (copycard.OpenType === 'popview') { // 待完善
|
copycard.linkTab = ''
|
}
|
|
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)
|
}
|
|
const [, drop] = useDrop({
|
accept: 'action',
|
drop(item) {
|
if (item.hasOwnProperty('originalIndex')) {
|
return
|
}
|
|
let newcard = {}
|
newcard.uuid = Utils.getuuid()
|
newcard.focus = true
|
|
newcard.label = 'button'
|
newcard.sqlType = ''
|
newcard.Ot = 'requiredSgl'
|
newcard.OpenType = item.subType
|
newcard.tabType = 'SubTable'
|
newcard.icon = ''
|
newcard.class = 'default'
|
newcard.intertype = 'system'
|
newcard.method = 'POST'
|
newcard.position = 'toolbar'
|
newcard.execSuccess = 'grid'
|
newcard.execError = 'never'
|
newcard.popClose = 'never'
|
newcard.errorTime = 10
|
newcard.verify = null
|
|
if (item.subType === 'excelIn') {
|
// 导入和导出excel,按钮名称直接为导入、导出
|
newcard.label = item.label
|
newcard.class = 'border-dgreen'
|
newcard.Ot = 'notRequired'
|
} else if (item.subType === 'excelOut') {
|
newcard.label = item.label
|
newcard.intertype = setting.interType
|
newcard.innerFunc = setting.innerFunc
|
newcard.sysInterface = setting.sysInterface
|
newcard.outerFunc = setting.outerFunc
|
newcard.interface = setting.interface
|
newcard.class = 'dgreen'
|
}
|
|
let targetId = ''
|
|
if (item.dropTargetId) {
|
targetId = item.dropTargetId
|
delete item.dropTargetId
|
} else if (cards.length > 0) {
|
targetId = cards[cards.length - 1].uuid
|
}
|
|
const { index: overIndex } = findCard(`${targetId}`)
|
const _cards = update(cards, { $splice: [[overIndex + 1, 0, newcard]] })
|
|
handleList(_cards, newcard)
|
}
|
})
|
|
return (
|
<div ref={drop} className="ant-row">
|
{cards.map(card => (
|
<Card
|
id={card.uuid}
|
key={card.uuid}
|
card={card}
|
moveCard={moveCard}
|
copyCard={copyCard}
|
editCard={editCard}
|
delCard={delCard}
|
findCard={findCard}
|
profileCard={profileCard}
|
doubleClickCard={doubleClickBtn}
|
/>
|
))}
|
|
{cards.length === 0 ?
|
<div className="common-drawarea-placeholder">
|
{placeholder}
|
</div> : null
|
}
|
</div>
|
)
|
}
|
export default Container
|