1 |
|
2 | var EPSILON = 0.001;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | exports.orderByStart = function(items) {
|
9 | items.sort(function (a, b) {
|
10 | return a.data.start - b.data.start;
|
11 | });
|
12 | };
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | exports.orderByEnd = function(items) {
|
20 | items.sort(function (a, b) {
|
21 | var aTime = ('end' in a.data) ? a.data.end : a.data.start,
|
22 | bTime = ('end' in b.data) ? b.data.end : b.data.start;
|
23 |
|
24 | return aTime - bTime;
|
25 | });
|
26 | };
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | exports.stack = function(items, margin, force, shouldBailItemsRedrawFunction) {
|
43 | if (force) {
|
44 |
|
45 | for (var i = 0; i < items.length; i++) {
|
46 | items[i].top = null;
|
47 | }
|
48 | }
|
49 |
|
50 |
|
51 | for (var i = 0; i < items.length; i++) {
|
52 | var item = items[i];
|
53 | if (item.stack && item.top === null) {
|
54 |
|
55 | item.top = margin.axis;
|
56 | var shouldBail = false;
|
57 |
|
58 | do {
|
59 |
|
60 |
|
61 | var collidingItem = null;
|
62 | for (var j = 0, jj = items.length; j < jj; j++) {
|
63 | var other = items[j];
|
64 | shouldBail = shouldBailItemsRedrawFunction() || false;
|
65 |
|
66 | if (shouldBail) { return true; }
|
67 |
|
68 | if (other.top !== null && other !== item && other.stack && exports.collision(item, other, margin.item, other.options.rtl)) {
|
69 | collidingItem = other;
|
70 | break;
|
71 | }
|
72 | }
|
73 |
|
74 | if (collidingItem != null) {
|
75 |
|
76 | item.top = collidingItem.top + collidingItem.height + margin.item.vertical;
|
77 | }
|
78 | } while (collidingItem);
|
79 | }
|
80 | }
|
81 | return shouldBail;
|
82 | };
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | exports.substack = function (items, margin, subgroup) {
|
95 | for (var i = 0; i < items.length; i++) {
|
96 | items[i].top = null;
|
97 | }
|
98 |
|
99 |
|
100 | var subgroupHeight = subgroup.height;
|
101 |
|
102 |
|
103 | for (i = 0; i < items.length; i++) {
|
104 | var item = items[i];
|
105 |
|
106 | if (item.stack && item.top === null) {
|
107 |
|
108 | item.top = item.baseTop;
|
109 |
|
110 | do {
|
111 |
|
112 |
|
113 | var collidingItem = null;
|
114 | for (var j = 0, jj = items.length; j < jj; j++) {
|
115 | var other = items[j];
|
116 | if (other.top !== null && other !== item && exports.collision(item, other, margin.item, other.options.rtl)) {
|
117 | collidingItem = other;
|
118 | break;
|
119 | }
|
120 | }
|
121 |
|
122 | if (collidingItem != null) {
|
123 |
|
124 | item.top = collidingItem.top + collidingItem.height + margin.item.vertical;
|
125 | }
|
126 |
|
127 | if (item.top + item.height > subgroupHeight) {
|
128 | subgroupHeight = item.top + item.height;
|
129 | }
|
130 | } while (collidingItem);
|
131 | }
|
132 | }
|
133 |
|
134 |
|
135 | subgroup.height = subgroupHeight - subgroup.top + 0.5 * margin.item.vertical;
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | exports.nostack = function(items, margin, subgroups, stackSubgroups) {
|
149 | for (var i = 0; i < items.length; i++) {
|
150 | if (items[i].data.subgroup == undefined) {
|
151 | items[i].top = margin.item.vertical;
|
152 | } else if (items[i].data.subgroup !== undefined && stackSubgroups) {
|
153 | var newTop = 0;
|
154 | for (var subgroup in subgroups) {
|
155 | if (subgroups.hasOwnProperty(subgroup)) {
|
156 | if (subgroups[subgroup].visible == true && subgroups[subgroup].index < subgroups[items[i].data.subgroup].index) {
|
157 | newTop += subgroups[subgroup].height;
|
158 | subgroups[items[i].data.subgroup].top = newTop;
|
159 | }
|
160 | }
|
161 | }
|
162 | items[i].top = newTop + 0.5 * margin.item.vertical;
|
163 | }
|
164 | }
|
165 | if (!stackSubgroups) {
|
166 | exports.stackSubgroups(items, margin, subgroups)
|
167 | }
|
168 | };
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | exports.stackSubgroups = function(items, margin, subgroups) {
|
179 | for (var subgroup in subgroups) {
|
180 | if (subgroups.hasOwnProperty(subgroup)) {
|
181 |
|
182 |
|
183 | subgroups[subgroup].top = 0;
|
184 | do {
|
185 |
|
186 |
|
187 | var collidingItem = null;
|
188 | for (var otherSubgroup in subgroups) {
|
189 | if (subgroups[otherSubgroup].top !== null && otherSubgroup !== subgroup && subgroups[subgroup].index > subgroups[otherSubgroup].index && exports.collisionByTimes(subgroups[subgroup], subgroups[otherSubgroup])) {
|
190 | collidingItem = subgroups[otherSubgroup];
|
191 | break;
|
192 | }
|
193 | }
|
194 |
|
195 | if (collidingItem != null) {
|
196 |
|
197 | subgroups[subgroup].top = collidingItem.top + collidingItem.height;
|
198 | }
|
199 | } while (collidingItem);
|
200 | }
|
201 | }
|
202 | for (var i = 0; i < items.length; i++) {
|
203 | if (items[i].data.subgroup !== undefined) {
|
204 | items[i].top = subgroups[items[i].data.subgroup].top + 0.5 * margin.item.vertical;
|
205 | }
|
206 | }
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 | exports.stackSubgroupsWithInnerStack = function (subgroupItems, margin, subgroups) {
|
220 | var doSubStack = false;
|
221 |
|
222 |
|
223 | var subgroupOrder = [];
|
224 |
|
225 | for(var subgroup in subgroups) {
|
226 | if (subgroups[subgroup].hasOwnProperty("index")) {
|
227 | subgroupOrder[subgroups[subgroup].index] = subgroup;
|
228 | }
|
229 | else {
|
230 | subgroupOrder.push(subgroup);
|
231 | }
|
232 | }
|
233 |
|
234 | for(var j = 0; j < subgroupOrder.length; j++) {
|
235 | subgroup = subgroupOrder[j];
|
236 | if (subgroups.hasOwnProperty(subgroup)) {
|
237 |
|
238 | doSubStack = doSubStack || subgroups[subgroup].stack;
|
239 | subgroups[subgroup].top = 0;
|
240 |
|
241 | for (var otherSubgroup in subgroups) {
|
242 | if (subgroups[otherSubgroup].visible && subgroups[subgroup].index > subgroups[otherSubgroup].index) {
|
243 | subgroups[subgroup].top += subgroups[otherSubgroup].height;
|
244 | }
|
245 | }
|
246 |
|
247 | var items = subgroupItems[subgroup];
|
248 | for(var i = 0; i < items.length; i++) {
|
249 | if (items[i].data.subgroup !== undefined) {
|
250 | items[i].top = subgroups[items[i].data.subgroup].top + 0.5 * margin.item.vertical;
|
251 |
|
252 | if (subgroups[subgroup].stack) {
|
253 | items[i].baseTop = items[i].top;
|
254 | }
|
255 | }
|
256 | }
|
257 |
|
258 | if (doSubStack && subgroups[subgroup].stack) {
|
259 | exports.substack(subgroupItems[subgroup], margin, subgroups[subgroup]);
|
260 | }
|
261 | }
|
262 | }
|
263 | };
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | exports.collision = function(a, b, margin, rtl) {
|
277 | if (rtl) {
|
278 | return ((a.right - margin.horizontal + EPSILON) < (b.right + b.width) &&
|
279 | (a.right + a.width + margin.horizontal - EPSILON) > b.right &&
|
280 | (a.top - margin.vertical + EPSILON) < (b.top + b.height) &&
|
281 | (a.top + a.height + margin.vertical - EPSILON) > b.top);
|
282 | } else {
|
283 | return ((a.left - margin.horizontal + EPSILON) < (b.left + b.width) &&
|
284 | (a.left + a.width + margin.horizontal - EPSILON) > b.left &&
|
285 | (a.top - margin.vertical + EPSILON) < (b.top + b.height) &&
|
286 | (a.top + a.height + margin.vertical - EPSILON) > b.top);
|
287 | }
|
288 | };
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | exports.collisionByTimes = function(a, b) {
|
298 | return (
|
299 | (a.start <= b.start && a.end >= b.start && a.top < (b.top + b.height) && (a.top + a.height) > b.top ) ||
|
300 | (b.start <= a.start && b.end >= a.start && b.top < (a.top + a.height) && (b.top + b.height) > a.top )
|
301 | )
|
302 | } |
\ | No newline at end of file |