1 | import {
|
2 | TaxonomyNode, CompoundWidget, NodeIdentifier, QuerySegment, TaxonomyPath,
|
3 | PathSegment
|
4 | } from '@/types';
|
5 | import { DraggableConstructor } from 'gsap/Draggable';
|
6 | import * as log from 'loglevel';
|
7 |
|
8 | function getNodeById(rootNode: TaxonomyNode, wantedId: NodeIdentifier): TaxonomyNode {
|
9 | const result = rootNode.all(function(node: TaxonomyNode): boolean {
|
10 | return node.model.uri === wantedId;
|
11 | });
|
12 | if (result.length === 1) {
|
13 | return result[0];
|
14 | } else {
|
15 | throw new Error("not found: " + wantedId);
|
16 | }
|
17 | }
|
18 |
|
19 | function getVirtualRoot(rootNode: TaxonomyNode, wantedPath: NodeIdentifier[]): TaxonomyNode {
|
20 | log.debug("inside getVirtualRoot");
|
21 | if (wantedPath.length === 0) {
|
22 | log.debug("returning root node");
|
23 | return rootNode;
|
24 | }
|
25 |
|
26 |
|
27 | const leaf = wantedPath[wantedPath.length - 1]
|
28 | return getNodeById(rootNode, leaf);
|
29 | }
|
30 |
|
31 | function findValidChildren(
|
32 | rootNode: TaxonomyNode, wantedPath: NodeIdentifier[]
|
33 | ): TaxonomyNode[] {
|
34 | const virtualRoot = getVirtualRoot(rootNode, wantedPath);
|
35 | return virtualRoot.children;
|
36 | };
|
37 |
|
38 |
|
39 | function getCollidingElements(d: DraggableConstructor, validElements: Element[]) {
|
40 | return validElements.filter(e => d.hitTest(e, '50%'));
|
41 | }
|
42 |
|
43 | function makeEmptyCompoundWidget(): CompoundWidget {
|
44 | return {
|
45 | taxonomyRef: null,
|
46 | isCurrentlyBeingDragged: false,
|
47 | selectedPath: [],
|
48 | hasTentativeTaxonSelector: false,
|
49 | };
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function getFlatPath(selectedPath: PathSegment[]): TaxonomyPath {
|
56 | const result: NodeIdentifier[] = [];
|
57 |
|
58 | selectedPath.forEach(segment => {
|
59 | result.push.apply(result, segment.toPathElements());
|
60 | });
|
61 |
|
62 | return result;
|
63 | }
|
64 |
|
65 | function getQuerySegment(c: CompoundWidget): QuerySegment {
|
66 | if (c.taxonomyRef === null) throw new Error("null taxonomy");
|
67 |
|
68 | return {
|
69 | taxonomyRef: c.taxonomyRef,
|
70 | selectedPath: getFlatPath(c.selectedPath)
|
71 | };
|
72 | }
|
73 |
|
74 |
|
75 | export default {
|
76 | findValidChildren, getCollidingElements, makeEmptyCompoundWidget,
|
77 | getNodeById, getQuerySegment, getFlatPath
|
78 | };
|