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
|
export {staticSqrLayout, staticRectLayout, sweepToSideLayout};
const TILE_SPACING = 5;
const HEADER_SZ = 20;
const staticSqrLayout = { //lays out nodes as squares in a rectangle, with spacing
genLayout(node, x, y, w, h, hideHeader){
//get number-of-columns with lowest leftover empty space
let headerSz = (hideHeader ? 0 : HEADER_SZ);
let availW = w - TILE_SPACING, availH = h - headerSz - TILE_SPACING;
let numChildren = node.children.length, ar = availW/availH;
let lowestEmp = Number.POSITIVE_INFINITY, numCols, numRows, tileSize;
for (let nc = 1; nc <= numChildren; nc++){
let nr = Math.ceil(numChildren/nc);
let ar2 = nc/nr;
let frac = ar > ar2 ? ar2/ar : ar/ar2;
let tileSz = ar > ar2 ? availH/nr-TILE_SPACING : availW/nc-TILE_SPACING;
let empSpc = (1-frac)*availW*availH + (nc*nr-numChildren)*(tileSz - TILE_SPACING)**2;
if (empSpc < lowestEmp){
lowestEmp = empSpc;
numCols = nc;
numRows = nr;
tileSize = tileSz;
}
}
let childLayouts = Array(numChildren).fill();
for (let i = 0; i < numChildren; i++){
let childX = TILE_SPACING + (i % numCols)*(tileSize + TILE_SPACING);
let childY = TILE_SPACING + headerSz + Math.floor(i / numCols)*(tileSize + TILE_SPACING);
if (node.children[i].children.length == 0){
childLayouts[i] = {
x: childX, y: childY, w: tileSize, h: tileSize, headerSz: 0,
children: [],
contentW: tileSize, contentH: tileSize, empSpc: 0,
}
} else {
childLayouts[i] = this.genLayout(node.children[i], childX, childY, tileSize, tileSize, false);
lowestEmp += childLayouts[i].empSpc;
}
}
return {
x: x, y: y, w: w, h: h, headerSz: headerSz,
children: childLayouts,
contentW: numCols * (tileSize + TILE_SPACING) + TILE_SPACING,
contentH: numRows * (tileSize + TILE_SPACING) + TILE_SPACING + headerSz,
empSpc: lowestEmp,
}
},
initLayoutInfo(tree){
return;
},
updateLayoutInfoOnExpand(nodeList){
return;
},
updateLayoutInfoOnCollapse(nodeList){
return;
}
};
const staticRectLayout = {
genLayout(node, x, y, w, h, hideHeader, subLayout = staticRectLayout){
if (node.children.every(n => n.children.length == 0))
return staticSqrLayout.genLayout(node, x, y, w, h, hideHeader);
//find grid-arrangement with lowest leftover empty space
let headerSz = (hideHeader ? 0 : HEADER_SZ);
let availW = w - TILE_SPACING, availH = h - TILE_SPACING - headerSz;
let numChildren = node.children.length;
let rowBrks = null; //will holds node indices at which each row starts
let lowestEmp = Number.POSITIVE_INFINITY, rowBreaks, rowsOfCounts, childLayouts;
rowBrksLoop:
while (true){
//update rowBrks or exit loop
if (rowBrks == null){
rowBrks = [0];
} else {
let i = rowBrks.length-1;
while (true){
if (i > 0 && rowBrks[i] < numChildren-1 - (rowBrks.length-1 - i)){
rowBrks[i]++;
break;
} else if (i > 0){
i--;
} else {
if (rowBrks.length < numChildren){
rowBrks = Array.from({length: rowBrks.length+1}, (x,i) => i);
} else {
break rowBrksLoop;
}
break;
}
}
}
//create list-of-lists representing each row's cells' tileCounts
let rowsOfCnts = Array(rowBrks.length).fill();
for (let r = 0; r < rowBrks.length; r++){
let numNodes = (r == rowBrks.length-1) ? numChildren-rowBrks[r] : rowBrks[r+1]-rowBrks[r];
let rowNodeIdxs = Array.from({length: numNodes}, (x,i) => i+rowBrks[r]);
rowsOfCnts[r] = rowNodeIdxs.map(idx => node.children[idx].tileCount);
}
//get cell dims
let totalTileCount = node.children.map(n => n.tileCount).reduce((x,y) => x+y);
let cellHs = rowsOfCnts.map(row => row.reduce((x, y) => x+y) / totalTileCount * availH);
let cellWs = Array(numChildren).fill();
for (let r = 0; r < rowsOfCnts.length; r++){
let rowCount = rowsOfCnts[r].reduce((x,y) => x+y);
for (let c = 0; c < rowsOfCnts[r].length; c++){
cellWs[rowBrks[r]+c] = rowsOfCnts[r][c] / rowCount * availW;
}
}
//get cell x/y coords
let cellXs = Array(cellWs.length).fill(0);
for (let r = 0; r < rowBrks.length; r++){
for (let c = 1; c < rowsOfCnts[r].length; c++){
let nodeIdx = rowBrks[r]+c;
cellXs[nodeIdx] = cellXs[nodeIdx-1] + cellWs[nodeIdx-1];
}
}
let cellYs = Array(cellHs.length).fill(0);
for (let r = 1; r < rowBrks.length; r++){
cellYs[r] = cellYs[r-1] + cellHs[r-1];
}
//get child layouts and empty-space
let childLyts = Array(numChildren).fill(), empSpc = 0;
for (let r = 0; r < rowBrks.length; r++){
for (let c = 0; c < rowsOfCnts[r].length; c++){
let nodeIdx = rowBrks[r]+c;
let child = node.children[nodeIdx];
let childX = cellXs[nodeIdx] + TILE_SPACING, childY = cellYs[r] + TILE_SPACING + headerSz,
childW = cellWs[nodeIdx] - TILE_SPACING, childH = cellHs[r] - TILE_SPACING;
if (child.children.length == 0){
let contentSz = Math.min(childW, childH);
childLyts[nodeIdx] = {
x: childX, y: childY, w: contentSz, h: contentSz, headerSz: 0,
children: [],
contentW: contentSz, contentH: contentSz, empSpc: childW*childH - contentSz**2,
};
} else if (child.children.every(n => n.children.length == 0)){
childLyts[nodeIdx] = staticSqrLayout.genLayout(child, childX, childY, childW, childH, false);
} else {
childLyts[nodeIdx] = subLayout.genLayout(child, childX, childY, childW, childH, false);
}
empSpc += childLyts[nodeIdx].empSpc;
}
}
//check with best-so-far
if (empSpc < lowestEmp){
lowestEmp = empSpc;
rowBreaks = [...rowBrks];
rowsOfCounts = rowsOfCnts;
childLayouts = childLyts;
}
}
//for each row, shift empty right-space to rightmost cell
let minEmpHorzTotal = Number.POSITIVE_INFINITY;
for (let r = 0; r < rowBreaks.length; r++){
let empHorzTotal = 0, leftShiftTotal = 0;
for (let c = 0; c < rowsOfCounts[r].length - 1; c++){
let nodeIdx = rowBreaks[r] + c;
let empHorz = childLayouts[nodeIdx].w - childLayouts[nodeIdx].contentW;
childLayouts[nodeIdx].w -= empHorz;
empHorzTotal += empHorz;
childLayouts[nodeIdx+1].x -= empHorzTotal;
}
childLayouts[rowBreaks[r] + rowsOfCounts[r].length - 1].w += empHorzTotal;
if (empHorzTotal < minEmpHorzTotal)
minEmpHorzTotal = empHorzTotal;
}
//shift empty bottom-space to bottom-most row
let empVertTotal = 0;
for (let r = 0; r < rowBreaks.length - 1; r++){
let nodeIdxs = Array.from({length: rowsOfCounts[r].length}, (x,i) => rowBreaks[r] + i);
nodeIdxs.forEach(idx => childLayouts[idx].y -= empVertTotal);
let empVerts = nodeIdxs.map(idx => childLayouts[idx].h - childLayouts[idx].contentH);
let minEmpVert = Math.min(...empVerts);
nodeIdxs.forEach(idx => childLayouts[idx].h -= minEmpVert);
empVertTotal += minEmpVert;
}
let lastRowIdx = rowBreaks.length-1;
let lastNodeIdxs = Array.from({length: rowsOfCounts[lastRowIdx].length}, (x,i) => rowBreaks[lastRowIdx] + i);
lastNodeIdxs.forEach(idx => childLayouts[idx].y -= empVertTotal);
lastNodeIdxs.map(idx => childLayouts[idx].h += empVertTotal);
//determine layout
return {
x: x, y: y, w: w, h: h, headerSz: headerSz,
children: childLayouts,
contentW: w - minEmpHorzTotal,
contentH: h - empVertTotal,
empSpc: lowestEmp,
}
},
initLayoutInfo(tree){
if (tree.children.length > 0){
tree.children.forEach(e => this.initLayoutInfo(e));
}
this.updateLayoutInfo(tree);
},
updateLayoutInfoOnExpand(nodeList){ //given list of tree-nodes from expanded_child-to-parent, update layout-info
nodeList[0].children.forEach(this.updateLayoutInfo);
for (let node of nodeList){
this.updateLayoutInfo(node);
}
},
updateLayoutInfoOnCollapse(nodeList){ //given list of tree-nodes from child_to_collapse-to-parent, update layout-info
for (let node of nodeList){
this.updateLayoutInfo(node);
}
},
updateLayoutInfo(tree){
if (tree.children.length == 0){
tree.tileCount = 1;
} else {
tree.tileCount = tree.children.map(e => e.tileCount).reduce((x,y) => x+y);
}
}
};
const sweepToSideLayout = {
genLayout(node, x, y, w, h, hideHeader){
//separate leaf and non-leaf nodes
let leaves = [], nonLeaves = [];
node.children.forEach(n => (n.children.length == 0 ? leaves : nonLeaves).push(n));
//determine layout
let tempTree;
if (nonLeaves.length == 0){ //if all leaves, use squares-layout
return staticSqrLayout.genLayout(node, x, y, w, h, hideHeader);
} else if (leaves.length == 0){
tempTree = {tolNode: null, children: nonLeaves};
return staticRectLayout.genLayout(tempTree, x, y, w, h, hideHeader);
} else {
let ratio = leaves.length / (leaves.length + nonLeaves.map(e => e.tileCount).reduce((x,y) => x+y));
let headerSz = (hideHeader ? 0 : HEADER_SZ);
//get swept-area layout
let area = {x: x, y: y+headerSz, w: w, h: h-headerSz};
tempTree = {tolNode: null, children: leaves};
let leftLayout = staticSqrLayout.genLayout(tempTree, area.x, area.y, area.w*ratio, area.h, true);
let topLayout = staticSqrLayout.genLayout(tempTree, area.x, area.y, area.w, area.h*ratio, true);
//let sweptLayout = leftLayout;
let sweptLayout = (leftLayout.empSpc < topLayout.empSpc) ? leftLayout : topLayout;
sweptLayout.children.forEach(layout => {layout.y += headerSz});
//get remaining-area layout
let xyChg;
if (sweptLayout == leftLayout){
xyChg = [sweptLayout.contentW - TILE_SPACING, 0];
area.w += -sweptLayout.contentW + TILE_SPACING;
} else {
xyChg = [0, sweptLayout.contentH - TILE_SPACING];
area.h += -sweptLayout.contentH + TILE_SPACING;
}
tempTree = {tolNode: null, children: nonLeaves}
let nonLeavesLayout = staticRectLayout.genLayout(
tempTree, area.x, area.y, area.w, area.h, true, sweepToSideLayout);
nonLeavesLayout.children.forEach(layout => {layout.x += xyChg[0]; layout.y += xyChg[1] + headerSz;});
//return combined layouts
let children = leaves.concat(nonLeaves);
let layouts = sweptLayout.children.concat(nonLeavesLayout.children);
let layoutsInOldOrder = [...Array(node.children.length).keys()]
.map(i => children.findIndex(n => n == node.children[i]))
.map(i => layouts[i]);
return {
x: x, y: y, w: w, h: h, headerSz: headerSz,
//children: [...sweptLayout.children, ...nonLeavesLayout.children],
children: layoutsInOldOrder,
contentW: (sweptLayout == leftLayout) ?
sweptLayout.contentW + nonLeavesLayout.contentW - TILE_SPACING :
Math.max(sweptLayout.contentW, nonLeavesLayout.contentW),
contentH: (sweptLayout == leftLayout) ?
Math.max(sweptLayout.contentH, nonLeavesLayout.contentH) :
sweptLayout.contentH + nonLeavesLayout.contentH - TILE_SPACING,
empSpc: sweptLayout.empSpc + nonLeavesLayout.empSpc
};
}
},
initLayoutInfo(tree){
if (tree.children.length > 0){
tree.children.forEach(e => this.initLayoutInfo(e));
}
this.updateLayoutInfo(tree);
},
updateLayoutInfoOnExpand(nodeList){
nodeList[0].children.forEach(this.updateLayoutInfo);
for (let node of nodeList){
this.updateLayoutInfo(node);
}
},
updateLayoutInfoOnCollapse(nodeList){
for (let node of nodeList){
this.updateLayoutInfo(node);
}
},
updateLayoutInfo(tree){
if (tree.children.length == 0){
tree.tileCount = 1;
} else {
tree.tileCount = tree.children.map(e => e.tileCount).reduce((x,y) => x+y);
}
}
};
|