datasource.jsx 9.5 KB

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