| import { randomID } from "@/Core/Utils/MiscUtils"; |
| import { FileInfo } from "@/FileSystem/FileInfo"; |
| import { |
| getMoleculesFromStore, |
| pushToStoreList, |
| setStoreVar, |
| } from "@/Store/StoreExternalAccess"; |
| import type { GLModel } from "@/UI/Panels/Viewer/GLModelType"; |
| import { |
| TreeNodeType, |
| SelectedType, |
| ITreeNodeData, |
| IAtom, |
| IRegion, |
| IBox, |
| RegionType, |
| } from "../../UI/Navigation/TreeView/TreeInterfaces"; |
| import { TreeNodeList } from "../TreeNodeList/TreeNodeList"; |
| import { newTreeNodeList, setupMakerFuncs } from "../TreeNodeMakers"; |
| import { TreeNodeAncestry } from "./_Ancestry"; |
| import { TreeNodeDescriptions } from "./_Descriptions"; |
| import { store } from "@/Store"; |
| import { visualizationApi } from "@/Api/Visualization"; |
| import { expandAndShowAllMolsInTree } from "@/Testing/SetupTests"; |
| import { IFileInfo } from "@/FileSystem/Types"; |
| import { makeEasyParser } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/EasyParser"; |
| import { ILoadMolParams } from "@/FileSystem/LoadSaveMolModels/ParseMolModels/Types"; |
| import { ISelAndStyle } from "@/Core/Styling/SelAndStyleInterfaces"; |
| import { updateStylesInViewer } from "@/Core/Styling/StyleManager"; |
| import { getSetting } from "@/Plugins/Core/Settings/LoadSaveSettings"; |
| import { isTest } from "@/Core/GlobalVars"; |
| import { _convertTreeNodeList } from "@/FileSystem/LoadSaveMolModels/ConvertMolModels/_ConvertTreeNodeList"; |
|
|
| |
| export interface ITreeNode { |
| |
| title: string; |
| type?: TreeNodeType; |
| id?: string; |
| parentId?: string; |
| src?: string; |
| treeExpanded: boolean; |
| visible: boolean; |
| selected: SelectedType; |
| focused: boolean; |
| viewerDirty: boolean; |
| data?: { [key: string]: ITreeNodeData }; |
| tags?: string[]; |
|
|
| |
| styles?: ISelAndStyle[]; |
| model?: IAtom[] | GLModel | IFileInfo; |
| region?: IRegion; |
|
|
| |
| nodes?: TreeNodeList; |
| } |
|
|
| |
| |
| |
| export class TreeNode { |
| |
| title: string; |
| type?: TreeNodeType; |
| id?: string; |
| parentId?: string; |
| src?: string; |
| treeExpanded: boolean; |
| _visible: boolean; |
| _selected: SelectedType; |
| focused: boolean; |
| viewerDirty: boolean; |
| data?: { [key: string]: ITreeNodeData }; |
| tags?: string[]; |
|
|
| |
| nodes?: TreeNodeList; |
|
|
| |
| model?: IAtom[] | GLModel | IFileInfo; |
| styles?: ISelAndStyle[]; |
| region?: IRegion; |
|
|
| public triggerId = ""; |
|
|
| private _descriptions: TreeNodeDescriptions; |
| private _ancestry: TreeNodeAncestry; |
|
|
| |
| |
| |
| |
| |
| constructor(params: ITreeNode) { |
| |
| this.title = params.title; |
| this.type = params.type; |
|
|
| |
| if (!params.id) { |
| params.id = randomID(); |
| } |
| this.id = params.id; |
|
|
| this.parentId = params.parentId; |
| this.src = params.src; |
| this.treeExpanded = params.treeExpanded; |
| this._visible = params.visible; |
| this._selected = params.selected; |
| this.focused = params.focused; |
| this.viewerDirty = params.viewerDirty; |
| this.data = params.data; |
| this.tags = params.tags; |
| this.nodes = params.nodes; |
| this.model = params.model; |
| this.styles = params.styles; |
| this.region = params.region; |
|
|
| this._descriptions = new TreeNodeDescriptions(this); |
| this._ancestry = new TreeNodeAncestry(this); |
|
|
| |
| return this; |
| } |
|
|
| |
| |
| |
| |
| |
| get selected(): SelectedType { |
| return this._selected; |
| } |
|
|
| |
| |
| |
| |
| |
| set selected(val: SelectedType) { |
| |
| |
| this.viewerDirty = true; |
|
|
| this._selected = val; |
| } |
|
|
| |
| |
| |
| |
| public triggerReactivity() { |
| this.triggerId = randomID(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| public get visible(): boolean { |
| return this._visible; |
| } |
|
|
| |
| |
| |
| |
| |
| public set visible(val: boolean) { |
| |
| this.nodes?.flattened.forEach((nd) => { |
| nd._visible = val; |
| }); |
| this._visible = val; |
| } |
|
|
| |
| |
| |
| public set visibleWithoutChildren(val: boolean) { |
| |
| this._visible = val; |
| } |
|
|
| |
| |
| |
| |
| |
| public get descriptions(): TreeNodeDescriptions { |
| return this._descriptions; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public get parentTreeNode(): TreeNode | undefined { |
| const ancestors = this.getAncestry(store.state.molecules); |
| return ancestors.get(ancestors.length - 2); |
| } |
|
|
| |
| |
| |
| |
| |
| public serialize(): ITreeNode { |
| const obj: { [key: string]: any } = {}; |
| for (const key in this) { |
| if (key === "nodes" && this.nodes) { |
| obj["nodes"] = |
| (this.nodes as TreeNodeList).serialize !== undefined |
| ? (this.nodes as TreeNodeList).serialize() |
| : this.nodes; |
| } else if (key === "model" && this.model) { |
| |
| |
| if ((this.model as GLModel).selectedAtoms !== undefined) { |
| obj["model"] = (this.model as GLModel).selectedAtoms({}); |
|
|
| |
| |
| const keysToRemove = [ |
| "pdbline", |
| "uMat", |
| |
| ]; |
| obj["model"] = obj["model"].map((atom: IAtom) => { |
| for (const key of keysToRemove) { |
| if ((atom as any)[key]) { |
| delete (atom as any)[key]; |
| } |
| } |
| return atom; |
| }); |
| } else { |
| obj["model"] = JSON.parse(JSON.stringify(this.model)); |
| } |
| } else { |
| const element = this[key]; |
| if (element !== undefined) { |
| if (key.startsWith("_")) { |
| |
| continue; |
| } |
| obj[key] = JSON.parse(JSON.stringify(element)); |
| } |
| } |
| } |
|
|
| |
| |
| obj["visible"] = this.visible; |
|
|
| return obj as ITreeNode; |
| } |
|
|
| |
| |
| |
| |
| |
| public shallowCopy(): TreeNode { |
| const prop: { [key: string]: any } = {}; |
| for (const key in this) { |
| prop[key] = this[key]; |
| } |
|
|
| if (prop.nodes) { |
| prop.nodes = (prop.nodes as TreeNodeList).copy.shallow; |
| } |
|
|
| return new TreeNode(prop as ITreeNode); |
| } |
|
|
| |
| |
| |
| public clearChildren() { |
| if (this.nodes) { |
| this.nodes.clear(); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public getAncestry(mols?: TreeNodeList): TreeNodeList { |
| if (mols === undefined) { |
| mols = getMoleculesFromStore(); |
| } |
|
|
| return this._ancestry.getAncestry(mols); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public toFileInfo( |
| targetExt: string, |
| considerDescendants = false |
| ): Promise<FileInfo> { |
| let nodesToConvert: TreeNodeList; |
| let merge = false; |
| if (this.model) { |
| |
| nodesToConvert = new TreeNodeList([this]); |
| merge = false; |
| } else if (this.nodes && this.nodes.terminals.length > 0) { |
| |
| if (considerDescendants) { |
| |
| nodesToConvert = this.nodes.terminals; |
| merge = true; |
| } else { |
| |
| return Promise.reject( |
| new Error( |
| `Cannot convert container node "${this.title}" directly. To convert its contents, set considerDescendants to true.` |
| ) |
| ); |
| } |
| } else { |
| |
| return Promise.reject( |
| new Error( |
| `Node "${this.title}" has no model or descendant nodes with models to convert.` |
| ) |
| ); |
| } |
| return _convertTreeNodeList(nodesToConvert, targetExt, merge) |
| .then((fileInfos: FileInfo[]) => { |
| if (fileInfos.length === 0) { |
| |
| throw new Error( |
| `Conversion of node "${this.title}" to "${targetExt}" resulted in an empty file.` |
| ); |
| } |
| return fileInfos[0]; |
| }) |
| .catch((err: Error) => { |
| throw err; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public newTreeNode(params: ITreeNode): TreeNode { |
| |
| return new TreeNode(params); |
| } |
|
|
| |
| |
| |
| |
| |
| public get depth(): number { |
| let maxDepthFound = 1; |
|
|
| const recurse = (node: TreeNode, depthSoFar: number) => { |
| if (node.nodes) { |
| node.nodes.forEach((child: TreeNode) => { |
| recurse(child, depthSoFar + 1); |
| }); |
| } |
| if (depthSoFar > maxDepthFound) { |
| maxDepthFound = depthSoFar; |
| } |
| return depthSoFar; |
| }; |
|
|
| recurse(this, 1); |
|
|
| return maxDepthFound; |
| } |
|
|
| |
| |
| |
| |
| |
| public mergeInto(otherNode: TreeNode) { |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| otherNode.nodes?.forEach((otherChild: TreeNode) => { |
| const otherNodeType = otherChild.type; |
|
|
| |
| const thisChild = this.nodes?.find((child: TreeNode) => { |
| return child.type === otherNodeType; |
| }); |
|
|
| if (thisChild === undefined) { |
| |
| otherChild.parentId = this.id; |
| this.nodes?.push(otherChild); |
| } else { |
| |
|
|
| if ( |
| thisChild.nodes === undefined && |
| otherChild.nodes === undefined |
| ) { |
| |
| otherChild.parentId = this.id; |
| this.nodes?.push(otherChild); |
| } else { |
| |
| |
| thisChild.mergeInto(otherChild); |
| } |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| reassignAllIds() { |
| |
| const allNodes = new TreeNodeList([this]).flattened; |
|
|
| |
| allNodes.forEach((node: TreeNode) => { |
| node.id = randomID(); |
| }); |
|
|
| |
| allNodes.forEach((node: TreeNode) => { |
| if (node.nodes) { |
| node.nodes.forEach((child: TreeNode) => { |
| child.parentId = node.id; |
| }); |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public async addToMainTree( |
| tag: string | null, |
| reassignIds = true, |
| terminalNodeTitleRevisable = true, |
| resetVisibilityAndSelection = true |
| ) { |
| if (reassignIds) { |
| this.reassignAllIds(); |
| } |
| if (isTest) { |
| |
| expandAndShowAllMolsInTree(); |
| } |
|
|
| |
| const allNodesInSubtree = new TreeNodeList([this]).flattened; |
|
|
| |
| if (tag) { |
| allNodesInSubtree.forEach((node) => { |
| if (node.tags === undefined) { |
| node.tags = []; |
| } |
| |
| if (!node.tags.includes(tag)) { |
| node.tags.push(tag); |
| } |
| }); |
| } |
|
|
| if (resetVisibilityAndSelection) { |
| |
| allNodesInSubtree.forEach((node) => { |
| if (node.nodes) { |
| |
| node.visible = true; |
| } |
| }); |
| this.visible = true; |
|
|
| |
| const terminalNodes = this.nodes |
| ? this.nodes.terminals |
| : new TreeNodeList([]); |
|
|
| if (this.model) { |
| |
| terminalNodes.push(this); |
| } |
|
|
| const initialCompoundsVisible = await getSetting( |
| "initialCompoundsVisible" |
| ); |
| terminalNodes.forEach((node, i) => { |
| node.visible = i < initialCompoundsVisible; |
| }); |
|
|
| |
| allNodesInSubtree.forEach((node) => { |
| node.selected = SelectedType.False; |
| }); |
| } |
|
|
| |
| |
| if ( |
| terminalNodeTitleRevisable && |
| this.nodes && |
| this.nodes.terminals.length === 1 |
| ) { |
| this.nodes.terminals.get(0).title = `${this.title}:${ |
| this.nodes.terminals.get(0).title |
| }`; |
| } |
|
|
| pushToStoreList("molecules", this); |
|
|
| if (store.state.projectTitle === "") { |
| const topAncestor = this.getAncestry(getMoleculesFromStore()).get( |
| 0 |
| ); |
| if (topAncestor && topAncestor.title) { |
| setStoreVar("projectTitle", topAncestor.title); |
| } |
| } |
|
|
| |
| const viewer = await visualizationApi.viewer; |
| |
| updateStylesInViewer(); |
|
|
| viewer.zoomOnFocused(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public getBoxRegion(padding = 3.4): IBox { |
| |
|
|
| |
| const nodesWithModels = newTreeNodeList([this]).filters.keepModels( |
| true, |
| true |
| ).nodes; |
| const xs: number[] = []; |
| const ys: number[] = []; |
| const zs: number[] = []; |
| nodesWithModels.forEach((node: TreeNode) => { |
| const model = node.model as GLModel; |
| |
| const { atoms } = makeEasyParser(model); |
| xs.push(...atoms.map((atom: IAtom) => atom.x as number)); |
| ys.push(...atoms.map((atom: IAtom) => atom.y as number)); |
| zs.push(...atoms.map((atom: IAtom) => atom.z as number)); |
| }); |
|
|
| |
| const minX = Math.min(...xs); |
| const maxX = Math.max(...xs); |
| const minY = Math.min(...ys); |
| const maxY = Math.max(...ys); |
| const minZ = Math.min(...zs); |
| const maxZ = Math.max(...zs); |
|
|
| |
| const centerX = (minX + maxX) / 2; |
| const centerY = (minY + maxY) / 2; |
| const centerZ = (minZ + maxZ) / 2; |
|
|
| |
| let color: string | undefined = undefined; |
| if (this.styles && this.styles.length > 0) { |
| for (const style of this.styles) { |
| const colors = [ |
| style.surface?.color, |
| style.sphere?.color, |
| style.cartoon?.color, |
| style.stick?.color, |
| style.line?.color, |
| ]; |
| |
| color = colors.find((c: string | undefined) => c !== undefined); |
| if (color !== undefined) { |
| break; |
| } |
| } |
| } |
|
|
| if (color === undefined) { |
| |
| color = "red"; |
| } |
|
|
| return { |
| type: RegionType.Box, |
| center: [centerX, centerY, centerZ], |
| opacity: 0.5, |
| color: color, |
| movable: true, |
| dimensions: [ |
| maxX - minX + padding, |
| maxY - minY + padding, |
| maxZ - minZ + padding, |
| ], |
| } as IBox; |
| } |
| } |
|
|
| |
| |
| |
| |
| export function defineMakerFuncs() { |
| setupMakerFuncs( |
| new TreeNode({} as ITreeNode).newTreeNode, |
| new TreeNodeList([]).newTreeNodeList |
| ); |
| } |
|
|