-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathinitial-column-widths.ts
More file actions
61 lines (49 loc) · 1.59 KB
/
initial-column-widths.ts
File metadata and controls
61 lines (49 loc) · 1.59 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
import {percent, Percent} from '../includes/sizes.js';
const PERCENT_FULL = percent(100);
export type ColumnWidth = Percent | 'auto';
export interface Column {
preferredWidth: ColumnWidth;
minWidth: Percent;
}
export function calculateInitialWidths(columns: Column[]): Percent[] {
const finalWidths: Percent[] = columns.map(
(c) =>
typeof c.preferredWidth === 'number'
? percent(Math.max(c.preferredWidth, c.minWidth))
: percent(0) // auto placeholder
);
const autoIndices = columns
.map((c, i) => (c.preferredWidth === 'auto' ? i : -1))
.filter((i) => i >= 0);
const totalMinWidth = columns.reduce(
(sum, c) => percent(sum + c.minWidth),
percent(0)
);
if (totalMinWidth > PERCENT_FULL) {
const scale = PERCENT_FULL / totalMinWidth;
return columns.map((c) => percent(c.minWidth * scale));
}
const fixedWidthSum = finalWidths.reduce(
(sum, w) => percent(sum + w),
percent(0)
);
const remainingSpace = percent(PERCENT_FULL - fixedWidthSum);
if (remainingSpace > 0 && autoIndices.length > 0) {
const extraPerAuto = remainingSpace / autoIndices.length;
for (const i of autoIndices) {
finalWidths[i] = percent(Math.max(columns[i].minWidth, extraPerAuto));
}
return finalWidths;
}
if (autoIndices.length > 0) {
for (const i of autoIndices) {
finalWidths[i] = columns[i].minWidth;
}
return finalWidths;
}
if (remainingSpace > 0 && autoIndices.length === 0) {
const scale = PERCENT_FULL / fixedWidthSum;
return finalWidths.map((w) => percent(w * scale));
}
return finalWidths;
}