viewLayout.jsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import React from "react"
  2. import "./viewLayout.less"
  3. import ReactGridLayout from 'react-grid-layout'
  4. import { Modal } from 'antd'
  5. import { connect } from 'dva'
  6. import EmptyContent from '../common/emptyContent/index'
  7. import DataPreview from '../common/dataPreview/dataPreview'
  8. import ViewLayoutItem from './viewLayoutItem';
  9. class ViewLayout extends React.PureComponent {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. previewItem: null,
  14. visiblePreviewBox: false,
  15. screenWidth: document.documentElement.clientWidth || document.body.clientWidth,
  16. screenHeight: document.documentElement.clientHeight || document.body.clientHeight,
  17. editingKey: null,
  18. richTextReadOnly: false,
  19. };
  20. }
  21. componentDidMount() {
  22. window.addEventListener('resize', this.onWindowResize);
  23. }
  24. componentWillUnmount() {
  25. window.removeEventListener('resize', this.onWindowResize);
  26. }
  27. onWindowResize = () => {
  28. this.setState({
  29. screenWidth: document.documentElement.clientWidth || document.body.clientWidth,
  30. screenHeight: document.documentElement.clientHeight || document.body.clientHeight
  31. });
  32. }
  33. createElement = (item, isPreview, reload) => {
  34. const { code, layout } = item;
  35. return <div key={code} data-grid={layout}>
  36. <ViewLayoutItem item={item} isPreview={isPreview} reload={reload}/>
  37. </div>
  38. }
  39. onLayoutChange = (layout) => {
  40. const { dispatch } = this.props;
  41. if(!(layout.length === 1 && layout[0].i === 'default-chartview')) {
  42. dispatch({ type: 'dashboardDesigner/changeLayout', layout });
  43. }
  44. }
  45. onResize = (Layout, oldItem, newItem, placeholder, e, element) => {
  46. const viewContentRef = this.viewContentRef;
  47. let box = viewContentRef.getBoundingClientRect();
  48. let bottomY = box.y + box.height;
  49. if(bottomY - e.clientY <= 5) { // 鼠标接近容器底部时滚动条滚到底
  50. window.clearTimeout(this.scrollToBottomKey);
  51. this.scrollToBottomKey = window.setTimeout(this.scrollToBottom, 50);
  52. }
  53. }
  54. scrollToBottom = () => {
  55. const viewContentRef = this.viewContentRef;
  56. viewContentRef.scrollTo && viewContentRef.scrollTo(0, viewContentRef.scrollHeight - viewContentRef.clientHeight)
  57. }
  58. showPreviewBox = (item) => {
  59. this.setState({
  60. previewItem: item,
  61. visiblePreviewBox: true,
  62. lastViewCode: item.code,
  63. lastPage: item.chartOption ? item.chartOption.page : 1,
  64. lastPageSize: item.chartOption ? item.chartOption.pageSize : 0,
  65. });
  66. }
  67. hidePreviewBox = () => {
  68. const { dashboardDesigner, dispatch } = this.props;
  69. const { lastViewCode, lastPage, lastPageSize } = this.state;
  70. let item = dashboardDesigner.items.find(x => x.code === lastViewCode)
  71. this.setState({
  72. previewItem: null,
  73. visiblePreviewBox: false
  74. });
  75. if(item.chartOption) {
  76. dispatch({ type: 'setItemFields', code: lastViewCode, fields: [
  77. { name: 'chartOption', value: { ...item.chartOption, page: lastPage, pageSize: lastPageSize } }
  78. ] });
  79. }
  80. }
  81. render() {
  82. const { dashboardDesigner, contentSize, dispatch } = this.props;
  83. const { editingKey } = this.state;
  84. const { editMode, minLayoutHeight, layoutMargin, theme: themeName } = dashboardDesigner;
  85. const { visiblePreviewBox, previewItem } = this.state;
  86. const children = dashboardDesigner.items.map((item) => this.createElement(item, false, !item.chartOption));
  87. console.log(contentSize);
  88. return (<div className={`dashboard-viewcontent ${themeName}`} ref={node => this.viewContentRef = node}>
  89. <ReactGridLayout
  90. width={ contentSize.width }
  91. autoSize={true}
  92. cols={12}
  93. margin = {editMode ? layoutMargin : layoutMargin}
  94. rowHeight = {minLayoutHeight}
  95. isDraggable={editMode && !editingKey}
  96. isResizable={editMode && !editingKey}
  97. draggableHandle='.mover'
  98. onLayoutChange={this.onLayoutChange}
  99. onResize={this.onResize}
  100. verticalCompact={true}
  101. compactType='vertical'
  102. >
  103. {(children.length === 0) ? <div key='default-chartview' className='default-chartview' data-grid={{ x: 0, y: 0, w: 12, h: 2, minW: 12, maxW: 12, minH: 2, maxH: 2, static: true }}>
  104. <EmptyContent />
  105. </div> : children}
  106. </ReactGridLayout>
  107. {visiblePreviewBox && previewItem.chartType === 'dataView' && <DataPreview
  108. title={previewItem.name}
  109. visibleBox={visiblePreviewBox}
  110. hideBox={this.hidePreviewBox}
  111. fetchFunction={(page, pageSize) => {
  112. dispatch({ type: 'dashboardDesigner/fetchDataList', item: previewItem, mandatory: true, page, pageSize });
  113. }}
  114. />}
  115. {visiblePreviewBox && previewItem.chartType !== 'dataView' && <Modal
  116. className='previewbox'
  117. width='80%'
  118. height='80%'
  119. visible={visiblePreviewBox}
  120. onCancel={this.hidePreviewBox}
  121. footer={null}
  122. keyboard={true}
  123. maskClosable={true}
  124. >
  125. {!!previewItem && this.createElement(dashboardDesigner.items.find(item => item.code === previewItem.code), true, false)}
  126. </Modal>}
  127. </div>);
  128. }
  129. }
  130. export default connect(({ present: { main, dashboardDesigner } }) => ({ main, dashboardDesigner }))(ViewLayout);