Skip to content

Commit

Permalink
refactor: use map instead of switch to manage the marker painters (as…
Browse files Browse the repository at this point in the history
… to handle event icons)
  • Loading branch information
tbouffard committed Mar 5, 2024
1 parent f53e4ab commit f160555
Showing 1 changed file with 28 additions and 40 deletions.
68 changes: 28 additions & 40 deletions src/component/mxgraph/shape/activity-shapes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ export abstract class BaseActivityShape extends mxRectangleShape {
// The actual value is injected at runtime by BpmnCellRenderer
protected iconPainter: IconPainter = undefined;

private iconPainters = new Map<ShapeBpmnMarkerKind, (paintParameter: PaintParameter) => void>([
[ShapeBpmnMarkerKind.ADHOC, (paintParameter: PaintParameter) => this.iconPainter.paintAdHocIcon(paintParameter)],
[
ShapeBpmnMarkerKind.COMPENSATION,
(paintParameter: PaintParameter) =>
this.iconPainter.paintDoubleLeftArrowheadsIcon({
...paintParameter,
iconWidth: 16,
// ratioFromParent: 0.3,
// iconStyleConfig: {
// ...paintParameter.iconStyleConfig,
// strokeWidth: 1.5,
// // no impact, still filled
// isFilled: false,
// },
}),
],
[ShapeBpmnMarkerKind.EXPAND, (paintParameter: PaintParameter) => this.iconPainter.paintExpandIcon(paintParameter)],
[ShapeBpmnMarkerKind.LOOP, (paintParameter: PaintParameter) => this.iconPainter.paintLoopIcon(paintParameter)],
[ShapeBpmnMarkerKind.MULTI_INSTANCE_SEQUENTIAL, (paintParameter: PaintParameter) => this.iconPainter.paintSequentialMultiInstanceIcon(paintParameter)],
[ShapeBpmnMarkerKind.MULTI_INSTANCE_PARALLEL, (paintParameter: PaintParameter) => this.iconPainter.paintParallelMultiInstanceIcon(paintParameter)],
]);

constructor() {
super(undefined, undefined, undefined); // the configuration is passed with the styles at runtime
}
Expand All @@ -73,46 +96,11 @@ export abstract class BaseActivityShape extends mxRectangleShape {
setIconOriginFunct: getMarkerIconOriginFunction(orderedMarkers.length, index + 1),
};
paintParameter.canvas.save(); // ensure we can later restore the configuration
switch (marker) {
case ShapeBpmnMarkerKind.LOOP: {
this.iconPainter.paintLoopIcon(paintParameter);
break;
}
case ShapeBpmnMarkerKind.MULTI_INSTANCE_SEQUENTIAL: {
this.iconPainter.paintSequentialMultiInstanceIcon(paintParameter);
break;
}
case ShapeBpmnMarkerKind.MULTI_INSTANCE_PARALLEL: {
this.iconPainter.paintParallelMultiInstanceIcon(paintParameter);
break;
}
case ShapeBpmnMarkerKind.EXPAND: {
this.iconPainter.paintExpandIcon(paintParameter);
break;
}
case ShapeBpmnMarkerKind.ADHOC: {
this.iconPainter.paintAdHocIcon(paintParameter);
break;
}
case ShapeBpmnMarkerKind.COMPENSATION: {
// this.iconPainter.paintExpandIcon(paintParameter);
// TODO don't work, the icon dimensions are too large --> must ensure icon size + when used with loop marker, the marker is filled
// this.iconPainter.paintDoubleLeftArrowheadsIcon(paintParameter);
// this.iconPainter.paintAsteriskIcon({
this.iconPainter.paintDoubleLeftArrowheadsIcon({
...paintParameter,
iconWidth: 16,
// ratioFromParent: 0.3,
// iconStyleConfig: {
// ...paintParameter.iconStyleConfig,
// strokeWidth: 1.5,
// // no impact, still filled
// isFilled: false,
// },
});
break;
}
}

// inspired from event-shapes that could be updated to not used "IconPainter.emptyIcon" and not called the function if it is not defined
const markerPainter = this.iconPainters.get(marker as ShapeBpmnMarkerKind);
markerPainter?.(paintParameter);

// Restore original configuration to avoid side effects if the iconPainter changed the canvas configuration (colors, ....)
paintParameter.canvas.restore();
}
Expand Down

0 comments on commit f160555

Please sign in to comment.