util.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 { Tree, Icon, Popover } from 'antd'
  22. const { TreeNode } = Tree
  23. import { NodeKeyPrefix } from './constants'
  24. import {
  25. IPortal,
  26. IDashboard,
  27. IDisplayFormed,
  28. ISlideFormed
  29. } from 'containers/Viz/types'
  30. import { IScheduleVizConfigItem } from './types'
  31. import IconFont from 'app/components/IconFont'
  32. export const splitNodeKeyWithPrefix = (
  33. nodeKey: string
  34. ): [NodeKeyPrefix, number] => {
  35. const nodePrefix = [
  36. NodeKeyPrefix.Portal,
  37. NodeKeyPrefix.Dashboard,
  38. NodeKeyPrefix.Display,
  39. NodeKeyPrefix.Slide
  40. ].find((prefix) => nodeKey.includes(prefix))
  41. const nodeId = +nodeKey.slice(nodePrefix.length)
  42. return [nodePrefix, nodeId]
  43. }
  44. const getDashboardChildNodes = (
  45. portalId: number,
  46. mapDashboards: { [parentId: number]: IDashboard[] },
  47. ancestorIds: number[] = [0]
  48. ): [JSX.Element[], number[]] => {
  49. const currentDashboardId = ancestorIds[ancestorIds.length - 1]
  50. const childDashboards = mapDashboards[currentDashboardId]
  51. if (!Array.isArray(childDashboards)) {
  52. return [null, [currentDashboardId]]
  53. }
  54. let descendantIds = []
  55. const descendantNodes = childDashboards.map((child) => {
  56. const [childNodes, childIds] = getDashboardChildNodes(
  57. portalId,
  58. mapDashboards,
  59. ancestorIds.concat(child.id)
  60. )
  61. descendantIds = descendantIds.concat(child.id).concat(childIds)
  62. return (
  63. <TreeNode
  64. title={child.name}
  65. icon={
  66. <Icon type={mapDashboards[child.id] ? 'folder-open' : 'dot-chart'} />
  67. }
  68. key={`${NodeKeyPrefix.Dashboard}${child.id}`}
  69. isLeaf={!childNodes}
  70. dataRef={[portalId, ancestorIds, childIds]}
  71. >
  72. {childNodes}
  73. </TreeNode>
  74. )
  75. })
  76. return [descendantNodes, descendantIds]
  77. }
  78. export const renderPortalDashboardsTreeNodes = (
  79. portalId: number,
  80. dashboards: IDashboard[]
  81. ) => {
  82. if (!Array.isArray(dashboards)) {
  83. return null
  84. }
  85. const mapDashboards = dashboards.reduce<{ [parentId: number]: IDashboard[] }>(
  86. (map, dashboard) => {
  87. if (!map[dashboard.parentId]) {
  88. map[dashboard.parentId] = []
  89. }
  90. map[dashboard.parentId].push(dashboard)
  91. return map
  92. },
  93. {}
  94. )
  95. const [dashboardNodes] = getDashboardChildNodes(portalId, mapDashboards)
  96. return dashboardNodes
  97. }
  98. const slideIcon = <IconFont type="icon-dashboard" />
  99. export const renderDisplaySlidesTreeNodes = (
  100. displayId: number,
  101. slides: ISlideFormed[]
  102. ) => {
  103. if (!Array.isArray(slides)) {
  104. return null
  105. }
  106. const childNodes = slides.map(({ id: slideId, config }, idx) => {
  107. const {
  108. slideParams: { avatar }
  109. } = config
  110. const icon = avatar ? (
  111. <Popover content={<img src={avatar} width={250} />}>{slideIcon}</Popover>
  112. ) : (
  113. slideIcon
  114. )
  115. return (
  116. <TreeNode
  117. title={idx + 1}
  118. icon={icon}
  119. key={`${NodeKeyPrefix.Slide}${slideId}`}
  120. isLeaf={true}
  121. dataRef={[displayId]}
  122. />
  123. )
  124. })
  125. return childNodes
  126. }
  127. export const getCheckedVizKeys = (
  128. value: IScheduleVizConfigItem[],
  129. type: IScheduleVizConfigItem['contentType'],
  130. vizs: IPortal[] | IDisplayFormed[],
  131. vizPrefix: string,
  132. vizItemPrefix: string
  133. ) => {
  134. if (!Array.isArray(vizs)) {
  135. return []
  136. }
  137. const checkedVizKeys = value
  138. .filter(({ contentType }) => contentType === type)
  139. .reduce<string[]>((acc, { id, items }) => {
  140. // checked all viz items under this viz
  141. if (!Array.isArray(items)) {
  142. if (~vizs.findIndex((viz) => viz.id === id)) {
  143. acc.push(`${vizPrefix}${id}`)
  144. }
  145. } else {
  146. // check the partial viz item under this viz
  147. items.forEach((vizItemId) => {
  148. acc.push(`${vizItemPrefix}${vizItemId}`)
  149. })
  150. }
  151. return acc
  152. }, [])
  153. return checkedVizKeys
  154. }