File size: 1,870 Bytes
6434c78 |
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 |
import type { Datatype, Headers, HeadersWithIDs } from "./utils";
import type { CellValue } from "../types";
import { cast_value_to_type } from "./utils";
export function make_headers(
_head: Headers,
col_count: [number, "fixed" | "dynamic"],
els: Record<
string,
{ cell: null | HTMLTableCellElement; input: null | HTMLTextAreaElement }
>,
make_id: () => string
): HeadersWithIDs {
let _h = _head || [];
if (col_count[1] === "fixed" && _h.length < col_count[0]) {
const fill = Array(col_count[0] - _h.length)
.fill("")
.map((_, i) => `${i + _h.length}`);
_h = _h.concat(fill);
}
if (!_h || _h.length === 0) {
return Array(col_count[0])
.fill(0)
.map((_, i) => {
const _id = make_id();
els[_id] = { cell: null, input: null };
return { id: _id, value: JSON.stringify(i + 1) };
});
}
return _h.map((h, i) => {
const _id = make_id();
els[_id] = { cell: null, input: null };
return { id: _id, value: h ?? "" };
});
}
export function process_data(
values: CellValue[][],
els: Record<
string,
{ cell: null | HTMLTableCellElement; input: null | HTMLTextAreaElement }
>,
data_binding: Record<string, any>,
make_id: () => string,
display_value: string[][] | null = null,
datatype: Datatype | Datatype[]
): { id: string; value: CellValue; display_value?: string }[][] {
if (!values || values.length === 0) {
return [];
}
const result = values.map((row, i) => {
return row.map((value, j) => {
const _id = make_id();
els[_id] = { cell: null, input: null };
data_binding[_id] = value;
let display = display_value?.[i]?.[j];
if (display === undefined) {
display = String(value);
}
const dtype = Array.isArray(datatype) ? datatype[j] : datatype;
return {
id: _id,
value: cast_value_to_type(value, dtype),
display_value: display
};
});
});
return result;
}
|