import warning from 'warning' ; import { CurvedLink } from '../objects' ; import { LinkObject } from '../objects/Link' ; import { NodeObject } from '../objects/Node' ; import { PortObject } from '../objects/Port' ; import Handler from './Handler' ; export interface LinkOption { type { string } type : string ; type { string } fromNodeId ? : string ; type { string } fromPortId ? : string ; type { string } toNodeId ? : string ; type { string } toPortId ? : string ; } author salgum1114
 *  class class LinkHandler { private port : PortObject = null ; handler : Handler ; constructor ( handler ) { this . handler = handler ; } param { PortObject } port init = ( port ) => { if ( this . isDrawing ( ) ) { return ; } if ( this . isConnected ( port ) ) { return ; } this . port = port ; this . port . set ( { fill : port . selectFill , } ) ; this . handler . interactionHandler . linking ( ) ; const { left , top , nodeId , id } = port ; const fromPort = { left , top , id } ; const toPort = { left , top } ; const fromNode = this . handler . objectMap [ nodeId ] ; this . handler . activeLine = new CurvedLink ( fromNode , fromPort , null , toPort , { strokeWidth : 4 , fill : '#999999' , stroke : '#999999' , class : 'line' , originX : 'center' , originY : 'center' , selectable : false , hasBorders : false , hasControls : false , evented : false , } ) ; this . handler . canvas . add ( this . handler . activeLine ) ; } ; /**
     * End drawing link.
     */ finish = ( link ? ) => { if ( ! link ) { this . port . set ( { fill : this . port . originFill , } ) ; } this . handler . interactionHandler . selection ( ) ; this . handler . canvas . remove ( this . handler . activeLine ) ; this . handler . activeLine = null ; this . handler . canvas . renderAll ( ) ; } ; param { PortObject } port generate = ( port ) => { if ( ! port ) { warning ( ! port , 'Does not exist target port.' ) ; return ; } if ( this . isDuplicate ( port ) ) { return ; } if ( this . isSameNode ( port ) ) { return ; } const link = { type : 'curvedLink' , fromNodeId : this . handler . activeLine . fromNode . id , fromPortId : this . handler . activeLine . fromPort . id , toNodeId : port . nodeId , toPortId : port . id , } ; const createdLink = this . create ( link ) ; this . finish ( createdLink ) ; if ( ! this . handler . transactionHandler . active ) { this . handler . transactionHandler . save ( 'add' ) ; } } ; param { LinkOption } link param { boolean } loaded returns create = ( link , loaded = false ) => { const fromNode = this . handler . objectMap [ link . fromNodeId ] as NodeObject ; const fromPort = fromNode . fromPort . filter ( port => port . id === link . fromPortId || ! port . id ) [ 0 ] ; const toNode = this . handler . objectMap [ link . toNodeId ] as NodeObject ; const { toPort } = toNode ; const createdObj = this . handler . fabricObjects [ link . type ] . create ( fromNode , fromPort , toNode , toPort , { ... link , } ) as LinkObject ; this . handler . canvas . add ( createdObj ) ; this . handler . objects = this . handler . getObjects ( ) ; const { editable } = this . handler ; if ( this . handler . onAdd && editable && ! loaded ) { this . handler . onAdd ( createdObj ) ; } this . handler . canvas . renderAll ( ) ; createdObj . setPort ( fromNode , fromPort , toNode , toPort ) ; this . handler . portHandler . setCoords ( fromNode ) ; this . handler . portHandler . setCoords ( toNode ) ; this . handler . canvas . requestRenderAll ( ) ; return createdObj ; } ; param { number } x1 param { number } y1 param { number } x2 param { number } y2 param { LinkObject } link setCoords = ( x1 , y1 , x2 , y2 , link ) => { link . set ( { x1 , y1 , x2 , y2 , } ) ; link . setCoords ( ) ; } ; param { LinkObject } link removeFrom = ( link ) => { if ( link . fromPort ) { let index = - 1 ; if ( link . fromPort . links . length ) { link . fromPort . links . some ( ( portLink , i ) => { if ( link . id === portLink . id ) { index = i ; return true ; } return false ; } ) ; if ( index > - 1 ) { link . fromPort . links . splice ( index , 1 ) ; } } link . setPortEnabled ( link . fromNode , link . fromPort , true ) ; } } ; param { LinkObject } link removeTo = ( link ) => { if ( link . toNode . toPort . links . length ) { let index = - 1 ; link . toNode . toPort . links . some ( ( portLink , i ) => { if ( link . id === portLink . id ) { index = i ; return true ; } return false ; } ) ; if ( index > - 1 ) { link . toNode . toPort . links . splice ( index , 1 ) ; } link . setPortEnabled ( link . toNode , link . toNode . toPort , true ) ; } } ; param { LinkObject } link removeAll = ( link ) => { this . removeFrom ( link ) ; this . removeTo ( link ) ; } ; param { LinkObject } link param { string } type remove = ( link , type ? ) => { if ( type === 'from' ) { this . removeFrom ( link ) ; } else if ( type === 'to' ) { this . removeTo ( link ) ; } else { this . removeAll ( link ) ; } this . handler . canvas . remove ( link ) ; this . handler . objects = this . handler . getObjects ( ) ; } ; param { PortObject } port returns isConnected = ( port ) => { warning ( port . enabled , 'A connected node already exists.' ) ; return ! port . enabled ; } ; param { PortObject } port returns isSameNode = ( port ) => { const validate = port . nodeId === this . handler . activeLine . fromNode . id ; warning ( ! validate , 'Cannot select the same node.' ) ; return validate ; } ; param { PortObject } port returns isDuplicate = ( port ) => { const validate = port . links . some ( link => link . fromNode . id === this . handler . activeLine . fromNode . id ) ; warning ( ! validate , 'Duplicate connections cannot be made.' ) ; return validate ; } ; returns isDrawing = ( ) => { const validate = this . handler . interactionMode === 'link' && this . handler . activeLine ; warning ( ! validate , 'Already drawing links.' ) ; return validate ; } ; } export default LinkHandler ;