index.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react'
  2. import { Layout, Tabs, Dropdown, Menu } from 'antd'
  3. import { connect } from 'dva'
  4. import MenuLayout from './sider'
  5. import DashboardViewToolbar from './toolbar'
  6. import DashboardView from './view'
  7. import EmptyContent from '../common/emptyContent/index'
  8. import CusIcon from '../common/cusIcon/index'
  9. import './index.less'
  10. const { Sider, Content } = Layout
  11. const TabPane = Tabs.TabPane
  12. class Home extends React.Component {
  13. componentDidMount() {
  14. const { home, dispatch } = this.props;
  15. dispatch({ type: 'home/getFixedDashboard' }).then(data => {
  16. if(data && home.tabs.length === 0) {
  17. dispatch({ type: 'home/openTab', tab: data })
  18. }
  19. });
  20. }
  21. // componentWillUnmount() {
  22. // const { home, dispatch } = this.props;
  23. // const { tabs } = home;
  24. // // 离开页面后将报表中的图表配置移除,以保证下次进入时触发自动刷新
  25. // tabs.forEach(t => {
  26. // t.config = null;
  27. // });
  28. // dispatch({ type: 'home/setField', name: 'tabs', value: tabs });
  29. // }
  30. generateTabs() {
  31. const { home } = this.props;
  32. const { tabs, fixedDashboard } = home;
  33. return tabs.map(t => (
  34. <TabPane tab={this.generateTabTitle(t.name, t.code)} key={t.code} closable={!fixedDashboard || t.code !== fixedDashboard.code}>
  35. {/* <TabPane tab={<span><CusIcon type='bi-nav-dashboard'/>{t.name}</span>} key={t.code}> */}
  36. <DashboardViewToolbar />
  37. <DashboardView code={t.code} config={t.config}/>
  38. </TabPane>
  39. ));
  40. }
  41. generateTabTitle = (text, tabKey) => (
  42. <Dropdown overlay={this.generateMenu(tabKey)} trigger={["contextMenu"]}>
  43. <span style={{ userSelect: "none" }}><CusIcon type='bi-nav-dashboard'/>{text}</span>
  44. </Dropdown>
  45. )
  46. generateMenu = (tabKey) => {
  47. const { fixedDashboard } = this.props.home
  48. return <Menu>
  49. <Menu.Item disabled={fixedDashboard && tabKey === fixedDashboard.code} key="1" onClick={e => {
  50. // 点击菜单项后触发tab的切换事件,这里将其事件阻断
  51. e.domEvent.stopPropagation();
  52. this.remove(tabKey);
  53. }}>关闭当前</Menu.Item>
  54. <Menu.Item key="2"onClick={e => {
  55. // 点击菜单项后触发tab的切换事件,这里将其事件阻断
  56. e.domEvent.stopPropagation();
  57. this.removeOther(tabKey);
  58. }}>关闭其他</Menu.Item>
  59. <Menu.Item key="3" onClick={e => {
  60. // 点击菜单项后触发tab的切换事件,这里将其事件阻断
  61. e.domEvent.stopPropagation();
  62. this.removeAll();
  63. }}>关闭所有</Menu.Item>
  64. </Menu>
  65. }
  66. onChange = (activeKey) => {
  67. const { dispatch, home } = this.props;
  68. const { selectedTab, tabs } = home;
  69. let tab = tabs.find(t => t.code === activeKey);
  70. dispatch({ type: 'home/saveCurrentTabDashboardConfig', code: selectedTab.code });
  71. dispatch({ type: 'home/changeTab', tab });
  72. }
  73. onEdit = (targetKey, action) => {
  74. this[action](targetKey);
  75. }
  76. remove = (targetKey) => {
  77. const { dispatch, home } = this.props;
  78. const { tabs: allTabs } = home;
  79. let tab = allTabs.find(t => t.code === targetKey);
  80. dispatch({ type: 'home/closeTab', tab });
  81. }
  82. removeOther = (targetKey) => {
  83. const { dispatch, home } = this.props;
  84. const { tabs: allTabs, fixedDashboard } = home;
  85. let tabs = allTabs.filter(t => (t.code !== targetKey && (!fixedDashboard || t.code !== fixedDashboard.code) ));
  86. let tab = allTabs.find(t => t.code === targetKey);
  87. dispatch({ type: 'home/closeTabs', tabs });
  88. dispatch({ type: 'home/changeTab', tab });
  89. }
  90. removeAll = () => {
  91. const { dispatch } = this.props;
  92. dispatch({ type: 'home/closeAllTabs' });
  93. }
  94. render() {
  95. const { home } = this.props;
  96. const { tabs, selectedTab } = home;
  97. return <Layout className='layout-home'>
  98. <Sider
  99. width={300}
  100. collapsible
  101. collapsedWidth={0}
  102. trigger={null}
  103. theme='light'
  104. className='sider-home'
  105. >
  106. <MenuLayout />
  107. </Sider>
  108. <Content className='content-home'>
  109. {tabs.length > 0 ? <Tabs
  110. type="editable-card"
  111. hideAdd
  112. onEdit={this.onEdit}
  113. onChange={this.onChange}
  114. activeKey={selectedTab ? selectedTab.code : null}
  115. >
  116. { this.generateTabs() }
  117. </Tabs> : <EmptyContent />}
  118. </Content>
  119. </Layout>
  120. }
  121. }
  122. export default connect(({ present: { home, dashboard } }) => ({ home, dashboard }))(Home)