annotate src/layout/ViewConfig.ts @ 143:5adf79d4a9f4

release v0.11.0
author drewp@bigasterisk.com
date Mon, 08 May 2023 13:29:48 -0700
parents cf642d395be4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
1 import Immutable from "immutable"; // mostly using this for the builtin equals() testing, since NamedNode(x)!=NamedNode(x)
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
2 import { DataFactory, NamedNode, Quad_Predicate, Term } from "n3";
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
3 import { MultiStore } from "../MultiStore";
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
4 import { EX, RDF } from "./namespaces";
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
5 import { uriValue } from "./rdf_value";
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
6 import { Quad_Graph } from "rdf-js";
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
7 const Uri = DataFactory.namedNode;
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
8
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
9 function firstElem<E>(seq: Iterable<E>): E {
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
10 for (let e of seq) {
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
11 return e;
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
12 }
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
13 throw new Error("no elems");
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
14 }
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
15
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
16 export interface Link {
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
17 // If you display a subject u1 with a `pred` edge to u2, then treat u2 as an alias of u1.
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
18 pred: NamedNode;
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
19 }
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
20
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
21 interface TableDesc {
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
22 uri: NamedNode;
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
23 primary: NamedNode;
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
24 joins: NamedNode[];
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
25 links: Link[];
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
26 }
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
27
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
28 // High-level guide to how to draw the page, independent of the graph data.
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
29 // Layout.ts turns this plus the actual graph data into a structure that's
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
30 // close to the final render.
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
31 export class ViewConfig {
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
32 viewRoot: NamedNode; // this structure...
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
33 graph: MultiStore; // in this graph...
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
34 tables: TableDesc[] = []; // populates all the rest of these fields for use by Layout
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
35 hidePredFrees: Immutable.Set<Quad_Predicate> = Immutable.Set();
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
36 hideGraphEverywhere: Immutable.Set<Quad_Graph> = Immutable.Set();
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
37
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
38 constructor(graph: MultiStore, viewUri: NamedNode) {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
39 this.graph = graph;
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
40 this.viewRoot = viewUri;
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
41 // todo
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
42 // const here = "https://bigasterisk.com/lanscape/";
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
43 // if (this.viewRoot.value.startsWith(here)) {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
44 // this.viewRoot = new NamedNode(this.viewRoot.value.slice(here.length));
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
45 // }
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
46 this.read();
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
47 }
95
47d3b5a5bd5e refactor
drewp@bigasterisk.com
parents: 94
diff changeset
48
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
49 private read() {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
50 for (let table of this.graph.getObjects(this.viewRoot, EX("table"), null)) {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
51 const t = this.readTable(table);
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
52 this.tables.push(t);
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
53 }
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
54 this.tables.sort();
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
55
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
56 this.readHides();
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
57 }
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
58
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
59 private readHides() {
139
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
60 for (let instr of this.graph.getObjects(this.viewRoot, EX("hide"), null)) {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
61 const types = this.graph.getObjects(instr, RDF("type"), null);
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
62 if (types.length == 1 && types[0].equals(EX("HideEverywhere"))) {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
63 for (let g of this.graph.getObjects(instr, EX("graph"), null)) {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
64 this.hideGraphEverywhere = this.hideGraphEverywhere.add(
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
65 g as Quad_Graph
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
66 );
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
67 }
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
68 } else if (types.length == 1 && types[0].equals(EX("HideFreeStatements"))) {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
69 for (let pred of this.graph.getObjects(instr, EX("predicate"), null)) {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
70 this.hidePredFrees = this.hidePredFrees.add(pred as Quad_Predicate);
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
71 }
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
72 } else {
cf642d395be4 new simpler Patch class; fancier 'hide' view config support
drewp@bigasterisk.com
parents: 128
diff changeset
73 throw new Error(":hide instruction must have 1 valid type");
97
26c55d5d5202 WIP on views & joins
drewp@bigasterisk.com
parents: 95
diff changeset
74 }
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
75 }
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
76 }
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
77
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
78 private readTable(table: Term): TableDesc {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
79 const tableType = uriValue(this.graph, table, EX("primaryType"));
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
80 const joins: NamedNode[] = [];
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
81 for (let joinType of this.graph.getObjects(table, EX("joinType"), null)) {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
82 joins.push(joinType as NamedNode);
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
83 }
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
84 joins.sort();
122
2e8fa3fec0c8 support joining subjects into wider rows
drewp@bigasterisk.com
parents: 110
diff changeset
85
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
86 const links: Link[] = [];
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
87 for (let linkDesc of this.graph.getObjects(table, EX("link"), null)) {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
88 links.push({
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
89 pred: uriValue(this.graph, linkDesc, EX("predicate")),
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
90 });
94
a5f53d397526 view: pick types to show at top-level
drewp@bigasterisk.com
parents: 93
diff changeset
91 }
102
ab7dca42afbd rewrite ViewConfig
drewp@bigasterisk.com
parents: 97
diff changeset
92
128
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
93 return {
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
94 uri: table as NamedNode,
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
95 primary: tableType,
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
96 joins: joins,
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
97 links: links,
5a1a79f54779 big rewrite
drewp@bigasterisk.com
parents: 122
diff changeset
98 };
93
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
99 }
955cde1550c3 start the View work: parse view document
drewp@bigasterisk.com
parents:
diff changeset
100 }