diff options
| -rw-r--r-- | src/App.vue | 27 | ||||
| -rw-r--r-- | src/components/SearchModal.vue | 3 | ||||
| -rw-r--r-- | src/components/TileInfoModal.vue | 2 | ||||
| -rw-r--r-- | src/lib.ts | 15 | ||||
| -rw-r--r-- | src/tol.ts | 49 |
5 files changed, 58 insertions, 38 deletions
diff --git a/src/App.vue b/src/App.vue index 0eacbe4..8789653 100644 --- a/src/App.vue +++ b/src/App.vue @@ -6,36 +6,15 @@ import TileInfoModal from './components/TileInfoModal.vue'; import SearchModal from './components/SearchModal.vue'; import HelpModal from './components/HelpModal.vue'; import SettingsPane from './components/SettingsPane.vue'; -import {TolNode, LayoutNode, initLayoutTree, initLayoutMap, tryLayout} from './lib'; +import {TolNode, TolNodeRaw, tolFromRaw, getTolMap} from './tol'; +import {LayoutNode, initLayoutTree, initLayoutMap, tryLayout} from './lib'; import type {LayoutOptions} from './lib'; import {arraySum, randWeightedChoice} from './lib'; // Import paths lack a .ts or .js extension because .ts makes vue-tsc complain, and .js makes vite complain // Obtain tree-of-life data import tolRaw from './tol.json'; -function preprocessTol(node: any): any { - function helper(node: any, parent: any){ - //Add 'children' field if missing - if (node.children == null){ - node.children = []; - } - //Add 'parent' field - node.parent = parent; - node.children.forEach((child: any) => helper(child, node)); - } - helper(node, null); - return node; -} -const tol: TolNode = preprocessTol(tolRaw); -function getTolMap(tol: TolNode): Map<string,TolNode> { - function helper(node: TolNode, map: Map<string,TolNode>){ - map.set(node.name, node); - node.children.forEach(child => helper(child, map)); - } - let map = new Map(); - helper(tol, map); - return map; -} +const tol: TolNode = tolFromRaw(tolRaw); const tolMap = getTolMap(tol); // Configurable settings diff --git a/src/components/SearchModal.vue b/src/components/SearchModal.vue index cdf1206..647181b 100644 --- a/src/components/SearchModal.vue +++ b/src/components/SearchModal.vue @@ -1,6 +1,7 @@ <script lang="ts"> import {defineComponent, PropType} from 'vue'; -import {TolNode, LayoutNode} from '../lib'; +import {TolNode} from '../tol'; +import {LayoutNode} from '../lib'; export default defineComponent({ props: { diff --git a/src/components/TileInfoModal.vue b/src/components/TileInfoModal.vue index 297484b..9d9dd15 100644 --- a/src/components/TileInfoModal.vue +++ b/src/components/TileInfoModal.vue @@ -1,6 +1,6 @@ <script lang="ts"> import {defineComponent, PropType} from 'vue'; -import {TolNode} from '../lib'; +import {TolNode} from '../tol'; export default defineComponent({ props: { @@ -1,5 +1,5 @@ /* - * Contains classes used for representing tree-of-life data, and tile-based layouts of such data. + * Contains classes used for representing tile-based layouts of tree-of-life data. * * Generally, given a TolNode with child TolNodes representing tree-of-life T, * initLayoutTree() produces a tree structure representing a subtree of T, @@ -7,17 +7,8 @@ * The tree structure consists of LayoutNode objects, each of which holds placement info for a linked TolNode. */ -// Represents a tree-of-life node/tree -export class TolNode { - name: string; - children: TolNode[]; - parent: TolNode | null; - constructor(name: string, children: TolNode[] = [], parent = null){ - this.name = name; - this.children = children; - this.parent = parent; - } -} +import {TolNode} from './tol'; + // Represents a node/tree, and holds layout data for a TolNode node/tree export class LayoutNode { tolNode: TolNode; diff --git a/src/tol.ts b/src/tol.ts new file mode 100644 index 0000000..0f05ba9 --- /dev/null +++ b/src/tol.ts @@ -0,0 +1,49 @@ +/* + * Contains classes used for representing tree-of-life data. + */ + +// Represents a tree-of-life node/tree +export class TolNode { + name: string; + children: TolNode[]; + parent: TolNode | null; + constructor(name: string, children: TolNode[] = [], parent = null){ + this.name = name; + this.children = children; + this.parent = parent; + } +} +// Represents a tree-of-life node obtained from tol.json +export class TolNodeRaw { + name: string; + children?: TolNodeRaw[]; + constructor(name: string, children: TolNodeRaw[] = []){ + this.name = name; + this.children = children; + } +} +// Converts a TolNodeRaw tree to a TolNode tree +export function tolFromRaw(node: TolNodeRaw): TolNode { + function helper(node: TolNodeRaw, parent: TolNode | null){ + let tolNode = new TolNode(node.name); + if (node.children == null){ + tolNode.children = []; + } else { + tolNode.children = Array(node.children.length); + node.children.forEach((child, idx) => {tolNode.children[idx] = helper(child, tolNode)}); + } + tolNode.parent = parent; + return tolNode; + } + return helper(node, null); +} +// Returns a mapping from TolNode names to TolNodes in a given tree +export function getTolMap(tolTree: TolNode): Map<string, TolNode> { + function helper(node: TolNode, map: Map<string, TolNode>){ + map.set(node.name, node); + node.children.forEach(child => helper(child, map)); + } + let map = new Map(); + helper(tolTree, map); + return map; +} |
