comparison src/index.ts @ 36:8b4dc9e87b56

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