Mercurial > code > home > repos > streamed-graph
annotate src/layout/suffixLabels.ts @ 143:5adf79d4a9f4
release v0.11.0
author | drewp@bigasterisk.com |
---|---|
date | Mon, 08 May 2023 13:29:48 -0700 |
parents | 5a1a79f54779 |
children |
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!)!; | |
128 | 55 const e = curs.children.entries() |
56 const n = e.next() | |
57 if (n.done) { | |
58 return; // todo - this ignores the bad url | |
59 throw new Error() | |
60 } | |
61 const nextLeftSeg = n.value | |
36 | 62 if (nextLeftSeg[1].usedBy) { |
63 throw new Error("unexpected"); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
64 } |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
36 | 66 clashNode.label = nextLeftSeg[0] + "/" + clashNode.label; |
67 nextLeftSeg[1].usedBy = curs.usedBy; | |
68 curs.usedBy = undefined; | |
69 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
70 |
36 | 71 // a substring to show for this uri |
72 getLabelForNode(node: string) { | |
97 | 73 const dn = this.displayNodes.get(node); |
74 if (dn === undefined) { | |
75 throw new Error(`you never called planDisplayForNode on ${node}`); | |
76 } | |
77 return dn.label; | |
36 | 78 } |
79 | |
80 static _tailSegments(uri: string, n: number) { | |
81 let i = uri.length; | |
82 for (let rep = 0; rep < n; rep++) { | |
83 i = uri.lastIndexOf("/", i - 1); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
84 } |
36 | 85 return uri.substr(i + 1); |
86 } | |
87 } |