import Map from "../map" ; import Node , { Coordinates , ExportNodeProperties } from "../models/node" ; import Log from "../../utils/log" ; import Utils from "../../utils/utils" ; /**
 * Manage the drag events of the nodes.
 */ export default class CopyPaste { private map : Map ; private copiedNodes : ExportNodeProperties [ ] ; param { Map } map constructor ( map ) { this . map = map ; } param { string } id public copy = ( id ) => { if ( id && typeof id !== "string" ) { Log . error ( "The node id must be a string" , "type" ) ; } const node = id ? this . map . nodes . getNode ( id ) : this . map . nodes . getSelectedNode ( ) ; if ( node === undefined ) { Log . error ( "There are no nodes with id \"" + id + "\"" ) ; } if ( ! node . isRoot ( ) ) { this . copiedNodes = [ this . map . nodes . getNodeProperties ( node , false ) ] ; this . map . nodes . getDescendants ( node ) . forEach ( ( node ) => { this . copiedNodes . push ( this . map . nodes . getNodeProperties ( node , false ) ) ; } ) ; } else { Log . error ( "The root node can not be copied" ) ; } } ; param { string } id public cut = ( id ) => { if ( id && typeof id !== "string" ) { Log . error ( "The node id must be a string" , "type" ) ; } const node = id ? this . map . nodes . getNode ( id ) : this . map . nodes . getSelectedNode ( ) ; if ( node === undefined ) { Log . error ( "There are no nodes with id \"" + id + "\"" ) ; } if ( ! node . isRoot ( ) ) { this . copiedNodes = [ this . map . nodes . getNodeProperties ( node , false ) ] ; this . map . nodes . getDescendants ( node ) . forEach ( ( node ) => { this . copiedNodes . push ( this . map . nodes . getNodeProperties ( node , false ) ) ; } ) ; this . map . nodes . removeNode ( node . id ) ; } else { Log . error ( "The root node can not be cut" ) ; } } ; param { string } id public paste = ( id ) => { if ( this . copiedNodes === undefined ) { Log . error ( "There are not nodes in the mmp clipboard" ) ; } if ( id && typeof id !== "string" ) { Log . error ( "The node id must be a string" , "type" ) ; } const node = id ? this . map . nodes . getNode ( id ) : this . map . nodes . getSelectedNode ( ) ; if ( node === undefined ) { Log . error ( "There are no nodes with id \"" + id + "\"" ) ; } const rootNode = this . map . nodes . getRoot ( ) ; const addNodes = ( nodeProperties , newParentNode ) => { let coordinates ; if ( nodeProperties . id !== this . copiedNodes [ 0 ] . id ) { coordinates = { x : 0 , y : 0 } ; const oldParentNode = ( < any > this . copiedNodes ) . find ( ( np ) => { return np . id === nodeProperties . parent ; } ) ; let dx = oldParentNode . coordinates . x - nodeProperties . coordinates . x ; let dy = oldParentNode . coordinates . y - nodeProperties . coordinates . y ; const newParentOrientation = this . map . nodes . getOrientation ( newParentNode ) ; const oldParentOrientation = oldParentNode . coordinates . x < rootNode . coordinates . x ; if ( oldParentOrientation !== newParentOrientation ) { dx = - dx ; } coordinates . x = newParentNode . coordinates . x - dx ; coordinates . y = newParentNode . coordinates . y - dy ; coordinates = this . map . nodes . fixCoordinates ( coordinates , true ) ; } const nodePropertiesCopy = Utils . cloneObject ( nodeProperties ) ; this . map . nodes . addNode ( { name : nodePropertiesCopy . name , coordinates : coordinates , image : nodePropertiesCopy . image , colors : nodePropertiesCopy . colors , font : nodePropertiesCopy . font , locked : nodePropertiesCopy . locked } , newParentNode . id ) ; const children = this . copiedNodes . filter ( ( np ) => { return np . parent === nodeProperties . id ; } ) ; if ( children . length > 0 ) { const nodes = this . map . nodes . getNodes ( ) ; newParentNode = nodes [ nodes . length - 1 ] ; children . forEach ( ( np ) => { addNodes ( np , newParentNode ) ; } ) ; } } ; addNodes ( this . copiedNodes [ 0 ] , node ) ; } ; }