-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColumnWidth.ts
executable file
·65 lines (55 loc) · 1.93 KB
/
ColumnWidth.ts
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
import type { Column } from './DataGridVue'
import { asPxSize, getElementWidth, isPercentageSize, isPxSize } from './Html'
export function isRelativeSize(width?: string): boolean {
return /^\d+\*$/.test(width?.trim() ?? '')
}
export function calculateColumnWidths(columns: Column[], table: HTMLElement): string[] {
var map = new Map<string, string>()
var width = getElementWidth(table)
let trackedWidth = 0
// first set columns set with absolute px and % to know how much width is remaining
var remainingColumns = [] as Column[]
for (const column of columns) {
if (column.hidden) {
continue
}
if (!column.width) {
remainingColumns.push(column)
continue
}
if (isPxSize(column.width)) {
trackedWidth += Number.parseFloat(column.width)
map.set(column.field.fieldName, column.width)
continue
}
if (isPercentageSize(column.width)) {
const calculated = (Number.parseFloat(column.width) / 100) * width
trackedWidth += calculated
map.set(column.field.fieldName, asPxSize(calculated))
continue
}
remainingColumns.push(column)
}
// determine how many "weighted columns" we have based on relative widths set (e.g. 2*)
let weightedColumnNum = 0
for (const column of remainingColumns) {
if (isRelativeSize(column.width)) {
weightedColumnNum += Number.parseInt(column.width ?? '')
} else {
weightedColumnNum++
}
}
// set remaining columns to fill remaining space with weighted equal width
var equalWidth = (width - trackedWidth) / weightedColumnNum
for (const column of remainingColumns) {
const weight = isRelativeSize(column.width) ? Number.parseInt(column.width ?? '') : 1
map.set(column.field.fieldName, asPxSize(equalWidth * weight))
}
const widths = [] as string[]
for (const column of columns) {
if (!column.hidden) {
widths.push(map.get(column.field.fieldName) ?? '')
}
}
return widths
}