import { Annotation , Annotations , AnnotationValue , XAnnotation , YAnnotation } from '../types' ; import { renderXAnnotations , removeXAnnotations } from './XAnnotations/XAnnotations' ; import { renderYAnnotations , removeYAnnotations , renderYAnnotationsEditable } from './YAnnotations/YAnnotations' ; import { DataStream , ViewPort } from '../../../../utils/dataTypes' ; import { DraggableAnnotationsOptions } from './draggableAnnotations' ; export type RenderAnnotationsOptions = { container : SVGElement ; resolution : number ; annotations : Annotations ; viewport : ViewPort ; size : { width : number ; height : number ; } ; onUpdate : ( { start , end } : { start : Date ; end : Date ; } , hasDataChanged : boolean , hasSizeChanged : boolean , hasAnnotationChanged : boolean ) => void ; activeViewPort : ( ) => ViewPort ; emitUpdatedWidgetConfiguration : ( dataStreams ? : DataStream [ ] ) => void ; draggable : ( draggableOptions : DraggableAnnotationsOptions ) => void ; startStopDragging : ( dragState : boolean ) => void ; inDragState : ( ) => boolean ; } ; type AnnotationPredicate = ( annotation : Annotation < AnnotationValue > ) => boolean ; const withinViewport = ( viewport ) : AnnotationPredicate => { return ( { value } ) => { if ( typeof value === 'number' ) { return viewport . yMin <= value && viewport . yMax >= value ; } return viewport . start <= value && viewport . end >= value ; } ; } ; let dragHandler ; export const renderAnnotations = ( { container , resolution , annotations , viewport , size , onUpdate , activeViewPort , emitUpdatedWidgetConfiguration , draggable , startStopDragging , inDragState , } ) => { if ( typeof annotations === 'object' && typeof annotations . show === 'boolean' && ! annotations . show ) { removeXAnnotations ( { container } ) ; removeYAnnotations ( { container } ) ; return ; } const xAnnotations = annotations . x == null ? [ ] : annotations . x . filter ( withinViewport ( viewport ) ) ; const yAnnotations = annotations . y == null ? [ ] : annotations . y . filter ( withinViewport ( viewport ) ) ; /**
     * X Annotations
     */ renderXAnnotations ( { container , xAnnotations , viewport , resolution , size , } ) ; /**
     * Y Annotations
     */ renderYAnnotations ( { container , yAnnotations , viewport , resolution , size , } ) ; if ( ! inDragState ( ) ) { /**
         * Y Annotations Editable (Draggable)
         */ dragHandler = renderYAnnotationsEditable ( { container , yAnnotations , viewport , resolution , size , } ) ; draggable ( { container , size , onUpdate , activeViewPort , emitUpdatedWidgetConfiguration , startStopDragging , resolution , dragHandle : dragHandler , } ) ; } } ;