@aerogel/core 0.0.0-next.d824b40e5d06757cd9f47c9f771d916185df4f05 → 0.0.0-next.eed7a057cf5b844cd9f7fc6bda2d8df49fcd6736
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.
- package/dist/aerogel-core.d.ts +3212 -485
- package/dist/aerogel-core.js +2789 -0
- package/dist/aerogel-core.js.map +1 -0
- package/package.json +25 -31
- package/src/bootstrap/bootstrap.test.ts +7 -10
- package/src/bootstrap/index.ts +43 -15
- package/src/bootstrap/options.ts +4 -1
- package/src/components/AGAppLayout.vue +7 -2
- package/src/components/AGAppModals.vue +1 -1
- package/src/components/AGAppOverlays.vue +6 -2
- package/src/components/AGAppSnackbars.vue +2 -2
- package/src/components/composition.ts +23 -0
- package/src/components/forms/AGButton.vue +2 -2
- package/src/components/forms/AGCheckbox.vue +10 -3
- package/src/components/forms/AGForm.vue +11 -12
- package/src/components/forms/AGInput.vue +13 -7
- package/src/components/forms/AGSelect.story.vue +46 -0
- package/src/components/forms/AGSelect.vue +60 -0
- package/src/components/forms/index.ts +5 -6
- package/src/components/headless/forms/AGHeadlessButton.ts +3 -0
- package/src/components/headless/forms/AGHeadlessButton.vue +24 -13
- package/src/components/headless/forms/AGHeadlessInput.ts +37 -4
- package/src/components/headless/forms/AGHeadlessInput.vue +24 -8
- package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
- package/src/components/headless/forms/AGHeadlessInputError.vue +2 -2
- package/src/components/headless/forms/AGHeadlessInputInput.vue +45 -6
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +9 -3
- package/src/components/headless/forms/AGHeadlessInputTextArea.vue +43 -0
- package/src/components/headless/forms/AGHeadlessSelect.ts +42 -0
- package/src/components/headless/forms/AGHeadlessSelect.vue +77 -0
- package/src/components/headless/forms/AGHeadlessSelectButton.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectError.vue +26 -0
- package/src/components/headless/forms/AGHeadlessSelectLabel.vue +24 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.ts +4 -0
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +39 -0
- package/src/components/headless/forms/AGHeadlessSelectOptions.ts +3 -0
- package/src/components/headless/forms/composition.ts +10 -0
- package/src/components/headless/forms/index.ts +13 -1
- package/src/components/headless/modals/AGHeadlessModal.ts +30 -1
- package/src/components/headless/modals/AGHeadlessModal.vue +16 -12
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +13 -9
- package/src/components/headless/modals/AGHeadlessModalTitle.vue +14 -4
- package/src/components/headless/modals/index.ts +4 -6
- package/src/components/headless/snackbars/index.ts +25 -10
- package/src/components/index.ts +3 -1
- package/src/components/interfaces.ts +24 -0
- package/src/components/lib/AGErrorMessage.vue +16 -0
- package/src/components/lib/AGMarkdown.vue +54 -0
- package/src/components/lib/AGMeasured.vue +16 -0
- package/src/components/lib/AGProgressBar.vue +55 -0
- package/src/components/lib/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +6 -0
- package/src/components/modals/AGAlertModal.ts +18 -0
- package/src/components/modals/AGAlertModal.vue +4 -15
- package/src/components/modals/AGConfirmModal.ts +42 -0
- package/src/components/modals/AGConfirmModal.vue +10 -14
- package/src/components/modals/AGErrorReportModal.ts +32 -3
- package/src/components/modals/AGErrorReportModal.vue +8 -16
- package/src/components/modals/AGErrorReportModalButtons.vue +13 -11
- package/src/components/modals/AGErrorReportModalTitle.vue +3 -3
- package/src/components/modals/AGLoadingModal.ts +29 -0
- package/src/components/modals/AGLoadingModal.vue +4 -8
- package/src/components/modals/AGModal.ts +2 -1
- package/src/components/modals/AGModal.vue +18 -13
- package/src/components/modals/AGModalContext.ts +1 -1
- package/src/components/modals/AGModalContext.vue +15 -5
- package/src/components/modals/AGPromptModal.ts +41 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +13 -19
- package/src/components/snackbars/AGSnackbar.vue +4 -10
- package/src/components/utils.ts +13 -0
- package/src/directives/index.ts +7 -5
- package/src/directives/measure.ts +40 -0
- package/src/errors/Errors.state.ts +1 -1
- package/src/errors/Errors.ts +32 -30
- package/src/errors/JobCancelledError.ts +3 -0
- package/src/errors/index.ts +19 -29
- package/src/errors/utils.ts +35 -0
- package/src/forms/Form.test.ts +32 -3
- package/src/forms/Form.ts +90 -21
- package/src/forms/composition.ts +2 -2
- package/src/forms/index.ts +3 -1
- package/src/forms/utils.ts +34 -3
- package/src/forms/validation.ts +19 -0
- package/src/{main.ts → index.ts} +3 -0
- package/src/jobs/Job.ts +147 -0
- package/src/jobs/index.ts +10 -0
- package/src/jobs/listeners.ts +3 -0
- package/src/jobs/status.ts +4 -0
- package/src/lang/DefaultLangProvider.ts +46 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +44 -29
- package/src/lang/index.ts +7 -5
- package/src/plugins/Plugin.ts +1 -1
- package/src/plugins/index.ts +10 -7
- package/src/services/App.state.ts +29 -7
- package/src/services/App.ts +41 -6
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +112 -32
- package/src/services/Service.ts +154 -49
- package/src/services/Storage.ts +20 -0
- package/src/services/index.ts +14 -5
- package/src/services/store.ts +8 -5
- package/src/services/utils.ts +18 -0
- package/src/testing/index.ts +26 -0
- package/src/testing/setup.ts +11 -0
- package/src/ui/UI.state.ts +9 -2
- package/src/ui/UI.ts +251 -41
- package/src/ui/index.ts +13 -7
- package/src/ui/utils.ts +16 -0
- package/src/utils/composition/events.ts +3 -2
- package/src/utils/composition/persistent.test.ts +33 -0
- package/src/utils/composition/persistent.ts +11 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +24 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/markdown.test.ts +50 -0
- package/src/utils/markdown.ts +19 -6
- package/src/utils/tailwindcss.test.ts +26 -0
- package/src/utils/tailwindcss.ts +7 -0
- package/src/utils/vue.ts +30 -9
- package/.eslintrc.js +0 -3
- package/dist/aerogel-core.cjs.js +0 -2
- package/dist/aerogel-core.cjs.js.map +0 -1
- package/dist/aerogel-core.esm.js +0 -2
- package/dist/aerogel-core.esm.js.map +0 -1
- package/dist/virtual.d.ts +0 -11
- package/noeldemartin.config.js +0 -5
- package/src/components/basic/AGErrorMessage.vue +0 -16
- package/src/components/basic/AGMarkdown.vue +0 -36
- package/src/components/basic/index.ts +0 -5
- package/src/directives/initial-focus.ts +0 -11
- package/src/types/virtual.d.ts +0 -11
- package/tsconfig.json +0 -11
- package/vite.config.ts +0 -14
- /package/src/components/{basic → lib}/AGLink.vue +0 -0
package/src/forms/Form.test.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { describe, expect, expectTypeOf, it } from 'vitest';
|
|
2
2
|
|
|
3
|
-
import { useForm } from '
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import { useForm } from '@aerogel/core/forms/composition';
|
|
4
|
+
import { numberInput, requiredStringInput } from '@aerogel/core/forms/utils';
|
|
5
|
+
|
|
6
|
+
import { FormFieldTypes } from './Form';
|
|
6
7
|
|
|
7
8
|
describe('Form', () => {
|
|
8
9
|
|
|
@@ -55,4 +56,32 @@ describe('Form', () => {
|
|
|
55
56
|
expect(form.name).toBeNull();
|
|
56
57
|
});
|
|
57
58
|
|
|
59
|
+
it('trims values', () => {
|
|
60
|
+
// Arrange
|
|
61
|
+
const form = useForm({
|
|
62
|
+
trimmed: {
|
|
63
|
+
type: FormFieldTypes.String,
|
|
64
|
+
rules: 'required',
|
|
65
|
+
},
|
|
66
|
+
untrimmed: {
|
|
67
|
+
type: FormFieldTypes.String,
|
|
68
|
+
rules: 'required',
|
|
69
|
+
trim: false,
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
// Act
|
|
74
|
+
form.trimmed = ' ';
|
|
75
|
+
form.untrimmed = ' ';
|
|
76
|
+
|
|
77
|
+
form.submit();
|
|
78
|
+
|
|
79
|
+
// Assert
|
|
80
|
+
expect(form.valid).toBe(false);
|
|
81
|
+
expect(form.submitted).toBe(true);
|
|
82
|
+
expect(form.trimmed).toEqual('');
|
|
83
|
+
expect(form.untrimmed).toEqual(' ');
|
|
84
|
+
expect(form.errors).toEqual({ trimmed: ['required'], untrimmed: null });
|
|
85
|
+
});
|
|
86
|
+
|
|
58
87
|
});
|
package/src/forms/Form.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
2
|
+
import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
|
|
3
|
+
import { validate } from './validation';
|
|
3
4
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
4
5
|
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
5
6
|
|
|
@@ -7,16 +8,20 @@ export const FormFieldTypes = {
|
|
|
7
8
|
String: 'string',
|
|
8
9
|
Number: 'number',
|
|
9
10
|
Boolean: 'boolean',
|
|
11
|
+
Object: 'object',
|
|
12
|
+
Date: 'date',
|
|
10
13
|
} as const;
|
|
11
14
|
|
|
12
15
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
13
16
|
type: TType;
|
|
17
|
+
trim?: boolean;
|
|
14
18
|
default?: GetFormFieldValue<TType>;
|
|
15
19
|
rules?: TRules;
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
19
23
|
export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
|
|
24
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
20
25
|
|
|
21
26
|
export type FormData<T> = {
|
|
22
27
|
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
@@ -33,10 +38,19 @@ export type FormErrors<T> = {
|
|
|
33
38
|
export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.String
|
|
34
39
|
? string
|
|
35
40
|
: TType extends typeof FormFieldTypes.Number
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
? number
|
|
42
|
+
: TType extends typeof FormFieldTypes.Boolean
|
|
43
|
+
? boolean
|
|
44
|
+
: TType extends typeof FormFieldTypes.Object
|
|
45
|
+
? object
|
|
46
|
+
: TType extends typeof FormFieldTypes.Date
|
|
47
|
+
? Date
|
|
48
|
+
: never;
|
|
49
|
+
|
|
50
|
+
const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
|
|
51
|
+
|
|
52
|
+
export type SubmitFormListener = () => unknown;
|
|
53
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
40
54
|
|
|
41
55
|
export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
42
56
|
|
|
@@ -44,9 +58,9 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
44
58
|
|
|
45
59
|
private _fields: Fields;
|
|
46
60
|
private _data: FormData<Fields>;
|
|
47
|
-
private _valid: ComputedRef<boolean>;
|
|
48
61
|
private _submitted: Ref<boolean>;
|
|
49
62
|
private _errors: FormErrors<Fields>;
|
|
63
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
50
64
|
|
|
51
65
|
constructor(fields: Fields) {
|
|
52
66
|
super();
|
|
@@ -55,13 +69,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
55
69
|
this._submitted = ref(false);
|
|
56
70
|
this._data = this.getInitialData(fields);
|
|
57
71
|
this._errors = this.getInitialErrors(fields);
|
|
58
|
-
|
|
72
|
+
|
|
73
|
+
validForms.set(
|
|
74
|
+
this,
|
|
75
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
76
|
+
);
|
|
59
77
|
|
|
60
78
|
this.errors = readonly(this._errors);
|
|
61
79
|
}
|
|
62
80
|
|
|
63
81
|
public get valid(): boolean {
|
|
64
|
-
return this
|
|
82
|
+
return !!validForms.get(this)?.value;
|
|
65
83
|
}
|
|
66
84
|
|
|
67
85
|
public get submitted(): boolean {
|
|
@@ -69,7 +87,13 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
69
87
|
}
|
|
70
88
|
|
|
71
89
|
public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
|
|
72
|
-
|
|
90
|
+
const definition =
|
|
91
|
+
this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
|
|
92
|
+
|
|
93
|
+
this._data[field] =
|
|
94
|
+
definition.type === FormFieldTypes.String && (definition.trim ?? true)
|
|
95
|
+
? (toString(value).trim() as FormData<Fields>[T])
|
|
96
|
+
: value;
|
|
73
97
|
|
|
74
98
|
if (this._submitted.value) {
|
|
75
99
|
this.validate();
|
|
@@ -80,12 +104,23 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
80
104
|
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
81
105
|
}
|
|
82
106
|
|
|
107
|
+
public getFieldRules<T extends keyof Fields>(field: T): string[] {
|
|
108
|
+
return this._fields[field]?.rules?.split('|') ?? [];
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public data(): FormData<Fields> {
|
|
112
|
+
return { ...this._data };
|
|
113
|
+
}
|
|
114
|
+
|
|
83
115
|
public validate(): boolean {
|
|
84
|
-
const errors = Object.entries(this._fields).reduce(
|
|
85
|
-
formErrors[name
|
|
116
|
+
const errors = Object.entries(this._fields).reduce(
|
|
117
|
+
(formErrors, [name, definition]) => {
|
|
118
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
86
119
|
|
|
87
|
-
|
|
88
|
-
|
|
120
|
+
return formErrors;
|
|
121
|
+
},
|
|
122
|
+
{} as Record<string, string[] | null>,
|
|
123
|
+
);
|
|
89
124
|
|
|
90
125
|
this.resetErrors(errors);
|
|
91
126
|
|
|
@@ -102,32 +137,66 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
102
137
|
public submit(): boolean {
|
|
103
138
|
this._submitted.value = true;
|
|
104
139
|
|
|
105
|
-
|
|
140
|
+
const valid = this.validate();
|
|
141
|
+
|
|
142
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
143
|
+
|
|
144
|
+
return valid;
|
|
106
145
|
}
|
|
107
146
|
|
|
108
|
-
|
|
147
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
148
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
149
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
150
|
+
this._listeners[event] ??= [];
|
|
151
|
+
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
153
|
+
this._listeners[event]?.push(listener as any);
|
|
154
|
+
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
156
|
+
return () => this.off(event as any, listener);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
160
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
161
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
162
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
public async focus(input: string): Promise<void> {
|
|
166
|
+
await nextTick();
|
|
167
|
+
|
|
168
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
protected override __get(property: string): unknown {
|
|
109
172
|
if (!(property in this._fields)) {
|
|
110
173
|
return super.__get(property);
|
|
111
174
|
}
|
|
112
175
|
|
|
113
|
-
return this.
|
|
176
|
+
return this.getFieldValue(property);
|
|
114
177
|
}
|
|
115
178
|
|
|
116
|
-
protected __set(property: string, value: unknown): void {
|
|
179
|
+
protected override __set(property: string, value: unknown): void {
|
|
117
180
|
if (!(property in this._fields)) {
|
|
118
181
|
super.__set(property, value);
|
|
119
182
|
|
|
120
183
|
return;
|
|
121
184
|
}
|
|
122
185
|
|
|
123
|
-
|
|
186
|
+
this.setFieldValue(property, value as FormData<Fields>[string]);
|
|
124
187
|
}
|
|
125
188
|
|
|
126
189
|
private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
|
|
127
190
|
const errors = [];
|
|
191
|
+
const value = this._data[name];
|
|
192
|
+
const rules = definition.rules?.split('|') ?? [];
|
|
193
|
+
|
|
194
|
+
for (const rule of rules) {
|
|
195
|
+
if (rule !== 'required' && (value === null || value === undefined)) {
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
128
198
|
|
|
129
|
-
|
|
130
|
-
errors.push('required');
|
|
199
|
+
errors.push(...validate(value, rule));
|
|
131
200
|
}
|
|
132
201
|
|
|
133
202
|
return errors.length > 0 ? errors : null;
|
package/src/forms/composition.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import Form from '
|
|
2
|
-
import type { FormData, FormFieldDefinitions } from '
|
|
1
|
+
import Form from '@aerogel/core/forms/Form';
|
|
2
|
+
import type { FormData, FormFieldDefinitions } from '@aerogel/core/forms/Form';
|
|
3
3
|
|
|
4
4
|
export function useForm<const T extends FormFieldDefinitions>(fields: T): Form<T> & FormData<T> {
|
|
5
5
|
return new Form(fields) as Form<T> & FormData<T>;
|
package/src/forms/index.ts
CHANGED
package/src/forms/utils.ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
1
|
import { FormFieldTypes } from './Form';
|
|
2
2
|
import type { FormFieldDefinition } from './Form';
|
|
3
3
|
|
|
4
|
-
export function booleanInput(
|
|
4
|
+
export function booleanInput(
|
|
5
|
+
defaultValue?: boolean,
|
|
6
|
+
options: { rules?: string } = {},
|
|
7
|
+
): FormFieldDefinition<typeof FormFieldTypes.Boolean> {
|
|
5
8
|
return {
|
|
6
9
|
default: defaultValue,
|
|
7
10
|
type: FormFieldTypes.Boolean,
|
|
11
|
+
rules: options.rules,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function dateInput(
|
|
16
|
+
defaultValue?: Date,
|
|
17
|
+
options: { rules?: string } = {},
|
|
18
|
+
): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
19
|
+
return {
|
|
20
|
+
default: defaultValue,
|
|
21
|
+
type: FormFieldTypes.Date,
|
|
22
|
+
rules: options.rules,
|
|
8
23
|
};
|
|
9
24
|
}
|
|
10
25
|
|
|
@@ -18,6 +33,14 @@ export function requiredBooleanInput(
|
|
|
18
33
|
};
|
|
19
34
|
}
|
|
20
35
|
|
|
36
|
+
export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
37
|
+
return {
|
|
38
|
+
default: defaultValue,
|
|
39
|
+
type: FormFieldTypes.Date,
|
|
40
|
+
rules: 'required',
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
21
44
|
export function requiredNumberInput(
|
|
22
45
|
defaultValue?: number,
|
|
23
46
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
|
@@ -38,16 +61,24 @@ export function requiredStringInput(
|
|
|
38
61
|
};
|
|
39
62
|
}
|
|
40
63
|
|
|
41
|
-
export function numberInput(
|
|
64
|
+
export function numberInput(
|
|
65
|
+
defaultValue?: number,
|
|
66
|
+
options: { rules?: string } = {},
|
|
67
|
+
): FormFieldDefinition<typeof FormFieldTypes.Number> {
|
|
42
68
|
return {
|
|
43
69
|
default: defaultValue,
|
|
44
70
|
type: FormFieldTypes.Number,
|
|
71
|
+
rules: options.rules,
|
|
45
72
|
};
|
|
46
73
|
}
|
|
47
74
|
|
|
48
|
-
export function stringInput(
|
|
75
|
+
export function stringInput(
|
|
76
|
+
defaultValue?: string,
|
|
77
|
+
options: { rules?: string } = {},
|
|
78
|
+
): FormFieldDefinition<typeof FormFieldTypes.String> {
|
|
49
79
|
return {
|
|
50
80
|
default: defaultValue,
|
|
51
81
|
type: FormFieldTypes.String,
|
|
82
|
+
rules: options.rules,
|
|
52
83
|
};
|
|
53
84
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { arrayFrom } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
const builtInRules: Record<string, FormFieldValidator> = {
|
|
4
|
+
required: (value) => (value ? undefined : 'required'),
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type FormFieldValidator<T = unknown> = (value: T) => string | string[] | undefined;
|
|
8
|
+
|
|
9
|
+
export const validators: Record<string, FormFieldValidator> = { ...builtInRules };
|
|
10
|
+
|
|
11
|
+
export function defineFormValidationRule<T>(rule: string, validator: FormFieldValidator<T>): void {
|
|
12
|
+
validators[rule] = validator as FormFieldValidator;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function validate(value: unknown, rule: string): string[] {
|
|
16
|
+
const errors = validators[rule]?.(value);
|
|
17
|
+
|
|
18
|
+
return errors ? arrayFrom(errors) : [];
|
|
19
|
+
}
|
package/src/{main.ts → index.ts}
RENAMED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export * from './bootstrap';
|
|
2
2
|
export * from './components';
|
|
3
|
+
export * from './directives';
|
|
3
4
|
export * from './errors';
|
|
4
5
|
export * from './forms';
|
|
6
|
+
export * from './jobs';
|
|
5
7
|
export * from './lang';
|
|
6
8
|
export * from './plugins';
|
|
7
9
|
export * from './services';
|
|
10
|
+
export * from './testing';
|
|
8
11
|
export * from './ui';
|
|
9
12
|
export * from './utils';
|
package/src/jobs/Job.ts
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { ListenersManager, PromisedValue, round, tap, toError } from '@noeldemartin/utils';
|
|
2
|
+
import type { Listeners } from '@noeldemartin/utils';
|
|
3
|
+
|
|
4
|
+
import JobCancelledError from '@aerogel/core/errors/JobCancelledError';
|
|
5
|
+
|
|
6
|
+
import type { JobListener } from './listeners';
|
|
7
|
+
import type { JobStatus } from './status';
|
|
8
|
+
|
|
9
|
+
export default abstract class Job<
|
|
10
|
+
Listener extends JobListener = JobListener,
|
|
11
|
+
Status extends JobStatus = JobStatus,
|
|
12
|
+
SerializedStatus extends JobStatus = JobStatus,
|
|
13
|
+
> {
|
|
14
|
+
|
|
15
|
+
protected status: Status;
|
|
16
|
+
protected _listeners: ListenersManager<JobListener>;
|
|
17
|
+
protected _progress?: number;
|
|
18
|
+
protected _cancelled?: PromisedValue<void>;
|
|
19
|
+
protected _started: PromisedValue<void>;
|
|
20
|
+
protected _completed: PromisedValue<void>;
|
|
21
|
+
|
|
22
|
+
constructor() {
|
|
23
|
+
this.status = this.getInitialStatus();
|
|
24
|
+
this._listeners = new ListenersManager();
|
|
25
|
+
this._started = new PromisedValue();
|
|
26
|
+
this._completed = new PromisedValue();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public async start(): Promise<void> {
|
|
30
|
+
this.beforeStart();
|
|
31
|
+
this._started.resolve();
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
await this.updateProgress();
|
|
35
|
+
await this.run();
|
|
36
|
+
await this.updateProgress();
|
|
37
|
+
|
|
38
|
+
this._completed.resolve();
|
|
39
|
+
} catch (error) {
|
|
40
|
+
if (error instanceof JobCancelledError) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throw tap(toError(error), (realError) => {
|
|
45
|
+
this._completed.reject(realError);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public async cancel(): Promise<void> {
|
|
51
|
+
this._cancelled = new PromisedValue();
|
|
52
|
+
|
|
53
|
+
await this._cancelled;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
public serialize(): SerializedStatus {
|
|
57
|
+
return this.serializeStatus(this.status);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public get listeners(): Listeners<Listener> {
|
|
61
|
+
return this._listeners;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public get progress(): number {
|
|
65
|
+
return this._progress ?? 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public get cancelled(): boolean {
|
|
69
|
+
return !!this._cancelled?.isResolved();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public get started(): Promise<void> {
|
|
73
|
+
return this._started;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public get completed(): Promise<void> {
|
|
77
|
+
return this._completed;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
protected abstract run(): Promise<void>;
|
|
81
|
+
|
|
82
|
+
protected getInitialStatus(): Status {
|
|
83
|
+
return { completed: false } as Status;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
protected beforeStart(): void {
|
|
87
|
+
if (!this._started.isResolved()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (this._cancelled) {
|
|
92
|
+
delete this._progress;
|
|
93
|
+
delete this._cancelled;
|
|
94
|
+
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
throw new Error('Job already started!');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
protected assertNotCancelled(): void {
|
|
102
|
+
if (!this._cancelled) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this._cancelled.resolve();
|
|
107
|
+
|
|
108
|
+
throw new JobCancelledError();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
protected calculateCurrentProgress(status?: JobStatus): number {
|
|
112
|
+
status ??= this.status;
|
|
113
|
+
|
|
114
|
+
if (status.completed) {
|
|
115
|
+
return 1;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!status.children) {
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return round(
|
|
123
|
+
status.children.reduce((total, child) => total + this.calculateCurrentProgress(child), 0) /
|
|
124
|
+
status.children.length,
|
|
125
|
+
2,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
protected async updateProgress(update?: (status: Status) => unknown): Promise<void> {
|
|
130
|
+
await update?.(this.status);
|
|
131
|
+
|
|
132
|
+
const progress = this.calculateCurrentProgress();
|
|
133
|
+
|
|
134
|
+
if (progress === this._progress) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
this._progress = progress;
|
|
139
|
+
|
|
140
|
+
await this._listeners.emit('onUpdated', progress);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
protected serializeStatus(status: Status): SerializedStatus {
|
|
144
|
+
return { ...status } as unknown as SerializedStatus;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isDevelopment } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import type { LangProvider } from './Lang';
|
|
4
|
+
|
|
5
|
+
export default class DefaultLangProvider implements LangProvider {
|
|
6
|
+
|
|
7
|
+
constructor(
|
|
8
|
+
private locale: string,
|
|
9
|
+
private fallbackLocale: string,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
public getLocale(): string {
|
|
13
|
+
return this.locale;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public async setLocale(locale: string): Promise<void> {
|
|
17
|
+
this.locale = locale;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public getFallbackLocale(): string {
|
|
21
|
+
return this.fallbackLocale;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async setFallbackLocale(fallbackLocale: string): Promise<void> {
|
|
25
|
+
this.fallbackLocale = fallbackLocale;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public getLocales(): string[] {
|
|
29
|
+
return ['en'];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public translate(key: string): string {
|
|
33
|
+
// eslint-disable-next-line no-console
|
|
34
|
+
isDevelopment() && console.warn('Lang provider is missing');
|
|
35
|
+
|
|
36
|
+
return key;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public translateWithDefault(_: string, defaultMessage: string): string {
|
|
40
|
+
// eslint-disable-next-line no-console
|
|
41
|
+
isDevelopment() && console.warn('Lang provider is missing');
|
|
42
|
+
|
|
43
|
+
return defaultMessage;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineServiceState } from '@aerogel/core/services/Service';
|
|
2
|
+
|
|
3
|
+
export default defineServiceState({
|
|
4
|
+
name: 'lang',
|
|
5
|
+
persist: ['locale', 'fallbackLocale'],
|
|
6
|
+
initialState: {
|
|
7
|
+
locale: null as string | null,
|
|
8
|
+
locales: ['en'],
|
|
9
|
+
fallbackLocale: 'en',
|
|
10
|
+
},
|
|
11
|
+
});
|