import { hasWindow } from '@devexpress/dx-core' ; import { getEventCoords , getOffset } from './common' ; import { TrackerTarget , HandlerFnList , SeriesList , HitTesters , EventHandlerFn , TargetData , EventHandlers , HandlersObject , } from '../types' ; const DISTANCE_THRESHOLD = 20 ; const compareHitTargets = ( t1 , t2 ) => { const distanceDelta = t1 . distance - t2 . distance ; if ( Math . abs ( distanceDelta ) <= DISTANCE_THRESHOLD ) { const orderDelta = t2 . order - t1 . order ; return orderDelta !== 0 ? orderDelta : distanceDelta ; } return distanceDelta ; } ; const buildEventHandler = ( seriesList , handlers ) : EventHandlerFn => { let hitTesters = null ; const createHitTesters = ( ) => { const obj = { } ; seriesList . forEach ( ( seriesItem ) => { obj [ seriesItem . symbolName as unknown as string ] = seriesItem . createHitTester ( seriesItem . points , seriesItem . rotated ) ; } ) ; return obj ; } ; return ( e ) => { const location = getEventCoords ( e , getOffset ( e . currentTarget ) ) ; hitTesters = hitTesters || createHitTesters ( ) ; const targets = [ ] ; seriesList . forEach ( ( { name : series , index : order , symbolName } ) => { const status = hitTesters ! [ symbolName as unknown as string ] ( location ) ; if ( status ) { targets . push ( ... status . points . map ( point => ( { series , order , point : point . index , distance : point . distance , } ) ) ) ; } } ) ; targets . sort ( compareHitTargets ) ; const arg = { location , targets , event : e . nativeEvent } ; handlers . forEach ( handler => handler ( arg ) ) ; } ; } ; const buildLeaveEventHandler = ( handlers ) : EventHandlerFn => ( e ) => { const location = getEventCoords ( e , getOffset ( e . currentTarget ) ) ; const arg = { location , targets : [ ] } ; handlers . forEach ( handler => handler ( arg ) ) ; } ; internal export const buildEventHandlers = ( seriesList , { clickHandlers , pointerMoveHandlers } ) => { const handlers = { } ; if ( ! hasWindow ( ) ) return handlers ; if ( clickHandlers . length ) { handlers . click = buildEventHandler ( seriesList , clickHandlers ) ; } if ( pointerMoveHandlers . length ) { const moveHandler = buildEventHandler ( seriesList , pointerMoveHandlers ) ; const leaveHandler = buildLeaveEventHandler ( pointerMoveHandlers ) ; if ( 'ontouchstart' in window ) { handlers . touchstart = moveHandler ; } else { handlers . mousemove = moveHandler ; handlers . mouseleave = leaveHandler ; } } return handlers ; } ;