content.jsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import React from 'react'
  2. import { Layout, Tag, Icon } from 'antd'
  3. import { connect } from 'dva'
  4. import ViewLayout from './viewLayout'
  5. import FilterBox from '../common/filterBox/filterBox2'
  6. import Filter from '../common/filterBox/filter2'
  7. import ConfigSider from './configSider'
  8. const { Header, Content, Sider } = Layout
  9. class DashboardDesignerContent extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. lastContentSize: {
  14. scroll: false,
  15. width: 0,
  16. height: 0,
  17. },
  18. visibleFilterBox: false,
  19. closeFilters: false,
  20. };
  21. this.contentRef=React.createRef();
  22. }
  23. componentDidMount() {
  24. // this.refreshContentSize();
  25. const { dashboardDesigner } = this.props;
  26. const { filters } = dashboardDesigner;
  27. this.setState({
  28. closeFilters: filters.length > 6
  29. });
  30. window.addEventListener("resize", this.refreshContentSize);
  31. }
  32. componentWillUnmount() {
  33. window.removeEventListener('resize', this.refreshContentSize);
  34. }
  35. showFilterBox = (e) => {
  36. this.setState({
  37. visibleFilterBox: true
  38. });
  39. }
  40. hideFilterBox = (e) => {
  41. this.setState({
  42. visibleFilterBox: false,
  43. });
  44. }
  45. /**
  46. * 重设容器宽度
  47. */
  48. refreshContentSize = () => {
  49. this.setState({
  50. refresh: true
  51. });
  52. }
  53. getContentSize = () => {
  54. const { lastContentSize } = this.state;
  55. let contentEl = this.contentRef.current;
  56. if(!contentEl) {
  57. return {
  58. width: 0,
  59. height: 0
  60. };
  61. }
  62. let viewcontent = contentEl.getElementsByClassName('dashboard-viewcontent')[0];
  63. let _scroll = viewcontent.scrollHeight > viewcontent.clientHeight;
  64. let padding = 0;
  65. let width = contentEl.offsetWidth - padding * 2 - (_scroll ? 10 : 2) // 有滚动条时需要减去滚动条的宽度
  66. if(width > 0 && width !== lastContentSize.width) {
  67. this.setState({
  68. lastContentSize: {
  69. width,
  70. height: contentEl.clientHeight,
  71. scroll: _scroll
  72. }
  73. });
  74. }
  75. return {
  76. width: width,
  77. height: contentEl.clientHeight
  78. }
  79. }
  80. createFilters = (filters) => {
  81. const { dispatch, afterRefresh } = this.props;
  82. dispatch({ type: 'dashboardDesigner/changeFilters', filters }).then(() => {
  83. afterRefresh && typeof afterRefresh === 'function' && afterRefresh()
  84. });
  85. }
  86. filterUsingChange = (e) => {
  87. const key = e.target.dataset.key;
  88. const { dashboardDesigner, dispatch, afterRefresh } = this.props;
  89. const filters = dashboardDesigner.filters;
  90. let filter = filters.find(f => +f.key === +key);
  91. dispatch({ type: 'dashboardDesigner/changeFilter', filter: { ...filter, using: filter.type ? !filter.using : false} }).then(() => {
  92. afterRefresh && typeof afterRefresh === 'function' && afterRefresh()
  93. });
  94. }
  95. changeFilterValue = (filter) => {
  96. const { dispatch, afterRefresh } = this.props;
  97. dispatch({ type: 'dashboardDesigner/changeFilter', filter }).then(() => {
  98. afterRefresh && typeof afterRefresh === 'function' && afterRefresh()
  99. });
  100. }
  101. render() {
  102. const { dashboardDesigner, isOwner, isShareView, isShareKeyView, isViewMode } = this.props;
  103. const { dataSources, editMode, filters, relationColumns } = dashboardDesigner;
  104. const { visibleFilterBox, lastContentSize, closeFilters } = this.state;
  105. const contentSize = this.getContentSize();
  106. return <Layout className='content'>
  107. <Content className={`dashboard-content${(isShareView || isShareKeyView || isViewMode) ? ' nomargin' : ''}`}>
  108. <Layout className='content-layout'>
  109. {isOwner && editMode && !isShareView && !isShareKeyView && !isViewMode && <Sider className={`config-sider${ (isOwner && editMode) ? '' : ' config-sider-closed' }`} width={(isOwner && editMode && !isShareView && !isShareKeyView && !isViewMode) ? 200 : 0}>
  110. <ConfigSider/>
  111. </Sider>}
  112. {/**
  113. 这里直接用main标签而不用antd的Header组件是为了让ref能够定位到对应的dom元素
  114. */}
  115. <main ref={this.contentRef} className='viewlayout ant-layout-content'>
  116. {(editMode || (filters && filters.length > 0) )&& <Header>
  117. {/* <div className='toggle'><Icon type="caret-up" /></div> */}
  118. <div className={`filters${closeFilters ? ' closed': ''}`}>
  119. <div className='content'>
  120. {isOwner && editMode && !isShareView && !isShareKeyView && !isViewMode && <Tag
  121. onClick={this.showFilterBox}
  122. className={`filter-tag filter-tag-add`}
  123. >
  124. <Icon type="filter" theme="outlined" />
  125. </Tag>}
  126. {filters.map(f => (
  127. <Filter
  128. key={f.key}
  129. filter={f}
  130. changeFilterValue={this.changeFilterValue}
  131. />
  132. ))}
  133. </div>
  134. <div className='right' style={{ display: filters.length > 0 ? 'block' : 'none' }}>
  135. <Icon title={closeFilters ? '展开' : '收起'} type={closeFilters ? 'caret-up' : 'caret-down'} onClick={() => {
  136. this.setState({
  137. closeFilters: !closeFilters
  138. });
  139. }}/>
  140. </div>
  141. </div>
  142. {visibleFilterBox && <FilterBox key={Math.random()} dataSources={dataSources} relationColumns={relationColumns} filterData={filters} visibleFilterBox={visibleFilterBox} showFilterBox={this.showFilterBox} hideFilterBox={this.hideFilterBox} createFilters={this.createFilters} />}
  143. </Header>}
  144. <ViewLayout isOwner={isOwner} isShareView={isShareView} isShareKeyView={isShareKeyView} isViewMode={isViewMode} contentSize={contentSize} lastContentSize={lastContentSize} editMode={editMode}/>
  145. </main>
  146. </Layout>
  147. </Content>
  148. </Layout>
  149. }
  150. }
  151. export default connect(({ present: { dashboardDesigner } }) => ({ dashboardDesigner }))(DashboardDesignerContent);