DashboardForm.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React from 'react'
  21. import { Form, Row, Col, Input, Radio, Select, Tabs, Checkbox} from 'antd'
  22. import { IExludeRoles } from 'containers/Viz/components/PortalList'
  23. import { FormComponentProps } from 'antd/lib/form'
  24. const styles = require('containers/Viz/Viz.less')
  25. const TabPane = Tabs.TabPane
  26. const Option = Select.Option
  27. const FormItem = Form.Item
  28. const RadioGroup = Radio.Group
  29. const utilStyles = require('assets/less/util.less')
  30. interface IDashboardFormProps extends FormComponentProps {
  31. portalId: number
  32. type: string
  33. itemId: number
  34. dashboards: any[]
  35. onCheckUniqueName: (pathname: string, data: any, resolve: () => any, reject: (error: string) => any) => any
  36. exludeRoles?: IExludeRoles[]
  37. onChangePermission: (scope: object, e: any) => any
  38. }
  39. export class DashboardForm extends React.PureComponent<IDashboardFormProps, {}> {
  40. private checkNameUnique = (rule, value = '', callback) => {
  41. const { onCheckUniqueName, type, form, portalId} = this.props
  42. const { id } = form.getFieldsValue()
  43. const data = {
  44. portal: portalId,
  45. id: type === ('add' || 'copy') ? '' : id,
  46. name: value
  47. }
  48. if (!value) {
  49. callback()
  50. }
  51. type === 'move'
  52. ? callback()
  53. : onCheckUniqueName('dashboard', data,
  54. () => {
  55. callback()
  56. }, (err) => {
  57. callback(err)
  58. })
  59. }
  60. public render () {
  61. const { getFieldDecorator } = this.props.form
  62. const { dashboards, type, itemId, exludeRoles } = this.props
  63. const commonFormItemStyle = {
  64. labelCol: { span: 6 },
  65. wrapperCol: { span: 16 }
  66. }
  67. const authControl = exludeRoles && exludeRoles.length ? exludeRoles.map((role) => (
  68. <div className={styles.excludeList} key={`${role.name}key`}>
  69. <Checkbox checked={role.permission} onChange={this.props.onChangePermission.bind(this, role)}/>
  70. <b>{role.name}</b>
  71. </div>
  72. )) : []
  73. const dashboardsArr = (dashboards as any[]).filter((d) => d.type === 0)
  74. const folderOptions = (dashboardsArr as any[]).map((s) => <Option key={`${s.id}`} value={`${s.id}`}>{s.name}</Option>)
  75. const deleteItem = (dashboards as any[]).find((d) => d.id === Number(itemId))
  76. let deleteType = ''
  77. let deleteName = ''
  78. if (deleteItem) {
  79. deleteType = deleteItem.type === 0 ? '文件夹' : '仪表板'
  80. deleteName = deleteItem.name
  81. }
  82. return (
  83. <Form>
  84. {type !== 'add' && type !== 'copy' && (
  85. <FormItem className={utilStyles.hide}>
  86. {getFieldDecorator('id', {})(
  87. <Input />
  88. )}
  89. </FormItem>
  90. )}
  91. <Row gutter={8} className={type === 'move' ? '' : utilStyles.hide}>
  92. <Col span={24}>
  93. <FormItem label="所属文件夹" {...commonFormItemStyle}>
  94. {getFieldDecorator('folder', {
  95. rules: [{
  96. required: true,
  97. message: '请选择所属文件夹'
  98. }],
  99. initialValue: '0'
  100. })(
  101. <Select>
  102. <Option key="0" value="0">根目录</Option>
  103. {folderOptions}
  104. </Select>
  105. )}
  106. </FormItem>
  107. </Col>
  108. </Row>
  109. <Row gutter={8} className={type === 'delete' || type === 'move' ? utilStyles.hide : ''}>
  110. <Tabs defaultActiveKey="dashboardInfo">
  111. <TabPane tab="基本信息" key="dashboardInfo">
  112. <Col span={24}>
  113. <FormItem label="所属文件夹" {...commonFormItemStyle}>
  114. {getFieldDecorator('folder', {
  115. rules: [{
  116. required: true,
  117. message: '请选择所属文件夹'
  118. }],
  119. // initialValue: (folderOptions as any[]).length ? `${dashboardsArr[0].name}` : ''
  120. initialValue: '0'
  121. })(
  122. <Select>
  123. <Option key="0" value="0">根目录</Option>
  124. {folderOptions}
  125. </Select>
  126. )}
  127. </FormItem>
  128. <FormItem className={utilStyles.hide}>
  129. {getFieldDecorator('config', {})(
  130. <Input />
  131. )}
  132. </FormItem>
  133. <FormItem className={utilStyles.hide}>
  134. {getFieldDecorator('index', {})(
  135. <Input />
  136. )}
  137. </FormItem>
  138. <FormItem
  139. label={type === 'copy' ? '重命名' : '名称'}
  140. {...commonFormItemStyle}
  141. hasFeedback
  142. className={type === 'move' ? utilStyles.hide : ''}
  143. >
  144. {getFieldDecorator('name', {
  145. rules: [{
  146. required: true,
  147. message: '名称 不能为空'
  148. }, {
  149. validator: this.checkNameUnique
  150. }]
  151. })(
  152. <Input placeholder="名称" />
  153. )}
  154. </FormItem>
  155. <FormItem
  156. label="选择类型"
  157. {...commonFormItemStyle}
  158. className={type === 'move' ? utilStyles.hide : ''}
  159. >
  160. {getFieldDecorator('selectType', {
  161. initialValue: 1
  162. })(
  163. <RadioGroup disabled={type === 'edit' || type === 'copy' || type === 'move'}>
  164. <Radio value={0}>文件夹</Radio>
  165. <Radio value={1}>仪表板</Radio>
  166. {/* <Radio value={2}>Report</Radio> */}
  167. {/* <Select disabled={type === 'edit' || type === 'copy' || type === 'move'}>
  168. <Option key="0" value="0">文件夹</Option>
  169. <Option key="Dashboard" value="1">Dashboard</Option>
  170. <Option key="Report" value="2">Report</Option>
  171. </Select> */}
  172. </RadioGroup>
  173. )}
  174. </FormItem>
  175. </Col>
  176. </TabPane>
  177. <TabPane tab="权限管理" key="dashboardControl">
  178. {
  179. authControl
  180. }
  181. </TabPane>
  182. </Tabs>
  183. </Row>
  184. <p className={type === 'delete' ? '' : utilStyles.hide}>
  185. 确定要删除 {deleteType}:{deleteName} ?
  186. </p>
  187. </Form>
  188. )
  189. }
  190. }
  191. export default Form.create<IDashboardFormProps>()(DashboardForm)