import checkData from './dataChecker' ; import { GraphinProps , Data , ForceSimulation , ExtendLayout } from '../../types' ; import Graphin from '../../Graphin' ; import defaultLayouts , { LayoutOption } from './defaultLayouts' ; interface LayoutParams { data : Data ; prevProps ? : GraphinProps ; } const layoutInfo = ( layouts ) => { return layouts . map ( ( item ) => { const { desc , name , icon } = item ; return { desc , name , icon , } ; } ) ; } ; const noopLayout = ( ) => { return [ ] ; } ; const layoutController = ( graphin , params ) : { data : Data ; forceSimulation ? : ForceSimulation ; } => { const { props , forceSimulation , graph } = graphin ; const width = graph ! . get ( 'width' ) ; const height = graph ! . get ( 'height' ) ; const { data : PropsData , prevProps = props } = params ; let { layout } = props ; const data = checkData ( PropsData , { edge : { autoPoly : ! ! graphin . g6Options ?. autoPolyEdge , autoLoop : ! ! graphin . g6Options ?. autoLoopEdge , } , } ) ; if ( forceSimulation ) { forceSimulation . stop ( ) ; } const { extend = { } } = props ; const extendLayout = extend . layout || noopLayout ; const layouts = [ ... defaultLayouts ( graphin , prevProps ) , ... extendLayout ( graphin , prevProps ) , ] ; graphin . getLayoutInfo = ( ) => { return layoutInfo ( layouts ) ; } ; if ( data . nodes && data . nodes . length === 0 ) { return { data , } ; } const hasPosition = data . nodes . every ( ( node ) => { return ! window . isNaN ( Number ( node . x ) ) && ! window . isNaN ( Number ( node . y ) ) ; } ) ; if ( ! ( layout && layout . name ) ) { if ( hasPosition ) { return { data } ; } layout = { name : 'concentric' } ; } const { name } = layout ; const options = { graph , width , height , data , ... layout . options , } ; const matchLayout = layouts . find ( ( item ) => item . name === name ) || { name : '' , icon : '' , layout : ( inputData ) => { return { data : inputData , } ; } , } ; const layoutData = matchLayout . layout ( data , options as LayoutOption ) ; return { ... layoutData , data : { ... layoutData . data , combos : data . combos , } , } ; } ; export default layoutController ;