DashboardAction.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 { Icon, Tooltip, Popover } from 'antd'
  22. const styles = require('../Dashboard.less')
  23. import { IProject } from 'containers/Projects/types'
  24. import ShareDownloadPermission from 'containers/Account/components/checkShareDownloadPermission'
  25. import ModulePermission from 'containers/Account/components/checkModulePermission'
  26. import { getTextWidth } from 'utils/util'
  27. interface IDashboardActionProps {
  28. currentProject: IProject
  29. depth: number
  30. item: {
  31. id: number,
  32. type: number,
  33. name: string
  34. }
  35. splitWidth: number
  36. onInitOperateMore: (item: any, type: string) => any
  37. initChangeDashboard: (id: number) => any
  38. }
  39. interface IDashboardActionState {
  40. popoverVisible: boolean
  41. }
  42. export class DashboardAction extends React.PureComponent<IDashboardActionProps, IDashboardActionState> {
  43. constructor (props) {
  44. super(props)
  45. this.state = {
  46. popoverVisible: false
  47. }
  48. }
  49. private handleVisibleChange = (visible) => {
  50. this.setState({
  51. popoverVisible: visible
  52. })
  53. }
  54. private operateMore = (item, type) => (e) => {
  55. const { popoverVisible } = this.state
  56. const { onInitOperateMore } = this.props
  57. if (this.state.popoverVisible) {
  58. this.setState({
  59. popoverVisible: false
  60. })
  61. }
  62. onInitOperateMore(item, type)
  63. }
  64. private computeTitleWidth (text: string, wrapperWidth: number) {
  65. const textWidth = getTextWidth(text)
  66. const textLength = text.length
  67. return text
  68. }
  69. public render () {
  70. const {
  71. currentProject,
  72. depth,
  73. item,
  74. initChangeDashboard,
  75. splitWidth
  76. } = this.props
  77. const { popoverVisible } = this.state
  78. const EditActionButton = ModulePermission<React.DetailedHTMLProps<React.HTMLAttributes<HTMLLIElement>, HTMLLIElement>>(currentProject, 'viz')(Li)
  79. const editAction = (
  80. <EditActionButton onClick={this.operateMore(item, 'edit')}>
  81. <Icon type="edit" /> 编辑
  82. </EditActionButton>
  83. )
  84. const DownloadButton = ShareDownloadPermission<React.DetailedHTMLProps<React.HTMLAttributes<HTMLLIElement>, HTMLLIElement>>(currentProject, 'download')(Li)
  85. const downloadAction = (
  86. <DownloadButton style={{cursor: 'pointer'}} onClick={this.operateMore(item, 'download')}>
  87. <Icon type="download" className={styles.swap} /> 下载
  88. </DownloadButton>
  89. )
  90. const moveAction = (
  91. <EditActionButton onClick={this.operateMore(item, 'move')}>
  92. <Icon type="swap" className={styles.swap} /> 移动
  93. </EditActionButton>
  94. )
  95. const DeleteActionButton = ModulePermission<React.DetailedHTMLProps<React.HTMLAttributes<HTMLLIElement>, HTMLLIElement>>(currentProject, 'viz', true)(Li)
  96. const deleteAction = (
  97. <DeleteActionButton onClick={this.operateMore(item, 'delete')}>
  98. <Icon type="delete" /> 删除
  99. </DeleteActionButton>
  100. )
  101. const ulActionAll = (
  102. <ul className={styles.menu}>
  103. <li>{editAction}</li>
  104. {/* <li>{downloadAction}</li> */}
  105. <li>{moveAction}</li>
  106. <li>{deleteAction}</li>
  107. </ul>
  108. )
  109. const ulActionPart = (
  110. <ul className={styles.menu}>
  111. <li>{editAction}</li>
  112. {/* <li>{downloadAction}</li> */}
  113. <li>{moveAction}</li>
  114. </ul>
  115. )
  116. const icon = (
  117. <Icon
  118. type="ellipsis"
  119. className={styles.itemAction}
  120. title="More"
  121. />
  122. )
  123. let ulPopover
  124. if (currentProject && currentProject.permission) {
  125. const currentPermission = currentProject.permission.vizPermission
  126. if (currentPermission === 0) {
  127. ulPopover = null
  128. } else {
  129. ulPopover = (
  130. <Popover
  131. placement="bottomRight"
  132. content={currentPermission === 2 ? ulActionPart : ulActionAll}
  133. trigger="click"
  134. visible={popoverVisible}
  135. onVisibleChange={this.handleVisibleChange}
  136. >
  137. {icon}
  138. </Popover>)
  139. }
  140. }
  141. const computeWidth: number = splitWidth - 60 - 18 * depth - 6
  142. const titleWidth: string = `${computeWidth}px`
  143. const computeTitleWidth = this.computeTitleWidth
  144. return (
  145. <span className={styles.portalTreeItem}>
  146. <Tooltip placement="right" title={`名称:${item.name}`}>
  147. {
  148. item.type === 0
  149. ? <h4 className={styles.dashboardTitle} style={{ width: titleWidth }}>{computeTitleWidth(item.name, computeWidth)}</h4>
  150. : <span className={styles.dashboardTitle} style={{width: titleWidth}} onClick={initChangeDashboard(item.id)}>
  151. <Icon type={`${item.type === 2 ? 'table' : 'dot-chart'}`} />
  152. <span className={styles.itemName}>{computeTitleWidth(item.name, computeWidth)}</span>
  153. </span>
  154. }
  155. {ulPopover}
  156. </Tooltip>
  157. </span>
  158. )
  159. }
  160. }
  161. function Li (props: React.DetailedHTMLProps<React.HTMLAttributes<HTMLLIElement>, HTMLLIElement>) {
  162. return (
  163. <span {...props} >{props.children}</span>
  164. )
  165. }
  166. export default DashboardAction