Skip to content

Commit 926337e

Browse files
author
Tim Berners-Lee
committed
setFieldStyle incorprats style from form and default params
1 parent 2a7db32 commit 926337e

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

src/widgets/forms/formStyle.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as ns from '../../ns'
2+
import { Node } from 'rdflib'
3+
import { kb } from '../../logic'
4+
import { fieldParams } from './fieldParams'
5+
6+
import { mostSpecificClassURI } from './fieldFunction'
7+
8+
const STYLE_URI_PREFIX = 'https://www.w3.org/ns/css/attribute#'
9+
10+
/* eslint-disable no-console */
11+
12+
export function setFieldStyle (ele:HTMLElement, field:Node) {
13+
const classUri = mostSpecificClassURI(field)
14+
const params = fieldParams[classUri] || {}
15+
16+
const style = kb.any(field as any, ns.ui('style'))
17+
if (!style) {
18+
if (params.style) {
19+
ele.setAttribute('style', params.style)
20+
}
21+
return
22+
}
23+
if (style.termType === 'Literal') {
24+
if (style) ele.setAttribute('style', style.value)
25+
} else {
26+
const sts = kb.statementsMatching(style as any, null, null, (field as any).doc())
27+
sts.forEach(st => {
28+
if (st.predicate.uri && st.predicate.uri.startsWith(STYLE_URI_PREFIX)) {
29+
const cssAttribute = st.predicate.uri.slice(STYLE_URI_PREFIX.length)
30+
try {
31+
ele.style[cssAttribute] = st.object.value
32+
} catch (err) {
33+
console.warn(`setFieldStyle: Error setting element style ${cssAttribute} to "${st.object.value}"`)
34+
console.warn(`setFieldStyle: ... Element tagName was "${ele.tagName || '???'}"`)
35+
}
36+
}
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)