dataSource.jsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import React from 'react'
  2. import { Layout, Row, Col, Input, Button, Table, Icon, Tag, Menu, Dropdown, Card } from 'antd'
  3. import { connect } from 'dva'
  4. import '../../models/dataSource'
  5. import '../../models/dataConnect'
  6. import './dataSource.less'
  7. import { dateFormat } from '../../utils/baseUtils'
  8. const { Content } = Layout
  9. const { Search } = Input
  10. class DataSource extends React.Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. loading: false,
  15. activeTab: 'dataSource',
  16. visibleCreateBox: false,
  17. selectedDataSourceCode: -1, // 当前选中的dataSource的code
  18. filterDropdownVisible: false,
  19. search: {} // 搜索条件
  20. }
  21. };
  22. componentDidMount() {
  23. this.setScrollTableHeight();
  24. }
  25. /**
  26. * 根据视图设置表格高度以呈现滚动条
  27. */
  28. setScrollTableHeight() {
  29. const mainContent = document.getElementsByClassName('main-content')[0];
  30. //const tabBar = mainContent.getElementsByClassName('datasource-view')[0];
  31. const toolbar = mainContent.getElementsByClassName('datasource-tools')[0];
  32. const tableHeader = mainContent.getElementsByClassName('ant-table-header')[0];
  33. const tableBody = mainContent.getElementsByClassName('ant-table-body')[0];
  34. tableBody.style.maxHeight=`${mainContent.offsetHeight - toolbar.offsetHeight - tableHeader.offsetHeight - 58}px`;
  35. }
  36. onInputChange = (name, value) => {
  37. const { search } = this.state;
  38. let newSearch = Object.assign({}, search );
  39. newSearch[name] = value;
  40. this.setState({ search: newSearch });
  41. }
  42. onSearch = () => {
  43. this.setState({
  44. filterDropdownVisible: false
  45. });
  46. }
  47. render() {
  48. const { dataSource, dispatch } = this.props;
  49. const { loading, activeTab, selectedDataSourceCode } = this.state;
  50. const moreOperatingMenu = (
  51. <Menu className='operationmenu'>
  52. <Menu.Item
  53. onClick={() => {
  54. dispatch({ type: 'main/redirect', path: '/chart/create' });
  55. dispatch({ type: 'chartDesigner/changeDataSource', value: {
  56. dataSource: selectedDataSourceCode,
  57. viewType: 'bar'
  58. } });
  59. }}
  60. >
  61. <Icon type="file-add" />创建图表
  62. </Menu.Item>
  63. <Menu.Item
  64. onClick={(e) => {
  65. let selectedModel = dataSource.list.find((i) => { return i.code === selectedDataSourceCode })
  66. dispatch({type: 'main/redirect', path: {pathname: '/datasource/'+ selectedModel.type +'/' + selectedModel.code + '/base'}});
  67. }}>
  68. <Icon type="info-circle-o" />属性设置
  69. </Menu.Item>
  70. <Menu.Item onClick={() => {
  71. let selectedModel = dataSource.list.find((i) => { return i.code === selectedDataSourceCode })
  72. dispatch({type: 'main/redirect', path: {pathname: '/datasource/'+ selectedModel.type +'/' + selectedModel.code + '/column'}});
  73. }}>
  74. <Icon type="table" />数据列设置
  75. </Menu.Item>
  76. <Menu.Item><Icon type="search" />预览数据</Menu.Item>
  77. <Menu.Item onClick={() => {
  78. let selectedModel = dataSource.list.find((i) => { return i.code === selectedDataSourceCode })
  79. dispatch({type: 'main/redirect', path: {pathname: '/datasource/'+ selectedModel.type +'/' + selectedModel.code + '/access'}});
  80. }}><Icon type="lock" />权限设置</Menu.Item>
  81. <Menu.Divider />
  82. <Menu.Item
  83. onClick={(e) => {
  84. dispatch({ type: 'dataSource/remoteDelete', code: selectedDataSourceCode });
  85. }}
  86. ><Icon type="delete" />删除</Menu.Item>
  87. </Menu>
  88. );
  89. const dataSourceColumns = [{
  90. title: '名称',
  91. dataIndex: 'name',
  92. key: 'name',
  93. width: 100,
  94. render: (text, record) => {
  95. return <div className='datasource-name'>
  96. <div className={`datasource-type type-${record.type.key}`}></div>
  97. <div>{text}</div>
  98. </div>
  99. }
  100. }, {
  101. title: '标签',
  102. dataIndex: 'tags',
  103. key: 'tag',
  104. width: 150,
  105. render: (text, record) => {
  106. text=text.join(',');
  107. let tags = text ? text.split(',').map((t, i) => {
  108. return <Tag className='datasource-tag' key={i}>{t}</Tag>
  109. }) : '';
  110. return (<div>
  111. {tags}
  112. </div>)
  113. }
  114. }, {
  115. title: '说明',
  116. dataIndex: 'description',
  117. key: 'description',
  118. width: 200
  119. }, {
  120. title: '创建人',
  121. dataIndex: 'creator',
  122. key: 'creator',
  123. width: 100
  124. }, {
  125. title: '创建时间',
  126. dataIndex: 'createTime',
  127. key: 'createTime',
  128. render: (text, record) => dateFormat(text, 'yyyy-MM-dd hh:mm:ss'),
  129. width: 100
  130. }, {
  131. title: '图表',
  132. dataIndex: 'chartSum',
  133. key: 'chartSum',
  134. width: 80
  135. }, {
  136. title: '操作',
  137. key: 'action',
  138. render: (text, record, index) => (
  139. <Dropdown code={record.code} overlay={moreOperatingMenu} trigger={['click']} >
  140. <Icon type="setting" />
  141. </Dropdown>
  142. ),
  143. width: 80
  144. }];
  145. return (
  146. <Layout className='datasource-view'>
  147. <Content>
  148. <Card className='datasource-body' title={
  149. <Row className='datasource-tools' type='flex' justify='end'>
  150. <Col className='search'>
  151. <Search
  152. placeholder="请输入关键字"
  153. onSearch={value => console.log(value)}
  154. />
  155. </Col>
  156. <Col>
  157. <Dropdown overlay={(
  158. <Menu onClick={(item, key, keyPath) => {
  159. const type = item.key;
  160. dispatch({ type: 'dataSource/resetNewModel' });
  161. dispatch({ type: 'dataSource/setNewModelField', name: 'type', value: type });
  162. dispatch({type: 'main/redirect', path: {pathname: '/datasource/'+ type +'/create'}});
  163. }}>
  164. <Menu.Item key='database'>数据库</Menu.Item>
  165. <Menu.Item key='file'>文件</Menu.Item>
  166. </Menu>
  167. )} trigger={['click']}>
  168. <Button style={{ display: activeTab==='dataConnect'?'none':'inline-block' }}>
  169. <Icon type="plus" />添加数据源
  170. </Button>
  171. </Dropdown>
  172. </Col>
  173. </Row>
  174. }>
  175. <Table
  176. className='datasource-table datasource-table'
  177. columns={dataSourceColumns}
  178. dataSource={dataSource.list.sort((a, b) => {
  179. return new Date(b.createTime) - new Date(a.createTime);
  180. })}
  181. loading={loading}
  182. size='small'
  183. scroll={{x: false, y: true}}
  184. pagination={false}
  185. onRow={(record) => {
  186. return {
  187. onClick: () => {this.setState({ selectedDataSourceCode: record.code})}
  188. }
  189. }}
  190. />
  191. </Card>
  192. </Content>
  193. </Layout>
  194. )
  195. }
  196. }
  197. function mapStateToProps({present: {dataSource, dataConnect}}) {
  198. return { dataSource, dataConnect }
  199. }
  200. export default connect(mapStateToProps)(DataSource)