-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathSpaceToolBehavior.js
207 lines (169 loc) · 5.44 KB
/
SpaceToolBehavior.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { forEach } from 'min-dash';
import { is } from '../../../util/ModelUtil';
import {
isExpanded,
isHorizontal
} from '../../../util/DiUtil';
import {
GROUP_MIN_DIMENSIONS,
LANE_MIN_DIMENSIONS,
VERTICAL_LANE_MIN_DIMENSIONS,
PARTICIPANT_MIN_DIMENSIONS,
VERTICAL_PARTICIPANT_MIN_DIMENSIONS,
SUB_PROCESS_MIN_DIMENSIONS,
TEXT_ANNOTATION_MIN_DIMENSIONS
} from './ResizeBehavior';
import { getChildLanes } from '../util/LaneUtil';
/**
* @typedef {import('diagram-js/lib/core/EventBus').default} EventBus
*
* @typedef {import('../../../model/Types').Shape} Shape
*/
var max = Math.max;
/**
* @param {EventBus} eventBus
*/
export default function SpaceToolBehavior(eventBus) {
eventBus.on('spaceTool.getMinDimensions', function(context) {
var shapes = context.shapes,
axis = context.axis,
start = context.start,
minDimensions = {};
forEach(shapes, function(shape) {
var id = shape.id;
if (is(shape, 'bpmn:Participant')) {
minDimensions[ id ] = getParticipantMinDimensions(shape, axis, start);
}
if (is(shape, 'bpmn:Lane')) {
minDimensions[ id ] = isHorizontal(shape) ? LANE_MIN_DIMENSIONS : VERTICAL_LANE_MIN_DIMENSIONS;
}
if (is(shape, 'bpmn:SubProcess') && isExpanded(shape)) {
minDimensions[ id ] = SUB_PROCESS_MIN_DIMENSIONS;
}
if (is(shape, 'bpmn:TextAnnotation')) {
minDimensions[ id ] = TEXT_ANNOTATION_MIN_DIMENSIONS;
}
if (is(shape, 'bpmn:Group')) {
minDimensions[ id ] = GROUP_MIN_DIMENSIONS;
}
});
return minDimensions;
});
}
SpaceToolBehavior.$inject = [ 'eventBus' ];
// helpers //////////
function isHorizontalAxis(axis) {
return axis === 'x';
}
/**
* Get minimum dimensions for participant taking lanes into account.
*
* @param {Shape} participant
* @param {Axis} axis
* @param {number} start
*
* @return {number}
*/
function getParticipantMinDimensions(participant, axis, start) {
var isHorizontalLane = isHorizontal(participant);
if (!hasChildLanes(participant)) {
return isHorizontalLane ? PARTICIPANT_MIN_DIMENSIONS : VERTICAL_PARTICIPANT_MIN_DIMENSIONS;
}
var isHorizontalResize = isHorizontalAxis(axis);
var minDimensions = {};
if (isHorizontalResize) {
if (isHorizontalLane) {
minDimensions = PARTICIPANT_MIN_DIMENSIONS;
} else {
minDimensions = {
width: getParticipantMinWidth(participant, start, isHorizontalResize),
height: VERTICAL_PARTICIPANT_MIN_DIMENSIONS.height
};
}
} else {
if (isHorizontalLane) {
minDimensions = {
width: PARTICIPANT_MIN_DIMENSIONS.width,
height: getParticipantMinHeight(participant, start, isHorizontalResize)
};
} else {
minDimensions = VERTICAL_PARTICIPANT_MIN_DIMENSIONS;
}
}
return minDimensions;
}
/**
* Get minimum height for participant taking lanes into account.
*
* @param {Shape} participant
* @param {number} start
* @param {boolean} isHorizontalResize
*
* @return {number}
*/
function getParticipantMinHeight(participant, start, isHorizontalResize) {
var lanesMinHeight;
lanesMinHeight = getLanesMinHeight(participant, start, isHorizontalResize);
return max(PARTICIPANT_MIN_DIMENSIONS.height, lanesMinHeight);
}
/**
* Get minimum width for participant taking lanes into account.
*
* @param {Shape} participant
* @param {number} start
* @param {boolean} isHorizontalResize
*
* @return {number}
*/
function getParticipantMinWidth(participant, start, isHorizontalResize) {
var lanesMinWidth;
lanesMinWidth = getLanesMinWidth(participant, start, isHorizontalResize);
return max(VERTICAL_PARTICIPANT_MIN_DIMENSIONS.width, lanesMinWidth);
}
function hasChildLanes(element) {
return !!getChildLanes(element).length;
}
function getLanesMinHeight(participant, resizeStart, isHorizontalResize) {
var lanes = getChildLanes(participant),
resizedLane;
// find the nested lane which is currently resized
resizedLane = findResizedLane(lanes, resizeStart, isHorizontalResize);
// resized lane cannot shrink below the minimum height
// but remaining lanes' dimensions are kept intact
return participant.height - resizedLane.height + LANE_MIN_DIMENSIONS.height;
}
function getLanesMinWidth(participant, resizeStart, isHorizontalResize) {
var lanes = getChildLanes(participant),
resizedLane;
// find the nested lane which is currently resized
resizedLane = findResizedLane(lanes, resizeStart, isHorizontalResize);
// resized lane cannot shrink below the minimum width
// but remaining lanes' dimensions are kept intact
return participant.width - resizedLane.width + VERTICAL_LANE_MIN_DIMENSIONS.width;
}
/**
* Find nested lane which is currently resized.
*
* @param {Shape[]} lanes
* @param {number} resizeStart
* @param {boolean} isHorizontalResize
*
* @return {Shape}
*/
function findResizedLane(lanes, resizeStart, isHorizontalResize) {
var i, lane, childLanes;
for (i = 0; i < lanes.length; i++) {
lane = lanes[i];
// resizing current lane or a lane nested
if (!isHorizontalResize && resizeStart >= lane.y && resizeStart <= lane.y + lane.height ||
isHorizontalResize && resizeStart >= lane.x && resizeStart <= lane.x + lane.width) {
childLanes = getChildLanes(lane);
// a nested lane is resized
if (childLanes.length) {
return findResizedLane(childLanes, resizeStart, isHorizontalResize);
}
// current lane is the resized one
return lane;
}
}
}