-
Notifications
You must be signed in to change notification settings - Fork 0
/
RdfTable.js
111 lines (93 loc) · 2.51 KB
/
RdfTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { html } from 'lit'
import * as ns from './src/namespaces.js'
import RdfData from './src/RdfData.js'
import style from './src/style.js'
import termElement from './src/termElement.js'
import toTree from './src/toTree.js'
class RdfTable extends RdfData {
static get properties () {
return {
resource: {
type: Object
},
scrollToSubject: {
type: Boolean,
default: false
}
}
}
static get styles () {
return [style()]
}
constructor () {
super()
this.subjects = null
}
requestUpdate (name, oldValue, options) {
if (name === 'resource') {
this.subjects = toTree({ ...this.resource, processList: false })
}
return super.requestUpdate(name, oldValue, options)
}
render () {
if (!this.subjects) {
return html`<div></div>`
}
const rows = this.subjects.flatMap(subject => {
const types = subject.types.map(type => {
const object = { term: type }
const predicate = { term: ns.rdf.type }
return this.renderRow({ object, predicate, subject, type })
})
const content = subject.predicates.flatMap(predicate => {
return predicate.objects.map(object => {
return this.renderRow({ object, predicate, subject })
})
})
return [...types, ...content]
})
return html`<table class="table table-bordered table-hover">
<thead>
<tr>
<th>subject</th>
<th>predicate</th>
<th>object</th>
</tr>
</thead>
<tbody>${rows}</tbody>
</table>`
}
renderRow ({ object, predicate, subject, type }) {
const subjectLink = termElement({
click: () => this.clickSubject({ subject }),
label: subject.label,
term: subject.term
})
const predicateLink = termElement({
click: () => this.clickPredicate({ predicate, subject }),
label: predicate.label,
term: predicate.term
})
let objectLink
if (type) {
objectLink = termElement({
click: () => this.clickType({ subject, type }),
label: object.label,
term: object.term
})
} else {
objectLink = termElement({
click: () => this.clickObject({ object, predicate, subject }),
label: object.label,
term: object.term
})
}
return html`
<tr>
<td data-subject="${subject.term.toCanonical()}">${subjectLink}</td>
<td>${predicateLink}</td>
<td>${objectLink}</td>
</tr>`
}
}
export default RdfTable