@punica/form 1.2.0 → 2.1.0-next.0

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.
Files changed (73) hide show
  1. package/dist/controller/index.d.ts +46 -51
  2. package/dist/decorator/Readers.d.ts +2 -2
  3. package/dist/decorator/Services.d.ts +2 -2
  4. package/dist/decorator/Starters.d.ts +2 -2
  5. package/dist/define/FormItem.d.ts +12 -6
  6. package/dist/define/FormItemProperty.d.ts +5 -5
  7. package/dist/define/FormProperty.d.ts +5 -4
  8. package/dist/define/decorator.d.ts +16 -0
  9. package/dist/define/index.d.ts +1 -0
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +620 -22
  12. package/dist/index.mjs +572 -0
  13. package/dist/metadata/index.d.ts +16 -0
  14. package/dist/model/command/item.d.ts +11 -5
  15. package/dist/model/command/service.d.ts +25 -10
  16. package/dist/model/constants/index.d.ts +13 -2
  17. package/dist/model/form/index.d.ts +9 -4
  18. package/dist/model/index.d.ts +0 -1
  19. package/dist/model/item/index.d.ts +41 -12
  20. package/dist/model/reader/index.d.ts +5 -1
  21. package/dist/model/service/index.d.ts +28 -10
  22. package/dist/reader/index.d.ts +10 -17
  23. package/dist/register/index.d.ts +26 -39
  24. package/dist/shared.d.ts +20 -0
  25. package/dist/utils/deepCopy.d.ts +1 -1
  26. package/dist/utils/devtools.d.ts +11 -70
  27. package/dist/utils/index.d.ts +2 -1
  28. package/dist/utils/path.d.ts +10 -0
  29. package/package.json +21 -11
  30. package/dist/controller/index.js +0 -238
  31. package/dist/decorator/Readers.js +0 -15
  32. package/dist/decorator/Services.js +0 -15
  33. package/dist/decorator/Starters.js +0 -15
  34. package/dist/decorator/index.js +0 -19
  35. package/dist/define/FormItem.js +0 -26
  36. package/dist/define/FormItemProperty.js +0 -21
  37. package/dist/define/FormProperty.js +0 -15
  38. package/dist/define/index.js +0 -12
  39. package/dist/model/command/index.js +0 -18
  40. package/dist/model/command/item.js +0 -2
  41. package/dist/model/command/service.js +0 -2
  42. package/dist/model/constants/index.js +0 -2
  43. package/dist/model/form/index.js +0 -2
  44. package/dist/model/index.js +0 -24
  45. package/dist/model/item/index.js +0 -2
  46. package/dist/model/reader/index.js +0 -2
  47. package/dist/model/service/index.js +0 -2
  48. package/dist/model/starter/index.js +0 -2
  49. package/dist/model/util/GetItem.d.ts +0 -9
  50. package/dist/model/util/GetItem.js +0 -2
  51. package/dist/model/util/WriteItems.d.ts +0 -8
  52. package/dist/model/util/WriteItems.js +0 -2
  53. package/dist/model/util/index.d.ts +0 -2
  54. package/dist/model/util/index.js +0 -18
  55. package/dist/reader/decorators/base/index.d.ts +0 -35
  56. package/dist/reader/decorators/base/index.js +0 -82
  57. package/dist/reader/decorators/readers/FormItemsReader.d.ts +0 -17
  58. package/dist/reader/decorators/readers/FormItemsReader.js +0 -42
  59. package/dist/reader/decorators/readers/ItemDataReader.d.ts +0 -24
  60. package/dist/reader/decorators/readers/ItemDataReader.js +0 -42
  61. package/dist/reader/decorators/readers/ItemInitializerReader.d.ts +0 -16
  62. package/dist/reader/decorators/readers/ItemInitializerReader.js +0 -34
  63. package/dist/reader/decorators/readers/index.d.ts +0 -3
  64. package/dist/reader/decorators/readers/index.js +0 -12
  65. package/dist/reader/index.js +0 -30
  66. package/dist/reader/reader/Reader.d.ts +0 -13
  67. package/dist/reader/reader/Reader.js +0 -19
  68. package/dist/reader/reader/index.d.ts +0 -1
  69. package/dist/reader/reader/index.js +0 -8
  70. package/dist/register/index.js +0 -72
  71. package/dist/utils/deepCopy.js +0 -45
  72. package/dist/utils/devtools.js +0 -136
  73. package/dist/utils/index.js +0 -18
