comparison src/graph_view.ts @ 96:4d19759d0d9a

factor NodeDisplay
author drewp@bigasterisk.com
date Thu, 13 Jan 2022 21:32:18 -0800
parents a5f53d397526
children 26c55d5d5202
comparison
equal deleted inserted replaced
95:47d3b5a5bd5e 96:4d19759d0d9a
1 import { html, TemplateResult } from "lit"; 1 import { html, TemplateResult } from "lit";
2 import { DataFactory, Literal, NamedNode, Quad, Store, Term, Util } from "n3"; 2 import { DataFactory, Literal, NamedNode, Quad, Store, Term, Util } from "n3";
3 import { NodeDisplay } from "./NodeDisplay";
3 import { SuffixLabels } from "./suffixLabels"; 4 import { SuffixLabels } from "./suffixLabels";
4 import { 5 import {
5 groupByRdfType, 6 groupByRdfType,
6 MultiSubjsTypeBlockLayout, 7 MultiSubjsTypeBlockLayout,
7 predsForSubj, 8 predsForSubj,
16 (Literal.prototype as any).hashCode = () => 0; 17 (Literal.prototype as any).hashCode = () => 0;
17 } 18 }
18 if ((NamedNode.prototype as any).hashCode === undefined) { 19 if ((NamedNode.prototype as any).hashCode === undefined) {
19 (NamedNode.prototype as any).hashCode = () => 0; 20 (NamedNode.prototype as any).hashCode = () => 0;
20 } 21 }
21 class NodeDisplay {
22 labels: SuffixLabels;
23 constructor(labels: SuffixLabels) {
24 this.labels = labels;
25 }
26 render(n: Term | NamedNode): TemplateResult {
27 if (Util.isLiteral(n)) {
28 n = n as Literal;
29 let dtPart: any = "";
30 if (
31 n.datatype &&
32 n.datatype.value != "http://www.w3.org/2001/XMLSchema#string" && // boring
33 n.datatype.value !=
34 "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString" // boring
35 ) {
36 dtPart = html`
37 ^^<span class="literalType"> ${this.render(n.datatype)} </span>
38 `;
39 }
40 return html` <span class="literal">${n.value}${dtPart}</span> `;
41 }
42
43 if (Util.isNamedNode(n)) {
44 n = n as NamedNode;
45 let shortened = false;
46 let uriValue: string = n.value;
47 for (let [long, short] of [
48 ["http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:"],
49 ["http://www.w3.org/2000/01/rdf-schema#", "rdfs:"],
50 ["http://purl.org/dc/elements/1.1/", "dc:"],
51 ["http://www.w3.org/2001/XMLSchema#", "xsd:"],
52 ]) {
53 if (uriValue.startsWith(long)) {
54 uriValue = short + uriValue.substr(long.length);
55 shortened = true;
56 break;
57 }
58 }
59 if (!shortened) {
60 let dn: string | undefined = this.labels.getLabelForNode(uriValue);
61 if (dn === undefined) {
62 throw new Error(`dn=${dn}`);
63 }
64 uriValue = dn;
65 }
66
67 return html` <a class="graphUri" href="${n.value}">${uriValue}</a> `;
68 }
69
70 return html` [${n.termType} ${n.value}] `;
71 }
72 }
73
74 export class GraphView { 22 export class GraphView {
75 url: string; 23 url: string;
76 view: View; 24 view: View;
77 graph: Store; 25 graph: Store;
78 nodeDisplay: NodeDisplay; 26 nodeDisplay: NodeDisplay;