| 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
208
209
210
211
212
213
214
215
216
217
218 |
1×
1×
7×
7×
7×
2×
2×
2×
12×
12×
12×
2×
2×
12×
1×
1×
7×
7×
1×
2×
2×
2×
2×
2×
2×
2×
2×
1×
2×
2×
2×
2×
2×
2×
2×
2×
1×
4×
4×
4×
4×
4×
4×
4×
20×
20×
20×
5×
15×
15×
4×
1×
4×
4×
4×
1×
19×
1×
20×
20×
20×
| import {
sortBy,
forEach,
filter
} from 'min-dash';
var AXIS_DIMENSIONS = {
horizontal: [ 'x', 'width' ],
vertical: [ 'y', 'height' ]
};
var THRESHOLD = 5;
/**
* Groups and filters elements and then trigger even distribution.
*/
export default function DistributeElements(modeling) {
this._modeling = modeling;
this._filters = [];
// register filter for filtering big elements
this.registerFilter(function(elements, axis, dimension) {
var elementsSize = 0,
numOfShapes = 0,
avgDimension;
forEach(elements, function(element) {
Iif (element.waypoints || element.labelTarget) {
return;
}
elementsSize += element[dimension];
numOfShapes += 1;
});
avgDimension = Math.round(elementsSize / numOfShapes);
return filter(elements, function(element) {
return element[dimension] < (avgDimension + 50);
});
});
}
DistributeElements.$inject = [ 'modeling' ];
/**
* Registers filter functions that allow external parties to filter
* out certain elements.
*
* @param {Function} filterFn
*/
DistributeElements.prototype.registerFilter = function(filterFn) {
Iif (typeof filterFn !== 'function') {
throw new Error('the filter has to be a function');
}
this._filters.push(filterFn);
};
/**
* Distributes the elements with a given orientation
*
* @param {Array} elements [description]
* @param {String} orientation [description]
*/
DistributeElements.prototype.trigger = function(elements, orientation) {
var modeling = this._modeling;
var groups,
distributableElements;
Iif (elements.length < 3) {
return;
}
this._setOrientation(orientation);
distributableElements = this._filterElements(elements);
groups = this._createGroups(distributableElements);
// nothing to distribute
Iif (groups.length <= 2) {
return;
}
modeling.distributeElements(groups, this._axis, this._dimension);
return groups;
};
/**
* Filters the elements with provided filters by external parties
*
* @param {Array[Elements]} elements
*
* @return {Array[Elements]}
*/
DistributeElements.prototype._filterElements = function(elements) {
var filters = this._filters,
axis = this._axis,
dimension = this._dimension,
distributableElements = [].concat(elements);
Iif (!filters.length) {
return elements;
}
forEach(filters, function(filterFn) {
distributableElements = filterFn(distributableElements, axis, dimension);
});
return distributableElements;
};
/**
* Create range (min, max) groups. Also tries to group elements
* together that share the same range.
*
* @example
* var distributableElements = [
* {
* range: {
* min: 100,
* max: 200
* },
* elements: [ { id: 'shape1', .. }]
* }
* ]
*
* @param {Array} elements
*
* @return {Array[Objects]}
*/
DistributeElements.prototype._createGroups = function(elements) {
var rangeGroups = [],
self = this,
axis = this._axis,
dimension = this._dimension;
Iif (!axis) {
throw new Error('must have a defined "axis" and "dimension"');
}
// sort by 'left->right' or 'top->bottom'
var sortedElements = sortBy(elements, axis);
forEach(sortedElements, function(element, idx) {
var elementRange = self._findRange(element, axis, dimension),
range;
var previous = rangeGroups[rangeGroups.length - 1];
if (previous && self._hasIntersection(previous.range, elementRange)) {
rangeGroups[rangeGroups.length - 1].elements.push(element);
} else {
range = { range: elementRange, elements: [ element ] };
rangeGroups.push(range);
}
});
return rangeGroups;
};
/**
* Maps a direction to the according axis and dimension
*
* @param {String} direction 'horizontal' or 'vertical'
*/
DistributeElements.prototype._setOrientation = function(direction) {
var orientation = AXIS_DIMENSIONS[direction];
this._axis = orientation[0];
this._dimension = orientation[1];
};
/**
* Checks if the two ranges intercept each other
*
* @param {Object} rangeA {min, max}
* @param {Object} rangeB {min, max}
*
* @return {Boolean}
*/
DistributeElements.prototype._hasIntersection = function(rangeA, rangeB) {
return Math.max(rangeA.min, rangeA.max) >= Math.min(rangeB.min, rangeB.max) &&
Math.min(rangeA.min, rangeA.max) <= Math.max(rangeB.min, rangeB.max);
};
/**
* Returns the min and max values for an element
*
* @param {[type]} element [description]
* @param {[type]} axis [description]
* @param {[type]} dimension [description]
*
* @return {[type]} [description]
*/
DistributeElements.prototype._findRange = function(element) {
var axis = element[this._axis],
dimension = element[this._dimension];
return {
min: axis + THRESHOLD,
max: axis + dimension - THRESHOLD
};
};
|