-
Notifications
You must be signed in to change notification settings - Fork 319
/
Group.js
1136 lines (1002 loc) · 35.2 KB
/
Group.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import util from 'vis-util';
import * as stack from '../Stack';
const UNGROUPED = '__ungrouped__'; // reserved group id for ungrouped items
const BACKGROUND = '__background__'; // reserved group id for background items without group
export const ReservedGroupIds = {
UNGROUPED,
BACKGROUND
}
/**
* @constructor Group
*/
class Group {
/**
* @param {number | string} groupId
* @param {Object} data
* @param {ItemSet} itemSet
* @constructor Group
*/
constructor(groupId, data, itemSet) {
this.groupId = groupId;
this.subgroups = {};
this.subgroupStack = {};
this.subgroupStackAll = false;
this.subgroupVisibility = {};
this.doInnerStack = false;
this.shouldBailStackItems = false;
this.subgroupIndex = 0;
this.subgroupOrderer = data && data.subgroupOrder;
this.itemSet = itemSet;
this.isVisible = null;
this.stackDirty = true; // if true, items will be restacked on next redraw
if (data && data.nestedGroups) {
this.nestedGroups = data.nestedGroups;
if (data.showNested == false) {
this.showNested = false;
} else {
this.showNested = true;
}
}
if (data && data.subgroupStack) {
if (typeof data.subgroupStack === "boolean") {
this.doInnerStack = data.subgroupStack;
this.subgroupStackAll = data.subgroupStack;
}
else {
// We might be doing stacking on specific sub groups, but only
// if at least one is set to do stacking
for(const key in data.subgroupStack) {
this.subgroupStack[key] = data.subgroupStack[key];
this.doInnerStack = this.doInnerStack || data.subgroupStack[key];
}
}
}
if (data && data.heightMode) {
this.heightMode = data.heightMode;
} else {
this.heightMode = itemSet.options.groupHeightMode;
}
this.nestedInGroup = null;
this.dom = {};
this.props = {
label: {
width: 0,
height: 0
}
};
this.className = null;
this.items = {}; // items filtered by groupId of this group
this.visibleItems = []; // items currently visible in window
this.itemsInRange = []; // items currently in range
this.orderedItems = {
byStart: [],
byEnd: []
};
this.checkRangedItems = false; // needed to refresh the ranged items if the window is programatically changed with NO overlap.
const me = this;
this.itemSet.body.emitter.on("checkRangedItems", () => {
me.checkRangedItems = true;
})
this._create();
this.setData(data);
}
/**
* Create DOM elements for the group
* @private
*/
_create() {
const label = document.createElement('div');
if (this.itemSet.options.groupEditable.order) {
label.className = 'vis-label draggable';
} else {
label.className = 'vis-label';
}
this.dom.label = label;
const inner = document.createElement('div');
inner.className = 'vis-inner';
label.appendChild(inner);
this.dom.inner = inner;
const foreground = document.createElement('div');
foreground.className = 'vis-group';
foreground['vis-group'] = this;
this.dom.foreground = foreground;
this.dom.background = document.createElement('div');
this.dom.background.className = 'vis-group';
this.dom.axis = document.createElement('div');
this.dom.axis.className = 'vis-group';
// create a hidden marker to detect when the Timelines container is attached
// to the DOM, or the style of a parent of the Timeline is changed from
// display:none is changed to visible.
this.dom.marker = document.createElement('div');
this.dom.marker.style.visibility = 'hidden';
this.dom.marker.style.position = 'absolute';
this.dom.marker.innerHTML = '';
this.dom.background.appendChild(this.dom.marker);
}
/**
* Set the group data for this group
* @param {Object} data Group data, can contain properties content and className
*/
setData(data) {
if (this.itemSet.groupTouchParams.isDragging) return;
// update contents
let content;
let templateFunction;
if (data && data.subgroupVisibility) {
for (const key in data.subgroupVisibility) {
this.subgroupVisibility[key] = data.subgroupVisibility[key];
}
}
if (this.itemSet.options && this.itemSet.options.groupTemplate) {
templateFunction = this.itemSet.options.groupTemplate.bind(this);
content = templateFunction(data, this.dom.inner);
} else {
content = data && data.content;
}
if (content instanceof Element) {
while (this.dom.inner.firstChild) {
this.dom.inner.removeChild(this.dom.inner.firstChild);
}
this.dom.inner.appendChild(content);
} else if (content instanceof Object && content.isReactComponent) {
// Do nothing. Component was rendered into the node be ReactDOM.render.
// That branch is necessary for evasion of a second call templateFunction.
// Supports only React < 16(due to the asynchronous nature of React 16).
} else if (content instanceof Object) {
templateFunction(data, this.dom.inner);
} else if (content !== undefined && content !== null) {
this.dom.inner.innerHTML = content;
} else {
this.dom.inner.innerHTML = this.groupId || ''; // groupId can be null
}
// update title
this.dom.label.title = data && data.title || '';
if (!this.dom.inner.firstChild) {
util.addClassName(this.dom.inner, 'vis-hidden');
}
else {
util.removeClassName(this.dom.inner, 'vis-hidden');
}
if (data && data.nestedGroups) {
if (!this.nestedGroups || this.nestedGroups != data.nestedGroups) {
this.nestedGroups = data.nestedGroups;
}
if (data.showNested !== undefined || this.showNested === undefined) {
if (data.showNested == false) {
this.showNested = false;
} else {
this.showNested = true;
}
}
util.addClassName(this.dom.label, 'vis-nesting-group');
if (this.showNested) {
util.removeClassName(this.dom.label, 'collapsed');
util.addClassName(this.dom.label, 'expanded');
} else {
util.removeClassName(this.dom.label, 'expanded');
util.addClassName(this.dom.label, 'collapsed');
}
} else if (this.nestedGroups) {
this.nestedGroups = null;
util.removeClassName(this.dom.label, 'collapsed');
util.removeClassName(this.dom.label, 'expanded');
util.removeClassName(this.dom.label, 'vis-nesting-group');
}
if (data && (data.treeLevel|| data.nestedInGroup)) {
util.addClassName(this.dom.label, 'vis-nested-group');
if (data.treeLevel) {
util.addClassName(this.dom.label, 'vis-group-level-' + data.treeLevel);
} else {
// Nesting level is unknown, but we're sure it's at least 1
util.addClassName(this.dom.label, 'vis-group-level-unknown-but-gte1');
}
} else {
util.addClassName(this.dom.label, 'vis-group-level-0');
}
// update className
const className = data && data.className || null;
if (className != this.className) {
if (this.className) {
util.removeClassName(this.dom.label, this.className);
util.removeClassName(this.dom.foreground, this.className);
util.removeClassName(this.dom.background, this.className);
util.removeClassName(this.dom.axis, this.className);
}
util.addClassName(this.dom.label, className);
util.addClassName(this.dom.foreground, className);
util.addClassName(this.dom.background, className);
util.addClassName(this.dom.axis, className);
this.className = className;
}
// update style
if (this.style) {
util.removeCssText(this.dom.label, this.style);
this.style = null;
}
if (data && data.style) {
util.addCssText(this.dom.label, data.style);
this.style = data.style;
}
}
/**
* Get the width of the group label
* @return {number} width
*/
getLabelWidth() {
return this.props.label.width;
}
/**
* check if group has had an initial height hange
* @returns {boolean}
*/
_didMarkerHeightChange() {
const markerHeight = this.dom.marker.clientHeight;
if (markerHeight != this.lastMarkerHeight) {
this.lastMarkerHeight = markerHeight;
const redrawQueue = {};
let redrawQueueLength = 0;
util.forEach(this.items, (item, key) => {
item.dirty = true;
if (item.displayed) {
const returnQueue = true;
redrawQueue[key] = item.redraw(returnQueue);
redrawQueueLength = redrawQueue[key].length;
}
})
const needRedraw = redrawQueueLength > 0;
if (needRedraw) {
// redraw all regular items
for (let i = 0; i < redrawQueueLength; i++) {
util.forEach(redrawQueue, fns => {
fns[i]();
});
}
}
return true;
} else {
return false;
}
}
/**
* calculate group dimentions and position
* @param {number} pixels
*/
_calculateGroupSizeAndPosition() {
const { offsetTop, offsetLeft, offsetWidth } = this.dom.foreground;
this.top = offsetTop;
this.right = offsetLeft;
this.width = offsetWidth;
}
/**
* checks if should bail redraw of items
* @returns {boolean} should bail
*/
_shouldBailItemsRedraw() {
const me = this;
const timeoutOptions = this.itemSet.options.onTimeout;
const bailOptions = {
relativeBailingTime: this.itemSet.itemsSettingTime,
bailTimeMs: timeoutOptions && timeoutOptions.timeoutMs,
userBailFunction: timeoutOptions && timeoutOptions.callback,
shouldBailStackItems: this.shouldBailStackItems
};
let bail = null;
if (!this.itemSet.initialDrawDone) {
if (bailOptions.shouldBailStackItems) { return true; }
if (Math.abs(Date.now() - new Date(bailOptions.relativeBailingTime)) > bailOptions.bailTimeMs) {
if (bailOptions.userBailFunction && this.itemSet.userContinueNotBail == null) {
bailOptions.userBailFunction(didUserContinue => {
me.itemSet.userContinueNotBail = didUserContinue;
bail = !didUserContinue;
})
} else if (me.itemSet.userContinueNotBail == false) {
bail = true;
} else {
bail = false;
}
}
}
return bail;
}
/**
* redraws items
* @param {boolean} forceRestack
* @param {boolean} lastIsVisible
* @param {number} margin
* @param {object} range
* @private
*/
_redrawItems(forceRestack, lastIsVisible, margin, range) {
const restack = forceRestack || this.stackDirty || this.isVisible && !lastIsVisible;
// if restacking, reposition visible items vertically
if (restack) {
const visibleSubgroups = {};
const orderedItems = {
byEnd: this.orderedItems.byEnd.filter(item => !item.isCluster),
byStart: this.orderedItems.byStart.filter(item => !item.isCluster)
}
const orderedClusters = {
byEnd: [...new Set(this.orderedItems.byEnd.map(item => item.cluster).filter(item => !!item))],
byStart: [...new Set(this.orderedItems.byStart.map(item => item.cluster).filter(item => !!item))],
}
/**
* Get all visible items in range
* @return {array} items
*/
const getVisibleItems = () => {
const visibleItems = this._updateItemsInRange(orderedItems, this.visibleItems.filter(item => !item.isCluster), range);
const visibleClusters = this._updateClustersInRange(orderedClusters, this.visibleItems.filter(item => item.isCluster), range);
return [...visibleItems, ...visibleClusters];
}
if (typeof this.itemSet.options.order === 'function') {
// a custom order function
//show all items
const me = this;
if (this.doInnerStack && this.itemSet.options.stackSubgroups) {
// Order the items within each subgroup
for (const subgroup in this.subgroups) {
visibleSubgroups[subgroup] = this.subgroups[subgroup].items.slice().sort((a, b) => me.itemSet.options.order(a.data, b.data));
}
stack.stackSubgroupsWithInnerStack(visibleSubgroups, margin, this.subgroups);
this.visibleItems = getVisibleItems();
this._updateSubGroupHeights(margin);
}
else {
this.visibleItems = getVisibleItems();
this._updateSubGroupHeights(margin);
// order all items and force a restacking
// order all items outside clusters and force a restacking
const customOrderedItems = this.visibleItems
.slice()
.filter(item => item.isCluster || (!item.isCluster && !item.cluster))
.sort((a, b) => {
return me.itemSet.options.order(a.data, b.data);
});
this.shouldBailStackItems = stack.stack(customOrderedItems, margin, true, this._shouldBailItemsRedraw.bind(this));
}
} else {
// no custom order function, lazy stacking
const visibleItems = this._updateItemsInRange(orderedItems, this.visibleItems.filter(item => !item.isCluster), range);
const visibleClusters = this._updateClustersInRange(orderedClusters, this.visibleItems.filter(item => item.isCluster), range);
this.visibleItems = [...visibleItems, ...visibleClusters];
this._updateSubGroupHeights(margin);
if (this.itemSet.options.stack) {
if (this.doInnerStack && this.itemSet.options.stackSubgroups) {
for (const subgroup in this.subgroups) {
visibleSubgroups[subgroup] = this.subgroups[subgroup].items;
}
stack.stackSubgroupsWithInnerStack(visibleSubgroups, margin, this.subgroups);
}
else {
// TODO: ugly way to access options...
this.shouldBailStackItems = stack.stack(this.visibleItems, margin, true, this._shouldBailItemsRedraw.bind(this));
}
} else {
// no stacking
stack.nostack(this.visibleItems, margin, this.subgroups, this.itemSet.options.stackSubgroups);
}
}
for (let i = 0; i < this.visibleItems.length; i++) {
this.visibleItems[i].repositionX();
if (this.subgroupVisibility[this.visibleItems[i].data.subgroup] !== undefined) {
if (!this.subgroupVisibility[this.visibleItems[i].data.subgroup]) {
this.visibleItems[i].hide();
}
}
}
if (this.itemSet.options.cluster) {
util.forEach(this.items, item => {
if (item.cluster && item.displayed) {
item.hide();
}
});
}
if (this.shouldBailStackItems) {
this.itemSet.body.emitter.emit('destroyTimeline')
}
this.stackDirty = false;
}
}
/**
* check if group resized
* @param {boolean} resized
* @param {number} height
* @return {boolean} did resize
*/
_didResize(resized, height) {
resized = util.updateProperty(this, 'height', height) || resized;
// recalculate size of label
const labelWidth = this.dom.inner.clientWidth;
const labelHeight = this.dom.inner.clientHeight;
resized = util.updateProperty(this.props.label, 'width', labelWidth) || resized;
resized = util.updateProperty(this.props.label, 'height', labelHeight) || resized;
return resized;
}
/**
* apply group height
* @param {number} height
*/
_applyGroupHeight(height) {
this.dom.background.style.height = `${height}px`;
this.dom.foreground.style.height = `${height}px`;
this.dom.label.style.height = `${height}px`;
}
/**
* update vertical position of items after they are re-stacked and the height of the group is calculated
* @param {number} margin
*/
_updateItemsVerticalPosition(margin) {
for (let i = 0, ii = this.visibleItems.length; i < ii; i++) {
const item = this.visibleItems[i];
item.repositionY(margin);
if (!this.isVisible && this.groupId != ReservedGroupIds.BACKGROUND) {
if (item.displayed) item.hide();
}
}
}
/**
* Repaint this group
* @param {{start: number, end: number}} range
* @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
* @param {boolean} [forceRestack=false] Force restacking of all items
* @param {boolean} [returnQueue=false] return the queue or if the group resized
* @return {boolean} Returns true if the group is resized or the redraw queue if returnQueue=true
*/
redraw(range, margin, forceRestack, returnQueue) {
let resized = false;
const lastIsVisible = this.isVisible;
let height;
const queue = [
() => {
forceRestack = this._didMarkerHeightChange.call(this) || forceRestack;
},
// recalculate the height of the subgroups
this._updateSubGroupHeights.bind(this, margin),
// calculate actual size and position
this._calculateGroupSizeAndPosition.bind(this),
() => {
this.isVisible = this._isGroupVisible.bind(this)(range, margin);
},
() => {
this._redrawItems.bind(this)(forceRestack, lastIsVisible, margin, range)
},
// update subgroups
this._updateSubgroupsSizes.bind(this),
() => {
height = this._calculateHeight.bind(this)(margin);
},
// calculate actual size and position again
this._calculateGroupSizeAndPosition.bind(this),
() => {
resized = this._didResize.bind(this)(resized, height)
},
() => {
this._applyGroupHeight.bind(this)(height)
},
() => {
this._updateItemsVerticalPosition.bind(this)(margin)
},
(() => {
if (!this.isVisible && this.height) {
resized = false;
}
return resized
}).bind(this)
];
if (returnQueue) {
return queue;
} else {
let result;
queue.forEach(fn => {
result = fn();
});
return result;
}
}
/**
* recalculate the height of the subgroups
*
* @param {{item: timeline.Item}} margin
* @private
*/
_updateSubGroupHeights(margin) {
if (Object.keys(this.subgroups).length > 0) {
const me = this;
this._resetSubgroups();
util.forEach(this.visibleItems, item => {
if (item.data.subgroup !== undefined) {
me.subgroups[item.data.subgroup].height = Math.max(me.subgroups[item.data.subgroup].height, item.height + margin.item.vertical);
me.subgroups[item.data.subgroup].visible = typeof this.subgroupVisibility[item.data.subgroup] === 'undefined' ? true : Boolean(this.subgroupVisibility[item.data.subgroup]);
}
});
}
}
/**
* check if group is visible
*
* @param {timeline.Range} range
* @param {{axis: timeline.DataAxis}} margin
* @returns {boolean} is visible
* @private
*/
_isGroupVisible(range, margin) {
return (this.top <= range.body.domProps.centerContainer.height - range.body.domProps.scrollTop + margin.axis)
&& (this.top + this.height + margin.axis >= - range.body.domProps.scrollTop);
}
/**
* recalculate the height of the group
* @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
* @returns {number} Returns the height
* @private
*/
_calculateHeight(margin) {
// recalculate the height of the group
let height;
let items;
if (this.heightMode === 'fixed') {
items = util.toArray(this.items);
} else {
// default or 'auto'
items = this.visibleItems;
}
if (items.length > 0) {
let min = items[0].top;
let max = items[0].top + items[0].height;
util.forEach(items, item => {
min = Math.min(min, item.top);
max = Math.max(max, (item.top + item.height));
});
if (min > margin.axis) {
// there is an empty gap between the lowest item and the axis
const offset = min - margin.axis;
max -= offset;
util.forEach(items, item => {
item.top -= offset;
});
}
height = max + margin.item.vertical / 2;
if (this.heightMode !== "fitItems") {
height = Math.max(height, this.props.label.height);
}
}
else {
height = 0 || this.props.label.height;
}
return height;
}
/**
* Show this group: attach to the DOM
*/
show() {
if (!this.dom.label.parentNode) {
this.itemSet.dom.labelSet.appendChild(this.dom.label);
}
if (!this.dom.foreground.parentNode) {
this.itemSet.dom.foreground.appendChild(this.dom.foreground);
}
if (!this.dom.background.parentNode) {
this.itemSet.dom.background.appendChild(this.dom.background);
}
if (!this.dom.axis.parentNode) {
this.itemSet.dom.axis.appendChild(this.dom.axis);
}
}
/**
* Hide this group: remove from the DOM
*/
hide() {
const label = this.dom.label;
if (label.parentNode) {
label.parentNode.removeChild(label);
}
const foreground = this.dom.foreground;
if (foreground.parentNode) {
foreground.parentNode.removeChild(foreground);
}
const background = this.dom.background;
if (background.parentNode) {
background.parentNode.removeChild(background);
}
const axis = this.dom.axis;
if (axis.parentNode) {
axis.parentNode.removeChild(axis);
}
}
/**
* Add an item to the group
* @param {Item} item
*/
add(item) {
this.items[item.id] = item;
item.setParent(this);
this.stackDirty = true;
// add to
if (item.data.subgroup !== undefined) {
this._addToSubgroup(item);
this.orderSubgroups();
}
if (!this.visibleItems.includes(item)) {
const range = this.itemSet.body.range; // TODO: not nice accessing the range like this
this._checkIfVisible(item, this.visibleItems, range);
}
}
/**
* add item to subgroup
* @param {object} item
* @param {string} subgroupId
*/
_addToSubgroup(item, subgroupId=item.data.subgroup) {
if (subgroupId != undefined && this.subgroups[subgroupId] === undefined) {
this.subgroups[subgroupId] = {
height: 0,
top: 0,
start: item.data.start,
end: item.data.end || item.data.start,
visible: false,
index: this.subgroupIndex,
items: [],
stack: this.subgroupStackAll || this.subgroupStack[subgroupId] || false
};
this.subgroupIndex++;
}
if (new Date(item.data.start) < new Date(this.subgroups[subgroupId].start)) {
this.subgroups[subgroupId].start = item.data.start;
}
const itemEnd = item.data.end || item.data.start;
if (new Date(itemEnd) > new Date(this.subgroups[subgroupId].end)) {
this.subgroups[subgroupId].end = itemEnd;
}
this.subgroups[subgroupId].items.push(item);
}
/**
* update subgroup sizes
*/
_updateSubgroupsSizes() {
const me = this;
if (me.subgroups) {
for (const subgroup in me.subgroups) {
const initialEnd = me.subgroups[subgroup].items[0].data.end || me.subgroups[subgroup].items[0].data.start;
let newStart = me.subgroups[subgroup].items[0].data.start;
let newEnd = initialEnd - 1;
me.subgroups[subgroup].items.forEach(item => {
if (new Date(item.data.start) < new Date(newStart)) {
newStart = item.data.start;
}
const itemEnd = item.data.end || item.data.start;
if (new Date(itemEnd) > new Date(newEnd)) {
newEnd = itemEnd;
}
})
me.subgroups[subgroup].start = newStart;
me.subgroups[subgroup].end = new Date(newEnd - 1) // -1 to compensate for colliding end to start subgroups;
}
}
}
/**
* order subgroups
*/
orderSubgroups() {
if (this.subgroupOrderer !== undefined) {
const sortArray = [];
if (typeof this.subgroupOrderer == 'string') {
for (const subgroup in this.subgroups) {
sortArray.push({subgroup, sortField: this.subgroups[subgroup].items[0].data[this.subgroupOrderer]})
}
sortArray.sort((a, b) => a.sortField - b.sortField)
}
else if (typeof this.subgroupOrderer == 'function') {
for (const subgroup in this.subgroups) {
sortArray.push(this.subgroups[subgroup].items[0].data);
}
sortArray.sort(this.subgroupOrderer);
}
if (sortArray.length > 0) {
for (let i = 0; i < sortArray.length; i++) {
this.subgroups[sortArray[i].subgroup].index = i;
}
}
}
}
/**
* add item to subgroup
*/
_resetSubgroups() {
for (const subgroup in this.subgroups) {
if (this.subgroups.hasOwnProperty(subgroup)) {
this.subgroups[subgroup].visible = false;
this.subgroups[subgroup].height = 0;
}
}
}
/**
* Remove an item from the group
* @param {Item} item
*/
remove(item) {
delete this.items[item.id];
item.setParent(null);
this.stackDirty = true;
// remove from visible items
const index = this.visibleItems.indexOf(item);
if (index != -1) this.visibleItems.splice(index, 1);
if(item.data.subgroup !== undefined){
this._removeFromSubgroup(item);
this.orderSubgroups();
}
}
/**
* remove item from subgroup
* @param {object} item
* @param {string} subgroupId
*/
_removeFromSubgroup(item, subgroupId=item.data.subgroup) {
if (subgroupId != undefined) {
const subgroup = this.subgroups[subgroupId];
if (subgroup){
const itemIndex = subgroup.items.indexOf(item);
// Check the item is actually in this subgroup. How should items not in the group be handled?
if (itemIndex >= 0) {
subgroup.items.splice(itemIndex,1);
if (!subgroup.items.length){
delete this.subgroups[subgroupId];
} else {
this._updateSubgroupsSizes();
}
}
}
}
}
/**
* Remove an item from the corresponding DataSet
* @param {Item} item
*/
removeFromDataSet(item) {
this.itemSet.removeItem(item.id);
}
/**
* Reorder the items
*/
order() {
const array = util.toArray(this.items);
const startArray = [];
const endArray = [];
for (let i = 0; i < array.length; i++) {
if (array[i].data.end !== undefined) {
endArray.push(array[i]);
}
startArray.push(array[i]);
}
this.orderedItems = {
byStart: startArray,
byEnd: endArray
};
stack.orderByStart(this.orderedItems.byStart);
stack.orderByEnd(this.orderedItems.byEnd);
}
/**
* Update the visible items
* @param {{byStart: Item[], byEnd: Item[]}} orderedItems All items ordered by start date and by end date
* @param {Item[]} oldVisibleItems The previously visible items.
* @param {{start: number, end: number}} range Visible range
* @return {Item[]} visibleItems The new visible items.
* @private
*/
_updateItemsInRange(orderedItems, oldVisibleItems, range) {
const visibleItems = [];
const visibleItemsLookup = {}; // we keep this to quickly look up if an item already exists in the list without using indexOf on visibleItems
if (!this.isVisible && this.groupId != ReservedGroupIds.BACKGROUND) {
for (let i = 0; i < oldVisibleItems.length; i++) {
var item = oldVisibleItems[i];
if (item.displayed) item.hide();
}
return visibleItems;
}
const interval = (range.end - range.start) / 4;
const lowerBound = range.start - interval;
const upperBound = range.end + interval;
// this function is used to do the binary search for items having start date only.
const startSearchFunction = value => {
if (value < lowerBound) {return -1;}
else if (value <= upperBound) {return 0;}
else {return 1;}
};
// this function is used to do the binary search for items having start and end dates (range).
const endSearchFunction = value => {
if (value < lowerBound) {return -1;}
else {return 0;}
}
// first check if the items that were in view previously are still in view.
// IMPORTANT: this handles the case for the items with startdate before the window and enddate after the window!
// also cleans up invisible items.
if (oldVisibleItems.length > 0) {
for (let i = 0; i < oldVisibleItems.length; i++) {
this._checkIfVisibleWithReference(oldVisibleItems[i], visibleItems, visibleItemsLookup, range);
}
}
// we do a binary search for the items that have only start values.
const initialPosByStart = util.binarySearchCustom(orderedItems.byStart, startSearchFunction, 'data','start');
// trace the visible items from the inital start pos both ways until an invisible item is found, we only look at the start values.
this._traceVisible(initialPosByStart, orderedItems.byStart, visibleItems, visibleItemsLookup, item => item.data.start < lowerBound || item.data.start > upperBound);
// if the window has changed programmatically without overlapping the old window, the ranged items with start < lowerBound and end > upperbound are not shown.
// We therefore have to brute force check all items in the byEnd list
if (this.checkRangedItems == true) {
this.checkRangedItems = false;
for (let i = 0; i < orderedItems.byEnd.length; i++) {
this._checkIfVisibleWithReference(orderedItems.byEnd[i], visibleItems, visibleItemsLookup, range);
}
}
else {
// we do a binary search for the items that have defined end times.
const initialPosByEnd = util.binarySearchCustom(orderedItems.byEnd, endSearchFunction, 'data','end');
// trace the visible items from the inital start pos both ways until an invisible item is found, we only look at the end values.
this._traceVisible(initialPosByEnd, orderedItems.byEnd, visibleItems, visibleItemsLookup, item => item.data.end < lowerBound || item.data.start > upperBound);
}
const redrawQueue = {};
let redrawQueueLength = 0;
for (let i = 0; i < visibleItems.length; i++) {
const item = visibleItems[i];
if (!item.displayed) {
const returnQueue = true;
redrawQueue[i] = item.redraw(returnQueue);
redrawQueueLength = redrawQueue[i].length;
}
}
const needRedraw = redrawQueueLength > 0;
if (needRedraw) {
// redraw all regular items
for (let j = 0; j < redrawQueueLength; j++) {
util.forEach(redrawQueue, fns => {
fns[j]();
});
}
}
for (let i = 0; i < visibleItems.length; i++) {
visibleItems[i].repositionX();
}
return visibleItems;
}
/**
* trace visible items in group
* @param {number} initialPos
* @param {array} items
* @param {aray} visibleItems
* @param {object} visibleItemsLookup
* @param {function} breakCondition
*/
_traceVisible(initialPos, items, visibleItems, visibleItemsLookup, breakCondition) {
if (initialPos != -1) {
for (let i = initialPos; i >= 0; i--) {
let item = items[i];
if (breakCondition(item)) {
break;
}
else {
if (!(item.isCluster && !item.hasItems()) && !item.cluster) {
if (visibleItemsLookup[item.id] === undefined) {
visibleItemsLookup[item.id] = true;
visibleItems.push(item);
}
}
}