Skip to content

Commit 13c541e

Browse files
committed
keep link vs branch language consistent
1 parent f2bfe40 commit 13c541e

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/main.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ TidyTree.validNodeColorModes = ["none", "list"]; // later, highlight on hover, o
116116
* The available color modes for rendering branches.
117117
* @type {Array}
118118
*/
119-
TidyTree.validLinkColorModes = ["none", "monophyletic"]; // later, toRoot?
119+
TidyTree.validBranchColorModes = ["none", "monophyletic"]; // later, toRoot?
120120

121121
/**
122122
* Draws a Phylogenetic on the element referred to by selector
@@ -403,33 +403,41 @@ function findNodeColor(node, colorOptions) {
403403
* @param {object} colorOptions - The options for different link colors.
404404
* @return {string} The color of the link.
405405
*/
406-
function findLinkColor(link, colorOptions) {
407-
if (colorOptions.linkColorMode === "none") {
406+
function findBranchColor(link, colorOptions) {
407+
if (colorOptions.branchColorMode === "none") {
408408
return colorOptions.defaultColor ?? "#ccc";
409409
}
410-
410+
411411
let source = link.source;
412-
let children = getAllLeaves(source);
413-
414-
let allChildrenInNodeList = children.every(child =>
415-
colorOptions.nodeList.includes(child.data._guid)
412+
let childLeaves = getAllLeaves(source);
413+
console.log("childLeaves", childLeaves);
414+
let allChildLeavesInNodeList = childLeaves.every(childLeaf =>
415+
colorOptions.nodeList?.includes(childLeaf.data._guid)
416416
);
417417

418-
if (allChildrenInNodeList) {
418+
if (allChildLeavesInNodeList) {
419419
return colorOptions.highlightColor ?? "#feb640";
420420
}
421421

422422
return colorOptions.defaultColor ?? "#ccc";
423423
}
424424

425-
function getAllLeaves(node) {
425+
/**
426+
* Returns an array of all the child leaf nodes of the given node in a tree.
427+
*
428+
* @param {Object} node - A node of the tree.
429+
* @param {boolean} includeSelf - Whether to include the given node itself as a leaf node. Defaults to false.
430+
* @return {Array} An array of leaf nodes.
431+
*/
432+
function getAllLeaves(node, includeSelf) {
433+
includeSelf = includeSelf ?? false;
426434
let leaves = [];
427435

428-
if (node.children.length === 0) {
436+
if (includeSelf && node.height === 0) {
429437
leaves.push(node);
430438
} else {
431439
node.children.forEach(child => {
432-
leaves.push(...getAllLeaves(child));
440+
leaves.push(...getAllLeaves(child, true));
433441
});
434442
}
435443

@@ -574,7 +582,7 @@ TidyTree.prototype.redraw = function () {
574582
newLinks
575583
.append("path")
576584
.attr("fill", "none")
577-
.attr("stroke", d => findLinkColor(d, this.colorOptions))
585+
.attr("stroke", d => findBranchColor(d, this.colorOptions))
578586
.attr("d", linkTransformer)
579587
.transition()
580588
.duration(this.animation)
@@ -601,7 +609,7 @@ TidyTree.prototype.redraw = function () {
601609
paths
602610
.transition()
603611
.duration(this.animation / 2)
604-
.attr("stroke", d => findLinkColor(d, this.colorOptions))
612+
.attr("stroke", d => findBranchColor(d, this.colorOptions))
605613
.attr("opacity", 0)
606614
.end()
607615
.then(() => {
@@ -857,13 +865,13 @@ TidyTree.prototype.setColorOptions = function (newColorOptions) {
857865
if (!TidyTree.validNodeColorModes.includes(newColorOptions.nodeColorMode)) {
858866
throw Error(`
859867
Cannot set TidyTree colorOptions: ${newColorOptions.nodeColorMode}\n
860-
Valid nodeColorModes are: ${TidyTree.validnodeColorModes.join(', ')}
868+
Valid nodeColorModes are: ${TidyTree.validNodeColorModes.join(', ')}
861869
`);
862870
}
863-
if (!TidyTree.validLinkColorModes.includes(newColorOptions.linkColorMode)) {
871+
if (!TidyTree.validBranchColorModes.includes(newColorOptions.branchColorMode)) {
864872
throw Error(`
865-
Cannot set TidyTree colorOptions: ${newColorOptions.linkColorMode}\n
866-
Valid linkColorModes are: ${TidyTree.validLinkColorModes.join(', ')}
873+
Cannot set TidyTree colorOptions: ${newColorOptions.branchColorMode}\n
874+
Valid branchColorModes are: ${TidyTree.validBranchColorModes.join(', ')}
867875
`);
868876
}
869877

@@ -873,8 +881,8 @@ TidyTree.prototype.setColorOptions = function (newColorOptions) {
873881
}
874882
} else {
875883
// nodeColorMode === 'none'
876-
if (newColorOptions.linkColorMode !== 'none') {
877-
throw Error('linkColorMode must be "none" for nodeColorMode "none"');
884+
if (newColorOptions.branchColorMode !== 'none') {
885+
throw Error('branchColorMode must be "none" for nodeColorMode "none"');
878886
}
879887
}
880888

0 commit comments

Comments
 (0)