@olenbetong/appframe-core 1.3.7 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,73 +0,0 @@
1
- function getFormValue(value, type) {
2
- if (value === "") {
3
- return null;
4
- }
5
- else if (type === "number") {
6
- let numValue = Number(value.replaceAll(",", ".").replaceAll(/\s+/g, ""));
7
- return isNaN(numValue) ? undefined : numValue;
8
- }
9
- else if (["date", "datetime", "time"].includes(type)) {
10
- let dateValue = new Date(value);
11
- return isNaN(dateValue.getTime()) ? undefined : dateValue;
12
- }
13
- else if (type === "boolean") {
14
- return value.toLowerCase() === "true";
15
- }
16
- else if (type === "bigint") {
17
- return BigInt(value);
18
- }
19
- return value;
20
- }
21
- /**
22
- * Creates an object from form data. Uses the type map passed to convert the form values to the correct type.
23
- * @param form HTML form element to get form data from.
24
- * @param typeMap A mapping from field name to field type
25
- * @returns
26
- */
27
- export function formDataToObject(form, typeMap = {}) {
28
- let formData = new FormData(form);
29
- let result = {};
30
- for (let entry of formData.entries()) {
31
- let field = entry[0];
32
- let type = typeMap?.[field] ?? "string";
33
- let value = getFormValue(entry[1], type);
34
- if (result.hasOwnProperty(field)) {
35
- if (Array.isArray(result[field])) {
36
- result[field].push(value);
37
- }
38
- else {
39
- result[field] = [result[field], value];
40
- }
41
- }
42
- else {
43
- result[field] = value;
44
- }
45
- }
46
- return result;
47
- }
48
- /**
49
- * Creates a partial data object record from form data. The name property is mapped
50
- * to field name.
51
- *
52
- * @param form HTML form element to get FormData from
53
- * @param dataObject Data object for which to create a record from the form data
54
- * @returns {Partial<T>} Partial data object record
55
- */
56
- export function formDataToDataObjectRecord(form, dataObject) {
57
- let fields = dataObject.getFields();
58
- let types = Object.fromEntries(fields.map((field) => [field.name, field.type]));
59
- return formDataToObject(form, types);
60
- }
61
- /**
62
- * Creates procedure parameters from form data. The name property is mapped
63
- * to parameter names.
64
- *
65
- * @param form HTML Form element to get FormData from
66
- * @param procedure Appframe Procedure to create parameters for
67
- * @returns
68
- */
69
- export function formDataToProcedureParameters(form, procedure) {
70
- let parameters = procedure.getParameters();
71
- let types = Object.fromEntries(parameters.map((param) => [param.name, param.type]));
72
- return formDataToObject(form, types);
73
- }
@@ -1,110 +0,0 @@
1
- import { DataObject, DataObjectOptions, FieldDefinition } from "@olenbetong/appframe-data";
2
- import { RetrieveParameters } from "@olenbetong/appframe-data";
3
-
4
- import { downloadRequest } from "./downloadRequest.js";
5
-
6
- export type DataExportColumn = {
7
- name: string;
8
- caption?: string;
9
- cellFormat?: string;
10
- format?: string;
11
- };
12
-
13
- export type DataExportOptions = RetrieveParameters<any> & { columns: DataExportColumn[] };
14
- export type DataObjectReportParams = {
15
- articleId: string;
16
- controller?: AbortController;
17
- dataObject: string;
18
- format: "pdf" | "xlsx" | "csv" | "xls";
19
- fileName?: string;
20
- options: DataExportOptions;
21
- id?: string;
22
- title?: string;
23
- subTitle?: string;
24
- /**
25
- * Whether the title should be shown in the exported file (default true)
26
- */
27
- includeTitle?: boolean;
28
- /**
29
- * Whether the filter should be shown in the exported file (default true)
30
- */
31
- includefilter?: boolean;
32
- /**
33
- * Whether we should try to convert from camelCase to human readable names (default true)
34
- */
35
- formatHeaders?: boolean;
36
- /**
37
- * Whether the header should be frozen on top of the excel file.
38
- */
39
- freezeHeader?: boolean;
40
- };
41
-
42
- /**
43
- * Get columns and retrieve parameters from a data object to be used to create a report.
44
- * @param dataObject Data object to get parameters and columns from
45
- * @param specifiedOptions Options to override the data object options
46
- * @returns
47
- */
48
- export function exportOptionsFromDataObject(
49
- dataObject: DataObject<any>,
50
- specifiedOptions: Partial<DataObjectOptions<any>["parameters"] & { columns: FieldDefinition[] }>
51
- ) {
52
- return {
53
- columns: dataObject.getFields().map((field) => ({ name: field.name })),
54
- distinctRows: dataObject.getParameter("distinctRows"),
55
- filterObject: dataObject.getParameter("filterObject"),
56
- filterString: dataObject.getParameter("filterString"),
57
- maxRecords: dataObject.getParameter("maxRecords"),
58
- sortOrder: dataObject.getParameter("sortOrder"),
59
- whereClause: dataObject.getParameter("whereClause"),
60
- whereObject: dataObject.getParameter("whereObject"),
61
- ...specifiedOptions,
62
- };
63
- }
64
-
65
- const defaultOptions: DataExportOptions = {
66
- columns: [],
67
- distinctRows: false,
68
- filterObject: null,
69
- filterString: "",
70
- maxRecords: -1,
71
- sortOrder: [],
72
- whereClause: "",
73
- whereObject: null,
74
- };
75
-
76
- /**
77
- * Get a request object to download a report from a data object.
78
- * @param params Parameters to use to create a report
79
- * @returns Request object for the report
80
- */
81
- export function getDataObjectReportRequest(params: DataObjectReportParams) {
82
- const { articleId, dataObject, format, options } = params;
83
- const reportOptions = options ? Object.assign({}, defaultOptions, options) : defaultOptions;
84
- const body = "data=" + encodeURIComponent(JSON.stringify(reportOptions));
85
- const requestUrl = `/api/data-object/export/${articleId}/${dataObject}/${format}`;
86
-
87
- const controller = params.controller ?? new AbortController();
88
-
89
- return {
90
- controller,
91
- request: new Request(requestUrl, {
92
- body,
93
- credentials: "same-origin",
94
- headers: {
95
- "Content-Type": "application/x-www-form-urlencoded",
96
- },
97
- method: "POST",
98
- signal: controller.signal,
99
- }),
100
- };
101
- }
102
-
103
- export function openDataObjectReport(params: DataObjectReportParams) {
104
- const { dataObject, fileName, format } = params;
105
- const { request } = getDataObjectReportRequest(params);
106
-
107
- return downloadRequest(request, {
108
- fileName: `${fileName || dataObject}.${format}`,
109
- });
110
- }
@@ -1,80 +0,0 @@
1
- import type { DataObject, Procedure } from "@olenbetong/appframe-data";
2
-
3
- function getFormValue(value: string, type: string): string | number | boolean | Date | BigInt | null | undefined {
4
- if (value === "") {
5
- return null;
6
- } else if (type === "number") {
7
- let numValue = Number(value.replaceAll(",", ".").replaceAll(/\s+/g, ""));
8
-
9
- return isNaN(numValue) ? undefined : numValue;
10
- } else if (["date", "datetime", "time"].includes(type)) {
11
- let dateValue = new Date(value);
12
-
13
- return isNaN(dateValue.getTime()) ? undefined : dateValue;
14
- } else if (type === "boolean") {
15
- return value.toLowerCase() === "true";
16
- } else if (type === "bigint") {
17
- return BigInt(value);
18
- }
19
-
20
- return value;
21
- }
22
-
23
- /**
24
- * Creates an object from form data. Uses the type map passed to convert the form values to the correct type.
25
- * @param form HTML form element to get form data from.
26
- * @param typeMap A mapping from field name to field type
27
- * @returns
28
- */
29
- export function formDataToObject<T = any>(form: HTMLFormElement, typeMap: Record<string, string> = {}): Partial<T> {
30
- let formData = new FormData(form);
31
- let result: any = {};
32
-
33
- for (let entry of formData.entries()) {
34
- let field = entry[0];
35
- let type = typeMap?.[field] ?? "string";
36
- let value = getFormValue(entry[1] as string, type);
37
-
38
- if (result.hasOwnProperty(field)) {
39
- if (Array.isArray(result[field])) {
40
- result[field].push(value);
41
- } else {
42
- result[field] = [result[field], value];
43
- }
44
- } else {
45
- result[field] = value;
46
- }
47
- }
48
-
49
- return result as T;
50
- }
51
-
52
- /**
53
- * Creates a partial data object record from form data. The name property is mapped
54
- * to field name.
55
- *
56
- * @param form HTML form element to get FormData from
57
- * @param dataObject Data object for which to create a record from the form data
58
- * @returns {Partial<T>} Partial data object record
59
- */
60
- export function formDataToDataObjectRecord<T>(form: HTMLFormElement, dataObject: DataObject<T>): Partial<T> {
61
- let fields = dataObject.getFields();
62
- let types = Object.fromEntries(fields.map((field) => [field.name, field.type])) as Record<string, string>;
63
-
64
- return formDataToObject<T>(form, types);
65
- }
66
-
67
- /**
68
- * Creates procedure parameters from form data. The name property is mapped
69
- * to parameter names.
70
- *
71
- * @param form HTML Form element to get FormData from
72
- * @param procedure Appframe Procedure to create parameters for
73
- * @returns
74
- */
75
- export function formDataToProcedureParameters<T>(form: HTMLFormElement, procedure: Procedure<T, any>): T {
76
- let parameters = procedure.getParameters();
77
- let types = Object.fromEntries(parameters.map((param) => [param.name, param.type])) as Record<string, string>;
78
-
79
- return formDataToObject(form, types) as T;
80
- }
package/src/global.d.ts DELETED
@@ -1,56 +0,0 @@
1
- import {
2
- AfDataObject,
3
- DataHandler,
4
- DataObject,
5
- Procedure,
6
- ProcedureAPI,
7
- } from "@olenbetong/appframe-data";
8
- import UserSession from "./UserSession.js";
9
-
10
- export type Appframe = {
11
- article: {
12
- dataObjects?: Record<string, DataObject<any>>;
13
- i18n: Record<string, string>;
14
- id: string;
15
- hostName: string;
16
- };
17
- common?: {
18
- getLocalizedString?: (string: string) => string;
19
- };
20
- controls?: {
21
- alert?: (message: string, callback?: () => void) => void;
22
- confirm?: (options: {
23
- title: string;
24
- message: string;
25
- buttons: string[];
26
- callback: (button: number) => void;
27
- }) => void;
28
- };
29
- data?: {
30
- [index: string]: { new (options?: any): DataHandler<any> };
31
- } & { version?: string };
32
- DataObject?: {
33
- new <T>(options: any): DataObject<T>;
34
- getByID?: (name: string) => AfDataObject<unknown> | null;
35
- };
36
- Procedure?: { new <T, V>(options: any): Procedure<T, V> };
37
- ProcedureAPI?: { new <T, V>(options: any): ProcedureAPI<T, V> };
38
- userSession: UserSession;
39
- template: {
40
- loop: (template: string, data: any, custom?: any) => string;
41
- run: (template: string, row: any, custom?: any) => string;
42
- run2: (
43
- template: string,
44
- currentRow: any,
45
- previousRow?: any,
46
- state?: any,
47
- custom?: any
48
- ) => string;
49
- };
50
- };
51
-
52
- declare global {
53
- var __VERSION__: string;
54
- var __DEV__: number;
55
- var af: Appframe;
56
- }