@lblod/ember-rdfa-editor-lblod-plugins 33.4.0 → 34.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/addon/components/variable-plugin/codelist/edit.gts +56 -9
  3. package/addon/components/variable-plugin/codelist/{insert.ts → insert.gts} +52 -7
  4. package/addon/components/variable-plugin/codelist/nodeview.gts +49 -0
  5. package/addon/components/variable-plugin/location/edit.ts +3 -4
  6. package/addon/plugins/roadsign-regulation-plugin/actions/insert-measure.ts +1 -1
  7. package/addon/plugins/roadsign-regulation-plugin/schemas/variable.ts +1 -1
  8. package/addon/plugins/variable-plugin/actions/create-codelist-variable.ts +57 -40
  9. package/addon/plugins/variable-plugin/actions/create-legacy-codelist-variable.ts +101 -0
  10. package/addon/plugins/variable-plugin/utils/codelist-utils.ts +38 -3
  11. package/addon/plugins/variable-plugin/utils/fetch-data.ts +37 -15
  12. package/addon/plugins/variable-plugin/variables/codelist.ts +101 -175
  13. package/addon/plugins/variable-plugin/variables/legacy-codelist.ts +261 -0
  14. package/declarations/addon/components/variable-plugin/codelist/edit.d.ts +2 -0
  15. package/declarations/addon/components/variable-plugin/codelist/insert.d.ts +15 -9
  16. package/declarations/addon/components/variable-plugin/codelist/nodeview.d.ts +18 -0
  17. package/declarations/addon/plugins/roadsign-regulation-plugin/queries/variable.d.ts +6 -6
  18. package/declarations/addon/plugins/roadsign-regulation-plugin/schemas/variable-instance.d.ts +25 -25
  19. package/declarations/addon/plugins/roadsign-regulation-plugin/schemas/variable.d.ts +36 -36
  20. package/declarations/addon/plugins/variable-plugin/actions/create-codelist-variable.d.ts +20 -11
  21. package/declarations/addon/plugins/variable-plugin/actions/create-legacy-codelist-variable.d.ts +28 -0
  22. package/declarations/addon/plugins/variable-plugin/utils/codelist-utils.d.ts +7 -0
  23. package/declarations/addon/plugins/variable-plugin/utils/fetch-data.d.ts +3 -2
  24. package/declarations/addon/plugins/variable-plugin/variables/codelist.d.ts +3 -1
  25. package/declarations/addon/plugins/variable-plugin/variables/legacy-codelist.d.ts +2 -0
  26. package/docs/plugins/variable.md +1 -0
  27. package/package.json +1 -1
  28. package/addon/components/variable-plugin/codelist/insert.hbs +0 -31
package/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # @lblod/ember-rdfa-editor-lblod-plugins
2
2
 
