diff --git a/.changeset/warm-months-rest.md b/.changeset/warm-months-rest.md new file mode 100644 index 00000000..4754c8e7 --- /dev/null +++ b/.changeset/warm-months-rest.md @@ -0,0 +1,5 @@ +--- +"frontend-reglementaire-bijlage": minor +--- + +Add a card to regenerate variable uris diff --git a/app/components/regenerate-variable-uris.hbs b/app/components/regenerate-variable-uris.hbs new file mode 100644 index 00000000..e0e8f481 --- /dev/null +++ b/app/components/regenerate-variable-uris.hbs @@ -0,0 +1,22 @@ + + + + {{t 'regenerate-variable-uris.modal-title'}} + + + + + {{t 'regenerate-variable-uris.button'}} + + + diff --git a/app/components/regenerate-variable-uris.js b/app/components/regenerate-variable-uris.js new file mode 100644 index 00000000..bb5a1ac7 --- /dev/null +++ b/app/components/regenerate-variable-uris.js @@ -0,0 +1,52 @@ +import Component from '@glimmer/component'; +import { action } from '@ember/object'; +import { getOutgoingTriple } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/namespace'; +import { EXT } from '@lblod/ember-rdfa-editor-lblod-plugins/utils/constants'; +import { v4 as uuidv4 } from 'uuid'; +import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory'; + +const VARIABLE_TYPES = [ + 'address', + 'date', + 'text_variable', + 'oslo_location', + 'person_variable', + 'autofilled_variable', + 'number', + 'codelist', +]; +export default class RegenerateVariableUrisComponent extends Component { + get controller() { + return this.args.controller; + } + + @action + regenerate() { + const doc = this.controller.mainEditorState.doc; + const nodesReversed = []; + doc.descendants((node, pos) => { + if (VARIABLE_TYPES.includes(node.type.name)) { + nodesReversed.unshift({ node, pos }); + } + }); + for (let { node, pos } of nodesReversed) { + this.recreateUris(node, pos); + } + } + recreateUris(node, pos) { + const newAttrs = { ...node.attrs }; + if (newAttrs.subject) { + newAttrs.subject = `http://data.lblod.info/mappings/--ref-uuid4-${uuidv4()}`; + } + const instanceTriple = getOutgoingTriple(newAttrs, EXT('instance')); + if (instanceTriple) { + let recreatedUri = `http://data.lblod.info/variables/--ref-uuid4-${uuidv4()}`; + instanceTriple.object = sayDataFactory.namedNode(recreatedUri); + } + this.controller.withTransaction((tr) => { + tr.setNodeAttribute(pos, 'subject', newAttrs.subject); + tr.setNodeAttribute(pos, 'properties', newAttrs.properties); + return tr; + }); + } +} diff --git a/app/templates/snippet-management/edit/edit-snippet.hbs b/app/templates/snippet-management/edit/edit-snippet.hbs index 233ef83b..bd59e13a 100644 --- a/app/templates/snippet-management/edit/edit-snippet.hbs +++ b/app/templates/snippet-management/edit/edit-snippet.hbs @@ -143,6 +143,7 @@ @options={{this.config.structures}} /> + {{/unless}} + {{/if}} + + {{! VariablePlugin::TemplateVariableCard is not added on purpose as an RB-document creator should not be able to set a "default" codelist option.}} diff --git a/translations/en-us.yaml b/translations/en-us.yaml index ff84e39b..e30b518c 100644 --- a/translations/en-us.yaml +++ b/translations/en-us.yaml @@ -22,8 +22,8 @@ codelist: options: "Options" id: Id types: - singleSelect: 'Single select' - multiSelect: 'Multi select' + singleSelect: "Single select" + multiSelect: "Multi select" options: label: Label label-explanation: Optional shorter description @@ -216,3 +216,7 @@ editor: search-form: aria-label: Search + +regenerate-variable-uris: + modal-title: Regenerate variable URIs + button: Regenerate diff --git a/translations/nl-BE.yaml b/translations/nl-BE.yaml index f1f379c7..64bc96e2 100644 --- a/translations/nl-BE.yaml +++ b/translations/nl-BE.yaml @@ -22,8 +22,8 @@ codelist: options: "Items" id: Id types: - singleSelect: 'Single select' - multiSelect: 'Multi select' + singleSelect: "Single select" + multiSelect: "Multi select" options: label: Label label-explanation: Optionele kortere weergave @@ -102,7 +102,7 @@ template-management: save: Opslaan cancel: Terug remove-modal: - title: Weet u zeker dat u dit sjabloon wilt verwijderen? + title: Weet u zeker dat u dit sjabloon wilt verwijderen? remove: Sjabloon verwijderen cancel: Terug template-edit: @@ -210,3 +210,7 @@ editor: search-form: aria-label: Zoek + +regenerate-variable-uris: + modal-title: Variabele URI's opnieuw genereren + button: Regenereer