-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
1013 lines (917 loc) · 34.5 KB
/
Copy pathtree.js
File metadata and controls
1013 lines (917 loc) · 34.5 KB
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
// ==================== Tree plugin ====================
const treeClass = "ff-tree";
const treeEventClass = ".ff-tree";
// Defines default options for the tree view plugin.
let treeDefaults = {
// The items of the tree view.
items: [],
// Indicates whether a single root item is shown that cannot be collapsed.
singleRoot: false,
// A function that returns the icon element for an item.
iconGetter: undefined,
// A function that returns the label text for an item.
labelGetter: undefined,
// A function that returns the CSS style text for an item.
styleGetter: undefined,
// The object key of the child items for an item.
childrenKey: undefined,
// A function that is called when an item was hovered or left. Parameters:
// - item
// - hoverState (true, false)
itemHover: undefined,
// A function that is called when an item was selected or deselected. Parameters:
// - item
// - selectedState (true, false)
itemSelected: undefined,
// A function that is called when an item was expanded or collapsed. Parameters:
// - item
// - expandedState (true, false)
itemExpanded: undefined,
// A function that is called when an item was double-clicked. Parameters:
// - item
itemDoubleClicked: undefined,
// Indicates whether an expandable item will be expanded or collapsed when double-clicked.
doubleClickExpand: true,
// Indicates whether multiple items can be selected at the same time.
multiSelect: false,
// Enables drag&drop of items.
dragdrop: false,
// A function that indicates whether an item is a drop target itself. Parameters:
// - item
isDropTarget: undefined,
// A function that is called while dragging items. Parameters:
// - dragged items
// - drop target element
dragHandler: undefined,
// A function that is called after a drag&drop operation was completed. Parameters:
// - dragged items
// - drop target item
// - drop position
dropHandler: undefined,
// A function that is called when an unhandled key is pressed. Parameters:
// - event
keyHandler: undefined
};
// Shows a tree view on the element.
function tree(options) {
let container = this.first;
if (!container) return; // Nothing to do
if (container.classList.contains(treeClass)) return container.F.tree; // Already created
let opt = F.initOptions("tree", container, {}, options);
opt._getSelectedItems = getSelectedItems;
opt._selectItem = selectItem;
opt._deselectItem = deselectItem;
opt._isItemSelected = isItemSelected;
opt._hoverItem = hoverItem;
opt._isItemExpandedPublic = isItemExpandedPublic;
opt._setItemExpanded = setItemExpanded;
opt._updateItem = updateItem;
opt._insertItem = insertItem;
opt._removeItem = removeItem;
opt._updateView = updateView;
let focusedItem, focusedItemElement;
let selectionStartElement;
const minDragDistance = 4;
let isPointerDown = false;
let pointerDownPosition;
let isDragging = false;
let draggedItems, dragSelected;
let dragIndicator;
let dropTarget, dropPosition;
let dragExpandTimeout, dragExpandTarget;
container.classList.add(treeClass);
if (container.tabIndex === -1)
container.tabIndex = 0;
container.F.on("blur" + treeEventClass, () => container.classList.remove("ff-focus-visible"));
container.F.on("keydown" + treeEventClass, event => {
if (event.key !== "Control" && event.key !== "Shift")
container.classList.add("ff-focus-visible");
if (event.key === "ArrowUp") {
event.preventDefault();
let prevElement = getPreviousVisibleItemElement(focusedItemElement);
if (prevElement) {
if (event.ctrlKey && !event.shiftKey) {
// Just move focused item, can then be selected with Space
const prevItem = prevElement.firstChild._item;
setFocusedItem(prevItem, prevElement);
}
else {
// Apply single or range selection (Ctrl+Shift is not supported here)
clickItem(prevElement, event.shiftKey, event.ctrlKey);
}
scrollItemIntoView(prevElement);
}
}
else if (event.key === "ArrowDown") {
event.preventDefault();
let nextElement = getNextVisibleItemElement(focusedItemElement);
if (nextElement) {
if (event.ctrlKey && !event.shiftKey) {
// Just move focused item, can then be selected with Space
const nextItem = nextElement.firstChild._item;
setFocusedItem(nextItem, nextElement);
}
else {
// Apply single or range selection (Ctrl+Shift is not supported here)
clickItem(nextElement, event.shiftKey, event.ctrlKey);
}
scrollItemIntoView(nextElement);
}
}
else if (event.key === "PageUp") {
event.preventDefault();
// TODO: for scrolling
}
else if (event.key === "PageDown") {
event.preventDefault();
// TODO: for scrolling
}
else if (event.key === " ") {
event.preventDefault();
// Select/deselect item (intended for use after moving focused item with Ctrl+Arrow)
clickItem(focusedItemElement, /*shiftKey*/ false, /*ctrlKey*/ true);
}
else if (event.key === "ArrowLeft") {
event.preventDefault();
let expanded = isItemExpanded(focusedItemElement);
if (!expanded || event.ctrlKey) {
// Already collapsed or not expandable, go to parent
// (with Ctrl key: always go to parent, never collapse)
const parentElement = getParentItemElement(focusedItemElement);
if (parentElement) {
if (event.ctrlKey) {
const parentItem = parentElement.firstChild._item;
setFocusedItem(parentItem, parentElement);
}
else {
clickItem(parentElement, /*shiftKey*/ false, /*ctrlKey*/ false);
}
}
}
else {
// Collapse item
toggleItemExpanded(focusedItemElement, false);
}
}
else if (event.key === "ArrowRight") {
event.preventDefault();
let expanded = isItemExpanded(focusedItemElement);
if (expanded) {
// Already expanded, go to first child
let nextElement = getNextVisibleItemElement(focusedItemElement);
if (nextElement) {
if (event.ctrlKey) {
const nextItem = nextElement.firstChild._item;
setFocusedItem(nextItem, nextElement);
}
else {
clickItem(nextElement, event.shiftKey, event.ctrlKey);
}
scrollItemIntoView(nextElement);
}
}
else {
// Expand item if possible
toggleItemExpanded(focusedItemElement, true);
}
}
else if (event.key === "Enter") {
event.preventDefault();
// Expand/collapse item (does nothing if not supported, i.e. there's no expander icon)
let expanded = isItemExpanded(focusedItemElement);
if (typeof expanded === "boolean")
toggleItemExpanded(focusedItemElement);
}
else {
opt.keyHandler && opt.keyHandler(event);
}
});
if (opt.items.length > 0)
focusedItem = opt.items[0];
updateView();
function updateView() {
// Remember the current scroll position and clear all elements
const scroll = container.scrollY;
container.replaceChildren();
if (opt.singleRoot && opt.items.length > 0) {
container.appendChild(createItemElement(opt.items[0], true));
}
else {
for (let item of opt.items) {
container.appendChild(createItemElement(item));
}
}
// Restore scrolling
container.scroll(container.scrollX, scroll);
}
function createItemElement(item, isRoot) {
const element = document.createElement("div");
element.classList.add("tree-item");
if (isRoot)
element.classList.add("single-root");
const mainPart = document.createElement("div");
mainPart._item = item;
mainPart.classList.add("main-part");
element.appendChild(mainPart);
const expander = document.createElement("div");
const expanderIcon = document.createElement("div");
if (!isRoot) {
expander.classList.add("expander");
expanderIcon.classList.add("expander-icon");
expander.appendChild(expanderIcon);
}
mainPart.appendChild(expander);
const itemContent = document.createElement("div");
itemContent.classList.add("content");
mainPart.appendChild(itemContent);
// Add hover events
itemContent.addEventListener("pointerenter", () => {
opt.itemHover && opt.itemHover(item, true);
});
itemContent.addEventListener("pointerleave", () => {
isPointerDown = false;
clearTimeout(dragExpandTimeout);
draggedItems = dropTarget = dropPosition = undefined;
itemContent.style.cursor = "";
if (dragIndicator) {
dragIndicator.remove();
dragIndicator = undefined;
}
opt.itemHover && opt.itemHover(item, false);
});
// Add click event
itemContent.addEventListener("click", e => {
if (isDragging)
return; // Not a click but a completed drag
clickItem(element, e.shiftKey, e.ctrlKey, e.detail);
});
// Add drag&drop events
itemContent.addEventListener("pointerdown", e => {
isDragging = false;
isPointerDown = true;
pointerDownPosition = [e.clientX, e.clientY];
});
itemContent.addEventListener("pointerup", () => {
if (isDragging && dropTarget) {
// Drop items, use dropTarget and dropPosition ("before"/"after"/"replace")
const parentElement = getParentItemElement(dropTarget);
const parentItem = parentElement?.firstChild._item;
const parentChildren = parentItem ? parentItem[opt.childrenKey] : opt.items;
const dropTargetItem = dropTarget.firstChild._item;
let newIndex = parentChildren.indexOf(dropTargetItem);
if (dropPosition === "after")
newIndex++;
for (let i = 0; i < draggedItems.length; i++) {
const draggedItem = draggedItems[i];
const draggedItemElement = findItemElement(draggedItem);
const draggedParentElement = getParentItemElement(draggedItemElement);
const draggedParent = draggedParentElement?.firstChild._item;
const draggedParentChildren = draggedParent ? draggedParent[opt.childrenKey] : opt.items;
// Remove the tree item
removeItem(draggedItem);
// Also remove the item from the data source
const oldIndex = draggedParentChildren.indexOf(draggedItem);
draggedParentChildren.splice(oldIndex, 1);
if (draggedParent === parentItem && oldIndex < newIndex)
newIndex--;
// Insert it back to the target place
parentChildren.splice(newIndex, 0, draggedItem);
newIndex++;
// Also create the new tree item for it
insertItem(draggedItem, parentItem);
// Remove replaced item
if (dropPosition === "replace" && i === 0) {
removeItem(dropTargetItem);
parentChildren.splice(newIndex, 1);
}
}
opt.dropHandler && opt.dropHandler(draggedItems, dropTargetItem, dropPosition);
if (dragSelected) {
// Restore selection of dragged items
draggedItems.forEach((item, index) => {
const dragItemElement = findItemElement(item);
const dragItemContent = getItemContentForItemElement(dragItemElement);
dragItemContent.classList.add("selected");
opt.itemSelected && opt.itemSelected(item, true);
if (index === 0)
setFocusedItem(item, dragItemElement);
});
}
}
isPointerDown = false;
clearTimeout(dragExpandTimeout);
draggedItems = dropTarget = dropPosition = undefined;
itemContent.style.cursor = "";
if (dragIndicator) {
dragIndicator.remove();
dragIndicator = undefined;
}
});
itemContent.addEventListener("pointermove", e => {
if (isPointerDown) {
if (!isDragging && opt.dragdrop) {
let distance = Math.sqrt(Math.pow(e.clientX - pointerDownPosition[0], 2) + Math.pow(e.clientY - pointerDownPosition[1], 2));
if (distance >= minDragDistance) {
isDragging = true;
itemContent.setPointerCapture(e.pointerId);
itemContent.style.cursor = "grabbing";
dragIndicator = document.createElement("div");
dragIndicator.classList.add("tree-drag-indicator");
if (container.F.dark)
dragIndicator.classList.add("dark");
dragIndicator.style.position = "fixed";
dragIndicator.style.zIndex = "9999";
document.body.appendChild(dragIndicator);
// If clicking on a selected item, drag all selected items, otherwise only
// the item that was clicked on
dragSelected = itemContent.classList.contains("selected");
if (dragSelected)
draggedItems = getSelectedItems();
else
draggedItems = [itemContent.parentNode._item];
}
}
if (isDragging) {
// Find position of insertion marker
let treeRect = container.getBoundingClientRect();
let treeItemUnderPointer;
for (let dy = 0; dy < 8; dy++) {
// Always test near the right edge of the tree view because near the left
// edge could be a gap next to the children container where the ancestor
// items are detected. This also allows vertical dragging outside of the
// actual tree view rectangle (thanks to pointer capture).
// Attention must be paid to with width of scrollbars (40px should always
// be wider than a scollbar).
treeItemUnderPointer = document.elementsFromPoint(treeRect.right - 40, e.clientY - dy)
.filter(e => e.classList.contains("tree-item") || e.classList.contains("children"))[0];
if (!treeItemUnderPointer)
return; // Nothing of interest at this position
if (treeItemUnderPointer.classList.contains("tree-item"))
break;
// Between items, look further up (continue loop)
}
if (treeItemUnderPointer.classList.contains("children"))
return; // No tree item rectangle to be found
// Make sure we're not offering to move any items into their own children.
// For this, see if any of the ancestors of the target is being dragged.
if (dropTarget === null)
dropTarget = undefined;
let parent = treeItemUnderPointer;
do {
if (draggedItems.includes(parent.firstChild._item)) {
// This item cannot be used as target
itemContent.style.cursor = "not-allowed";
dragIndicator.style.width = "0";
dropTarget = null; // Separate value from initial undefined
break;
}
parent = getParentItemElement(parent);
}
while (parent);
if (dropTarget !== null) {
const pointerItemContent = getItemContentForItemElement(treeItemUnderPointer);
//console.log(pointerItemContent.querySelector(".label").textContent);
const itemRect = pointerItemContent.getBoundingClientRect();
dragIndicator.style.width = itemRect.width + "px";
dragIndicator.style.left = itemRect.left + "px";
let cy, height;
if (opt.isDropTarget && opt.isDropTarget(treeItemUnderPointer.firstChild._item)) {
// Use the item as target to be replaced
dropTarget = treeItemUnderPointer;
dropPosition = "replace";
cy = (itemRect.top + itemRect.bottom) / 2;
height = itemRect.height;
}
else if (e.clientY < itemRect.top + itemRect.height / 2) {
// Upper half of the hovered item
dropTarget = treeItemUnderPointer;
dropPosition = "before";
const prevItemElement = getPreviousVisibleItemElement(treeItemUnderPointer);
if (prevItemElement) {
const prevItemContent = getItemContentForItemElement(prevItemElement);
const prevRect = prevItemContent.getBoundingClientRect();
cy = (itemRect.top + prevRect.bottom) / 2;
}
else {
cy = itemRect.top;
}
height = 3;
}
else {
// Lower half of the hovered item
dropTarget = treeItemUnderPointer;
dropPosition = "after";
const nextItemElement = getNextVisibleItemElement(treeItemUnderPointer);
if (nextItemElement) {
const nextItemContent = getItemContentForItemElement(nextItemElement);
const nextRect = nextItemContent.getBoundingClientRect();
const childrenContainer = treeItemUnderPointer.firstChild.nextSibling;
if (childrenContainer.style.display !== "none") {
// Lower half of an expanded parent item: Change horizontal
// placement to that of the first child to indicate that the new
// position is before the first child (not after the parent)
dropTarget = nextItemElement;
dropPosition = "before";
dragIndicator.style.width = nextRect.width + "px";
dragIndicator.style.left = nextRect.left + "px";
}
cy = (itemRect.bottom + nextRect.top) / 2;
}
else {
cy = itemRect.bottom;
}
height = 3;
}
if (opt.singleRoot && dropPosition === "before" && dropTarget.classList.contains("single-root")) {
// Prohibit dragging before the single root item
itemContent.style.cursor = "not-allowed";
dragIndicator.style.width = "0";
dropTarget = null; // Separate value from initial undefined
}
else if (opt.dragHandler && !opt.dragHandler(draggedItems, dropTarget)) {
// Dragging prohibited
itemContent.style.cursor = "not-allowed";
dragIndicator.style.width = "0";
dropTarget = null; // Separate value from initial undefined
}
else {
itemContent.style.cursor = "grabbing";
dragIndicator.style.top = (cy - height / 2) + "px";
dragIndicator.style.height = height + "px";
dragIndicator.classList.toggle("replace", dropPosition === "replace");
}
}
// When entering and hovering another expandable item for a moment, expand it
if (treeItemUnderPointer !== dragExpandTarget) {
clearTimeout(dragExpandTimeout);
dragExpandTarget = undefined;
if (isItemExpanded(treeItemUnderPointer) === false) {
dragExpandTarget = treeItemUnderPointer;
dragExpandTimeout = setTimeout(() => toggleItemExpanded(dragExpandTarget, true), 1000);
}
}
// TODO: When near the top/bottom end of the tree view, scroll up/down slowly (speed depending on nearness to the edge)
// - also implement that in the dashboard drag code
}
}
});
const icon = document.createElement("div");
icon.classList.add("icon");
itemContent.appendChild(icon);
const label = document.createElement("div");
label.classList.add("label");
itemContent.appendChild(label);
updateItemContent(itemContent, item);
const childrenContainer = document.createElement("div");
childrenContainer.classList.add("children");
if (!isRoot)
childrenContainer.style.display = "none";
element.appendChild(childrenContainer);
const childItems = opt.childrenKey ? item[opt.childrenKey] : null;
if (childItems && childItems.length > 0) {
expander.style.visibility = "visible";
for (let childItem of childItems) {
childrenContainer.appendChild(createItemElement(childItem));
}
}
expander.addEventListener("click", e => {
//e.stopPropagation();
toggleItemExpanded(element);
});
if (item === focusedItem) {
focusedItemElement = element;
focusedItemElement.classList.add("focused");
}
return element;
}
function insertItem(item, parent) {
const childItems = opt.childrenKey ? (parent ? parent[opt.childrenKey] : opt.items) : null;
if (!childItems)
return;
let parentElement = findItemElement(parent);
const index = childItems.indexOf(item);
const childrenContainer = parentElement?.querySelector(".children") ?? container;
const insertBeforeChild = index < childrenContainer.children.length ? childrenContainer.children[index] : null;
childrenContainer.insertBefore(createItemElement(item), insertBeforeChild);
}
function removeItem(item) {
const itemElement = findItemElement(item);
if (!itemElement)
return;
let parentElement = getParentItemElement(itemElement);
itemElement.remove();
if (!parentElement)
parentElement = container.firstChild;
// If the focused item is removed, move the focus to its parent
if (itemElement.contains(focusedItemElement)) {
const parentItem = parentElement.firstChild._item;
setFocusedItem(parentItem, parentElement);
}
// Same for selection start element
if (itemElement.contains(selectionStartElement)) {
selectionStartElement = parentElement;
}
}
// Scrolls the item into view if necessary.
function scrollItemIntoView(itemElement) {
const pos = getElementVerticalPosition(itemElement);
const viewportPos = pos - container.scrollTop;
const itemContent = getItemContentForItemElement(itemElement);
if (viewportPos < 0 || viewportPos + itemContent.offsetHeight > container.clientHeight)
container.scrollTo({ top: pos - container.clientHeight / 2, behavior: "smooth" });
}
function clickItem(itemElement, shiftKey, ctrlKey, count) {
const item = itemElement.firstChild._item;
const itemContent = getItemContentForItemElement(itemElement);
// Move focus to clicked element
setFocusedItem(item, itemElement);
// Update selection
if (shiftKey && opt.multiSelect) {
// Set selection range end
// With ctrlKey: Extend selection range (don't deselect anything)
const direction = getElementVerticalPosition(itemElement, selectionStartElement);
const selectedElements = new Set();
const newlySelectedElements = [];
let el = selectionStartElement;
let elItemContent = getItemContentForItemElement(el);
if (!elItemContent.classList.contains("selected")) {
newlySelectedElements.push(el);
elItemContent.classList.add("selected");
}
selectedElements.add(el);
if (itemElement !== selectionStartElement) {
while (true) {
if (direction > 0)
el = getNextVisibleItemElement(el);
else
el = getPreviousVisibleItemElement(el);
elItemContent = getItemContentForItemElement(el);
if (!elItemContent.classList.contains("selected")) {
newlySelectedElements.push(el);
elItemContent.classList.add("selected");
}
selectedElements.add(el);
if (el === itemElement)
break;
}
}
if (!ctrlKey) {
container.F.querySelectorAll(".selected").forEach(selectedItemContent => {
const element = selectedItemContent.parentNode.parentNode;
if (!selectedElements.has(element)) {
selectedItemContent.classList.remove("selected");
if (opt.itemSelected)
opt.itemSelected(selectedItemContent.parentNode._item, false);
}
});
}
if (opt.itemSelected)
newlySelectedElements.forEach(e => opt.itemSelected(e.firstChild._item, true));
}
else if (ctrlKey && opt.multiSelect) {
// Toggle single item
selectionStartElement = itemElement;
itemContent.classList.toggle("selected");
opt.itemSelected && opt.itemSelected(item, itemContent.classList.contains("selected"));
}
else {
// Select single
selectionStartElement = itemElement;
itemContent.classList.add("selected");
Array.from(container.getElementsByClassName("selected")).forEach(selectedItemContent => {
if (selectedItemContent !== itemContent) {
selectedItemContent.classList.remove("selected");
opt.itemSelected && opt.itemSelected(selectedItemContent.parentNode._item, false);
}
});
opt.itemSelected && opt.itemSelected(item, true);
if (count === 2) {
// Double click
let expanded = isItemExpanded(itemElement);
if (typeof expanded === "boolean") {
if (opt.doubleClickExpand)
toggleItemExpanded(itemElement);
}
else {
opt.itemDoubleClicked && opt.itemDoubleClicked(item);
}
}
}
}
function updateItemContent(itemContent, item) {
const icon = itemContent.getElementsByClassName("icon")[0];
const itemIcon = opt.iconGetter ? opt.iconGetter(item) : null;
if (typeof itemIcon === "string") {
icon.innerHTML = itemIcon;
}
else if (itemIcon) {
icon.innerHTML = "";
icon.appendChild(itemIcon);
}
else {
icon.innerHTML = "■";
}
const label = itemContent.getElementsByClassName("label")[0];
label.textContent = opt.labelGetter ? opt.labelGetter(item) : "Item";
if (opt.styleGetter)
itemContent.setAttribute("style", opt.styleGetter(item));
}
// Returns true/false for current expanded state; undefined if not expandable.
function isItemExpanded(itemElement) {
if (!itemElement)
return; // Item not found
const expanderIcon = itemElement.firstChild?.getElementsByClassName("expander-icon")[0]; // only own expander from mainPart, not children
if (!expanderIcon)
return; // Item has no expander, must be (expanded) root item
const expander = expanderIcon.parentElement;
if (expander.style.visibility !== "visible")
return; // Item cannot be expanded
return expanderIcon.classList.contains("expanded");
}
// Returns true/false for new expanded state; undefined if not expandable.
function toggleItemExpanded(itemElement, force) {
if (!itemElement)
return;
const expanderIcon = itemElement.firstChild?.getElementsByClassName("expander-icon")[0]; // only own expander from mainPart, not children
if (!expanderIcon)
return; // Item has no expander, nothing to do
const childrenContainer = itemElement.firstChild.nextSibling;
const item = itemElement.firstChild._item;
if (force === true || force !== false && childrenContainer.style.display === "none") {
// Currently collapsed: expand now
expanderIcon.classList.add("expanded");
childrenContainer.style.display = "";
opt.itemExpanded && opt.itemExpanded(item, true);
return true;
}
else {
// Currently expanded: collapse now
expanderIcon.classList.remove("expanded");
childrenContainer.style.display = "none";
opt.itemExpanded && opt.itemExpanded(item, false);
// Deselect all now collapsed items
Array.from(childrenContainer.getElementsByClassName("selected")).forEach(selectedItemContent => {
selectedItemContent.classList.remove("selected");
opt.itemSelected && opt.itemSelected(selectedItemContent.parentNode._item, false);
});
// If the focused item is now collapsed, move the focus to the current item
if (itemElement.contains(focusedItemElement)) {
setFocusedItem(item, itemElement);
}
// Same for selection start element
if (itemElement.contains(selectionStartElement)) {
selectionStartElement = itemElement;
}
return false;
}
}
function getSelectedItems() {
const selectedItems = [];
Array.from(container.getElementsByClassName("selected")).forEach(selectedItemContent => {
selectedItems.push(selectedItemContent.parentNode._item);
});
return selectedItems;
}
function setFocusedItem(item, element) {
focusedItemElement && focusedItemElement.classList.remove("focused");
focusedItem = item;
focusedItemElement = element;
focusedItemElement.classList.add("focused");
}
function getItemContentForItemElement(itemElement) {
return itemElement.firstChild.firstChild.nextSibling;
}
function getParentItemElement(itemElement) {
if (itemElement.parentNode !== container)
return itemElement.parentNode.parentNode;
return null;
}
function getNextVisibleItemElement(itemElement) {
const childrenContainer = itemElement.firstChild.nextSibling;
if (childrenContainer.style.display !== "none") {
return childrenContainer.firstChild;
}
else if (itemElement.nextSibling) {
return itemElement.nextSibling;
}
else {
// Walk up parents and find next sibling on the way
let parentElement = getParentItemElement(itemElement);
while (parentElement) {
if (parentElement.nextSibling)
return parentElement.nextSibling;
parentElement = getParentItemElement(parentElement);
}
return null;
}
}
function getPreviousVisibleItemElement(itemElement) {
if (!itemElement.previousSibling) {
if (itemElement.parentNode === container)
return null;
return itemElement.parentNode.parentNode;
}
else {
// Find last descendent of previous sibling
itemElement = itemElement.previousSibling;
while (true) {
const childrenContainer = itemElement.firstChild.nextSibling;
if (childrenContainer.style.display === "none")
return itemElement;
itemElement = childrenContainer.lastChild;
}
}
}
function getElementVerticalPosition(itemElement, baseElement) {
let pos = 0;
do {
pos += itemElement.offsetTop;
itemElement = itemElement.offsetParent;
}
while (itemElement);
itemElement = baseElement || container;
do {
pos -= itemElement.offsetTop;
itemElement = itemElement.offsetParent;
}
while (itemElement);
return pos;
}
function findItemElement(item, baseElement) {
if (baseElement === undefined)
baseElement = container;
for (let i = 0; i < baseElement.children.length; i++) {
const itemElement = baseElement.children[i];
if (itemElement.firstChild._item === item)
return itemElement;
const childrenContainer = itemElement.firstChild.nextSibling;
const childSearch = findItemElement(item, childrenContainer);
if (childSearch)
return childSearch;
}
return null;
}
// Selects an item and makes sure it's visible.
function selectItem(item, focus, keepOtherSelected) {
const itemElement = findItemElement(item);
if (!itemElement)
return;
// Expand tree to item
let parentElement = getParentItemElement(itemElement);
while (parentElement) {
toggleItemExpanded(parentElement, true);
parentElement = getParentItemElement(parentElement);
}
// Scroll into view
scrollItemIntoView(itemElement);
// Select item
selectionStartElement = itemElement;
const itemContent = getItemContentForItemElement(itemElement);
itemContent.classList.add("selected");
if (!keepOtherSelected) {
Array.from(container.getElementsByClassName("selected")).forEach(selectedItemContent => {
if (selectedItemContent !== itemContent) {
selectedItemContent.classList.remove("selected");
opt.itemSelected && opt.itemSelected(selectedItemContent.parentNode._item, false);
}
});
}
if (focus)
setFocusedItem(item, itemElement);
opt.itemSelected && opt.itemSelected(item, true);
}
// Deselects an item.
function deselectItem(item, focus) {
const itemElement = findItemElement(item);
if (!itemElement)
return;
const itemContent = getItemContentForItemElement(itemElement);
itemContent.classList.remove("selected");
if (focus)
setFocusedItem(item, itemElement);
opt.itemSelected && opt.itemSelected(item, false);
}
// Determines whether an item is selected.
function isItemSelected(item) {
const itemElement = findItemElement(item);
if (!itemElement)
return;
const itemContent = getItemContentForItemElement(itemElement);
return itemContent.classList.contains("selected");
}
// Sets or gets the hovered state of an item.
// NOTE: This state is used for highlighting items in the tree, it cannot be used to determine
// whether the user has currently placed the mouse cursor over an item. Use the callback
// function in options.itemHover for this purpose instead.
function hoverItem(item, isHovered) {
const itemElement = findItemElement(item);
if (!itemElement)
return;
const itemContent = getItemContentForItemElement(itemElement);
if (isHovered !== undefined)
itemContent.classList.toggle("hover", isHovered);
else
return itemContent.classList.contains("hover");
}
// Gets the expanded state of an item.
function isItemExpandedPublic(item) {
return isItemExpanded(findItemElement(item));
}
// Expands or collapses an item.
function setItemExpanded(item, expanded) {
return toggleItemExpanded(findItemElement(item), !!expanded);
}
// Updates the item in the tree view.
function updateItem(item) {
const itemElement = findItemElement(item);
if (!itemElement)
return;
const itemContent = getItemContentForItemElement(itemElement);
updateItemContent(itemContent, item);
}
// Return current plugin instance
return container.F.tree;
}
// Gets all selected items in the visual order.
function getSelectedItems() {
return callMethod(this, "getSelectedItems", [])
}
// Selects an item and makes sure it's visible.
function selectItem(item, focus, keepOtherSelected) {
return callMethod(this, "selectItem", [item, focus, keepOtherSelected])
}
// Deselects an item.
function deselectItem(item, focus) {
return callMethod(this, "deselectItem", [item, focus])
}
// Determines whether an item is selected.
function isItemSelected(item) {
return callMethod(this, "isItemSelected", [item])
}
// Sets or gets the hovered state of an item.
// NOTE: This state is used for highlighting items in the tree, it cannot be used to determine
// whether the user has currently placed the mouse cursor over an item. Use the callback
// function in options.itemHover for this purpose instead.
function hoverItem(item, isHovered) {
return callMethod(this, "hoverItem", [item, isHovered])
}
// Gets the expanded state of an item.
function isItemExpanded(item) {
return callMethod(this, "isItemExpandedPublic", [item])
}
// Expands or collapses an item.
function setItemExpanded(item, expanded) {
return callMethod(this, "setItemExpanded", [item, expanded])
}
// Updates the item in the tree view.
function updateItem(item) {
return callMethod(this, "updateItem", [item])
}
// Inserts a new item into the tree view. The item must already exist in its parent collection.
function insertItem(item, parent) {
return callMethod(this, "insertItem", [item, parent])
}
// Removes the item from the tree view.
function removeItem(item) {
return callMethod(this, "removeItem", [item])
}
// Updates the entire tree view from the data source, resetting the view state.
function updateView() {
return callMethod(this, "updateView", [])
}
// Helper function for plugin methods.
function callMethod(self, name, args) {
let container = self.first;
if (!container) return; // Nothing to do
let opt = F.loadOptions("tree", container);
if (!opt) return; // Tree not initialized
return opt["_" + name].apply(null, args);
}
// Deinitializes the plugin.
function deinit() {
return this.forEach(elem => {
if (!elem.classList.contains(treeClass)) return;
elem.classList.remove(treeClass);
elem.classList.remove("ff-focus-visible")
elem.off(treeEventClass);
elem.replaceChildren();
F.deleteOptions("tree", elem);
});
}
F.registerPlugin("tree", tree, {
defaultOptions: treeDefaults,
methods: {
getSelectedItems: getSelectedItems,