import { cssClass } from '../styles' ; internal export interface IDragHandleOptions { default container : HTMLElement | SVGElement ; /**
     * filter to certain mouse events, e.g. shift only
     */ filter ( evt ) : boolean ; onStart ( handle , x , delta , evt ) : void ; onDrag ( handle , x , delta , evt ) : void ; onEnd ( handle , x , delta , evt ) : void ; default minDelta : number ; } internal export function dragHandle ( handle , options = { } ) { const o = Object . assign ( { container : handle . parentElement ! , filter : ( ) => true , onStart : ( ) => undefined , onDrag : ( ) => undefined , onEnd : ( ) => undefined , minDelta : 2 , } , options ) ; let ueberElement = null ; const toContainerRelative = ( x , elem ) => { const rect = elem . getBoundingClientRect ( ) ; return x - rect . left - elem . clientLeft ; } ; let start = 0 ; let last = 0 ; let handleShift = 0 ; const mouseMove = ( evt ) => { if ( ! o . filter ( evt ) ) { return ; } evt . stopPropagation ( ) ; evt . preventDefault ( ) ; const end = toContainerRelative ( evt . clientX , o . container ) - handleShift ; if ( Math . abs ( last - end ) < o . minDelta ) { return ; } last = end ; o . onDrag ( handle , end , last - end , evt ) ; } ; const mouseUp = ( evt ) => { if ( ! o . filter ( evt ) ) { return ; } evt . stopPropagation ( ) ; evt . preventDefault ( ) ; const end = toContainerRelative ( evt . clientX , o . container ) - handleShift ; ueberElement ! . removeEventListener ( 'mousemove' , mouseMove ) ; ueberElement ! . removeEventListener ( 'mouseup' , mouseUp ) ; ueberElement ! . removeEventListener ( 'mouseleave' , mouseUp ) ; ueberElement ! . classList . remove ( cssClass ( 'dragging' ) ) ; if ( Math . abs ( start - end ) < 2 ) { return ; } o . onEnd ( handle , end , start - end , evt ) ; } ; handle . onmousedown = ( evt ) => { if ( ! o . filter ( evt ) ) { return ; } evt . stopPropagation ( ) ; evt . preventDefault ( ) ; handleShift = toContainerRelative ( evt . clientX , handle ) ; start = last = toContainerRelative ( evt . clientX , o . container ) - handleShift ; ueberElement = handle . closest ( 'body' ) || handle . closest < HTMLElement > ( `.${ cssClass ( ) }` ) ! ; ueberElement . addEventListener ( 'mousemove' , mouseMove ) ; ueberElement . addEventListener ( 'mouseup' , mouseUp ) ; ueberElement . addEventListener ( 'mouseleave' , mouseUp ) ; ueberElement . classList . add ( cssClass ( 'dragging' ) ) ; o . onStart ( handle , start , 0 , evt ) ; } ; }