Mercurial > code > home > repos > streamed-graph
annotate src/layout/suffixLabels.ts @ 106:2468f2227d22
make src/layout/ and src/render/ separation
author | drewp@bigasterisk.com |
---|---|
date | Sun, 13 Mar 2022 22:00:30 -0700 |
parents | src/suffixLabels.ts@26c55d5d5202 |
children | 5a1a79f54779 |
rev | line source |
---|---|
36 | 1 import { Term } from "n3"; |
11 | 2 |
36 | 3 type SuffixesNode = { usedBy?: string; children: Map<string, SuffixesNode> }; |
4 type DisplayNode = { label?: string; link?: string }; | |
97 | 5 |
6 export class SuffixLabels { | |
36 | 7 displayNodes: Map<string, DisplayNode>; |
8 usedSuffixes: SuffixesNode; | |
9 constructor() { | |
10 this.displayNodes = new Map(); | |
11 this.usedSuffixes = { usedBy: undefined, children: new Map() }; | |
12 } | |
13 | |
14 planDisplayForNode(node: Term) { | |
15 const uri = node.value; | |
16 this._planDisplayForUri(uri); | |
17 } | |
18 | |
19 _planDisplayForUri(uri: string) { | |
20 if (this.displayNodes.has(uri)) { | |
21 return; | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
22 } |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
23 |
36 | 24 const segments = uri.split("/"); |
25 let curs = this.usedSuffixes; | |
26 let label: string | undefined = undefined; | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
27 |
36 | 28 for (let i = segments.length - 1; i >= 0; i--) { |
29 const seg = segments[i]; | |
30 if (curs.usedBy && curs.usedBy != uri) { | |
31 this._prependClashingUri(curs); | |
32 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
33 |
36 | 34 if (!curs.children.has(seg)) { |
35 const child: SuffixesNode = { usedBy: undefined, children: new Map() }; | |
36 curs.children.set(seg, child); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
37 |
36 | 38 if (label === undefined) { |
39 label = SuffixLabels._tailSegments(uri, segments.length - i); | |
40 child.usedBy = uri; | |
41 } | |
42 } | |
43 curs = curs.children.get(seg)!; | |
44 } | |
45 this.displayNodes.set(uri, { label: label }); | |
46 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
47 |
36 | 48 _prependClashingUri(curs: SuffixesNode) { |
49 // Claim: When a clash is discovered, only 1 uri needs to | |
50 // change its length, and there will be only one child node to | |
51 // follow, and the clashing uri can be changed to prepend that | |
52 // one child (since we'll see it again if that one wasn't | |
53 // enough). | |
54 const clashNode: DisplayNode = this.displayNodes.get(curs.usedBy!)!; | |
55 const nextLeftSeg = curs.children.entries().next().value; | |
56 if (nextLeftSeg[1].usedBy) { | |
57 throw new Error("unexpected"); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
58 } |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
59 |
36 | 60 clashNode.label = nextLeftSeg[0] + "/" + clashNode.label; |
61 nextLeftSeg[1].usedBy = curs.usedBy; | |
62 curs.usedBy = undefined; | |
63 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
64 |
36 | 65 // a substring to show for this uri |
66 getLabelForNode(node: string) { | |
97 | 67 const dn = this.displayNodes.get(node); |
68 if (dn === undefined) { | |
69 throw new Error(`you never called planDisplayForNode on ${node}`); | |
70 } | |
71 return dn.label; | |
36 | 72 } |
73 | |
74 static _tailSegments(uri: string, n: number) { | |
75 let i = uri.length; | |
76 for (let rep = 0; rep < n; rep++) { | |
77 i = uri.lastIndexOf("/", i - 1); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
78 } |
36 | 79 return uri.substr(i + 1); |
80 } | |
81 } |