import { Selector } from 'reselect' ; import { ChartType } from '../../..' ; import { AnnotationType , LineAnnotationDatum , ProjectedValues , RectAnnotationDatum , SettingsSpec , } from '../../../../specs' ; import { GlobalChartState , PointerState } from '../../../../state/chart_state' ; import { createCustomCachedSelector } from '../../../../state/create_selector' ; import { getLastClickSelector } from '../../../../state/selectors/get_last_click' ; import { getSettingsSpecSelector } from '../../../../state/selectors/get_settings_specs' ; import { isClicking } from '../../../../state/utils' ; import { IndexedGeometry , GeometryValue } from '../../../../utils/geometry' ; import { AnnotationTooltipState } from '../../annotations/types' ; import { XYChartSeriesIdentifier } from '../../utils/series' ; import { getMultipleRectangleAnnotations } from './get_multiple_rectangle_annotations' ; import { getProjectedScaledValues } from './get_projected_scaled_values' ; import { getHighlightedGeomsSelector } from './get_tooltip_values_highlighted_geoms' ; internal export function createOnClickCaller ( ) { let prevClick = null ; let selector = null ; return ( state ) => { if ( selector ) { return selector ( state ) ; } if ( state . chartType !== ChartType . XYAxis ) { return ; } selector = createCustomCachedSelector ( [ getLastClickSelector , getSettingsSpecSelector , getHighlightedGeomsSelector , getProjectedScaledValues , getMultipleRectangleAnnotations , ] , ( lastClick , { onElementClick , onProjectionClick , onAnnotationClick } , indexedGeometries , values , tooltipStates ) : void => { if ( ! isClicking ( prevClick , lastClick ) ) { return ; } const elementClickFired = tryFiringOnElementClick ( indexedGeometries , onElementClick ) ; if ( ! elementClickFired && onAnnotationClick && tooltipStates ) { tryFiringOnAnnotationClick ( tooltipStates , onAnnotationClick , indexedGeometries ) ; } else if ( ! elementClickFired ) { tryFiringOnProjectionClick ( values , onProjectionClick ) ; } prevClick = lastClick ; } ) ; } ; } function tryFiringOnElementClick ( indexedGeometries , onElementClick ) { if ( indexedGeometries . length === 0 || ! onElementClick ) { return false ; } const elements = indexedGeometries . map < [ GeometryValue , XYChartSeriesIdentifier ] > ( ( { value , seriesIdentifier } ) => [ value , seriesIdentifier , ] ) ; onElementClick ( elements ) ; return true ; } function tryFiringOnProjectionClick ( values , onProjectionClick ) { const properClick = values !== undefined && onProjectionClick ; if ( properClick ) onProjectionClick ( values ) ; return Boolean ( properClick ) ; } function tryFiringOnAnnotationClick ( annotationState , onAnnotationClick , indexedGeometries ) { if ( indexedGeometries . length > 0 ) return false ; if ( annotationState . length > 0 && onAnnotationClick ) { const rects = [ ] ; const lines = [ ] ; annotationState . forEach ( ( annotation ) => { if ( annotation . annotationType === AnnotationType . Rectangle ) { rects . push ( { id : annotation . id , datum : annotation . datum as RectAnnotationDatum , } ) ; } else if ( annotation . annotationType === AnnotationType . Line ) { lines . push ( { id : annotation . id , datum : annotation . datum as LineAnnotationDatum , } ) ; } } ) ; onAnnotationClick ( { rects , lines } ) ; return true ; } return false ; }