king
2025-01-17 cd14cfe4d574c3ddda341a36c76f98bc22438b44
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
import React, { useState } from 'react'
import { useDrop } from 'react-dnd'
import { is, fromJS } from 'immutable'
import update from 'immutability-helper'
import { Modal } from 'antd'
 
import Utils from '@/utils/utils.js'
import Card from './card'
import './index.scss'
 
const { confirm } = Modal
 
const Container = ({menu, handleList }) => {
  const [cards, setCards] = useState(menu.components)
 
  if (!is(fromJS(cards), fromJS(menu.components))) {
    setCards(menu.components)
  }
  
  const findCard = id => {
    const card = cards.filter(c => `${c.uuid}` === id)[0]
    return {
      card,
      index: cards.indexOf(card),
    }
  }
 
  const updateConfig = (element) => {
    let _cards = cards.map(item => item.uuid === element.uuid ? element : item)
 
    if (element.type === 'tabs' && element.subtabs.length === 0) {
      _cards = cards.filter(item => item.uuid !== element.uuid)
    }
 
    handleList({...menu, components: _cards})
  }
 
  const switchConfig = (dragKey, hoverKey, callback) => {
    let _tab = null
 
    let hoverTabsIndex = 0
    let hoverIndex = 0
    let _cards = cards.map((item, pindex) => {
      if (item.type === 'tabs') {
        item.subtabs = item.subtabs.filter((tab, index) => {
          if (tab.uuid === dragKey) {
            _tab = tab
            return false
          }
          if (tab.uuid === hoverKey) {
            hoverTabsIndex = pindex
            hoverIndex = index
          }
          return true
        })
      }
      return item
    })
 
    if (hoverTabsIndex && _tab) {
      _cards[hoverTabsIndex].subtabs.splice(hoverIndex + 1, 0, _tab)
    } else {
      return
    }
 
    _cards = _cards.filter(item => {
      if (item.type !== 'tabs') return true
      if (item.subtabs.length === 0) return false
 
      return true
    })
 
    handleList({...menu, components: _cards})
 
    setTimeout(() => {
      callback()
    }, 100)
  }
 
  const deleteCard = (id) => {
    const { card } = findCard(id)
 
    confirm({
      title: `确定删除子表组吗?`,
      onOk() {
        const _cards = cards.filter(item => item.uuid !== card.uuid)
        handleList({...menu, components: _cards})
      },
      onCancel() {}
    })
  }
 
  const [, drop] = useDrop({
    accept: 'menu',
    drop(item) {
      if (item.added || item.index) {
        delete item.added // 删除组件添加标记
        delete item.dropTargetId
        return
      }
      
      let newcard = {
        uuid: Utils.getuuid(),
        type: 'tabs',
        isNew: true
      }
      
      let targetId = ''
 
      if (item.dropTargetId) {
        targetId = item.dropTargetId
        delete item.dropTargetId
      } else if (cards.length > 0) {
        targetId = cards.slice(-1)[0].uuid
      }
 
      const { index: overIndex } = findCard(`${targetId}`)
      const _cards = update(cards, { $splice: [[overIndex + 1, 0, newcard]] })
 
      // setCards(_cards)
      handleList({...menu, components: _cards})
    }
  })
 
  let style = JSON.stringify(menu.style || {})
  style = style.replace(/@mywebsite@\//ig, window.GLOB.baseurl)
  style = JSON.parse(style)
 
  return (
    <div ref={drop} className="table-shell-inner" style={style}>
      <div className="ant-row">
        {cards.map(card => (
          <Card
            id={card.uuid}
            key={card.uuid}
            card={card}
            delCard={deleteCard}
            findCard={findCard}
            updateConfig={updateConfig}
            switchConfig={switchConfig}
          />
        ))}
      </div>
    </div>
  )
}
export default Container