123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- /*
- * <<
- * Davinci
- * ==
- * Copyright (C) 2016 - 2017 EDP
- * ==
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * >>
- */
- import React, { useMemo, useCallback } from 'react'
- import { Row, Col, Icon } from 'antd'
- import {
- IPortal,
- IDashboard,
- IDisplayFormed,
- ISlideFormed
- } from 'containers/Viz/types'
- import { IScheduleVizConfigItem } from './types'
- import { NodeKeyPrefix } from './constants'
- import {
- getCheckedVizKeys,
- renderPortalDashboardsTreeNodes,
- renderDisplaySlidesTreeNodes
- } from './util'
- import VizSelectTree from './VizSelectTree'
- interface IScheduleVizConfigProps {
- portals: IPortal[]
- displays: IDisplayFormed[]
- value: IScheduleVizConfigItem[]
- portalDashboards: { [portalId: number]: IDashboard[] }
- displaySlides: { [displayId: number]: ISlideFormed[] }
- onLoadPortalDashboards: (portalId: number) => void
- onLoadDisplaySlides: (displayId: number) => void
- onChange: (value: IScheduleVizConfigItem[]) => void
- }
- const ScheduleVizConfig: React.FC<IScheduleVizConfigProps> = (props) => {
- const {
- portals,
- displays,
- value,
- portalDashboards,
- displaySlides,
- onLoadPortalDashboards,
- onLoadDisplaySlides,
- onChange
- } = props
- const checkedPortalKeys = useMemo(
- () =>
- getCheckedVizKeys(
- value,
- 'portal',
- portals,
- NodeKeyPrefix.Portal,
- NodeKeyPrefix.Dashboard
- ),
- [value, portals]
- )
- const checkedDisplayKeys = useMemo(
- () =>
- getCheckedVizKeys(
- value,
- 'display',
- displays,
- NodeKeyPrefix.Display,
- NodeKeyPrefix.Slide
- ),
- [value, displays]
- )
- const loadVizItems = useCallback(
- (vizId: number, prefix: NodeKeyPrefix) => {
- switch (prefix) {
- case NodeKeyPrefix.Portal:
- if (!portalDashboards[vizId]) {
- onLoadPortalDashboards(vizId)
- }
- break
- case NodeKeyPrefix.Display:
- if (!displaySlides[vizId]) {
- onLoadDisplaySlides(vizId)
- }
- break
- }
- return new Promise<void>((resolve) => {
- resolve()
- })
- },
- [
- portalDashboards,
- displaySlides,
- onLoadPortalDashboards,
- onLoadDisplaySlides
- ]
- )
- const renderVizItems = useCallback(
- (nodeId: number, prefix: NodeKeyPrefix) => {
- switch (prefix) {
- case NodeKeyPrefix.Portal:
- return renderPortalDashboardsTreeNodes(
- nodeId,
- portalDashboards[nodeId]
- )
- case NodeKeyPrefix.Display:
- return renderDisplaySlidesTreeNodes(nodeId, displaySlides[nodeId])
- }
- },
- [portalDashboards, displaySlides]
- )
- const checkViz = useCallback(
- (
- type: IScheduleVizConfigItem['contentType'],
- vizId: number,
- checked: boolean
- ) => {
- // remove this Viz value first
- const newValue = value.filter(
- ({ id, contentType }) =>
- contentType !== type || (contentType === type && id !== vizId)
- )
- if (checked) {
- newValue.push({
- contentType: type,
- id: vizId,
- items: undefined // undefined stands for dynamic check all dashboards
- })
- }
- switch (type) {
- case 'portal':
- if (!portalDashboards[vizId]) {
- onLoadPortalDashboards(vizId)
- }
- break
- case 'display':
- if (!displaySlides[vizId]) {
- onLoadDisplaySlides(vizId)
- }
- break
- }
- onChange(newValue)
- },
- [value, onLoadPortalDashboards, onLoadDisplaySlides, onChange]
- )
- const checkVizItem = useCallback(
- (
- type: IScheduleVizConfigItem['contentType'],
- vizItemId: number,
- checked: boolean,
- dataRef: [number, number[]?, number[]?]
- ) => {
- const newValue = [...value]
- const [vizId, ancestorIds, descendantIds] = dataRef
- const removeAncestor = (itemId: number) =>
- !Array.isArray(ancestorIds) || !ancestorIds.includes(itemId)
- const removeDescendant = (itemId: number) =>
- !Array.isArray(descendantIds) || !descendantIds.includes(itemId)
- const vizConfigIdx = newValue.findIndex(
- ({ id, contentType }) => id === vizId && contentType === type
- )
- if (~vizConfigIdx) {
- const vizConfig = newValue[vizConfigIdx]
- const vizItems =
- type === 'portal'
- ? portalDashboards[vizId]
- : type === 'display'
- ? displaySlides[vizId]
- : []
- if (checked) {
- vizConfig.items = vizConfig.items
- .filter(removeDescendant)
- .concat(vizItemId)
- } else {
- vizConfig.items = (
- vizConfig.items ||
- (vizItems as Array<IDashboard | ISlideFormed>).map(({ id }) => id)
- ).filter(
- (id) =>
- id !== vizItemId && removeAncestor(id) && removeDescendant(id)
- )
- if (!vizConfig.items.length) {
- newValue.splice(vizConfigIdx, 1)
- }
- }
- } else {
- // new checked true
- newValue.push({
- contentType: type,
- id: vizId,
- items: [vizItemId]
- })
- }
- onChange(newValue)
- },
- [value, portalDashboards, displaySlides, onChange]
- )
- const checkVizTree = useCallback(
- (nodeId: number, prefix: NodeKeyPrefix, checked: boolean, dataRef: any) => {
- switch (prefix) {
- case NodeKeyPrefix.Portal:
- checkViz('portal', nodeId, checked)
- break
- case NodeKeyPrefix.Display:
- checkViz('display', nodeId, checked)
- break
- case NodeKeyPrefix.Dashboard:
- checkVizItem('portal', nodeId, checked, dataRef)
- break
- case NodeKeyPrefix.Slide:
- checkVizItem('display', nodeId, checked, dataRef)
- break
- }
- },
- [checkViz, checkVizItem]
- )
- return (
- <Row gutter={8}>
- <Col span={12}>
- <VizSelectTree
- title="仪表板"
- vizs={portals}
- vizIcon={<Icon type="layout" />}
- vizKeyPrefix={NodeKeyPrefix.Portal}
- checkedKeys={checkedPortalKeys}
- onLoadDetail={loadVizItems}
- onRenderVizItems={renderVizItems}
- onCheck={checkVizTree}
- />
- </Col>
- <Col span={12}>
- <VizSelectTree
- title="大屏"
- vizs={displays}
- vizIcon={<Icon type="layout" />}
- vizKeyPrefix={NodeKeyPrefix.Display}
- checkedKeys={checkedDisplayKeys}
- onLoadDetail={loadVizItems}
- onRenderVizItems={renderVizItems}
- onCheck={checkVizTree}
- />
- </Col>
- </Row>
- )
- }
- export default ScheduleVizConfig
|