gradio/frontend
1413k
1import type { Datatype, Headers, HeadersWithIDs } from "./utils";2import type { CellValue } from "../types";3import { cast_value_to_type } from "./utils";4 5export function make_headers(6 _head: Headers,7 col_count: [number, "fixed" | "dynamic"],8 els: Record<9 string,10 { cell: null | HTMLTableCellElement; input: null | HTMLTextAreaElement }11 >,12 make_id: () => string13): HeadersWithIDs {14 let _h = _head || [];15 if (col_count[1] === "fixed" && _h.length < col_count[0]) {16 const fill = Array(col_count[0] - _h.length)17 .fill("")18 .map((_, i) => `${i + _h.length}`);19 _h = _h.concat(fill);20 }21 22 if (!_h || _h.length === 0) {23 return Array(col_count[0])24 .fill(0)25 .map((_, i) => {26 const _id = make_id();27 els[_id] = { cell: null, input: null };28 return { id: _id, value: JSON.stringify(i + 1) };29 });30 }31 32 return _h.map((h, i) => {33 const _id = make_id();34 els[_id] = { cell: null, input: null };35 return { id: _id, value: h ?? "" };36 });37}38 39export function process_data(40 values: CellValue[][],41 els: Record<42 string,43 { cell: null | HTMLTableCellElement; input: null | HTMLTextAreaElement }44 >,45 data_binding: Record<string, any>,46 make_id: () => string,47 display_value: string[][] | null = null,48 datatype: Datatype | Datatype[]49): { id: string; value: CellValue; display_value?: string }[][] {50 if (!values || values.length === 0) {51 return [];52 }53 54 const result = values.map((row, i) => {55 return row.map((value, j) => {56 const _id = make_id();57 els[_id] = { cell: null, input: null };58 data_binding[_id] = value;59 60 let display = display_value?.[i]?.[j];61 62 if (display === undefined) {63 display = String(value);64 }65 66 const dtype = Array.isArray(datatype) ? datatype[j] : datatype;67 68 return {69 id: _id,70 value: cast_value_to_type(value, dtype),71 display_value: display72 };73 });74 });75 76 return result;77}78 