|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 Conduction / Dossiq Contributors |
| 3 | + * SPDX-License-Identifier: EUPL-1.2 |
| 4 | + * |
| 5 | + * Where a field's choices come from, when two places could answer. |
| 6 | + * |
| 7 | + * A choice list used to be typed into the attribute that uses it. Two case |
| 8 | + * types that both ask for a reden each carried their own copy, and a |
| 9 | + * municipality-wide list of wijken or afhandelkanalen was retyped per type and |
| 10 | + * drifted. OpenRegister holds those lists as SKOS concept schemes, so a |
| 11 | + * property can point at one instead of carrying a copy. |
| 12 | + * |
| 13 | + * That leaves two sources on one definition, and the rule between them has to |
| 14 | + * live in one place or the form and the index will each guess. This module is |
| 15 | + * that place: the scheme rules when it is set, the inline list rules when it |
| 16 | + * is not, and a definition carrying both is an authoring mistake that is said |
| 17 | + * out loud rather than resolved in silence. |
| 18 | + * |
| 19 | + * dossiq declares the binding and does not render the picker. The picker is |
| 20 | + * the platform's, and the case form reads the binding once OpenRegister |
| 21 | + * publishes `x-openregister-concept-scheme` in its property vocabulary. Until |
| 22 | + * then the binding is stored and shown, and no value is coerced. |
| 23 | + * |
| 24 | + * @see openspec/changes/code-lists-from-concepts/design.md |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * The scheme is the source of options. |
| 29 | + * |
| 30 | + * @type {string} |
| 31 | + */ |
| 32 | +export const SOURCE_SCHEME = 'scheme' |
| 33 | + |
| 34 | +/** |
| 35 | + * The inline `enumValues` list is the source of options. |
| 36 | + * |
| 37 | + * @type {string} |
| 38 | + */ |
| 39 | +export const SOURCE_INLINE = 'inline' |
| 40 | + |
| 41 | +/** |
| 42 | + * The field takes any answer its type allows. |
| 43 | + * |
| 44 | + * @type {string} |
| 45 | + */ |
| 46 | +export const SOURCE_NONE = 'none' |
| 47 | + |
| 48 | +/** |
| 49 | + * The scheme a definition is bound to, trimmed, or an empty string. |
| 50 | + * |
| 51 | + * A reference typed with a stray space is the same reference. An answer that |
| 52 | + * is not a string at all is no binding: it is read as absent rather than |
| 53 | + * stringified, because `[object Object]` is not a scheme anyone can resolve. |
| 54 | + * |
| 55 | + * @param {object} definition The property definition. |
| 56 | + * @return {string} The scheme reference, or an empty string. |
| 57 | + * @spec openspec/changes/code-lists-from-concepts/specs/property-definition-management/spec.md |
| 58 | + */ |
| 59 | +export function schemeOf(definition) { |
| 60 | + const scheme = definition?.conceptScheme |
| 61 | + if (typeof scheme !== 'string') { |
| 62 | + return '' |
| 63 | + } |
| 64 | + return scheme.trim() |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * The inline choices a definition carries. |
| 69 | + * |
| 70 | + * @param {object} definition The property definition. |
| 71 | + * @return {string[]} The values, empty when there are none. |
| 72 | + * @spec openspec/changes/code-lists-from-concepts/specs/property-definition-management/spec.md |
| 73 | + */ |
| 74 | +export function inlineValuesOf(definition) { |
| 75 | + const values = definition?.enumValues |
| 76 | + if (!Array.isArray(values)) { |
| 77 | + return [] |
| 78 | + } |
| 79 | + return values.filter((value) => typeof value === 'string' && value.trim()) |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Whether both sources are set on one definition. |
| 84 | + * |
| 85 | + * @param {object} definition The property definition. |
| 86 | + * @return {boolean} True when a scheme and an inline list are both present. |
| 87 | + * @spec openspec/changes/code-lists-from-concepts/specs/property-definition-management/spec.md |
| 88 | + */ |
| 89 | +export function hasCompetingSources(definition) { |
| 90 | + return schemeOf(definition) !== '' && inlineValuesOf(definition).length > 0 |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Which source rules for one definition, and what it offers. |
| 95 | + * |
| 96 | + * One answer, read by the authoring form and the index alike, so the warning |
| 97 | + * and the rendering cannot disagree about which list a handler will see. |
| 98 | + * |
| 99 | + * @param {object} definition The property definition. |
| 100 | + * @return {{source: string, scheme: string, values: string[], competing: boolean}} |
| 101 | + * The ruling source, the scheme it names, the inline values it would have |
| 102 | + * used, and whether the definition carries both. |
| 103 | + * @spec openspec/changes/code-lists-from-concepts/specs/property-definition-management/spec.md |
| 104 | + */ |
| 105 | +export function optionSourceFor(definition) { |
| 106 | + const scheme = schemeOf(definition) |
| 107 | + const values = inlineValuesOf(definition) |
| 108 | + const competing = scheme !== '' && values.length > 0 |
| 109 | + if (scheme !== '') { |
| 110 | + return { source: SOURCE_SCHEME, scheme, values, competing } |
| 111 | + } |
| 112 | + if (values.length > 0) { |
| 113 | + return { source: SOURCE_INLINE, scheme: '', values, competing } |
| 114 | + } |
| 115 | + return { source: SOURCE_NONE, scheme: '', values: [], competing } |
| 116 | +} |
0 commit comments