Skip to content

Commit

Permalink
Pull standard-template-service from plugins and pass templates in
Browse files Browse the repository at this point in the history
  • Loading branch information
piemonkey committed Oct 2, 2023
1 parent fc625a1 commit 817f18d
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-pants-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frontend-gelinkt-notuleren': patch
---

Pass standard templates into standard template plugin instead of it loading them itself
1 change: 0 additions & 1 deletion app/components/document-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default class DocumentCreatorComponent extends Component {
@tracked errorSaving;

@service store;
@service standardTemplatePlugin;
@service currentSession;
@service documentService;

Expand Down
1 change: 1 addition & 0 deletions app/controllers/agendapoints/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export default class AgendapointsEditController extends Controller {
@service router;
@service documentService;
@service currentSession;

@tracked hasDocumentValidationErrors = false;
@tracked displayDeleteModal = false;
@tracked _editorDocument;
Expand Down
9 changes: 8 additions & 1 deletion app/models/template.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export { default } from '@lblod/ember-rdfa-editor-lblod-plugins/models/template';
import Model, { attr } from '@ember-data/model';
export default class TemplateModel extends Model {
@attr title;
@attr('string-set', { defaultValue: () => [] }) matches;
@attr body;
@attr('string-set', { defaultValue: () => [] }) contexts;
@attr('string-set', { defaultValue: () => [] }) disabledInContexts;
}
4 changes: 4 additions & 0 deletions app/routes/agendapoints/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { service } from '@ember/service';
export default class AgendapointsEditRoute extends Route {
@service currentSession;
@service router;
@service standardTemplate;

beforeModel(transition) {
if (!this.currentSession.canWrite) {
Expand All @@ -16,10 +17,13 @@ export default class AgendapointsEditRoute extends Route {
async model() {
const { documentContainer, returnToMeeting } =
this.modelFor('agendapoints');
const standardTemplates =
await this.standardTemplate.fetchTemplates.perform();
return {
documentContainer,
editorDocument: await documentContainer.get('currentVersion'),
returnToMeeting,
standardTemplates,
};
}

Expand Down
7 changes: 3 additions & 4 deletions app/routes/inbox/agendapoints/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { service } from '@ember/service';
export default class InboxAgendapointsNewRoute extends Route {
@service currentSession;
@service router;
@service standardTemplatePlugin;
@service standardTemplate;

beforeModel() {
if (!this.currentSession.canWrite) {
Expand All @@ -13,9 +13,8 @@ export default class InboxAgendapointsNewRoute extends Route {
}

async model() {
const templates =
await this.standardTemplatePlugin.fetchTemplates.perform();
return this.standardTemplatePlugin.templatesForContext(templates, [
const templates = await this.standardTemplate.fetchTemplates.perform();
return this.standardTemplate.templatesForContext(templates, [
'http://data.vlaanderen.be/ns/besluit#BehandelingVanAgendapunt',
]);
}
Expand Down
4 changes: 4 additions & 0 deletions app/routes/regulatory-statements/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class RegulatoryStatementsEditRoute extends Route {
@service currentSession;
@service store;
@service router;
@service standardTemplate;

beforeModel(transition) {
if (!this.currentSession.canWrite) {
Expand All @@ -21,10 +22,13 @@ export default class RegulatoryStatementsEditRoute extends Route {
params.id,
{ include: 'status' },
);
const standardTemplates =
await this.standardTemplate.fetchTemplates.perform();
const currentVersion = await container.get('currentVersion');
return RSVP.hash({
documentContainer: container,
editorDocument: currentVersion,
standardTemplates,
});
}
setupController(controller, model) {
Expand Down
51 changes: 51 additions & 0 deletions app/services/standard-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Service, { inject as service } from '@ember/service';
import { task, waitForProperty } from 'ember-concurrency';
import { tracked } from '@glimmer/tracking';

const BLACKLISTED_TEMPLATES = new Set(['Citeeropschrift']);

export default class StandardTemplateService extends Service {
@service store;
@tracked templates;

constructor(...args) {
super(...args);
this.loadTemplates().catch((err) =>
console.error('Error loading standard templates', err),
);
}

fetchTemplates = task(async () => {
await waitForProperty(this, 'templates');
return this.templates;
});

async loadTemplates() {
const templates = await this.store.query('template', {
fields: { templates: 'title,contexts,matches,disabled-in-contexts' },
});
this.templates = templates.filter(
(template) => !BLACKLISTED_TEMPLATES.has(template.title),
);
}

/**
Filter the valid templates for a context.
@method templatesForContext
@param {Array} Array of templates
@param {Array} The path of rdfaContext objects from the root till the current context
@return {Array} Array of templates (filtered)
@private
*/
templatesForContext(templates, rdfaTypes) {
const isMatchingForContext = (template) => {
return (
rdfaTypes.filter((e) => template.get('contexts').includes(e)).length >
0 &&
rdfaTypes.filter((e) => template.get('disabledInContexts').includes(e))
.length === 0
);
};
return templates.filter(isMatchingForContext);
}
}
5 changes: 4 additions & 1 deletion app/templates/agendapoints/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@
@controller={{this.controller}}
@options={{this.config.roadsignRegulation}}
/>
<StandardTemplatePlugin::Card @controller={{this.controller}} />
<StandardTemplatePlugin::Card
@controller={{this.controller}}
@templates={{this.model.standardTemplates}}
/>
{{#if (feature-flag 'regulatoryStatements')}}
<EditorPlugins::RegulatoryStatements::SidebarInsert
@controller={{this.controller}}
Expand Down
5 changes: 4 additions & 1 deletion app/templates/regulatory-statements/edit.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@
@controller={{this.controller}}
@options={{this.config.date}}
/>
<StandardTemplatePlugin::Card @controller={{this.controller}} />
<StandardTemplatePlugin::Card
@controller={{this.controller}}
@templates={{this.model.standardTemplates}}
/>
<TemplateCommentsPlugin::Insert @controller={{this.controller}} />
<VariablePlugin::Address::Insert @controller={{this.controller}} />
</:sidebarCollapsible>
Expand Down

0 comments on commit 817f18d

Please sign in to comment.