comparison src/index.ts @ 33:b82c05e22d9a

change to npm with a build that worked better as a local package to be shared
author drewp@bigasterisk.com
date Tue, 24 Dec 2019 20:30:27 -0800
parents src/streamed-graph.ts@e54941d93356
children 8b4dc9e87b56
comparison
equal deleted inserted replaced
31:e54941d93356 33:b82c05e22d9a
1
2 // these are just for timebank- move them out
3 import '@polymer/polymer/lib/elements/dom-bind.js';
4
5 import { PolymerElement, html } from '@polymer/polymer';
6 import { render } from 'lit-html';
7 import { N3Store } from "n3"
8 import { customElement, property, computed } from '@polymer/decorators';
9
10 import { GraphView } from './graph_view';
11 import { StreamedGraphClient } from './streamed_graph_client';
12 export { StreamedGraphClient } from './streamed_graph_client';
13
14 interface VersionedGraph { version: number, store: N3Store | undefined };
15
16 @customElement('streamed-graph')
17 class StreamedGraph extends PolymerElement {
18 @property({ type: String })
19 url: string = '';
20
21 @property({ type: Object })
22 graph!: VersionedGraph;
23
24 @property({ type: Boolean })
25 expanded: boolean = false;
26
27 @computed('expanded')
28 get expandAction() {
29 return this.expanded ? '-' : '+';
30 }
31
32 @property({ type: String })
33 status: string = '';
34
35 sg!: StreamedGraphClient;
36 graphView!: Element;
37 graphViewDirty = true;
38
39 static get template() {
40 return html`
41 <link rel="stylesheet" href="../src/streamed-graph.css">
42 <div id="ui">
43 <span class="expander"><button on-click="toggleExpand">{{expandAction}}</button></span>
44 StreamedGraph <a href="{{url}}">[source]</a>:
45 {{status}}
46 </div>
47 <div id="graphView"></div>`;
48 }
49
50 ready() {
51 super.ready();
52 this.graph = { version: -1, store: undefined };
53 this.graphView = (this.shadowRoot as ShadowRoot).getElementById("graphView") as Element;
54
55 this._onUrl(this.url); // todo: watch for changes and rebuild
56 if (this.expanded) {
57 this.redrawGraph();
58 }
59 }
60
61 toggleExpand() {
62 this.expanded = !this.expanded;
63 if (this.expanded) {
64 this.redrawGraph()
65 } else {
66 this.graphViewDirty = false;
67 render(null, this.graphView);
68 }
69 }
70
71 redrawGraph() {
72 this.graphViewDirty = true;
73 requestAnimationFrame(this._redrawLater.bind(this));
74 }
75
76 _onUrl(url: string) {
77 if (this.sg) { this.sg.close(); }
78 this.sg = new StreamedGraphClient(
79 url,
80 this.onGraphChanged.bind(this),
81 this.set.bind(this, 'status'),
82 [],//window.NS,
83 []
84 );
85 }
86
87 onGraphChanged() {
88 this.graph = {
89 version: this.graph.version + 1,
90 store: this.sg.store
91 };
92 if (this.expanded) {
93 this.redrawGraph();
94 }
95 }
96
97 _redrawLater() {
98 if (!this.graphViewDirty) return;
99
100 if ((this.graph as VersionedGraph).store && this.graph.store) {
101 render(new GraphView(this.url, this.graph.store).makeTemplate(), this.graphView);
102 this.graphViewDirty = false;
103 } else {
104 render(html`<span>waiting for data...</span>`, this.graphView);
105 }
106 }
107 }