@@ -1,25 +1,54 @@
1
1
  import { CommandItem } from '../command';
2
+ /**
3
+ * A field of a form. Extend it with the properties of your own form system:
4
+ * `type AppItem<E> = FormItem<E> & { layout?: Layout }`.
5
+ */
2
6
  export type FormItem<E> = {
7
+ /** The property of the entity this item belongs to. */
3
8
  property: keyof E;
9
+ /** Path of the value inside the entity, for nested values (`address/city`). */
4
10
  path?: string;
11
+ /** The label or display text of the item. */
5
12
  label: string;
6
- required?: boolean;
13
+ /** The form item type the item was created with. */
7
14
  type?: string;
15
+ /**
16
+ * The value of the item. Deliberately `any`: its type depends on the
17
+ * property and on the form item type.
18
+ */
8
19
  value?: any;
9
- error?: boolean;
10
- errorMessages?: Array<string>;
20
+ required?: boolean;
11
21
  disabled?: boolean;
12
22
  hidden?: boolean;
13
- getData?: (params?: any) => Promise<any>;
14
- setValue?: (property: keyof E, value: any) => Promise<void>;
15
- control?: (command: CommandItem<E, any>) => Promise<Array<FormItem<E>>>;
16
- submitted?: (command: CommandItem<E, any>) => Promise<void>;
17
- initialize?: (command: CommandItem<E, any>) => Promise<Array<FormItem<E>>>;
18
- validation?: (command: CommandItem<E, any>) => Promise<ErrorDetail>;
19
- [key: string]: any;
23
+ /** Whether the last validation of the item failed. */
24
+ error?: boolean;
25
+ /** The messages of the last failed validation. */
26
+ errorMessages?: string[];
27
+ /** Runs once while the form is created; the returned items are written to the form. */
28
+ initialize?(command: CommandItem<E, FormItem<E>>): Promise<Array<FormItem<E>>>;
29
+ /** Runs after the value of the item was set; the returned items are written to the form. */
30
+ control?(command: CommandItem<E, FormItem<E>>): Promise<Array<FormItem<E>>>;
31
+ /** Validates the item. */
32
+ validation?(command: CommandItem<E, FormItem<E>>): Promise<ErrorDetail>;
20
33
  };
34
+ /**
35
+ * A form item whose `value` has the type of its property. Use it as the item
36
+ * type of a form to get checked values after narrowing by `property`:
37
+ *
38
+ * const controller = await FormController.fromEntity<User, TypedFormItem<User>>(user);
39
+ * const item = controller.getForm().items[0];
40
+ * if (item.property === 'age') item.value; // number | undefined
41
+ */
42
+ export type TypedFormItem<E, F extends FormItem<E> = FormItem<E>> = {
43
+ [K in keyof E]: Omit<F, 'property' | 'value'> & {
44
+ property: K;
45
+ value?: E[K];
46
+ };
47
+ }[keyof E];
48
+ /**
49
+ * The result of a validation.
50
+ */
21
51
  export type ErrorDetail = {
22
52
  error: boolean;
23
- errorMessages?: Array<string>;
24
- [key: string]: any;
53
+ errorMessages?: string[];
25
54
  };
@@ -1,4 +1,8 @@
1
1
  import { Form, FormItem } from '..';
2
+ /**
3
+ * Reads metadata of the entity into the form while the form is started.
4
+ * A reader changes the form it is given.
5
+ */
2
6
  export interface IReader<E, F extends FormItem<E>> {
3
- read(entity: E, form: Form<E, F>, itemsMap?: Record<keyof E, number>): Promise<void>;
7
+ read(entity: E, form: Form<E, F>): void | Promise<void>;
4
8
  }
