king
2023-05-09 7b0dbecd1d6155d26ec67be0a47a16264c738c85
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
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { is, fromJS } from 'immutable'
import { Tabs } from 'antd'
 
import asyncComponent from '@/utils/asyncComponent'
import MkIcon from '@/components/mk-icon'
import MKEmitter from '@/utils/events.js'
import './index.scss'
 
const TabTransfer = asyncComponent(() => import('../../share/tabtransfer'))
const { TabPane } = Tabs
 
class antvTabs extends Component {
  static propTpyes = {
    config: PropTypes.object,        // 组件配置信息
    mainSearch: PropTypes.any,       // 外层搜索条件
  }
 
  state = {
    tabs: null,
    activeIndex: 1
  }
 
  UNSAFE_componentWillMount () {
    const { config } = this.props
 
    let _tabs = fromJS(config).toJS()
 
    if (_tabs.setting.supModule) {
      _tabs.subtabs = []
 
      let data = window.GLOB.CacheData.get(_tabs.setting.supModule)
 
      if (data) {
        let val = ''
        Object.keys(data).forEach(key => {
          if (key.toLowerCase() === _tabs.setting.controlField) {
            val = data[key]
          }
        })
        _tabs.subtabs = config.subtabs.filter(tab => {
          if (tab.controlVal === val) {
            return false
          } else if (/,/ig.test(tab.controlVal)) {
            return !tab.controlVal.split(',').includes(val)
          }
          return true
        })
      }
    }
 
    this.setState({
      tabs: _tabs
    })
  }
 
  shouldComponentUpdate (nextProps, nextState) {
    return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState))
  }
 
  componentDidMount () {
    const { config } = this.props
 
    if (config.setting.autoSwitch === 'true' && config.subtabs.length > 1 && config.setting.interval) {
      this.autoSwitch(config.setting.interval)
    }
 
    if (config.activeKey) {
      let node = document.getElementById('tab' + config.activeKey)
      node && node.click()
    }
 
    MKEmitter.addListener('resetSelectLine', this.resetParentParam)
  }
 
  /**
   * @description 组件销毁,清除state更新,清除快捷键设置
   */
  componentWillUnmount () {
    this.setState = () => {
      return
    }
    MKEmitter.removeListener('resetSelectLine', this.resetParentParam)
  }
 
  resetParentParam = (MenuID, id, data) => {
    const { tabs } = this.state
 
    if (tabs.setting.supModule === MenuID) {
      if (!data) {
        this.setState({
          tabs: {...tabs, subtabs: []}
        })
      } else {
        let val = ''
        Object.keys(data).forEach(key => {
          if (key.toLowerCase() === tabs.setting.controlField) {
            val = data[key]
          }
        })
        this.setState({
          tabs: {...tabs, subtabs: this.props.config.subtabs.filter(tab => {
            if (tab.controlVal === val) {
              return false
            } else if (/,/ig.test(tab.controlVal)) {
              return !tab.controlVal.split(',').includes(val)
            }
            return true
          })}
        })
      }
    }
  }
 
  autoSwitch = (interval) => {
    const { tabs, activeIndex } = this.state
 
    if (!tabs) return
 
    let index = activeIndex
 
    if (!tabs.subtabs[index]) {
      index = 0
    }
 
    let id = 'tab' + tabs.subtabs[index].uuid
 
    this.setState({activeIndex: ++index})
 
    setTimeout(() => {
      let node = document.getElementById(id)
      if (node) {
        node.click()
        this.autoSwitch(interval)
      }
    }, interval * 1000)
  }
 
  render() {
    const { mainSearch } = this.props
    const { tabs } = this.state
 
    if (!tabs.subtabs.length) return null
 
    return (
      <div className={'menu-antv-tabs-wrap ' + (tabs.setting.tabLabel || '')} id={'anchor' + tabs.uuid} style={tabs.style}>
        <Tabs defaultActiveKey="1" tabBarStyle={{background: tabs.setting.backgroundColor || 'transparent'}} tabPosition={tabs.setting.position} type={tabs.setting.tabStyle}>
          {tabs.subtabs.map(tab => (
            <TabPane tab={<span id={'tab' + tab.uuid}>{tab.icon ? <MkIcon type={tab.icon} /> : null}{tab.label}</span>} style={{backgroundColor: tab.backgroundColor || 'transparent'}} key={tab.uuid}>
              <TabTransfer config={tab} mainSearch={mainSearch}/>
            </TabPane>
          ))}
        </Tabs>
      </div>
    )
  }
}
 
export default antvTabs