Skip to content

Commit

Permalink
feat(renderer): allow passing of attrs
Browse files Browse the repository at this point in the history
closes #578
  • Loading branch information
marstamm committed Oct 25, 2021
1 parent 5bff78e commit c290d6a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lib/draw/BaseRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ export default function BaseRenderer(eventBus, renderPriority) {
eventBus.on([ 'render.shape', 'render.connection' ], renderPriority, function(evt, context) {
var type = evt.type,
element = context.element,
visuals = context.gfx;
visuals = context.gfx,
attrs = context.attrs;

if (self.canRender(element)) {
if (type === 'render.shape') {
return self.drawShape(visuals, element);
return self.drawShape(visuals, element, attrs);
} else {
return self.drawConnection(visuals, element);
return self.drawConnection(visuals, element, attrs);
}
}
});
Expand Down
12 changes: 7 additions & 5 deletions lib/draw/DefaultRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
create as svgCreate
} from 'tiny-svg';

import { assign } from 'min-dash';

import {
isFrameElement
} from '../util/Elements';
Expand Down Expand Up @@ -44,7 +46,7 @@ DefaultRenderer.prototype.canRender = function() {
return true;
};

DefaultRenderer.prototype.drawShape = function drawShape(visuals, element) {
DefaultRenderer.prototype.drawShape = function drawShape(visuals, element, attrs) {
var rect = svgCreate('rect');

svgAttr(rect, {
Expand All @@ -55,19 +57,19 @@ DefaultRenderer.prototype.drawShape = function drawShape(visuals, element) {
});

if (isFrameElement(element)) {
svgAttr(rect, this.FRAME_STYLE);
svgAttr(rect, assign({}, this.FRAME_STYLE, attrs || {}));
} else {
svgAttr(rect, this.SHAPE_STYLE);
svgAttr(rect, assign({}, this.SHAPE_STYLE, attrs || {}));
}

svgAppend(visuals, rect);

return rect;
};

DefaultRenderer.prototype.drawConnection = function drawConnection(visuals, connection) {
DefaultRenderer.prototype.drawConnection = function drawConnection(visuals, connection, attrs) {

var line = createLine(connection.waypoints, this.CONNECTION_STYLE);
var line = createLine(connection.waypoints, assign({}, this.CONNECTION_STYLE, attrs || {}));
svgAppend(visuals, line);

return line;
Expand Down
48 changes: 48 additions & 0 deletions test/spec/draw/DefaultRendererSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
bootstrapDiagram,
inject
} from 'test/TestHelper';
import { create } from 'tiny-svg';

import drawModule from 'lib/draw';

Expand Down Expand Up @@ -93,4 +94,51 @@ describe('draw - DefaultRenderer', function() {

});


describe('#drawShape', function() {

var gfx;

beforeEach(function() {
gfx = create('svg');
});


it('should have default style', inject(function(eventBus) {

// given
var element = {
id: 'shapeA',
x: 100, y: 100, width: 100, height: 100
};

// when
var shape = eventBus.fire('render.shape', { gfx: gfx, element: element });

// then
expect(shape.style.strokeWidth).to.eql('2px');

}));


it('should apply supplied styles', inject(function(eventBus) {

// given
var element = {
id: 'shapeA',
x: 100, y: 100, width: 100, height: 100
};

var attrs = { strokeWidth: '10px' };

// when
var shape = eventBus.fire('render.shape', { gfx: gfx, element: element, attrs: attrs });

// then
expect(shape.style.strokeWidth).to.eql('10px');

}));

});

});

0 comments on commit c290d6a

Please sign in to comment.