@@ -1,20 +1,38 @@
1
- import { CommandService, Form, FormItem } from '..';
1
+ import { CommandService, FormItem } from '..';
2
+ /**
3
+ * A service of a form. Register it with `@Services` and read it back with
4
+ * `controller.getService(Class)`.
5
+ */
2
6
  export interface IService {
7
+ /** A label for the service, shown by developer tools. */
3
8
  get name(): string;
4
9
  }
10
+ /**
11
+ * A service that reads or changes the form.
12
+ */
5
13
  export interface IServiceInitialize<E, F extends FormItem<E>> extends IService {
6
- initialize: (command: CommandService<E, F>) => Promise<void>;
14
+ /** Called once when the form is started. */
15
+ initialize: (command: CommandService<E, F>) => void | Promise<void>;
7
16
  }
8
- export interface IServiceControl<T = any> extends IService {
17
+ /**
18
+ * A service that is executed.
19
+ */
20
+ export interface IServiceControl<T = unknown> extends IService {
9
21
  run: (...args: any[]) => Promise<T>;
10
22
  }
23
+ /**
24
+ * A service that adds features to what the functions of form items receive.
25
+ */
11
26
  export interface IServiceCommand extends IService {
12
- addCustomFeaturesForCommandItem: () => {
13
- [key: string]: any;
14
- };
27
+ addCustomFeaturesForCommandItem: () => Record<string, unknown>;
15
28
  }
16
- export interface IServiceStartup<E, F extends FormItem<E>> extends IService {
17
- start: (entity: E, form: Form<E, F>) => {
18
- [key: string]: any;
19
- };
29
+ /**
30
+ * The services `controller.getService` knows the types of. Packages add
31
+ * theirs by declaration merging, which binds them to the entity of the form:
32
+ *
33
+ * declare module '@punica/form' {
34
+ * interface ServiceRegistry<E, F> { myService: MyService<E, F> }
35
+ * }
36
+ */
37
+ export interface ServiceRegistry<E, F extends FormItem<E>> {
20
38
  }
@@ -1,19 +1,12 @@
1
- import { IReader } from '..';
1
+ import { FormItem, IReader } from '..';
2
2
  /**
3
- * Decorator Design Pattern for a Reader System
4
- *
5
- * - The `IReader` interface defines the contract for all types of readers.
6
- * - The `Reader` class serves as the base reader.
7
- * - Decorators are implemented as classes (e.g., `FormItemsReader`, `ItemDataReader`, `ItemInitializerReader`) that wrap existing readers and enhance their behavior.
8
- * - The `createInitialReader` function creates an initial reader object with a chain of decorators applied.
9
- *
10
- * - `FormItemsReader` adds functionality related to reading form items.
11
- * - `ItemDataReader` adds functionality related to reading item data.
12
- * - `ItemInitializerReader` adds functionality related to initializing items.
13
- *
14
- * - The final `itemInitializerReader` returned by `createInitialReader` has all the decorators chained together,
15
- * creating a powerful reader object that can perform a series of operations in a coordinated way.
16
- *
17
- * @returns {IReader<any, any>} - Initial reader object with a chain of decorators.
3
+ * Creates the reader that turns an entity into a form:
4
+ *
5
+ * 1. the services, readers and starters of the class decorators
6
+ * 2. one item for every decorated property, in the order of the properties
7
+ * of the entity; decorated properties the entity does not hold (declared
8
+ * without a value) follow
9
+ * 3. the values of the entity
10
+ * 4. the `initialize` function of every item that has one
18
11
  */
19
- export declare const createInitialReader: () => IReader<any, any>;
12
+ export declare const createInitialReader: <E extends object, F extends FormItem<E>>() => IReader<E, F>;
@@ -1,43 +1,30 @@
1
- import { FormItem } from '..';
2
- type Render<F> = (props: F) => any;
3
- export declare class FormItemRegister<E, F extends FormItem<E>> {
1
+ /**
2
+ * What renders the items of a form item type. Punica Form is headless: it
3
+ * stores renderers for the application and never calls them.
4
+ */
5
+ export type Renderer = (props: any) => any;
6
+ /**
7
+ * The renderers of the form item types, by type.
8
+ */
9
+ export declare class FormItemRegister {
4
10
  #private;
5
- private static _instance;
6
11
  /**
7
- * Private constructor to prevent external instantiation.
12
+ * Registers the renderer of a type. The first registration of a type wins.
8
13
  */
9
- private constructor();
10
- /**
11
- * Singleton pattern: Get the instance of FormItemRegister.
12
- * @returns The instance of FormItemRegister.
13
- */
14
- static getInstance(): FormItemRegister<any, any>;
15
- /**
16
- * Register a form item property with its corresponding renderer.
17
- * @param property - The form item property.
18
- * @param renderer - The rendering function for the form item.
19
- */
20
- register(property: string, renderer: (props: F) => any): void;
21
- /**
22
- * Unregister a form item property.
23
- * @param property - The form item property to unregister.
24
- */
25
- unRegister(property: string): void;
26
- /**
27
- * Get the rendering function for a specific form item property.
28
- * @param property - The form item property.
29
- * @returns The rendering function for the specified property.
30
- */
31
- getItem(property: string): Render<F>;
32
- /**
33
- * Get all registered form item properties and their corresponding renderers.
34
- * @returns A map containing form item properties and their renderers.
35
- */
36
- getItems(): Map<string, Render<F>>;
37
- /**
38
- * Get an iterable of all registered form item properties.
39
- * @returns An iterable of form item properties.
40
- */
41
- getItemKeys(): IterableIterator<string>;
14
+ register(type: string, renderer: Renderer): void;
15
+ unregister(type: string): void;
16
+ /** The renderer of a type. */
17
+ getItem(type: string): Renderer | undefined;
18
+ /** The registered types. */
19
+ getItemKeys(): string[];
42
20
  }
43
- export {};
21
+ /**
22
+ * Creates a register of its own, for tests or for an application that hosts
23
+ * more than one form system.
24
+ */
25
+ export declare const createFormItemRegister: () => FormItemRegister;
26
+ /**
27
+ * The register `defineFormItem` and `FormController` use unless they are
28
+ * given another one.
29
+ */
30
+ export declare const defaultFormItemRegister: FormItemRegister;
@@ -0,0 +1,20 @@
1
+ import type { FormItemRegister } from './register';
2
+ /**
3
+ * The state every copy of this package shares. A project can load the ESM
4
+ * and the CommonJS build side by side (an app and its test runner, for
5
+ * example); decorators applied through one copy must be visible to the other.
6
+ */
7
+ type Shared = {
8
+ /** Class → key → data of the class decorators. */
9
+ formProperties: WeakMap<object, Map<string, unknown>>;
10
+ /** Class → key → property → data of the property decorators. */
11
+ formItemProperties: WeakMap<object, Map<string, Record<string, unknown>>>;
12
+ /** The default form item register, created on first use. */
13
+ register?: FormItemRegister;
14
+ /** Whether forms report to the DevTools extension. */
15
+ devTools: boolean;
16
+ /** Service instances that were given to a controller. */
17
+ usedServices: WeakSet<object>;
18
+ };
19
+ export declare const shared: Shared;
20
+ export {};
@@ -5,4 +5,4 @@
5
5
  * @param value - The value to copy
6
6
  * @returns The copied value
7
7
  */
8
- export declare const deepCopy: <T>(value: T, seen?: WeakMap<object, any>) => T;
8
+ export declare const deepCopy: <T>(value: T, seen?: WeakMap<object, unknown>) => T;
@@ -1,77 +1,18 @@
1
- import { Form, FormItem } from '../model';
2
- /**
3
- * Marker used by the DevTools extension to recognize bridge messages
4
- */
1
+ /** Marker the DevTools extension recognizes bridge messages by. */
5
2
  export declare const DEVTOOLS_MESSAGE_SOURCE = "punica-form-devtools";
3
+ /** Version of the message format. */
4
+ export declare const DEVTOOLS_PROTOCOL_VERSION = 2;
6
5
  /**
7
- * DevTools bridge for sending form events to Chrome DevTools extension
8
- */
9
- declare class DevToolsBridge {
10
- private static instance;
11
- private enabled;
12
- /**
13
- * Get singleton instance
14
- */
15
- static getInstance(): DevToolsBridge;
16
- /**
17
- * Enable DevTools bridge
18
- */
19
- enable(): void;
20
- /**
21
- * Disable DevTools bridge
22
- */
23
- disable(): void;
24
- /**
25
- * Check if DevTools bridge is enabled
26
- */
27
- isEnabled(): boolean;
28
- /**
29
- * Send message to DevTools extension
30
- */
31
- private sendMessage;
32
- /**
33
- * Send initialization event
34
- */
35
- sendInit<E, F extends FormItem<E>>(form: Form<E, F>): void;
36
- /**
37
- * Send entity data
38
- */
39
- sendEntity<E>(entity: E): void;
40
- /**
41
- * Send form update event
42
- */
43
- sendUpdate<E, F extends FormItem<E>>(form: Form<E, F>): void;
44
- /**
45
- * Send form reset event
46
- */
47
- sendReset<E, F extends FormItem<E>>(form: Form<E, F>): void;
48
- /**
49
- * Send item update event
50
- */
51
- sendUpdateItem<E, F extends FormItem<E>>(item: F | F[]): void;
52
- /**
53
- * Send register items event
54
- */
55
- sendRegisterItems(itemKeys: string[]): void;
56
- /**
57
- * Send property value update event
58
- */
59
- sendUpdatePropertyValue<E, F extends FormItem<E>>(property: keyof E, value: any, item: F): void;
60
- }
61
- /**
62
- * Get DevTools bridge instance
63
- */
64
- export declare const getDevToolsBridge: () => DevToolsBridge;
65
- /**
66
- * Enable DevTools bridge
6
+ * Makes the forms of the page report to the Punica Form DevTools extension.
7
+ * Call it once, in development only.
67
8
  */
68
9
  export declare const enableDevTools: () => void;
69
- /**
70
- * Disable DevTools bridge
71
- */
72
10
  export declare const disableDevTools: () => void;
11
+ export declare const isDevToolsEnabled: () => boolean;
73
12
  /**
74
- * Check if DevTools bridge is enabled
13
+ * Sends a message to the DevTools extension, to the page's own origin only.
14
+ * @param formId - Identifies the form among the forms of the page
15
+ * @param type - `@@INIT`, `ENTITY` or the name of a form event
16
+ * @param formItemKey - The property the message is about
75
17
  */
76
- export declare const isDevToolsEnabled: () => boolean;
77
- export {};
18
+ export declare const sendToDevTools: (formId: string, type: string, data: unknown, formItemKey?: string) => void;
@@ -1,2 +1,3 @@
1
- export * from './devtools';
1
+ export { DEVTOOLS_MESSAGE_SOURCE, DEVTOOLS_PROTOCOL_VERSION, disableDevTools, enableDevTools, isDevToolsEnabled } from './devtools';
2
2
  export * from './deepCopy';
3
+ export * from './path';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Reads the value at a path inside an object.
3
+ * @returns The value, or `undefined` when the path does not exist
4
+ */
5
+ export declare const readPropertyPath: (entity: unknown, path: string) => unknown;
6
+ /**
7
+ * Writes a value at a path inside an object, creating the objects and
8
+ * arrays on the way that do not exist yet.
9
+ */
10
+ export declare const writeToPropertyPath: (entity: object, path: string, value: unknown) => void;
package/package.json CHANGED
@@ -1,21 +1,35 @@
1
1
  {
2
2
  "name": "@punica/form",
3
- "version": "1.2.0",
4
- "description": "Form",
3
+ "version": "2.1.0-next.0",
4
+ "description": "Headless, framework-agnostic form engine",
5
5
  "author": {
6
6
  "name": "Uryan Sezgin TEKE",
7
7
  "email": "uryan.sezgin.teke@gmail.com",
8
8
  "url": "https://github.com/uryansezginteke"
9
9
  },
10
10
  "keywords": [
11
- "form"
11
+ "form",
12
+ "headless",
13
+ "decorators",
14
+ "typescript"
12
15
  ],
13
16
  "private": false,
14
17
  "license": "MIT",
15
- "main": "dist/index.js",
16
- "types": "dist/index.d.ts",
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.mjs",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.mjs",
25
+ "require": "./dist/index.js"
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "sideEffects": false,
17
30
  "publishConfig": {
18
- "access": "public"
31
+ "access": "public",
32
+ "provenance": true
19
33
  },
20
34
  "repository": {
21
35
  "url": "git+https://github.com/punicalab/punica-form.git"
@@ -23,12 +37,8 @@
23
37
  "files": [
24
38
  "dist"
25
39
  ],
26
- "dependencies": {
27
- "@punica/common": "^1.0.1",
28
- "@punica/util": "^1.0.2"
29
- },
30
40
  "scripts": {
31
- "build": "tsc -p tsconfig.build.json",
41
+ "build": "tsup lib/index.ts --format esm,cjs --target es2020 --clean && tsc -p tsconfig.build.json --emitDeclarationOnly",
32
42
  "clean": "rimraf dist",
33
43
  "lint": "eslint lib"
34
44
  }
@@ -1,238 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _FormController_serviceMap, _FormController_itemsMap, _FormController_form, _FormController_initialForm, _FormController_initialEntity;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.FormController = void 0;
16
- const common_1 = require("@punica/common");
17
- const __1 = require("..");
18
- /**
19
- * Controller for managing a form and its associated services.
20
- */
21
- class FormController extends common_1.BaseListener {
22
- //#region constructor
23
- /**
24
- * Private constructor to prevent direct instantiation.
25
- */
26
- constructor() {
27
- super();
28
- _FormController_serviceMap.set(this, void 0); // Map to store services by their names
29
- _FormController_itemsMap.set(this, void 0); // Map to store form items by their properties
30
- _FormController_form.set(this, void 0); // Current form data
31
- _FormController_initialForm.set(this, void 0); // Initial deep copy of the form data
32
- _FormController_initialEntity.set(this, void 0); // Initial entity for the form
33
- //#endregion
34
- /**
35
- * Trigger a form event.
36
- * @param eventType The type of the event
37
- * @param data Data associated with the event
38
- */
39
- this.fireEvent = (eventType, data) => {
40
- this.trigger(eventType, data);
41
- const devTools = (0, __1.getDevToolsBridge)();
42
- const isEnabled = devTools.isEnabled();
43
- if (isEnabled) {
44
- switch (eventType) {
45
- case 'UPDATE_FORM':
46
- devTools.sendUpdate(__classPrivateFieldGet(this, _FormController_form, "f"));
47
- // Also send entity update
48
- this.sendEntityToDevTools();
49
- break;
50
- case 'RESET':
51
- devTools.sendReset(__classPrivateFieldGet(this, _FormController_form, "f"));
52
- break;
53
- case 'UPDATE_ITEM':
54
- devTools.sendUpdateItem(data);
55
- break;
56
- case 'REGISTER_ITEMS':
57
- devTools.sendRegisterItems(data);
58
- break;
59
- }
60
- }
61
- };
62
- /**
63
- * Send entity data to DevTools
64
- */
65
- this.sendEntityToDevTools = () => {
66
- const devTools = (0, __1.getDevToolsBridge)();
67
- if (!devTools.isEnabled()) {
68
- return;
69
- }
70
- const getValues = this.getServices('getValues');
71
- // Debugging must never break the form
72
- getValues === null || getValues === void 0 ? void 0 : getValues.run().then((entity) => devTools.sendEntity(entity)).catch(() => { });
73
- };
74
- /**
75
- * Create a command service for the form.
76
- * @returns A CommandService for the form
77
- */
78
- this.createCommandService = () => {
79
- const getItem = (property) => {
80
- const index = __classPrivateFieldGet(this, _FormController_itemsMap, "f")[property];
81
- return Number.isInteger(index) ? __classPrivateFieldGet(this, _FormController_form, "f").items[index] : null;
82
- };
83
- const writeItems = async (items) => {
84
- for (const item of items) {
85
- const index = __classPrivateFieldGet(this, _FormController_itemsMap, "f")[item.property];
86
- if (Number.isInteger(index)) {
87
- __classPrivateFieldGet(this, _FormController_form, "f").items[index] = item;
88
- }
89
- }
90
- };
91
- const createCommandItem = async (item) => {
92
- var _a, _b, _c;
93
- let itemCustomCommand = {};
94
- for (const service of (_a = __classPrivateFieldGet(this, _FormController_form, "f").services) !== null && _a !== void 0 ? _a : []) {
95
- // Called on the service so that its own state stays reachable
96
- const command = (_c = (_b = service).addCustomFeaturesForCommandItem) === null || _c === void 0 ? void 0 : _c.call(_b);
97
- itemCustomCommand = { ...itemCustomCommand, ...command };
98
- }
99
- return {
100
- formItem: item,
101
- initialEntity: __classPrivateFieldGet(this, _FormController_initialEntity, "f"),
102
- getItem,
103
- writeItems,
104
- ...itemCustomCommand
105
- };
106
- };
107
- const command = {
108
- initialEntity: __classPrivateFieldGet(this, _FormController_initialEntity, "f"),
109
- getItem,
110
- writeItems,
111
- fireEvent: this.fireEvent,
112
- createCommandItem
113
- };
114
- // Getters keep services in sync with the current state of the controller
115
- return Object.defineProperties(command, {
116
- form: { enumerable: true, get: () => __classPrivateFieldGet(this, _FormController_form, "f") },
117
- initialForm: { enumerable: true, get: () => __classPrivateFieldGet(this, _FormController_initialForm, "f") },
118
- itemsMap: { enumerable: true, get: () => __classPrivateFieldGet(this, _FormController_itemsMap, "f") }
119
- });
120
- };
121
- /**
122
- * Registers form items by triggering the REGISTER_ITEMS event.
123
- */
124
- this.registerFormItems = () => {
125
- this.fireEvent('REGISTER_ITEMS', Array.from(__1.FormItemRegister.getInstance().getItemKeys()));
126
- };
127
- /**
128
- * Maps form items to their properties.
129
- */
130
- this.mapFormItems = () => {
131
- let index = 0;
132
- __classPrivateFieldSet(this, _FormController_itemsMap, {}, "f");
133
- for (const item of __classPrivateFieldGet(this, _FormController_form, "f").items) {
134
- __classPrivateFieldGet(this, _FormController_itemsMap, "f")[item.property] = index++;
135
- }
136
- };
137
- /**
138
- * Initializes the services defined in the form data.
139
- */
140
- this.initializeServices = async () => {
141
- var _a;
142
- for (const service of (_a = __classPrivateFieldGet(this, _FormController_form, "f").services) !== null && _a !== void 0 ? _a : []) {
143
- const command = this.createCommandService();
144
- const serviceControl = service;
145
- __classPrivateFieldGet(this, _FormController_serviceMap, "f")[service.name] = service;
146
- if (serviceControl.initialize) {
147
- await serviceControl.initialize(command);
148
- }
149
- }
150
- };
151
- /**
152
- * Executes the readers defined in the form data.
153
- */
154
- this.runReaders = async () => {
155
- var _a;
156
- for (const reader of (_a = __classPrivateFieldGet(this, _FormController_form, "f").readers) !== null && _a !== void 0 ? _a : []) {
157
- await reader.read(__classPrivateFieldGet(this, _FormController_initialEntity, "f"), __classPrivateFieldGet(this, _FormController_form, "f"), __classPrivateFieldGet(this, _FormController_itemsMap, "f"));
158
- }
159
- };
160
- /**
161
- * Executes the starters defined in the form data.
162
- */
163
- this.runStarters = async () => {
164
- var _a;
165
- for (const starter of (_a = __classPrivateFieldGet(this, _FormController_form, "f").starters) !== null && _a !== void 0 ? _a : []) {
166
- __classPrivateFieldSet(this, _FormController_form, await starter.run(__classPrivateFieldGet(this, _FormController_form, "f"), __classPrivateFieldGet(this, _FormController_initialEntity, "f")), "f");
167
- }
168
- };
169
- /**
170
- * Keeps a deep copy of the form items for the initial state.
171
- * Services, readers and starters are shared, not copied.
172
- */
173
- this.deepCopyForm = () => {
174
- __classPrivateFieldSet(this, _FormController_initialForm, { ...__classPrivateFieldGet(this, _FormController_form, "f"), items: (0, __1.deepCopy)(__classPrivateFieldGet(this, _FormController_form, "f").items) }, "f");
175
- };
176
- __classPrivateFieldSet(this, _FormController_serviceMap, {}, "f");
177
- __classPrivateFieldSet(this, _FormController_itemsMap, {}, "f");
178
- }
179
- /**
180
- * Static method to initialize the FormController with form data.
181
- * @param form The initial form data
182
- */
183
- static async fromForm(form) {
184
- const instance = new FormController();
185
- __classPrivateFieldSet(instance, _FormController_form, form, "f");
186
- __classPrivateFieldSet(instance, _FormController_initialEntity, {}, "f");
187
- return instance;
188
- }
189
- /**
190
- * Static method to initialize the FormController with an entity.
191
- * @param entity The entity for the form
192
- */
193
- static async fromEntity(entity) {
194
- const instance = new FormController();
195
- const initialReader = (0, __1.createInitialReader)();
196
- __classPrivateFieldSet(instance, _FormController_initialEntity, entity, "f");
197
- __classPrivateFieldSet(instance, _FormController_form, {
198
- items: null,
199
- services: null,
200
- starters: null,
201
- readers: null
202
- }, "f");
203
- await initialReader.read(__classPrivateFieldGet(instance, _FormController_initialEntity, "f"), __classPrivateFieldGet(instance, _FormController_form, "f"));
204
- return instance;
205
- }
206
- getServices(serviceName, ...additionalServiceNames) {
207
- if (additionalServiceNames.length > 0) {
208
- const list = [
209
- __classPrivateFieldGet(this, _FormController_serviceMap, "f")[serviceName],
210
- ...additionalServiceNames.map((name) => __classPrivateFieldGet(this, _FormController_serviceMap, "f")[name])
211
- ];
212
- return list;
213
- }
214
- return __classPrivateFieldGet(this, _FormController_serviceMap, "f")[serviceName];
215
- }
216
- /**
217
- * Starts the form controller by executing all necessary steps to initialize the form.
218
- * @returns A promise that resolves when the form is fully initialized.
219
- */
220
- async start() {
221
- this.registerFormItems();
222
- this.mapFormItems();
223
- await this.runReaders();
224
- await this.runStarters();
225
- // Services are initialized against the final form produced by starters
226
- this.mapFormItems();
227
- await this.initializeServices();
228
- this.deepCopyForm();
229
- const devTools = (0, __1.getDevToolsBridge)();
230
- if (devTools.isEnabled()) {
231
- devTools.sendInit(__classPrivateFieldGet(this, _FormController_form, "f"));
232
- this.sendEntityToDevTools();
233
- }
234
- return __classPrivateFieldGet(this, _FormController_form, "f");
235
- }
236
- }
237
- exports.FormController = FormController;
238
- _FormController_serviceMap = new WeakMap(), _FormController_itemsMap = new WeakMap(), _FormController_form = new WeakMap(), _FormController_initialForm = new WeakMap(), _FormController_initialEntity = new WeakMap();