annotate src/index.ts @ 55:2936477ba3dd

dispatch graph-changed event. experimental.
author drewp@bigasterisk.com
date Mon, 13 Jan 2020 00:07:28 -0800
parents 6933336a8fae
children 1bb65cb4c685
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42
895ae7c5b0f4 don't pile all the deps in our sharable library. other fixes to make it work as a dep.
drewp@bigasterisk.com
parents: 38
diff changeset
1 import { customElement, property, computed } from "@polymer/decorators";
895ae7c5b0f4 don't pile all the deps in our sharable library. other fixes to make it work as a dep.
drewp@bigasterisk.com
parents: 38
diff changeset
2 import { N3Store } from "n3";
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
3 import { PolymerElement, html } from "@polymer/polymer";
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
4 import { render } from "lit-html";
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 15
diff changeset
5
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
6 import { GraphView } from "./graph_view";
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
7 import { StreamedGraphClient } from "./streamed_graph_client";
54
6933336a8fae better init for VersionedGraph- start with empty graph, not undefined
drewp@bigasterisk.com
parents: 53
diff changeset
8 import { Store } from "n3";
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
9
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
10 import style from "./style.styl";
53
601a604c097a new module with getStringValue
drewp@bigasterisk.com
parents: 48
diff changeset
11 export * from "./graph_queries";
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
12
38
d4036715028b try exporting the PolymerElement so it can be depended on and therefore not culled by tree-shaking
drewp@bigasterisk.com
parents: 36
diff changeset
13 export interface VersionedGraph {
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
14 version: number;
54
6933336a8fae better init for VersionedGraph- start with empty graph, not undefined
drewp@bigasterisk.com
parents: 53
diff changeset
15 store: N3Store;
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
16 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
17
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
18 function templateWithStyle(style: string, tmpl: HTMLTemplateElement) {
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
19 const styleEl = document.createElement("style");
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
20 styleEl.textContent = style;
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
21 tmpl.content.insertBefore(styleEl, tmpl.content.firstChild);
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
22 return tmpl;
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
23 }
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
24
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
25 @customElement("streamed-graph")
38
d4036715028b try exporting the PolymerElement so it can be depended on and therefore not culled by tree-shaking
drewp@bigasterisk.com
parents: 36
diff changeset
26 export class StreamedGraph extends PolymerElement {
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
27 @property({ type: String })
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
28 url: string = "";
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
29
45
3e414d575d96 can't use that graph property so well if it's non-notifying
drewp@bigasterisk.com
parents: 44
diff changeset
30 @property({ type: Object, notify: true })
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
31 graph!: VersionedGraph;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
32
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
33 @property({ type: Boolean })
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
34 expanded: boolean = false;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
35
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
36 @computed("expanded")
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
37 get expandAction() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
38 return this.expanded ? "-" : "+";
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
39 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
40
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
41 @property({ type: String })
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
42 status: string = "";
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
43
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
44 sg!: StreamedGraphClient;
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
45 graphViewEl!: Element;
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
46 graphViewDirty = true;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
47
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
48 static get template() {
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
49 return templateWithStyle(
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
50 style,
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
51 html`
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
52 <div id="ui">
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
53 <span class="expander"
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
54 ><button on-click="toggleExpand">{{expandAction}}</button></span
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
55 >
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
56 StreamedGraph <a href="{{url}}">[source]</a>: {{status}}
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
57 </div>
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
58 <div id="graphView"></div>
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
59 `
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
60 );
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
61 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
62
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
63 ready() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
64 super.ready();
54
6933336a8fae better init for VersionedGraph- start with empty graph, not undefined
drewp@bigasterisk.com
parents: 53
diff changeset
65 const emptyStore = new Store();
6933336a8fae better init for VersionedGraph- start with empty graph, not undefined
drewp@bigasterisk.com
parents: 53
diff changeset
66 this.graph = { version: -1, store: emptyStore };
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
67 this.graphViewEl = (this.shadowRoot as ShadowRoot).getElementById(
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
68 "graphView"
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
69 ) as Element;
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
70
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
71 this._onUrl(this.url); // todo: watch for changes and rebuild
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
72 if (this.expanded) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
73 this.redrawGraph();
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 15
diff changeset
74 }
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
75 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
76
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
77 toggleExpand() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
78 this.expanded = !this.expanded;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
79 if (this.expanded) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
80 this.redrawGraph();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
81 } else {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
82 this.graphViewDirty = false;
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
83 this._graphAreaClose();
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 15
diff changeset
84 }
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
85 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
86
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
87 redrawGraph() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
88 this.graphViewDirty = true;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
89 requestAnimationFrame(this._redrawLater.bind(this));
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
90 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
91
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
92 _onUrl(url: string) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
93 if (this.sg) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
94 this.sg.close();
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 15
diff changeset
95 }
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
96 this.sg = new StreamedGraphClient(
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
97 url,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
98 this.onGraphChanged.bind(this),
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
99 this.set.bind(this, "status"),
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
100 [], //window.NS,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
101 []
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
102 );
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
103 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
104
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
105 onGraphChanged() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
106 this.graph = {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
107 version: this.graph.version + 1,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
108 store: this.sg.store
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
109 };
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
110 if (this.expanded) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
111 this.redrawGraph();
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 15
diff changeset
112 }
55
2936477ba3dd dispatch graph-changed event. experimental.
drewp@bigasterisk.com
parents: 54
diff changeset
113 this.dispatchEvent(
2936477ba3dd dispatch graph-changed event. experimental.
drewp@bigasterisk.com
parents: 54
diff changeset
114 new CustomEvent("graph-changed", { detail: { graph: this.graph } })
2936477ba3dd dispatch graph-changed event. experimental.
drewp@bigasterisk.com
parents: 54
diff changeset
115 );
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
116 }
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
117
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
118 _redrawLater() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
119 if (!this.graphViewDirty) return;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
120
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
121 if ((this.graph as VersionedGraph).store && this.graph.store) {
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
122 this._graphAreaShowGraph(new GraphView(this.url, this.graph.store));
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
123 this.graphViewDirty = false;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
124 } else {
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
125 this._graphAreaShowPending();
22
e90d9021c6a0 add back s-g code; this breaks the build a little. WIP
drewp@bigasterisk.com
parents: 15
diff changeset
126 }
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
127 }
48
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
128
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
129 _graphAreaClose() {
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
130 render(null, this.graphViewEl);
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
131 }
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
132
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
133 _graphAreaShowPending() {
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
134 render(
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
135 html`
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
136 <span>waiting for data...</span>
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
137 `,
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
138 this.graphViewEl
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
139 );
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
140 }
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
141
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
142 _graphAreaShowGraph(graphView: GraphView) {
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
143 render(graphView.makeTemplate(), this.graphViewEl);
b8e5850acca0 local demo; styles
drewp@bigasterisk.com
parents: 45
diff changeset
144 }
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 33
diff changeset
145 }