Skip to content

Commit

Permalink
Hide leaf nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cmdcolin committed Feb 27, 2024
1 parent cc4056c commit 192868a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
16 changes: 13 additions & 3 deletions lib/src/components/TreePanel/TreeNodeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TreeMenu = observer(function ({
model: MsaViewModel
onClose: () => void
}) {
const { selectedStructures, collapsed, structures } = model
const { selectedStructures, collapsed, collapsed2, structures } = model
const nodeDetails = node ? model.getRowData(node.name) : undefined

return (
Expand Down Expand Up @@ -56,11 +56,21 @@ const TreeMenu = observer(function ({
<MenuItem
dense
onClick={() => {
model.toggleCollapsed(node.id)
if (collapsed.includes(node.id)) {
model.toggleCollapsed(node.id)
} else {
if (node.id.endsWith('-leafnode')) {
model.toggleCollapsed2(`${node.id}`)
} else {
model.toggleCollapsed2(`${node.id}-leafnode`)
}
}
onClose()
}}
>
{collapsed.includes(node.id) ? 'Show node' : 'Hide node'}
{collapsed.includes(node.id) || collapsed2.includes(node.id)
? 'Show node'
: 'Hide node'}
</MenuItem>

{structures[node.name]?.map(entry =>
Expand Down
1 change: 1 addition & 0 deletions lib/src/components/TreePanel/renderTreeCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export function renderNodeBubbles({
} = node
const { branchset, id = '', name = '' } = data
if (
!id.endsWith('-leafnode') &&
branchset.length &&
y > offsetY - extendBounds &&
y < offsetY + by + extendBounds
Expand Down
65 changes: 33 additions & 32 deletions lib/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import BaseViewModel from '@jbrowse/core/pluggableElementTypes/models/BaseViewMo
import {
clamp,
collapse,
filterHiddenLeafNodes,
generateNodeIds,
maxLength,
setBrLength,
Expand Down Expand Up @@ -92,6 +91,21 @@ export type BasicTrack = IBoxTrack | ITextTrack
*/
function x() {} // eslint-disable-line @typescript-eslint/no-unused-vars

function reparseTree(tree: NodeWithIds): NodeWithIds {
return {
...tree,
branchset: tree.branchset.map(r =>
r.branchset.length
? reparseTree(r)
: {
branchset: [r],
id: `${r.id}-leafnode`,
name: `${r.name}-hidden`,
},
),
}
}

export type DialogComponentType =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| React.LazyExoticComponent<React.FC<any>>
Expand Down Expand Up @@ -239,12 +253,7 @@ const model = types
*/
collapsed: types.array(types.string),

/**
* #property
* array of leaf nodes that are 'hidden', similar to collapsed but for leaf nodes
*/
hidden: types.array(types.string),

collapsed2: types.array(types.string),
/**
* #property
* focus on particular subtree
Expand Down Expand Up @@ -426,20 +435,6 @@ const model = types
self.drawTree = arg
},

/**
* #action
*/
hideNode(arg: string) {
self.hidden.push(arg)
},

/**
* #action
*/
clearHidden() {
self.hidden.clear()
},

/**
* #action
*/
Expand All @@ -451,6 +446,16 @@ const model = types
}
},

/**
* #action
*/
toggleCollapsed2(node: string) {
if (self.collapsed2.includes(node)) {
self.collapsed2.remove(node)
} else {
self.collapsed2.push(node)
}
},
/**
* #action
*/
Expand Down Expand Up @@ -613,14 +618,15 @@ const model = types
* #getter
*/
get _tree(): NodeWithIds {
return self.data.tree
const ret = self.data.tree
? generateNodeIds(parseNewick(self.data.tree))
: this.MSA?.getTree() || {
noTree: true,
branchset: [],
id: 'empty',
name: 'empty',
}
return reparseTree(ret)
},
/**
* #getter
Expand Down Expand Up @@ -659,18 +665,13 @@ const model = types
}
}

if (self.collapsed.length) {
self.collapsed
if (self.collapsed.length || self.collapsed2.length) {
;[...self.collapsed, ...self.collapsed2]
.map(collapsedId => hier.find(node => node.data.id === collapsedId))
.filter(notEmpty)
.map(node => collapse(node))
}
if (self.hidden.length) {
self.hidden
.map(hiddenId => hier.find(node => node.data.id === hiddenId))
.filter(notEmpty)
.map(node => filterHiddenLeafNodes(node.parent, node.id))
}

return hier
},
/**
Expand Down Expand Up @@ -1093,7 +1094,7 @@ const model = types
relativePxToBp(rowName: string, position: number) {
const { rowNames, rows } = self
const index = rowNames.indexOf(rowName)
if (index !== -1) {
if (index !== -1 && rows[index]) {
const row = rows[index][1]

let k = 0
Expand All @@ -1115,7 +1116,7 @@ const model = types
relativePxToBp2(rowName: string, position: number) {
const { rowNames, rows } = self
const index = rowNames.indexOf(rowName)
if (index !== -1) {
if (index !== -1 && rows[index]) {
const row = rows[index][1]

let k = 0
Expand Down
11 changes: 0 additions & 11 deletions lib/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,6 @@ export function collapse(d: HierarchyNode<NodeWithIds>) {
}
}

// Collapse the node and all it's children, from
// https://bl.ocks.org/d3noob/43a860bc0024792f8803bba8ca0d5ecd
export function filterHiddenLeafNodes(
d: HierarchyNode<NodeWithIds> | null,
hiddenId?: string,
) {
if (d?.children) {
d.children = d.children.filter(f => f.id !== hiddenId)
}
}

export function clamp(min: number, num: number, max: number) {
return Math.min(Math.max(num, min), max)
}

0 comments on commit 192868a

Please sign in to comment.