|
1 | 1 | import { isAvailable } from "@mendix/widget-plugin-platform/framework/is-available"; |
2 | | -import Big from "big.js"; |
3 | | -import { DynamicValue, ListValue, ObjectItem, ValueStatus } from "mendix"; |
| 2 | +import { ListValue, ObjectItem, ValueStatus } from "mendix"; |
4 | 3 | import { createNanoEvents, Emitter, Unsubscribe } from "nanoevents"; |
5 | | -import { ColumnsType, ShowContentAsEnum } from "../../../typings/DatagridProps"; |
6 | | - |
7 | | -/** Represents a single Excel cell (SheetJS compatible) */ |
8 | | -interface ExcelCell { |
9 | | - /** Cell type: 's' = string, 'n' = number, 'b' = boolean, 'd' = date */ |
10 | | - t: "s" | "n" | "b" | "d"; |
11 | | - /** Underlying value */ |
12 | | - v: string | number | boolean | Date; |
13 | | - /** Optional Excel number/date format, e.g. "yyyy-mm-dd" or "$0.00" */ |
14 | | - z?: string; |
15 | | - /** Optional pre-formatted display text */ |
16 | | - w?: string; |
17 | | -} |
18 | | - |
19 | | -type RowData = ExcelCell[]; |
20 | | - |
21 | | -type HeaderDefinition = { |
22 | | - name: string; |
23 | | - type: string; |
24 | | -}; |
25 | | - |
26 | | -type ValueReader = (item: ObjectItem, props: ColumnsType) => ExcelCell; |
27 | | - |
28 | | -type ReadersByType = Record<ShowContentAsEnum, ValueReader>; |
29 | | - |
30 | | -type RowReader = (item: ObjectItem) => RowData; |
| 4 | +import { ColumnsType } from "../../../typings/DatagridProps"; |
| 5 | +import { HeaderDefinition, RowData, readChunk } from "./cell-readers"; |
31 | 6 |
|
32 | 7 | type ColumnReader = (props: ColumnsType) => HeaderDefinition; |
33 | 8 |
|
@@ -262,132 +237,6 @@ export class DSExportRequest { |
262 | 237 | } |
263 | 238 | } |
264 | 239 |
|
265 | | -const readers: ReadersByType = { |
266 | | - attribute(item, props) { |
267 | | - const data = props.attribute?.get(item); |
268 | | - |
269 | | - if (data?.status !== "available") { |
270 | | - return makeEmptyCell(); |
271 | | - } |
272 | | - |
273 | | - const value = data.value; |
274 | | - const format = getCellFormat({ |
275 | | - exportType: props.exportType, |
276 | | - exportDateFormat: props.exportDateFormat, |
277 | | - exportNumberFormat: props.exportNumberFormat |
278 | | - }); |
279 | | - |
280 | | - if (value instanceof Date) { |
281 | | - return excelDate(format === undefined ? data.displayValue : value, format); |
282 | | - } |
283 | | - |
284 | | - if (typeof value === "boolean") { |
285 | | - return excelBoolean(value); |
286 | | - } |
287 | | - |
288 | | - if (value instanceof Big || typeof value === "number") { |
289 | | - const num = value instanceof Big ? value.toNumber() : value; |
290 | | - return excelNumber(num, format); |
291 | | - } |
292 | | - |
293 | | - return excelString(data.displayValue ?? ""); |
294 | | - }, |
295 | | - |
296 | | - dynamicText(item, props) { |
297 | | - const data = props.dynamicText?.get(item); |
298 | | - |
299 | | - switch (data?.status) { |
300 | | - case "available": |
301 | | - const format = getCellFormat({ |
302 | | - exportType: props.exportType, |
303 | | - exportDateFormat: props.exportDateFormat, |
304 | | - exportNumberFormat: props.exportNumberFormat |
305 | | - }); |
306 | | - |
307 | | - return excelString(data.value ?? "", format); |
308 | | - case "unavailable": |
309 | | - return excelString("n/a"); |
310 | | - default: |
311 | | - return makeEmptyCell(); |
312 | | - } |
313 | | - }, |
314 | | - |
315 | | - customContent(item, props) { |
316 | | - const value = props.exportValue?.get(item).value ?? ""; |
317 | | - const format = getCellFormat({ |
318 | | - exportType: props.exportType, |
319 | | - exportDateFormat: props.exportDateFormat, |
320 | | - exportNumberFormat: props.exportNumberFormat |
321 | | - }); |
322 | | - |
323 | | - return excelString(value, format); |
324 | | - } |
325 | | -}; |
326 | | - |
327 | | -function makeEmptyCell(): ExcelCell { |
328 | | - return { t: "s", v: "" }; |
329 | | -} |
330 | | - |
331 | | -function excelNumber(value: number, format?: string): ExcelCell { |
332 | | - return { |
333 | | - t: "n", |
334 | | - v: value, |
335 | | - z: format |
336 | | - }; |
337 | | -} |
338 | | - |
339 | | -function excelString(value: string, format?: string): ExcelCell { |
340 | | - return { |
341 | | - t: "s", |
342 | | - v: value, |
343 | | - z: format ?? undefined |
344 | | - }; |
345 | | -} |
346 | | - |
347 | | -function excelDate(value: string | Date, format?: string): ExcelCell { |
348 | | - return { |
349 | | - t: format === undefined ? "s" : "d", |
350 | | - v: value, |
351 | | - z: format |
352 | | - }; |
353 | | -} |
354 | | - |
355 | | -function excelBoolean(value: boolean): ExcelCell { |
356 | | - return { |
357 | | - t: "b", |
358 | | - v: value, |
359 | | - w: value ? "TRUE" : "FALSE" |
360 | | - }; |
361 | | -} |
362 | | - |
363 | | -interface DataExportProps { |
364 | | - exportType: "default" | "number" | "date" | "boolean"; |
365 | | - exportDateFormat?: DynamicValue<string>; |
366 | | - exportNumberFormat?: DynamicValue<string>; |
367 | | -} |
368 | | - |
369 | | -function getCellFormat({ exportType, exportDateFormat, exportNumberFormat }: DataExportProps): string | undefined { |
370 | | - switch (exportType) { |
371 | | - case "date": |
372 | | - return exportDateFormat?.status === "available" ? exportDateFormat.value : undefined; |
373 | | - case "number": |
374 | | - return exportNumberFormat?.status === "available" ? exportNumberFormat.value : undefined; |
375 | | - default: |
376 | | - return undefined; |
377 | | - } |
378 | | -} |
379 | | - |
380 | | -function createRowReader(columns: ColumnsType[]): RowReader { |
381 | | - return item => |
382 | | - columns.map(col => { |
383 | | - return readers[col.showContentAs](item, col); |
384 | | - }); |
385 | | -} |
386 | | - |
387 | | -function readChunk(data: ObjectItem[], columns: ColumnsType[]): RowData[] { |
388 | | - return data.map(createRowReader(columns)); |
389 | | -} |
390 | | - |
391 | 240 | declare global { |
392 | 241 | interface Window { |
393 | 242 | scheduler: { |
|
0 commit comments