index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 classnames from 'classnames'
  22. import { Icon, Empty, Popover, Tag, Badge } from 'antd'
  23. import { IDownloadRecord } from 'app/containers/App/types'
  24. import { DOWNLOAD_STATUS_COLORS, DOWNLOAD_STATUS_LOCALE, DownloadStatus } from 'app/containers/App/constants'
  25. const styles = require('./DownloadList.less')
  26. interface IDownloadListProps {
  27. downloadList: IDownloadRecord[]
  28. onLoadDownloadList: () => void
  29. onDownloadFile: (id) => void
  30. }
  31. export function DownloadList (props: IDownloadListProps) {
  32. const {
  33. downloadList
  34. } = props
  35. let listContent
  36. let downloadable = 0
  37. if (downloadList && downloadList.length) {
  38. downloadable = downloadList.filter((d) => d.status === DownloadStatus.Success).length
  39. const downloadListItems = downloadList.map((record) => {
  40. const { id, name, status, uuid } = record
  41. const titleClass = classnames({
  42. [styles.success]: status === DownloadStatus.Success,
  43. [styles.downloaded]: status === DownloadStatus.Downloaded
  44. })
  45. return (
  46. <li key={`${uuid || ''}${id}`} className={styles.item}>
  47. <p
  48. className={titleClass}
  49. onClick={download(props, record)}
  50. >
  51. {name}
  52. </p>
  53. <Tag color={DOWNLOAD_STATUS_COLORS[status]}>
  54. {DOWNLOAD_STATUS_LOCALE[status]}
  55. </Tag>
  56. </li>
  57. )
  58. })
  59. listContent = (
  60. <ul className={styles.downloadList}>
  61. {downloadListItems}
  62. </ul>
  63. )
  64. } else {
  65. listContent = (
  66. <Empty
  67. key="empty"
  68. className={styles.empty}
  69. image={Empty.PRESENTED_IMAGE_SIMPLE}
  70. />
  71. )
  72. }
  73. return (
  74. <Popover
  75. content={listContent}
  76. trigger="click"
  77. placement="bottomRight"
  78. onVisibleChange={downloadListPanelVisibleChange}
  79. >
  80. <Badge count={downloadable}>
  81. <Icon type="cloud-download" onClick={getDownloadList(props)} />
  82. </Badge>
  83. </Popover>
  84. )
  85. }
  86. let downloadListPanelVisibleRecorder = false
  87. function downloadListPanelVisibleChange (visible) {
  88. downloadListPanelVisibleRecorder = visible
  89. }
  90. function getDownloadList (props: IDownloadListProps) {
  91. return function () {
  92. if (!downloadListPanelVisibleRecorder) {
  93. props.onLoadDownloadList()
  94. }
  95. }
  96. }
  97. function download (props: IDownloadListProps, record: IDownloadRecord) {
  98. return function () {
  99. const { id, status } = record
  100. if (status === DownloadStatus.Success
  101. || status === DownloadStatus.Downloaded) {
  102. props.onDownloadFile(id)
  103. }
  104. }
  105. }
  106. export default DownloadList