-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathRDFForm.ts
More file actions
137 lines (122 loc) · 4.65 KB
/
Copy pathRDFForm.ts
File metadata and controls
137 lines (122 loc) · 4.65 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { property, state } from 'lit/decorators.js'
import { html } from 'lit/html.js'
import { customElement, WebComponent } from '@/lib/components'
import ns from '../../lib/ns'
import { loadDocument, sortBySequence } from '../../lib/forms/rdfFormsHelper'
import { sym, Namespace } from 'rdflib'
import { store } from 'solid-logic'
import '@/components/rdf-input'
@customElement('solid-ui-rdf-form')
export default class RDFForm extends WebComponent {
@state()
private accessor _parsedUrl: URL | null = null
@state()
private accessor _parsedUrl2: URL | null = null
@property({ type: String })
accessor whichForm = 'this'
@property({ type: String })
accessor rdfTurtleFormatSource = ''
@property({ type: String })
accessor rdfName = ''
@property({ type: String })
set rdfURI (value: string) {
try {
this._parsedUrl = new URL(value)
} catch {
this._parsedUrl = null // Handle invalid URL
}
}
get rdfURI (): string {
return this._parsedUrl ? this._parsedUrl.href : ''
}
@property({ type: String })
accessor whichSubject = 'me'
@property({ type: String })
accessor subjectTurtleFormatSource = ''
@property({ type: String })
accessor subjectName = ''
@property({ type: String })
set subjectURI (value: string) {
try {
this._parsedUrl2 = new URL(value)
} catch {
this._parsedUrl2 = null // Handle invalid URL
}
}
get subjectURI (): string {
return this._parsedUrl2 ? this._parsedUrl2.href : ''
}
render () {
// TODO: detect format
loadDocument(store, this.rdfTurtleFormatSource, this.rdfName, this.rdfURI) // load form
loadDocument(store, this.subjectTurtleFormatSource, this.subjectName, this.subjectURI) // load data
const document = sym(this.rdfURI) // rdflib NamedNode for the document
const exactForm = this.whichForm // If there are more 'a ui:Form' elements in a form file
const formThis = Namespace(this.rdfURI + '#')(exactForm) // NamedNode for #this in the form
const parts = store.each(formThis, ns.ui('parts'), null, document)
const partsBySequence = sortBySequence(store, parts)
const partItems = (partsBySequence || []).flatMap(item => {
if (item && typeof item === 'object' && 'elements' in item && Array.isArray((item as any).elements)) {
return (item as any).elements
}
return [item]
})
const uiFields = partItems.map(item => {
const types = store.each(item as any, ns.rdf('type'), null, document)
const typeNode = types[0]
const value = typeNode ? ((typeNode as any).value || String(typeNode)) : ((item as any).value || String(item))
const hashIndex = value.lastIndexOf('#')
return {
value: item,
fieldValue: hashIndex >= 0 ? value.slice(hashIndex + 1) : value
}
})
const me = Namespace(this.subjectURI + '#')(this.whichSubject)
return html`
${uiFields.map(part => {
switch (part.fieldValue) {
case 'PhoneField':
case 'EmailField':
case 'ColorField':
case 'DateField':
case 'DateTimeField':
case 'TimeField':
case 'NumericField':
case 'IntegerField':
case 'DecimalField':
case 'FloatField':
case 'TextField':
case 'SingleLineTextField':
case 'NamedNodeURIField': {
return html` <solid-ui-rdf-input
.store=${store}
.formSubject=${sym(part.value)}
.dataSubject=${me}
></solid-ui-rdf-input>`
}
case 'MultiLineTextField':
return html`<input .rdf=${part}></input>`
case 'BooleanField':
return html`<input .rdf=${part}></input>`
case 'TristateField':
return html`<input .rdf=${part}></input>`
case 'Classifier':
return html`<input .rdf=${part}></input>`
case 'Choice':
return html`<input .rdf=${part}></input>`
case 'Multiple':
return html`<input .rdf=${part}></input>`
case 'Options':
return html`<input .rdf=${part}></input>`
case 'AutocompleteField':
return html`<input .rdf=${part}></input>`
case 'Comment':
case 'Heading':
return html`<input .rdf=${part}></input>`
default:
return html`<div>Unknown part type: ${part}</div>`
}
})}
`
}
}