-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathValidator.js
147 lines (127 loc) · 4.4 KB
/
Validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { heightOfTopBands } from 'chor-js/lib/util/BandUtil';
import {
isChoreoActivity
} from './util/ValidatorUtil';
import eventBasedGatewayConstraint from './constraints/EventBasedGatewayConstraint';
import participantNameConstraint from './constraints/ParticipantNameConstraint';
import simpleFlowConstraint from './constraints/SimpleFlowConstraint';
import subChoreoParticipantsConstraint from './constraints/SubChoreoParticipantsConstraint';
import callChoreoParticipantsBijectivityConstraint from './constraints/CallChoreoParticipantsBijectivityConstraint';
import noCyclicCallChoreosConstraint from './constraints/NoCyclicCallChoreosConstraint';
const CONSTRAINTS = [
eventBasedGatewayConstraint,
participantNameConstraint,
simpleFlowConstraint,
subChoreoParticipantsConstraint,
callChoreoParticipantsBijectivityConstraint,
noCyclicCallChoreosConstraint,
];
const LEVEL = {
WARNING: 0,
ERROR: 1
};
const LEVEL_STRING = [
'warning',
'error'
];
export default function Reporter(viewer) {
this.overlays = viewer.get('overlays');
this.elementRegistry = viewer.get('elementRegistry');
this.annotations = []; // List of all annotations
this.shapeAnnotations = {}; // Mapping from shape id to annotations
this.overlayIDs = [];
}
Reporter.prototype.validateDiagram = function() {
this.clearAll();
this.elementRegistry.forEach(shape => CONSTRAINTS.forEach(constraint => constraint(shape, this)));
this.showAnnotations();
};
Reporter.prototype.addAnnotationToShape = function(shape, annotation) {
if (this.shapeAnnotations[shape.id]) {
this.shapeAnnotations[shape.id].push(annotation);
} else {
this.shapeAnnotations[shape.id] = [ annotation ];
}
};
Reporter.prototype.report = function(shape, level, text) {
this.annotations.push({ level: level, text: text, shape: shape });
};
Reporter.prototype.warn = function(shape, text) {
this.report(shape, LEVEL.WARNING, text);
};
Reporter.prototype.error = function(shape, text) {
this.report(shape, LEVEL.ERROR, text);
};
Reporter.prototype.showAnnotations = function() {
function findVisibleParent(shape) {
while (shape.hidden) {
shape = shape.parent;
}
return shape;
}
this.annotations.forEach(annotation => this.addAnnotationToShape(
findVisibleParent(annotation.shape),
annotation
));
Object.keys(this.shapeAnnotations).forEach(id => {
const shape = this.elementRegistry.get(id);
this.displayOnShape(shape, this.shapeAnnotations[id]);
});
};
/**
* Display a list of annotations on their shape.
* @param annotations {Array}
*/
Reporter.prototype.displayOnShape = function(shape, annotations) {
let childAnnotations = annotations.filter(annotation => annotation.shape !== shape);
let parentAnnotations = annotations
.filter(annotation => annotation.shape === shape)
.sort((a,b) => b.level - a.level);
const topOffset = isChoreoActivity(shape) ? heightOfTopBands(shape) : -7;
const level = Math.max(...annotations.map(annotation => annotation.level));
const count = annotations.length;
let infoText = '';
parentAnnotations.forEach(annotation => {
infoText += '<li class="li-' + LEVEL_STRING[annotation.level] + '">' + annotation.text + '</li>';
});
if (childAnnotations.length > 0) {
let errorCount = childAnnotations.filter(annotation => annotation.level === LEVEL.ERROR).length;
let warningCount = childAnnotations.filter(annotation => annotation.level === LEVEL.WARNING).length;
infoText += '<li class="li-note">';
if (errorCount > 0) {
infoText += '<b>' + errorCount + '</b> nested error';
if (errorCount > 1) {
infoText += 's';
}
if (warningCount > 0) {
infoText += ',';
}
}
if (warningCount > 0) {
infoText += warningCount + ' nested warning';
if (warningCount > 1) {
infoText += 's';
}
}
infoText += '</li>';
}
let html = '<div class="val-' + LEVEL_STRING[level] + '">';
if (count > 1) {
html += '<div class="validation-count">' + count + '</div>';
}
html += '<ul class="validation-info">' + infoText + '</ul>';
html += '</div>';
const newOverlayId = this.overlays.add(shape.id, {
position: {
top: topOffset,
left: shape.width - 12
},
html: html
});
this.overlayIDs.push(newOverlayId);
};
Reporter.prototype.clearAll = function() {
this.overlayIDs.forEach(id => this.overlays.remove(id));
this.shapeAnnotations = {};
this.annotations = [];
};