1 import inherits from 'inherits';
3 import BaseRenderer from 'diagram-js/lib/draw/BaseRenderer';
5 import { componentsToPath, createLine } from 'diagram-js/lib/util/RenderUtil';
14 * A renderer that knows how to render custom elements.
16 export default function CustomRenderer(eventBus, styles) {
17 BaseRenderer.call(this, eventBus, 2000);
19 var computeStyle = styles.computeStyle;
21 this.drawTriangle = function(p, side) {
22 var halfSide = side / 2,
26 points = [halfSide, 0, side, side, 0, side];
28 attrs = computeStyle(attrs, {
34 var polygon = svgCreate('polygon');
40 svgAttr(polygon, attrs);
42 svgAppend(p, polygon);
47 this.getTrianglePath = function(element) {
50 width = element.width,
51 height = element.height;
54 ['M', x + width / 2, y],
55 ['l', width / 2, height],
60 return componentsToPath(trianglePath);
63 this.drawCircle = function(p, width, height) {
67 var attrs = computeStyle(attrs, {
73 var circle = svgCreate('circle');
78 r: Math.round((width + height) / 4)
81 svgAttr(circle, attrs);
88 this.getCirclePath = function(shape) {
89 var cx = shape.x + shape.width / 2,
90 cy = shape.y + shape.height / 2,
91 radius = shape.width / 2;
96 ['a', radius, radius, 0, 1, 1, 0, 2 * radius],
97 ['a', radius, radius, 0, 1, 1, 0, -2 * radius],
101 return componentsToPath(circlePath);
104 this.drawCustomConnection = function(p, element) {
105 var attrs = computeStyle(attrs, {
110 return svgAppend(p, createLine(element.waypoints, attrs));
113 this.getCustomConnectionPath = function(connection) {
114 var waypoints = connection.waypoints.map(function(p) {
115 return p.original || p;
118 var connectionPath = [['M', waypoints[0].x, waypoints[0].y]];
120 waypoints.forEach(function(waypoint, index) {
122 connectionPath.push(['L', waypoint.x, waypoint.y]);
126 return componentsToPath(connectionPath);
130 inherits(CustomRenderer, BaseRenderer);
132 CustomRenderer.$inject = ['eventBus', 'styles'];
134 CustomRenderer.prototype.canRender = function(element) {
135 return /^custom:/.test(element.type);
138 CustomRenderer.prototype.drawShape = function(p, element) {
139 var type = element.type;
141 if (type === 'custom:triangle') {
142 return this.drawTriangle(p, element.width);
145 if (type === 'custom:circle') {
146 return this.drawCircle(p, element.width, element.height);
150 CustomRenderer.prototype.getShapePath = function(shape) {
151 var type = shape.type;
153 if (type === 'custom:triangle') {
154 return this.getTrianglePath(shape);
157 if (type === 'custom:circle') {
158 return this.getCirclePath(shape);
162 CustomRenderer.prototype.drawConnection = function(p, element) {
163 var type = element.type;
165 if (type === 'custom:connection') {
166 return this.drawCustomConnection(p, element);
170 CustomRenderer.prototype.getConnectionPath = function(connection) {
171 var type = connection.type;
173 if (type === 'custom:connection') {
174 return this.getCustomConnectionPath(connection);