annotate src/index.ts @ 84:067d66a45a51

enable more code. factor out setGraphView
author drewp@bigasterisk.com
date Wed, 17 Nov 2021 16:45:10 -0800
parents b973d7f95fdf
children 6ec759f9009f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
1 import { LitElement, html, css, render, TemplateResult } from "lit";
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
2 import { customElement, property } from "lit/decorators.js";
66
1bb65cb4c685 clean up import lines
drewp@bigasterisk.com
parents: 55
diff changeset
3
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
4 import { Store } from "n3";
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
5
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
6 import { GraphView } from "./graph_view";
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
7 import { StreamedGraphClient } from "./streamed_graph_client";
83
b973d7f95fdf bake out the stylus to plain css; add this https://ziga-petek.medium.com/using-fonts-in-web-components-6aba251ed4e5 to make fonts work
drewp@bigasterisk.com
parents: 80
diff changeset
8 import { style, addFontToRootPage } from './style';
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
9
79
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
10 // export * from "./graph_queries";
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
11
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
12 export interface VersionedGraph {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
13 version: number;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
14 store: Store;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
15 }
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
16
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
17 @customElement("streamed-graph")
79
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
18 export class StreamedGraph extends LitElement {
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
19 @property({ type: String })
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
20 url: string = "";
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
21
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
22 @property({ type: Object })
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
23 graph!: VersionedGraph;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
24
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
25 @property({ type: Boolean })
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
26 expanded: boolean = false;
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
27
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
28 @property({ type: String })
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
29 status: string = "";
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
30
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
31 sg!: StreamedGraphClient;
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
32 graphViewDirty = true;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
33
83
b973d7f95fdf bake out the stylus to plain css; add this https://ziga-petek.medium.com/using-fonts-in-web-components-6aba251ed4e5 to make fonts work
drewp@bigasterisk.com
parents: 80
diff changeset
34 static styles=style;
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
35 render() {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
36 const expandAction = this.expanded ? "-" : "+";
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
37 return html`
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
38 <div id="ui">
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
39 <span class="expander"
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
40 ><button @click="${this.toggleExpand}">${expandAction}</button></span
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
41 >
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
42 StreamedGraph <a href="${this.url}">[source]</a>: ${this.status}
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
43 </div>
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
44 <div id="graphView"></div>
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
45 `;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
46 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
47
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
48 connectedCallback() {
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
49 super.connectedCallback();
83
b973d7f95fdf bake out the stylus to plain css; add this https://ziga-petek.medium.com/using-fonts-in-web-components-6aba251ed4e5 to make fonts work
drewp@bigasterisk.com
parents: 80
diff changeset
50 addFontToRootPage();
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
51 const emptyStore = new Store();
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
52 this.graph = { version: -1, store: emptyStore };
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
53
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
54 this._onUrl(this.url); // todo: watch for changes and rebuild
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
55 if (this.expanded) {
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
56 this.redrawGraph();
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
57 }
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
58 }
79
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
59
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
60 toggleExpand() {
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
61 this.expanded = !this.expanded;
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
62 if (this.expanded) {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
63 this.redrawGraph();
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
64 } else {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
65 this.graphViewDirty = false;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
66 this._graphAreaClose();
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
67 }
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
68 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
69
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
70 redrawGraph() {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
71 this.graphViewDirty = true;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
72 requestAnimationFrame(this._redrawLater.bind(this));
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
73 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
74
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
75 _onUrl(url: string) {
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
76 if (this.sg) {
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
77 this.sg.close();
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
78 }
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
79 this.sg = new StreamedGraphClient(
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
80 url,
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
81 this.onGraphChanged.bind(this),
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
82 (st) => {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
83 this.status = st;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
84 },
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
85 [], //window.NS,
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
86 []
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
87 );
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
88 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
89
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
90 onGraphChanged() {
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
91 this.graph = {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
92 version: this.graph.version + 1,
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
93 store: this.sg.store,
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
94 };
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
95 if (this.expanded) {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
96 this.redrawGraph();
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
97 }
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
98 this.dispatchEvent(
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
99 new CustomEvent("graph-changed", { detail: { graph: this.graph } })
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
100 );
80
7c93e17b1111 more fixes, more original code uncommented
drewp@bigasterisk.com
parents: 79
diff changeset
101 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
102
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
103 _redrawLater() {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
104 if (!this.graphViewDirty) return;
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
105
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
106 if ((this.graph as VersionedGraph).store && this.graph.store) {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
107 this._graphAreaShowGraph(new GraphView(this.url, this.graph.store));
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
108 this.graphViewDirty = false;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
109 } else {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
110 this._graphAreaShowPending();
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
111 }
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
112 }
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
113
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
114 _graphAreaClose() {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
115 this._setGraphArea(html``);
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
116 }
79
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
117
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
118 _graphAreaShowPending() {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
119 this._setGraphArea(html` <span>waiting for data...</span> `);
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
120 }
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
121
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
122 _graphAreaShowGraph(graphView: GraphView) {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
123 this._setGraphArea(graphView.makeTemplate());
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
124 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
125
84
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
126 _setGraphArea(tmpl: TemplateResult) {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
127 const el = this.shadowRoot?.getElementById("graphView");
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
128 if (!el) {
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
129 return;
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
130 }
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
131 render(tmpl, el);
067d66a45a51 enable more code. factor out setGraphView
drewp@bigasterisk.com
parents: 83
diff changeset
132 }
79
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
133 }
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
134
79
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
135 // // allow child nodes to combine a few graphs and statics
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
136 // //<streamed-graph id="timebankGraph" graph="{{graph}}" expanded="true">
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
137 // // <member-graph url="graph/timebank/events"></member-graph>
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
138 // // <member-graph url="/some/static.n3"></member-graph>
0c188ed3bcd8 starting lit upgrade. total mess right now
drewp@bigasterisk.com
parents: 77
diff changeset
139 // //</streamed-graph>