@@ -1258,6 +1258,7 @@ var TidyTree = (function () {
12581258 layout : "vertical" ,
12591259 type : "tree" ,
12601260 mode : "smooth" ,
1261+ colorOptions : { colorMode : "none" } ,
12611262 leafNodes : true ,
12621263 leafLabels : false ,
12631264 equidistantLeaves : false ,
@@ -1352,6 +1353,12 @@ var TidyTree = (function () {
13521353 */
13531354 TidyTree . validModes = [ "smooth" , "square" , "straight" ] ;
13541355
1356+ /**
1357+ * The available color modes for rendering nodes.
1358+ * @type {Array }
1359+ */
1360+ TidyTree . validColorModes = [ "none" , "list" ] ; // later, highlight on hover, or maybe color by annotation on a node/ search
1361+
13551362 /**
13561363 * Draws a Phylogenetic on the element referred to by selector
13571364 * @param {String } selector A CSS selector
@@ -1606,6 +1613,30 @@ var TidyTree = (function () {
16061613
16071614 nodeTransformers . dendrogram = nodeTransformers . tree ;
16081615
1616+ /**
1617+ * Finds the color of a given node based on the color options provided.
1618+ *
1619+ * @param {Object } node - The node for which to find the color.
1620+ * @param {Object } colorOptions - The color options object containing the color mode, node list, default color, and highlight color.
1621+ * @return {string } The color of the node.
1622+ */
1623+ function findNodeColor ( node , colorOptions ) {
1624+ if ( colorOptions . colorMode === "none" ) {
1625+ // steelblue
1626+ return colorOptions . defaultColor ?? "#4682B4" ;
1627+ }
1628+
1629+ let nodeList = colorOptions . nodeList ;
1630+
1631+ if ( nodeList . includes ( node . data . _guid ) ) {
1632+ // charcoal
1633+ return colorOptions . highlightColor ?? "#feb640" ;
1634+ } else {
1635+ // yellowish
1636+ return colorOptions . defaultColor ?? "#243127" ;
1637+ }
1638+ }
1639+
16091640 const radToDeg = 180 / Math . PI ;
16101641
16111642 let labelTransformers = {
@@ -1833,6 +1864,7 @@ var TidyTree = (function () {
18331864 ( d . children && this . branchNodes ) ||
18341865 ( ! d . children && this . leafNodes ) ? 1 : 0
18351866 )
1867+ . style ( "fill" , d => findNodeColor ( d , this . colorOptions ) )
18361868 . on ( "mouseenter focusin" , d => this . trigger ( "showtooltip" , d ) )
18371869 . on ( "mouseout focusout" , d => this . trigger ( "hidetooltip" , d ) )
18381870 . on ( "contextmenu" , d => this . trigger ( "contextmenu" , d ) )
@@ -1884,6 +1916,9 @@ var TidyTree = (function () {
18841916 . duration ( this . animation )
18851917 . attr ( "transform" , nodeTransformer ) ;
18861918
1919+ let nodeGlyphs = update . select ( "circle" ) ;
1920+ nodeGlyphs . style ( "fill" , d => findNodeColor ( d , this . colorOptions ) ) ;
1921+
18871922 let nodeLabels = update . select ( "text" ) ;
18881923 if ( this . layout === "vertical" ) {
18891924 nodeLabels
@@ -2013,6 +2048,28 @@ var TidyTree = (function () {
20132048 return this ;
20142049 } ;
20152050
2051+ /**
2052+ * Set the TidyTree's colorOptions
2053+ * @param {Object } newColorOptions The new colorOptions
2054+ * @return {TidyTree } The TidyTree Object
2055+ */
2056+ TidyTree . prototype . setColorOptions = function ( newColorOptions ) {
2057+ if ( ! TidyTree . validColorModes . includes ( newColorOptions . colorMode ) ) {
2058+ throw Error ( `
2059+ Cannot set TidyTree to colorOptions: ${ newColorOptions . colorMode } \n
2060+ Valid colorModes are: ${ TidyTree . validColorModes . join ( ', ' ) }
2061+ ` ) ;
2062+ }
2063+ if ( newColorOptions . colorMode === 'list' ) {
2064+ if ( ! Array . isArray ( newColorOptions . nodeList ) ) {
2065+ throw Error ( 'nodeList must be an array for colorMode "list"' ) ;
2066+ }
2067+ }
2068+ this . colorOptions = newColorOptions ;
2069+ if ( this . parent ) return this . redraw ( ) ;
2070+ return this ;
2071+ } ;
2072+
20162073 /**
20172074 * Set the TidyTree's mode
20182075 * @param {String } newMode The new mode
@@ -2339,6 +2396,34 @@ var TidyTree = (function () {
23392396 return this ;
23402397 } ;
23412398
2399+ /**
2400+ * Retrieves the GUIDs of the nodes in the TidyTree instance.
2401+ *
2402+ * @param {boolean } leavesOnly - Whether to retrieve GUIDs only for leaf nodes.
2403+ * @return {Array } An array of GUIDs of the nodes.
2404+ */
2405+ TidyTree . prototype . getNodeGUIDs = function ( leavesOnly ) {
2406+ // todo: make sure these are returned in order
2407+ let nodeList = this . parent
2408+ . select ( "svg" )
2409+ . selectAll ( "g.tidytree-node-leaf circle" )
2410+ . _groups [ 0 ] ;
2411+
2412+ if ( ! leavesOnly ) {
2413+ nodeList = this . parent
2414+ . select ( "svg" )
2415+ . selectAll ( "g.tidytree-node-leaf circle, g.tidytree-node-internal circle" )
2416+ . _groups [ 0 ] ;
2417+ }
2418+
2419+ let nodeGUIDs = [ ] ;
2420+ for ( const node of nodeList . values ( ) ) {
2421+ nodeGUIDs . push ( node . __data__ . data . _guid ) ;
2422+ }
2423+
2424+ return nodeGUIDs ;
2425+ } ;
2426+
23422427 /**
23432428 * Searches the tree, returns Search Results
23442429 * @param {Function } test A function which takes a Branch and returns a Truthy
0 commit comments