import { Colors , Font , Image , UserNodeProperties } from "./models/node" ; import Utils from "../utils/utils" ; import Map from "./map" ; import * as d3 from "d3" ; import Log from "../utils/log" ; /**
 * Manage default map options.
 */ export default class Options implements OptionParameters { private map : Map ; public fontFamily : string ; public centerOnResize : boolean ; public drag : boolean ; public zoom : boolean ; public defaultNode : DefaultNodeProperties ; public rootNode : DefaultNodeProperties ; param { OptionParameters } parameters param { Map } map constructor ( parameters = { } , map ) { this . map = map ; this . fontFamily = parameters . fontFamily || "Arial, Helvetica, sans-serif" ; this . centerOnResize = parameters . centerOnResize !== undefined ? parameters . centerOnResize : true ; this . drag = parameters . drag !== undefined ? parameters . drag : true ; this . zoom = parameters . zoom !== undefined ? parameters . zoom : true ; this . defaultNode = Utils . mergeObjects ( { name : "Node" , image : { src : "" , size : 60 } , colors : { name : "#787878" , background : "#f9f9f9" , branch : "#577a96" } , font : { size : 16 , style : "normal" , weight : "normal" } , locked : true } , parameters . defaultNode , true ) as DefaultNodeProperties ; this . rootNode = Utils . mergeObjects ( { name : "Root node" , image : { src : "" , size : 70 } , colors : { name : "#787878" , background : "#f0f6f5" , branch : "" } , font : { size : 20 , style : "normal" , weight : "normal" } } , parameters . rootNode , true ) as DefaultNodeProperties ; } public update = ( property , value ) => { if ( typeof property !== "string" ) { Log . error ( "The property must be a string" , "type" ) ; } switch ( property ) { case "fontFamily" : this . updateFontFamily ( value ) ; break ; case "centerOnResize" : this . updateCenterOnResize ( value ) ; break ; case "drag" : this . updateDrag ( value ) ; break ; case "zoom" : this . updateZoom ( value ) ; break ; case "defaultNode" : this . updateDefaultNode ( value ) ; break ; case "rootNode" : this . updateDefaultRootNode ( value ) ; break ; default : Log . error ( "The property does not exist" ) ; } } ; param { string } font private updateFontFamily ( font ) { if ( typeof font !== "string" ) { Log . error ( "The font must be a string" , "type" ) ; } this . fontFamily = font ; this . map . draw . update ( ) ; } param { boolean } flag private updateCenterOnResize ( flag ) { if ( typeof flag !== "boolean" ) { Log . error ( "The value must be a boolean" , "type" ) ; } this . centerOnResize = flag ; if ( this . centerOnResize === true ) { d3 . select ( window ) . on ( "resize." + this . map . id , ( ) => { this . map . zoom . center ( ) ; } ) ; } else { d3 . select ( window ) . on ( "resize." + this . map . id , null ) ; } } param { boolean } flag private updateDrag ( flag ) { if ( typeof flag !== "boolean" ) { Log . error ( "The value must be a boolean" , "type" ) ; } this . drag = flag ; this . map . draw . clear ( ) ; this . map . draw . update ( ) ; } param { boolean } flag private updateZoom ( flag ) { if ( typeof flag !== "boolean" ) { Log . error ( "The value must be a boolean" , "type" ) ; } this . zoom = flag ; if ( this . zoom === true ) { this . map . dom . svg . call ( this . map . zoom . getZoomBehavior ( ) ) ; } else { this . map . dom . svg . on ( ".zoom" , null ) ; } } param { DefaultNodeProperties } properties private updateDefaultNode ( properties ) { this . defaultNode = Utils . mergeObjects ( this . defaultNode , properties , true ) as DefaultNodeProperties ; } param { DefaultNodeProperties } properties private updateDefaultRootNode ( properties ) { this . rootNode = Utils . mergeObjects ( this . rootNode , properties , true ) as DefaultNodeProperties ; } } export interface DefaultNodeProperties { name : string ; image : Image ; colors : Colors ; font : Font ; locked : boolean ; } export interface OptionParameters { fontFamily ? : string ; centerOnResize ? : boolean ; drag ? : boolean ; zoom ? : boolean ; defaultNode ? : DefaultNodeProperties ; rootNode ? : DefaultNodeProperties ; }