-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.svelte
132 lines (122 loc) · 3.04 KB
/
Tree.svelte
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
<script>
import { activeElement } from './store/index'
import Fa from 'svelte-fa'
import { clickHandler } from './helpers/clickHandlers'
import { faChevronRight } from '@fortawesome/free-solid-svg-icons'
export let tree;
export let secondaryIcon;
export let onSelectCallback;
export let faIcon;
/**
* Click handler for the leaf nodes
* marks the clicked element as the active element
* @param event DOM Click event
* @param key (string) unique identifier of the current node
*/
function leafNodeClickHandler(event, key) {
/**
* This event should not propagate to the parent (.node-desc) node
* as this might be misunderstood as a click event to expand/shrink
*/
event.stopPropagation();
/**
* Trigger the on select callback function
*/
onSelectCallback && onSelectCallback(key);
/**
* Remove tree-node-active class for previously active leaf node
*/
$activeElement && $activeElement.classList.remove('tree-node-active');
let node = event.target;
node.parentNode.classList.add('tree-node-active');
activeElement.set(node.parentNode);
}
</script>
<style>
.tree-child {
margin-left: 8px;
margin-top: 4px;
transform: scale(1,0);
transition: 0.2s;
height: 0px;
}
.complete-tree-node {
margin-bottom: 6px;
padding: 2px;
}
.node-desc .chev-right, .node-desc .secondary-icon {
transition: 0.4s ease-in-out;
transform-origin: center;
width: 20px;
margin: 1px 6px 2px 2px;
height: 20px;
}
.secondary-icon img {
width: 20px;
height: 20px;
}
.node-desc {
padding: 4px;
display: flex;
max-width: 60%;
}
:global(.tree-node-active) {
background: aliceblue;
}
.chevron-no-child {
opacity: 0.2;
}
.leaf-node {
cursor: pointer;
}
</style>
{#each tree as item,i}
{#if !item.child}
<div class="complete-tree-node">
<div class="node-desc leaf-node" on:click={(e) => clickHandler(e)}>
<div class="chev-right chevron-no-child">
<Fa icon={faChevronRight}/>
</div>
{#if secondaryIcon}
{#if faIcon}
<div class="secondary-icon">
<Fa icon={secondaryIcon}/>
</div>
{:else}
<div class="secondary-icon">
<img src={secondaryIcon} alt="secondaryIcon"/>
</div>
{/if}
{/if}
<div class="desc" on:click={(e) => leafNodeClickHandler(e, item.key, onSelectCallback, activeElement)}>
{item.desc}
</div>
</div>
</div>
{:else}
<div class="complete-tree-node">
<div class="node-desc" on:click={(e) => clickHandler(e)}>
<div class="chev-right">
<Fa icon={faChevronRight}/>
</div>
{#if secondaryIcon}
{#if faIcon}
<div class="secondary-icon">
<Fa icon={secondaryIcon}/>
</div>
{:else}
<div class="secondary-icon">
<img src={secondaryIcon} alt="secondaryIcon"/>
</div>
{/if}
{/if}
<div class="desc" on:click={(e) => e.stopPropagation()}>
{item.desc}
</div>
</div>
<div class='tree-child'>
<svelte:self tree={item.child} faIcon={faIcon} secondaryIcon={secondaryIcon} onSelectCallback={onSelectCallback}/>
</div>
</div>
{/if}
{/each}