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
|
<script lang="ts">
import {defineComponent, PropType} from 'vue';
import Tile from './Tile.vue';
import ParentBar from './ParentBar.vue';
import {TolNode, LayoutNode, initLayoutTree, tryLayout} from '../lib';
import type {LayoutOptions} from '../lib';
// Import paths lack a .ts or .js extension because .ts makes vue-tsc complain, and .js makes vite complain
// Obtain tree-of-life data
import tolRaw from '../tol.json';
function preprocessTol(node: any): any {
// Add 'children' fields if missing
if (node.children == null){
node.children = [];
} else {
node.children.forEach(preprocessTol);
}
return node;
}
const tol: TolNode = preprocessTol(tolRaw);
// Configurable settings
const defaultLayoutOptions: LayoutOptions = {
tileSpacing: 8, //px
headerSz: 20, //px
minTileSz: 50, //px
maxTileSz: 200, //px
layoutType: 'sweep', //'sqr' | 'rect' | 'sweep'
rectMode: 'auto', //'horz' | 'vert' | 'linear' | 'auto'
sweepMode: 'left', //'left' | 'top' | 'shorter' | 'auto'
sweptNodesPrio: 'pow-2/3', //'linear' | 'sqrt' | 'pow-2/3'
sweepingToParent: true,
};
const defaultComponentOptions = {
// For leaf/non_leaf tile and separated-parent components
borderRadius: 5, //px
shadowNormal: '0 0 2px black',
shadowHighlight: '0 0 1px 2px greenyellow',
// For leaf and separated-parent components
imgTilePadding: 4, //px
imgTileFontSz: 15, //px
imgTileColor: '#fafaf9',
expandableImgTileColor: 'greenyellow', //yellow, greenyellow, turquoise,
// For non-leaf tile-group components
nonLeafBgColors: ['#44403c', '#57534e'], //tiles at depth N use the Nth color, repeating from the start as needed
nonLeafHeaderFontSz: 15, //px
nonLeafHeaderColor: '#fafaf9',
nonLeafHeaderBgColor: '#1c1917',
// Timing related
transitionDuration: 300, //ms
dblClickWait: 200, //ms
};
const defaultOwnOptions = {
tileAreaOffset: 5, //px (space between root tile and display boundary)
parentBarSz: defaultLayoutOptions.minTileSz * 2, //px (breadth of separated-parents area)
};
// Component holds a tree structure representing a subtree of 'tol' to be rendered
// Collects events about tile expansion/collapse and window-resize, and initiates relayout of tiles
export default defineComponent({
data(){
let layoutTree = initLayoutTree(tol, 0);
return {
layoutTree: layoutTree,
activeRoot: layoutTree,
// Options
layoutOptions: {...defaultLayoutOptions},
componentOptions: {...defaultComponentOptions},
...defaultOwnOptions,
// For window-resize handling
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
resizeThrottled: false,
resizeDelay: 50, //ms (increasing to 100 seems to cause resize-skipping when opening browser mobile-view)
};
},
computed: {
wideArea(): boolean{
return this.width >= this.height;
},
separatedParents(): LayoutNode[] | null {
if (this.activeRoot == this.layoutTree){
return null;
}
let parents = [];
let node = this.activeRoot.parent;
while (node != null){
parents.push(node);
node = node.parent;
}
return parents.reverse();
},
tileAreaPos(){
let pos = [this.tileAreaOffset, this.tileAreaOffset] as [number, number];
if (this.separatedParents != null){
if (this.wideArea){
pos[0] += this.parentBarSz;
} else {
pos[1] += this.parentBarSz;
}
}
return pos;
},
tileAreaDims(){
let dims = [
this.width - this.tileAreaOffset*2,
this.height - this.tileAreaOffset*2
] as [number, number];
if (this.separatedParents != null){
if (this.wideArea){
dims[0] -= this.parentBarSz;
} else {
dims[1] -= this.parentBarSz;
}
}
return dims;
},
parentBarDims(): [number, number] {
if (this.wideArea){
return [this.parentBarSz, this.height];
} else {
return [this.width, this.parentBarSz];
}
},
styles(): Record<string,string> {
return {
position: 'absolute',
left: '0',
top: '0',
width: '100vw', // Making this dynamic causes white flashes when resizing
height: '100vh',
backgroundColor: '#292524',
};
},
},
methods: {
onResize(){
if (!this.resizeThrottled){
this.width = document.documentElement.clientWidth;
this.height = document.documentElement.clientHeight;
tryLayout(this.activeRoot, this.tileAreaPos, this.tileAreaDims, this.layoutOptions, true);
// Prevent re-triggering until after a delay
this.resizeThrottled = true;
setTimeout(() => {this.resizeThrottled = false;}, this.resizeDelay);
}
},
// For click events
onInnerLeafClicked({layoutNode, domNode}: {layoutNode: LayoutNode, domNode: HTMLElement}){
let success = tryLayout(this.activeRoot, this.tileAreaPos, this.tileAreaDims, this.layoutOptions, false,
{type: 'expand', node: layoutNode});
if (!success){
// Trigger failure animation
domNode.classList.remove('animate-expand-shrink');
domNode.offsetWidth; // Triggers reflow
domNode.classList.add('animate-expand-shrink');
}
},
onInnerHeaderClicked({layoutNode, domNode}: {layoutNode: LayoutNode, domNode: HTMLElement}){
let success = tryLayout(this.activeRoot, this.tileAreaPos, this.tileAreaDims, this.layoutOptions, false,
{type: 'collapse', node: layoutNode});
if (!success){
// Trigger failure animation
domNode.classList.remove('animate-shrink-expand');
domNode.offsetWidth; // Triggers reflow
domNode.classList.add('animate-shrink-expand');
}
},
onInnerLeafDblClicked(layoutNode: LayoutNode){
if (layoutNode == this.activeRoot){
console.log('Ignored expand-to-view on root node');
return;
}
LayoutNode.hideUpward(layoutNode);
this.activeRoot = layoutNode;
tryLayout(layoutNode, this.tileAreaPos, this.tileAreaDims, this.layoutOptions, true,
{type: 'expand', node: layoutNode});
},
onInnerHeaderDblClicked(layoutNode: LayoutNode){
if (layoutNode.parent == null){
console.log('Ignored expand-to-view on root node');
return;
}
LayoutNode.hideUpward(layoutNode);
this.activeRoot = layoutNode;
tryLayout(layoutNode, this.tileAreaPos, this.tileAreaDims, this.layoutOptions, true);
},
// For preventing double-clicks from highlighting text
onMouseDown(evt: UIEvent){
if (evt.detail == 2){
evt.preventDefault();
}
},
},
created(){
window.addEventListener('resize', this.onResize);
tryLayout(this.activeRoot, this.tileAreaPos, this.tileAreaDims, this.layoutOptions, true);
},
unmounted(){
window.removeEventListener('resize', this.onResize);
},
components: {
Tile,
ParentBar,
},
});
</script>
<template>
<div :style="styles" @mousedown="onMouseDown">
<tile :layoutNode="layoutTree"
:headerSz="layoutOptions.headerSz" :tileSpacing="layoutOptions.tileSpacing" :options="componentOptions"
@leaf-clicked="onInnerLeafClicked" @header-clicked="onInnerHeaderClicked"
@leaf-dbl-clicked="onInnerLeafDblClicked" @header-dbl-clicked="onInnerHeaderDblClicked"/>
<parent-bar v-if="separatedParents != null"
:pos="[0,0]" :dims="parentBarDims" :nodes="separatedParents" :options="componentOptions"/>
</div>
</template>
<style>
.animate-expand-shrink {
animation-name: expand-shrink;
animation-duration: 300ms;
animation-iteration-count: 1;
animation-timing-function: ease-in-out;
}
@keyframes expand-shrink {
from {
transform: scale(1, 1);
}
50% {
transform: scale(1.1, 1.1);
}
to {
transform: scale(1, 1);
}
}
.animate-shrink-expand {
animation-name: shrink-expand;
animation-duration: 300ms;
animation-iteration-count: 1;
animation-timing-function: ease-in-out;
}
@keyframes shrink-expand {
from {
transform: translate3d(0,0,0) scale(1, 1);
}
50% {
transform: translate3d(0,0,0) scale(0.9, 0.9);
}
to {
transform: translate3d(0,0,0) scale(1, 1);
}
}
</style>
|