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(render): display template name in header #627

Merged
merged 1 commit into from
Mar 29, 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
23 changes: 21 additions & 2 deletions src/render/PanelHeaderProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
isInterrupting
} from 'bpmn-js/lib/util/DiUtil';

import {
useService
} from '../hooks';

import iconsByType from '../icons';

export function getConcreteType(element) {
Expand Down Expand Up @@ -79,6 +83,18 @@ export const PanelHeaderProvider = {
},

getTypeLabel: (element) => {

// eslint-disable-next-line react-hooks/rules-of-hooks
const elementTemplates = useService('elementTemplates', false);

if (elementTemplates) {
const template = getTemplate(element, elementTemplates);

if (template && template.name) {
return template.name;
}
}

const concreteType = getConcreteType(element);

return concreteType
Expand Down Expand Up @@ -137,12 +153,15 @@ function isConditionalFlow(element) {
return businessObject.conditionExpression && is(sourceBusinessObject, 'bpmn:Activity');
}


// helpers //////////
function isPlane(element) {

// Backwards compatibility for bpmn-js<8
const di = element && (element.di || getBusinessObject(element).di);

return is(di, 'bpmndi:BPMNPlane');
}

function getTemplate(element, elementTemplates) {
const templateId = elementTemplates._getTemplateId(element);
return templateId && elementTemplates.get(templateId);
}
113 changes: 110 additions & 3 deletions test/spec/PanelHeaderProvider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import {
getConcreteType
} from 'src/render/PanelHeaderProvider';

import {
BpmnPropertiesPanelContext
} from 'src/context';

insertCoreStyles();

const noop = () => {};
Expand Down Expand Up @@ -178,6 +182,86 @@ describe('<PanelHeaderProvider>', function() {
});


describe('#getTypeLabel', function() {

it('should get element type - no element templates', function() {

// given
const element = createElement('bpmn:Task', {
name: 'foo'
});

// when
const result = createHeader({ container, element });

const typeNode = domQuery('.bio-properties-panel-header-type', result.container);

// then
expect(typeNode).to.exist;
expect(typeNode.innerText).to.equal('TASK');
});


it('should get element type - no template name', function() {

// given
const element = createElement('bpmn:Task', {
name: 'foo'
});

const elementTemplates = {
get: () => { return { id: 'foo' }; },
getTemplateId: () => 'foo'
};

const context = {
getService: () => {
return new ElementTemplates(elementTemplates);
}
};

// when
const result = createHeader({ container, element, context });

const typeNode = domQuery('.bio-properties-panel-header-type', result.container);

// then
expect(typeNode).to.exist;
expect(typeNode.innerText).to.equal('TASK');
});


it('should get element template name', function() {

// given
const element = createElement('bpmn:Task', {
name: 'foo'
});

const elementTemplates = {
get: () => { return { id: 'foo', name: 'Template Task' }; },
getTemplateId: () => 'foo'
};

const context = {
getService: () => {
return new ElementTemplates(elementTemplates);
}
};

// when
const result = createHeader({ container, element, context });

const typeNode = domQuery('.bio-properties-panel-header-type', result.container);

// then
expect(typeNode).to.exist;
expect(typeNode.innerText).to.equal('TEMPLATE TASK');
});

});


describe('#getConcreteType', function() {

it('should get type - task', async function() {
Expand Down Expand Up @@ -591,17 +675,40 @@ describe('<PanelHeaderProvider>', function() {

// helpers //////////////////////////

class ElementTemplates {
constructor(options = {}) {
const {
get,
getTemplateId
} = options;

this._getFn = get;
this._getTemplateIdFn = getTemplateId;
}

get(element) {
return this._getFn ? this._getFn(element) : null;
}

_getTemplateId(element) {
return this._getTemplateIdFn ? this._getTemplateIdFn(element) : null;
}
}

function createHeader(options = {}) {
const {
element = noopElement,
headerProvider = PanelHeaderProvider,
context = { getService: noop },
container
} = options;

return render(
<Header
element={ element }
headerProvider={ headerProvider } />,
<BpmnPropertiesPanelContext.Provider value={ context }>
<Header
element={ element }
headerProvider={ headerProvider } />
</BpmnPropertiesPanelContext.Provider>,
{
container
}
Expand Down