3
+ ## 34.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [`49c09bd`](https://github.com/lblod/ember-rdfa-editor-lblod-plugins/commit/49c09bd0836b639afbfb8056807c3e8e765349f2) Thanks [@elpoelma](https://github.com/elpoelma)! - codelist: fix issue with `selectedCodelistOption` in case of multi-select codelist
8
+
9
+ ## 34.0.1
10
+
11
+ ### Patch Changes
12
+
13
+ - [#615](https://github.com/lblod/ember-rdfa-editor-lblod-plugins/pull/615) [`29ed983`](https://github.com/lblod/ember-rdfa-editor-lblod-plugins/commit/29ed98336174e12667195f4b28cb9502b10cb479) Thanks [@elpoelma](https://github.com/elpoelma)! - Add missing `toDOM` method to `codelist_option` nodespec
14
+
15
+ ## 34.0.0
16
+
17
+ ### Major Changes
18
+
19
+ - [#610](https://github.com/lblod/ember-rdfa-editor-lblod-plugins/pull/610) [`2f70345`](https://github.com/lblod/ember-rdfa-editor-lblod-plugins/commit/2f7034552c477920d75ebf9dfff0f84bac7fba9a) Thanks [@elpoelma](https://github.com/elpoelma)! - Introduce new `codelist` variable which stores either a single or multiple `codelist_option` nodes.
20
+ Each `codelist_option` node is a resource node which represents a selected codelist-option, and a variable instance.
21
+ The `codelist` variable node itself is more of a container node and does not store any RDFa.
22
+ The old `codelist` variable node is renamed to `legacy_codelist`, and can still be used with the existing `codelist-edit` widget.
23
+ `legacy_codelist` nodes will not be automatically converted to new `codelist` nodes, as we lack the necessary information to do so (codelist-option URIs).
24
+
3
25
  ## 33.4.0
4
26
 
5
27
  ### Minor Changes
@@ -13,9 +13,11 @@ import {
13
13
  import { MULTI_SELECT_CODELIST_TYPE } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/utils/constants';
14
14
  import { NodeSelection } from '@lblod/ember-rdfa-editor';
15
15
  import { trackedFunction } from 'reactiveweb/function';
16
- import { updateCodelistVariable } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/utils/codelist-utils';
16
+ import {
17
+ updateCodelistVariable,
18
+ updateCodelistVariableLegacy,
19
+ } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/utils/codelist-utils';
17
20
  import { Option } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
18
- import { tracked } from '@glimmer/tracking';
19
21
  import AuCard from '@appuniversum/ember-appuniversum/components/au-card';
20
22
  import AuHeading from '@appuniversum/ember-appuniversum/components/au-heading';
21
23
  import AuLabel from '@appuniversum/ember-appuniversum/components/au-label';
@@ -23,6 +25,7 @@ import PowerSelectMultiple from 'ember-power-select/components/power-select-mult
23
25
  import AuButton from '@appuniversum/ember-appuniversum/components/au-button';
24
26
  import { AlertTriangleIcon } from '@appuniversum/ember-appuniversum/components/icons/alert-triangle';
25
27
  import AuAlert from '@appuniversum/ember-appuniversum/components/au-alert';
28
+ import { tracked } from '@glimmer/tracking';
26
29
 
27
30
  export type CodelistEditOptions = {
28
31
  endpoint: string;
@@ -35,17 +38,26 @@ type Sig = {
35
38
  };
36
39
 
37
40
  export default class CodelistEditComponent extends Component<Sig> {
38
- @tracked selectedCodelistOption?: CodeListOption | CodeListOption[];
41
+ @tracked
42
+ selectedCodelistOption?: CodeListOption | CodeListOption[];
39
43
 
40
44
  get controller() {
41
45
  return this.args.controller;
42
46
  }
43
47
 
48
+ get isLegacyCodelist() {
49
+ return (
50
+ this.selectedCodelist?.node.type ===
51
+ this.controller.schema.nodes.legacy_codelist
52
+ );
53
+ }
54
+
44
55
  get selectedCodelist() {
45
56
  const { selection } = this.controller.mainEditorState;
46
57
  if (
47
58
  selection instanceof NodeSelection &&
48
- selection.node.type === this.controller.schema.nodes.codelist
59
+ (selection.node.type === this.controller.schema.nodes.codelist ||
60
+ selection.node.type === this.controller.schema.nodes.legacy_codelist)
49
61
  ) {
50
62
  const codelist = {
51
63
  node: selection.node,
@@ -86,6 +98,10 @@ export default class CodelistEditComponent extends Component<Sig> {
86
98
  return this.selectedCodelist?.node.attrs.label as string | undefined;
87
99
  }
88
100
 
101
+ get schema() {
102
+ return this.args.controller.schema;
103
+ }
104
+
89
105
  codelistOptions = trackedFunction(this, async () => {
90
106
  let result: CodeListOptions | undefined;
91
107
  if (this.source && this.codelistUri) {
@@ -95,6 +111,29 @@ export default class CodelistEditComponent extends Component<Sig> {
95
111
  // Normally we'd do this with a `trackedReset`, but this gave us `write-after-read` dev-errors at the time of writing this.
96
112
  // TODO: convert this back to a `trackedReset` (or an alternative) once possible.
97
113
  this.selectedCodelistOption = undefined;
114
+ const codelistNode = this.selectedCodelist?.node;
115
+ let multiSelect: boolean;
116
+ const localSelectionStyle = this.selectedCodelist?.node.attrs
117
+ .selectionStyle as string;
118
+ if (localSelectionStyle) {
119
+ multiSelect = localSelectionStyle === 'multi';
120
+ } else {
121
+ multiSelect = result?.type === MULTI_SELECT_CODELIST_TYPE;
122
+ }
123
+ if (
124
+ !this.isLegacyCodelist &&
125
+ codelistNode &&
126
+ codelistNode.children.length > 0
127
+ ) {
128
+ const options = codelistNode.children.map((child) => ({
129
+ uri: child.attrs['subject'],
130
+ label: child.textContent,
131
+ }));
132
+ this.selectedCodelistOption = multiSelect ? options : options[0];
133
+ } else {
134
+ this.selectedCodelistOption = undefined;
135
+ }
136
+
98
137
  return result;
99
138
  });
100
139
 
@@ -112,11 +151,19 @@ export default class CodelistEditComponent extends Component<Sig> {
112
151
  if (!this.selectedCodelist || !this.selectedCodelistOption) {
113
152
  return;
114
153
  }
115
- updateCodelistVariable(
116
- this.selectedCodelist,
117
- this.selectedCodelistOption,
118
- this.controller,
119
- );
154
+ if (this.isLegacyCodelist) {
155
+ updateCodelistVariableLegacy(
156
+ this.selectedCodelist,
157
+ this.selectedCodelistOption,
158
+ this.controller,
159
+ );
160
+ } else {
161
+ updateCodelistVariable(
162
+ this.selectedCodelist,
163
+ this.selectedCodelistOption,
164
+ this.controller,
165
+ );
166
+ }
120
167
  }
121
168
 
122
169
  @action
@@ -10,6 +10,13 @@ import { service } from '@ember/service';
10
10
  import IntlService from 'ember-intl/services/intl';
11
11
  import { trackedFunction } from 'reactiveweb/function';
12
12
  import { replaceSelectionWithAndSelectNode } from '@lblod/ember-rdfa-editor-lblod-plugins/commands';
13
+ import PowerSelect from 'ember-power-select/components/power-select';
14
+ import AuFormRow from '@appuniversum/ember-appuniversum/components/au-form-row';
15
+ import AuButton from '@appuniversum/ember-appuniversum/components/au-button';
16
+ import { on } from '@ember/modifier';
17
+ import { not } from 'ember-truth-helpers';
18
+ import t from 'ember-intl/helpers/t';
19
+ import LabelInput from '../utils/label-input';
13
20
  import { createCodelistVariable } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/actions/create-codelist-variable';
14
21
 
15
22
  export type CodelistInsertOptions = {
@@ -25,14 +32,14 @@ type Args = {
25
32
 
26
33
  interface SelectStyle {
27
34
  label: string;
28
- value: string;
35
+ value: 'single' | 'multi';
29
36
  }
30
37
 
31
38
  export default class CodelistInsertComponent extends Component<Args> {
32
39
  @service declare intl: IntlService;
33
40
  @tracked selectedCodelist?: CodeList;
34
- @tracked label?: string;
35
- @tracked selectedStyleValue = 'single';
41
+ @tracked label: string = '';
42
+ @tracked selectedStyleValue: 'single' | 'multi' = 'single';
36
43
 
37
44
  get controller() {
38
45
  return this.args.controller;
@@ -58,11 +65,11 @@ export default class CodelistInsertComponent extends Component<Args> {
58
65
  const singleSelect = {
59
66
  label: this.intl.t('variable.codelist.single-select'),
60
67
  value: 'single',
61
- };
68
+ } as const;
62
69
  const multiSelect = {
63
70
  label: this.intl.t('variable.codelist.multi-select'),
64
71
  value: 'multi',
65
- };
72
+ } as const;
66
73
  return [singleSelect, multiSelect];
67
74
  }
68
75
 
@@ -84,6 +91,9 @@ export default class CodelistInsertComponent extends Component<Args> {
84
91
  @action
85
92
  insert() {
86
93
  const codelistResource = this.selectedCodelist?.uri;
94
+ if (!codelistResource) {
95
+ return;
96
+ }
87
97
  const label =
88
98
  this.label ??
89
99
  this.selectedCodelist?.label ??
@@ -96,10 +106,10 @@ export default class CodelistInsertComponent extends Component<Args> {
96
106
  selectionStyle: this.selectedStyleValue,
97
107
  codelist: codelistResource,
98
108
  source,
99
- label,
109
+ label: label ?? this.selectedCodelist?.label,
100
110
  });
101
111
 
102
- this.label = undefined;
112
+ this.label = '';
103
113
  this.controller.doCommand(replaceSelectionWithAndSelectNode(node), {
104
114
  view: this.controller.mainEditorView,
105
115
  });
@@ -114,4 +124,39 @@ export default class CodelistInsertComponent extends Component<Args> {
114
124
  selectStyle(style: SelectStyle) {
115
125
  this.selectedStyleValue = style.value;
116
126
  }
127
+
128
+ <template>
129
+ {{#if this.codelistData.value}}
130
+ <PowerSelect
131
+ @allowClear={{false}}
132
+ @searchEnabled={{true}}
133
+ @searchField='label'
134
+ @options={{this.codelistData.value}}
135
+ @selected={{this.selectedCodelist}}
136
+ @onChange={{this.selectCodelist}}
137
+ as |codelist|
138
+ >
139
+ {{codelist.label}}
140
+ </PowerSelect>
141
+ <PowerSelect
142
+ @allowClear={{false}}
143
+ @searchEnabled={{false}}
144
+ @options={{this.selectionStyles}}
145
+ @selected={{this.selectedStyle}}
146
+ @onChange={{this.selectStyle}}
147
+ as |style|
148
+ >
149
+ {{style.label}}
150
+ </PowerSelect>
151
+ <AuFormRow>
152
+ <LabelInput @label={{this.label}} @updateLabel={{this.updateLabel}} />
153
+ </AuFormRow>
154
+ <AuButton
155
+ {{on 'click' this.insert}}
156
+ @disabled={{not this.selectedCodelist}}
157
+ >
158
+ {{t 'variable-plugin.button'}}
159
+ </AuButton>
160
+ {{/if}}
161
+ </template>
117
162
  }
@@ -0,0 +1,49 @@
1
+ import Component from '@glimmer/component';
2
+ import {
3
+ DecorationSource,
4
+ PNode,
5
+ SayController,
6
+ SayView,
7
+ } from '@lblod/ember-rdfa-editor';
8
+ import getClassnamesFromNode from '@lblod/ember-rdfa-editor/utils/get-classnames-from-node';
9
+ import AuPill from '@appuniversum/ember-appuniversum/components/au-pill';
10
+ import { on } from '@ember/modifier';
11
+
12
+ type Args = {
13
+ getPos: () => number | undefined;
14
+ node: PNode;
15
+ selectNode: () => void;
16
+ updateAttribute: (attr: string, value: unknown) => void;
17
+ controller: SayController;
18
+ view: SayView;
19
+ selected: boolean;
20
+ contentDecorations?: DecorationSource;
21
+ };
22
+
23
+ export default class CodelistNodeviewComponent extends Component<Args> {
24
+ get filled() {
25
+ return this.args.node.childCount !== 0;
26
+ }
27
+
28
+ get humanReadableCodelistOption() {
29
+ if (this.filled) {
30
+ return this.args.node.children
31
+ .map((child) => child.textContent)
32
+ .join(', ');
33
+ } else {
34
+ return this.args.node.attrs['label'];
35
+ }
36
+ }
37
+
38
+ get class() {
39
+ return getClassnamesFromNode(this.args.node);
40
+ }
41
+
42
+ <template>
43
+ <AuPill class='{{this.class}} say-pill atomic' {{on 'click' @selectNode}}>
44
+ <span class='{{unless this.filled "unfilled-variable"}}'>
45
+ {{this.humanReadableCodelistOption}}
46
+ </span>
47
+ </AuPill>
48
+ </template>
49
+ }
@@ -8,9 +8,8 @@ import {
8
8
  import { MULTI_SELECT_CODELIST_TYPE } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/utils/constants';
9
9
  import { findParentNode } from '@curvenote/prosemirror-utils';
10
10
  import { NodeSelection } from '@lblod/ember-rdfa-editor';
11
- import { unwrap } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
12
11
  import { trackedFunction } from 'reactiveweb/function';
13
- import { updateCodelistVariable } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/utils/codelist-utils';
12
+ import { updateCodelistVariableLegacy } from '@lblod/ember-rdfa-editor-lblod-plugins/plugins/variable-plugin/utils/codelist-utils';
14
13
  import { tracked } from '@glimmer/tracking';
15
14
  import {
16
15
  ZONALITY_OPTIONS,
@@ -52,7 +51,7 @@ export default class LocationEditComponent extends Component<Args> {
52
51
  if (!this.selectedLocation || !this.selectedLocationOption) {
53
52
  return;
54
53
  }
55
- updateCodelistVariable(
54
+ updateCodelistVariableLegacy(
56
55
  this.selectedLocation,
57
56
  this.selectedLocationOption,
58
57
  this.controller,
@@ -126,8 +125,8 @@ export default class LocationEditComponent extends Component<Args> {
126
125
  : this.options.nonZonalLocationCodelistUri,
127
126
  );
128
127
  result.options = result.options.map((option) => ({
128
+ uri: option.uri,
129
129
  label: option.label,
130
- value: unwrap(option.value),
131
130
  }));
132
131
  // This a workaround/hack to be able to reset the `selected` option after the `locationOptions` change.
133
132
  // Normally we'd do this with a `trackedReset`, but this gave us `write-after-read` dev-errors at the time of writing this.
@@ -38,7 +38,6 @@ import { createTextVariable } from '../../variable-plugin/actions/create-text-va
38
38
  import { generateVariableInstanceUri } from '../../variable-plugin/utils/variable-helpers';
39
39
  import { createNumberVariable } from '../../variable-plugin/actions/create-number-variable';
40
40
  import { createDateVariable } from '../../variable-plugin/actions/create-date-variable';
41
- import { createCodelistVariable } from '../../variable-plugin/actions/create-codelist-variable';
42
41
  import { createClassicLocationVariable } from '../../variable-plugin/actions/create-classic-location-variable';
43
42
  import { isTrafficSignal, TrafficSignal } from '../schemas/traffic-signal';
44
43
  import { MobilityMeasureDesign } from '../schemas/mobility-measure-design';
@@ -46,6 +45,7 @@ import {
46
45
  isVariableInstance,
47
46
  VariableInstance,
48
47
  } from '../schemas/variable-instance';
48
+ import { createCodelistVariable } from '../../variable-plugin/actions/create-codelist-variable';
49
49
 
50
50
  type InsertMeasureArgs = {
51
51
  arDesignUri?: string;
@@ -3,7 +3,7 @@ import { z } from 'zod';
3
3
  const BaseVariableSchema = z.object({
4
4
  uri: z.string(),
5
5
  label: z.string(),
6
- source: z.string().optional(),
6
+ source: z.string(),
7
7
  });
8
8
  export const TextVariableSchema = BaseVariableSchema.extend({
9
9
  type: z.literal('text'),
@@ -1,53 +1,44 @@
1
1
  import { Schema } from '@lblod/ember-rdfa-editor';
2
2
  import {
3
3
  DCT,
4
- MOBILITEIT,
5
4
  RDF,
5
+ SKOS,
6
6
  VARIABLES,
7
- XSD,
8
7
  } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
9
8
  import { AllOrNone } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/types';
10
9
  import {
11
10
  FullTriple,
12
11
  IncomingTriple,
12
+ OutgoingTriple,
13
13
  } from '@lblod/ember-rdfa-editor/core/rdfa-processor';
14
14
  import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
15
15
 
16
16
  type CreateCodelistVariableArgs = {
17
17
  schema: Schema;
18
- value?: string;
19
18
  } & CreateCodelistVariableAttrsArgs;
20
19
 
21
20
  export function createCodelistVariable(args: CreateCodelistVariableArgs) {
22
- const { schema, value, label } = args;
21
+ const { schema } = args;
23
22
  const attrs = createCodelistVariableAttrs(args);
24
- return schema.nodes.codelist.create(
25
- attrs,
26
- value
27
- ? schema.text(value)
28
- : schema.node('placeholder', {
29
- placeholderText: label,
30
- }),
31
- );
23
+ return schema.nodes.codelist.create(attrs);
32
24
  }
33
25
 
34
26
  type CreateCodelistVariableAttrsArgs = {
27
+ selectionStyle?: 'single' | 'multi';
35
28
  label?: string;
36
- source?: string;
37
- codelist?: string;
38
- selectionStyle?: string;
29
+ source: string;
30
+ codelist: string;
39
31
  } & AllOrNone<{ variable: string; variableInstance: string }>;
40
32
 
41
33
  export function createCodelistVariableAttrs({
42
- variable,
43
- variableInstance,
34
+ selectionStyle,
44
35
  label,
45
36
  source,
46
37
  codelist,
47
- selectionStyle,
38
+ variable,
39
+ variableInstance,
48
40
  }: CreateCodelistVariableAttrsArgs) {
49
41
  const externalTriples: FullTriple[] = [];
50
- const backlinks: IncomingTriple[] = [];
51
42
  if (variable) {
52
43
  externalTriples.push(
53
44
  {
@@ -66,34 +57,60 @@ export function createCodelistVariableAttrs({
66
57
  object: sayDataFactory.literal('codelist'),
67
58
  },
68
59
  );
69
- if (codelist) {
70
- externalTriples.push({
71
- subject: sayDataFactory.namedNode(variableInstance),
72
- predicate: MOBILITEIT('codelijst').full,
73
- object: sayDataFactory.namedNode(codelist),
74
- });
75
- }
76
- if (source) {
77
- externalTriples.push({
78
- subject: sayDataFactory.namedNode(variableInstance),
79
- predicate: DCT('source').full,
80
- object: sayDataFactory.namedNode(source),
81
- });
82
- }
83
- backlinks.push({
84
- subject: sayDataFactory.resourceNode(variableInstance),
85
- predicate: RDF('value').full,
86
- });
87
60
  }
88
61
 
89
62
  return {
90
63
  rdfaNodeType: 'literal',
91
- datatype: XSD('string').namedNode,
92
64
  externalTriples,
93
- backlinks,
65
+ selectionStyle,
94
66
  source,
95
67
  codelist,
96
68
  label,
97
- selectionStyle,
69
+ variable,
70
+ variableInstance,
71
+ };
72
+ }
73
+
74
+ type CreateCodelistOptionNodeArgs = {
75
+ schema: Schema;
76
+ text: string;
77
+ } & CreateCodelistOptionNodeAttrsArgs;
78
+
79
+ export function createCodelistOptionNode(args: CreateCodelistOptionNodeArgs) {
80
+ const { schema, text } = args;
81
+ const attrs = createCodelistOptionNodeAttrs(args);
82
+ return schema.nodes.codelist_option.create(attrs, schema.text(text));
83
+ }
84
+
85
+ type CreateCodelistOptionNodeAttrsArgs = {
86
+ subject: string;
87
+ text: string;
88
+ variableInstance?: string;
89
+ };
90
+
91
+ function createCodelistOptionNodeAttrs({
92
+ subject,
93
+ text,
94
+ variableInstance,
95
+ }: CreateCodelistOptionNodeAttrsArgs) {
96
+ const backlinks: IncomingTriple[] = [];
97
+ if (variableInstance) {
98
+ backlinks.push({
99
+ subject: sayDataFactory.resourceNode(variableInstance),
100
+ predicate: RDF('value').full,
101
+ });
102
+ }
103
+ const properties: OutgoingTriple[] = [
104
+ {
105
+ predicate: SKOS('prefLabel').full,
106
+ object: sayDataFactory.literal(text),
107
+ },
108
+ ];
109
+
110
+ return {
111
+ rdfaNodeType: 'resource',
112
+ subject,
113
+ properties,
114
+ backlinks,
98
115
  };
99
116
  }
@@ -0,0 +1,101 @@
1
+ import { Schema } from '@lblod/ember-rdfa-editor';
2
+ import {
3
+ DCT,
4
+ MOBILITEIT,
5
+ RDF,
6
+ VARIABLES,
7
+ XSD,
8
+ } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants';
9
+ import { AllOrNone } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/types';
10
+ import {
11
+ FullTriple,
12
+ IncomingTriple,
13
+ } from '@lblod/ember-rdfa-editor/core/rdfa-processor';
14
+ import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
15
+
16
+ type CreateLegacyCodelistVariableArgs = {
17
+ schema: Schema;
18
+ value?: string;
19
+ } & CreateLegacyCodelistVariableAttrsArgs;
20
+
21
+ export function createLegacyCodelistVariable(
22
+ args: CreateLegacyCodelistVariableArgs,
23
+ ) {
24
+ const { schema, value, label } = args;
25
+ const attrs = createLegacyCodelistVariableAttrs(args);
26
+ return schema.nodes.legacy_codelist.create(
27
+ attrs,
28
+ value
29
+ ? schema.text(value)
30
+ : schema.node('placeholder', {
31
+ placeholderText: label,
32
+ }),
33
+ );
34
+ }
35
+
36
+ type CreateLegacyCodelistVariableAttrsArgs = {
37
+ label?: string;
38
+ source?: string;
39
+ codelist?: string;
40
+ selectionStyle?: string;
41
+ } & AllOrNone<{ variable: string; variableInstance: string }>;
42
+
43
+ export function createLegacyCodelistVariableAttrs({
44
+ variable,
45
+ variableInstance,
46
+ label,
47
+ source,
48
+ codelist,
49
+ selectionStyle,
50
+ }: CreateLegacyCodelistVariableAttrsArgs) {
51
+ const externalTriples: FullTriple[] = [];
52
+ const backlinks: IncomingTriple[] = [];
53
+ if (variable) {
54
+ externalTriples.push(
55
+ {
56
+ subject: sayDataFactory.namedNode(variableInstance),
57
+ predicate: RDF('type').full,
58
+ object: sayDataFactory.namedNode(VARIABLES('VariableInstance').full),
59
+ },
60
+ {
61
+ subject: sayDataFactory.namedNode(variableInstance),
62
+ predicate: VARIABLES('instanceOf').full,
63
+ object: sayDataFactory.namedNode(variable),
64
+ },
65
+ {
66
+ subject: sayDataFactory.namedNode(variableInstance),
67
+ predicate: DCT('type').full,
68
+ object: sayDataFactory.literal('codelist'),
69
+ },
70
+ );
71
+ if (codelist) {
72
+ externalTriples.push({
73
+ subject: sayDataFactory.namedNode(variableInstance),
74
+ predicate: MOBILITEIT('codelijst').full,
75
+ object: sayDataFactory.namedNode(codelist),
76
+ });
77
+ }
78
+ if (source) {
79
+ externalTriples.push({
80
+ subject: sayDataFactory.namedNode(variableInstance),
81
+ predicate: DCT('source').full,
82
+ object: sayDataFactory.namedNode(source),
83
+ });
84
+ }
85
+ backlinks.push({
86
+ subject: sayDataFactory.resourceNode(variableInstance),
87
+ predicate: RDF('value').full,
88
+ });
89
+ }
90
+
91
+ return {
92
+ rdfaNodeType: 'literal',
93
+ datatype: XSD('string').namedNode,
94
+ externalTriples,
95
+ backlinks,
96
+ source,
97
+ codelist,
98
+ label,
99
+ selectionStyle,
100
+ };
101
+ }
@@ -1,8 +1,9 @@
1
1
  // This file contains helpers for both the codelist and location variable types
2
2
 
3
- import { unwrap } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
4
3
  import { CodeListOption } from './fetch-data';
5
4
  import { PNode, ProseParser, SayController } from '@lblod/ember-rdfa-editor';
5
+ import { createCodelistOptionNode } from '../actions/create-codelist-variable';
6
+ import { unwrap } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/option';
6
7
 
7
8
  /**
8
9
  *
@@ -27,12 +28,46 @@ export function updateCodelistVariable(
27
28
  },
28
29
  selectedOption: CodeListOption | CodeListOption[],
29
30
  controller: SayController,
31
+ ) {
32
+ const selectedOptions = Array.isArray(selectedOption)
33
+ ? selectedOption
34
+ : [selectedOption];
35
+ const variableInstance = selectedCodelist.node.attrs['variableInstance'] as
36
+ | string
37
+ | undefined;
38
+ const codelistOptionNodes = selectedOptions.map((option) =>
39
+ createCodelistOptionNode({
40
+ schema: controller.schema,
41
+ text: option.label,
42
+ subject: option.uri,
43
+ variableInstance,
44
+ }),
45
+ );
46
+ const range = {
47
+ from: selectedCodelist.pos + 1,
48
+ to: selectedCodelist.pos + selectedCodelist.node.nodeSize - 1,
49
+ };
50
+ controller.withTransaction((tr) => {
51
+ return tr.replaceWith(range.from, range.to, codelistOptionNodes);
52
+ });
53
+ }
54
+
55
+ /**
56
+ * @deprecated use `updateCodelistVariable` instead
57
+ */
58
+ export function updateCodelistVariableLegacy(
59
+ selectedCodelist: {
60
+ node: PNode;
61
+ pos: number;
62
+ },
63
+ selectedOption: CodeListOption | CodeListOption[],
64
+ controller: SayController,
30
65
  ) {
31
66
  let htmlToInsert: string;
32
67
  if (Array.isArray(selectedOption)) {
33
- htmlToInsert = selectedOption.map((option) => option.value).join(', ');
68
+ htmlToInsert = selectedOption.map((option) => option.label).join(', ');
34
69
  } else {
35
- htmlToInsert = unwrap(selectedOption.value);
70
+ htmlToInsert = unwrap(selectedOption.label);
36
71
  }
37
72
  htmlToInsert = wrapVariableInHighlight(htmlToInsert);
38
73
  const domParser = new DOMParser();