Skip to content

Commit

Permalink
Merge pull request #302 from lblod/utils/imported-resources-fixes
Browse files Browse the repository at this point in the history
Quickly connect all articles in a snippet to an imported resource
  • Loading branch information
elpoelma authored Nov 25, 2024
2 parents 3dd3654 + 90c0f2e commit 3db1714
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-falcons-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"frontend-reglementaire-bijlage": minor
---

Add utility button to quickly connect all articles in a snippet to an imported resource
29 changes: 29 additions & 0 deletions app/components/connect-articles-to-imported-resource.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<AuCard
@flex={{true}}
@divided={{true}}
@isOpenInitially={{true}}
@expandable={{true}}
@shadow={{true}}
@size='small'
as |c|
>
<c.header>
{{!-- template-lint-disable no-bare-strings --}}
<AuHeading @level='3' @skin='6'>
Connect articles to imported resource
</AuHeading>
</c.header>
<c.content>
<AuButton
@disabled={{this.docIsConnected}}
{{on 'click' this.connect}}
>
{{!-- template-lint-disable no-bare-strings --}}
{{#if this.docIsConnected}}
Connected
{{else}}
Connect
{{/if}}
</AuButton>
</c.content>
</AuCard>
66 changes: 66 additions & 0 deletions app/components/connect-articles-to-imported-resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { addProperty } from '@lblod/ember-rdfa-editor/commands/rdfa-commands/add-property';
import { sayDataFactory } from '@lblod/ember-rdfa-editor/core/say-data-factory';
import { IMPORTED_RESOURCES_ATTR } from '@lblod/ember-rdfa-editor/plugins/imported-resources';

export default class ConnectArticlesToImportedResourceComponent extends Component {
get controller() {
return this.args.controller;
}

get importedResource() {
return this.args.importedResource;
}

get nonConnectedArticleURIs() {
const doc = this.controller.mainEditorState.doc;
const articleURIs = new Set();
doc.descendants((node) => {
if (
node.type === this.controller.schema.nodes.structure &&
node.attrs.structureType === 'article' &&
!this.isConnected(node)
) {
articleURIs.add(node.attrs.subject);
}
});
return articleURIs;
}

get docIsConnected() {
return this.nonConnectedArticleURIs.size === 0;
}

@action
connect() {
const articleURIs = this.nonConnectedArticleURIs;
for (const uri of articleURIs) {
this.controller.doCommand(
addProperty({
resource: this.importedResource,
property: {
predicate: 'http://data.europa.eu/eli/ontology#has_part',
object: sayDataFactory.resourceNode(uri),
},
isNewImportedResource: !this.documentImportedResources.includes(
this.importedResource,
),
}),
);
}
}

get documentImportedResources() {
return this.controller.getDocumentAttribute(IMPORTED_RESOURCES_ATTR) ?? [];
}

isConnected(articleNode) {
return Boolean(
(articleNode.attrs.backlinks ?? []).some(
(backlink) =>
backlink.predicate == 'http://data.europa.eu/eli/ontology#has_part',
),
);
}
}
4 changes: 4 additions & 0 deletions app/controllers/snippet-management/edit/edit-snippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,8 @@ export default class SnippetManagementEditSnippetController extends Controller {
);
return saveCollatedImportedResources(list);
});

get importedDecisionUri() {
return `http://example.org/imported-decision-${this.model.snippetList.id}`;
}
}
4 changes: 4 additions & 0 deletions app/templates/snippet-management/edit/edit-snippet.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
<:aside>
{{#if this.editor}}
<Sidebar as |Sidebar|>
<ConnectArticlesToImportedResource
@controller={{this.editor}}
@importedResource={{this.importedDecisionUri}}
/>
<Sidebar.Collapsible
@title={{t "snippet-edit.sidebar.general-nodes"}}
@expandedInitially={{false}}
Expand Down

0 comments on commit 3db1714

Please sign in to comment.