Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ElementTemplates#applyTemplate API #624

Merged
merged 2 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/provider/cloud-element-templates/ElementTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { default as DefaultElementTemplates } from '../element-templates/Element
* Registry for element templates.
*/
export default class ElementTemplates extends DefaultElementTemplates {
constructor(templateElementFactory) {
super();
this._templates = {};
constructor(templateElementFactory, commandStack) {
super(commandStack);

barmac marked this conversation as resolved.
Show resolved Hide resolved
this._commandStack = commandStack;
this._templateElementFactory = templateElementFactory;
}

Expand Down Expand Up @@ -39,6 +40,26 @@ export default class ElementTemplates extends DefaultElementTemplates {
return element;
}

/**
* Apply element template to a given element.
*
* @param {djs.model.Base} element
* @param {ElementTemplate} newTemplate
*
* @return {djs.model.Base} the updated element
*/
applyTemplate(element, newTemplate) {

const oldTemplate = this.get(element);

this._commandStack.execute('propertiesPanel.zeebe.changeTemplate', {
element: element,
newTemplate,
oldTemplate
});

return element;
}
}

ElementTemplates.$inject = [ 'templateElementFactory' ];
ElementTemplates.$inject = [ 'templateElementFactory', 'commandStack' ];
11 changes: 2 additions & 9 deletions src/provider/cloud-element-templates/util/templateUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ export function unlinkTemplate(element, injector) {
}

export function updateTemplate(element, newTemplate, injector) {
const commandStack = injector.get('commandStack'),
elementTemplates = injector.get('elementTemplates');
const elementTemplates = injector.get('elementTemplates');

const oldTemplate = elementTemplates.get(element);

commandStack.execute('propertiesPanel.zeebe.changeTemplate', {
element: element,
newTemplate,
oldTemplate
});
return elementTemplates.applyTemplate(element, newTemplate);
Comment on lines -17 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it 👍

}
27 changes: 26 additions & 1 deletion src/provider/element-templates/ElementTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import { isAny } from 'bpmn-js/lib/features/modeling/util/ModelingUtil';
* Registry for element templates.
*/
export default class ElementTemplates {
constructor() {
constructor(commandStack) {
this._commandStack = commandStack;

this._templates = {};
}

Expand Down Expand Up @@ -109,4 +111,27 @@ export default class ElementTemplates {
_getTemplateVersion(element) {
return getTemplateVersion(element);
}

/**
* Apply element template to a given element.
*
* @param {djs.model.Base} element
* @param {ElementTemplate} newTemplate
*
* @return {djs.model.Base} the updated element
*/
applyTemplate(element, newTemplate) {

const oldTemplate = this.get(element);

this._commandStack.execute('propertiesPanel.camunda.changeTemplate', {
element: element,
newTemplate,
oldTemplate
});

return element;
}
}

ElementTemplates.$inject = [ 'commandStack' ];
11 changes: 2 additions & 9 deletions src/provider/element-templates/util/templateUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,9 @@ export function removeTemplate(element, injector) {
}

export function updateTemplate(element, newTemplate, injector) {
const commandStack = injector.get('commandStack'),
elementTemplates = injector.get('elementTemplates');
const elementTemplates = injector.get('elementTemplates');

const oldTemplate = elementTemplates.get(element);

commandStack.execute('propertiesPanel.camunda.changeTemplate', {
element: element,
newTemplate,
oldTemplate
});
return elementTemplates.applyTemplate(element, newTemplate);
}

export function getVersionOrDateFromTemplate(template) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import TestContainer from 'mocha-test-container-support';

import {
isAny
} from 'bpmn-js/lib/util/ModelUtil';

import {
bootstrapModeler,
createCanvasEvent as canvasEvent,
Expand Down Expand Up @@ -191,4 +195,29 @@ describe('provider/cloud-element-templates - ElementTemplates', function() {

});


describe('applyTemplate', function() {

it('should set template on element', inject(function(elementRegistry, elementTemplates) {

// given
const task = elementRegistry.get('Task_1');

const template = elementTemplates.getAll().find(
t => isAny(task, t.appliesTo)
);

// assume
expect(template).to.exist;

// when
const updatedTask = elementTemplates.applyTemplate(task, template);

// then
expect(updatedTask).to.exist;
expect(elementTemplates.get(updatedTask)).to.equal(template);
}));

});

});
29 changes: 29 additions & 0 deletions test/spec/provider/element-templates/ElementTemplates.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import TestContainer from 'mocha-test-container-support';

import {
isAny
} from 'bpmn-js/lib/util/ModelUtil';

import { bootstrapModeler, inject } from 'test/TestHelper';

import coreModule from 'bpmn-js/lib/core';
Expand Down Expand Up @@ -197,4 +201,29 @@ describe('provider/element-templates - ElementTemplates', function() {

});


describe('applyTemplate', function() {

it('should set template on element', inject(function(elementRegistry, elementTemplates) {

// given
const task = elementRegistry.get('Task_1');

const template = elementTemplates.getAll().find(
t => isAny(task, t.appliesTo)
);

// assume
expect(template).to.exist;

// when
const updatedTask = elementTemplates.applyTemplate(task, template);

// then
expect(updatedTask).to.exist;
expect(elementTemplates.get(updatedTask)).to.equal(template);
}));

});

});