SlideLayerList.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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, { useContext, useMemo, useCallback } from 'react'
  21. import { useSelector, useDispatch } from 'react-redux'
  22. import { useHistory } from 'react-router-dom'
  23. import pick from 'lodash/pick'
  24. import {
  25. makeSelectCurrentLayerIds,
  26. makeSelectCurrentDisplayWidgets
  27. } from '../selectors'
  28. import {
  29. LayerListContext,
  30. LayerListContextValue,
  31. LayerItem
  32. } from '../components/Layer'
  33. import ViewActions from '../../View/actions'
  34. import DisplayActions from '../actions'
  35. import { RenderType } from '../../Widget/components/Widget'
  36. import { IQueryConditions } from '../../Dashboard/types'
  37. import { getRequestParamsByWidgetConfig } from 'containers/Viz/util'
  38. import { makeSelectFormedViews } from '../../View/selectors'
  39. import {
  40. makeSelectCurrentSlide,
  41. makeSelectCurrentDisplay
  42. } from '../../Viz/selectors'
  43. import SlideLayer from './SlideLayer'
  44. import { ContainerContext } from '../components/Container'
  45. import { makeSelectCurrentProject } from '../../Projects/selectors'
  46. import { DEFAULT_SPLITER } from 'app/globalConstants'
  47. import { IWidgetFormed } from '../../Widget/types'
  48. import { DeltaPosition } from '../components/types'
  49. import { DragTriggerTypes } from '../constants'
  50. import { ILayerOperationInfo } from 'app/containers/Display/components/types'
  51. const SlideLayerList: React.FC = (props) => {
  52. const dispatch = useDispatch()
  53. const history = useHistory()
  54. const { id: projectId } = useSelector(makeSelectCurrentProject())
  55. const { id: displayId } = useSelector(makeSelectCurrentDisplay())
  56. const {
  57. config: { slideParams }
  58. } = useSelector(makeSelectCurrentSlide())
  59. const currentLayerIds = useSelector(makeSelectCurrentLayerIds())
  60. const currentDisplayWidgets = useSelector(makeSelectCurrentDisplayWidgets())
  61. const formedViews = useSelector(makeSelectFormedViews())
  62. const { scale } = useContext(ContainerContext)
  63. const editWidget = useCallback(
  64. (widgetId: number) => {
  65. const editSign = [projectId, displayId].join(DEFAULT_SPLITER)
  66. sessionStorage.setItem('editWidgetFromDisplay', editSign)
  67. history.push(`/project/${projectId}/widget/${widgetId}`)
  68. },
  69. [projectId, displayId]
  70. )
  71. const onDrag = useCallback(
  72. (
  73. layerId,
  74. deltaPosition: DeltaPosition,
  75. eventTrigger: DragTriggerTypes,
  76. finish = false
  77. ) => {
  78. if (deltaPosition.deltaX === null && deltaPosition.deltaY === null) {
  79. return
  80. }
  81. dispatch(
  82. DisplayActions.dragLayer(
  83. pick(slideParams, 'width', 'height'),
  84. scale[0],
  85. deltaPosition,
  86. eventTrigger,
  87. finish,
  88. layerId
  89. )
  90. )
  91. },
  92. [slideParams, scale]
  93. )
  94. const onResize = useCallback(
  95. (layerId, deltaSize, finish = false) => {
  96. dispatch(
  97. DisplayActions.resizeLayer(
  98. pick(slideParams, 'width', 'height'),
  99. scale[0],
  100. layerId,
  101. deltaSize,
  102. finish
  103. )
  104. )
  105. },
  106. [slideParams, scale]
  107. )
  108. const onSelectionChange = useCallback((layerId, selected, exclusive) => {
  109. dispatch(DisplayActions.selectLayer(layerId, selected, exclusive))
  110. }, [])
  111. const onEditLabelChange = useCallback((layerId: number, changedInfo: Partial<ILayerOperationInfo>) => {
  112. dispatch(DisplayActions.changeLayerOperationInfo(layerId, changedInfo))
  113. }, [])
  114. const getWidgetViewModel = useCallback(
  115. (viewId: number) => {
  116. const viewModel = formedViews[viewId].model
  117. return viewModel
  118. },
  119. [formedViews]
  120. )
  121. const getChartData = useCallback(
  122. (
  123. renderType: RenderType,
  124. slideId: number,
  125. layerId: number,
  126. widget: IWidgetFormed,
  127. prevQueryConditions: Partial<IQueryConditions>,
  128. queryConditions?: Partial<IQueryConditions>
  129. ) => {
  130. const requestParams = getRequestParamsByWidgetConfig(
  131. renderType,
  132. widget.config,
  133. prevQueryConditions,
  134. queryConditions
  135. )
  136. dispatch(
  137. ViewActions.loadViewDataFromVizItem(
  138. renderType,
  139. [slideId, layerId],
  140. widget.viewId,
  141. requestParams,
  142. 'display',
  143. null
  144. )
  145. )
  146. },
  147. []
  148. )
  149. const layerListContextValue = useMemo<LayerListContextValue>(
  150. () => ({
  151. currentDisplayWidgets,
  152. editWidget,
  153. onDrag,
  154. onSelectionChange,
  155. onEditLabelChange,
  156. onResize,
  157. getWidgetViewModel,
  158. getChartData
  159. }),
  160. [
  161. currentDisplayWidgets,
  162. editWidget,
  163. onDrag,
  164. onSelectionChange,
  165. onEditLabelChange,
  166. onResize,
  167. getWidgetViewModel,
  168. getChartData
  169. ]
  170. )
  171. return (
  172. <LayerListContext.Provider value={layerListContextValue}>
  173. {currentLayerIds.map((id) => (
  174. <SlideLayer key={id} id={id}>
  175. <LayerItem />
  176. </SlideLayer>
  177. ))}
  178. </LayerListContext.Provider>
  179. )
  180. }
  181. export default React.memo(SlideLayerList)