@aerogel/core 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e → 0.0.0-next.bde642c4a8096c5fc3d5e676c2115da23f4bf1d8
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.cjs.js +1 -1
- package/dist/aerogel-core.cjs.js.map +1 -1
- package/dist/aerogel-core.d.ts +1604 -255
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/histoire.config.ts +7 -0
- package/noeldemartin.config.js +4 -1
- package/package.json +13 -4
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +3 -3
- package/src/bootstrap/index.ts +25 -5
- package/src/bootstrap/options.ts +3 -0
- package/src/components/AGAppLayout.vue +7 -2
- package/src/components/AGAppModals.vue +15 -0
- package/src/components/AGAppOverlays.vue +10 -8
- package/src/components/AGAppSnackbars.vue +13 -0
- package/src/components/composition.ts +23 -0
- package/src/components/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +25 -15
- package/src/components/forms/AGCheckbox.vue +7 -1
- package/src/components/forms/AGForm.vue +9 -10
- package/src/components/forms/AGInput.vue +10 -6
- 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 -12
- package/src/components/headless/forms/AGHeadlessInput.ts +29 -4
- package/src/components/headless/forms/AGHeadlessInput.vue +16 -7
- package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
- package/src/components/headless/forms/AGHeadlessInputInput.vue +43 -5
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
- package/src/components/headless/forms/AGHeadlessInputTextArea.vue +42 -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/index.ts +1 -0
- package/src/components/headless/modals/AGHeadlessModal.ts +27 -0
- package/src/components/headless/modals/AGHeadlessModal.vue +3 -5
- package/src/components/headless/modals/index.ts +4 -6
- package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +10 -0
- package/src/components/headless/snackbars/index.ts +40 -0
- package/src/components/index.ts +5 -1
- package/src/components/interfaces.ts +24 -0
- package/src/components/lib/AGErrorMessage.vue +16 -0
- package/src/components/lib/AGLink.vue +9 -0
- package/src/components/{basic → lib}/AGMarkdown.vue +13 -7
- package/src/components/lib/AGMeasured.vue +15 -0
- package/src/components/lib/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +5 -0
- package/src/components/modals/AGAlertModal.ts +15 -0
- package/src/components/modals/AGAlertModal.vue +4 -16
- package/src/components/modals/AGConfirmModal.ts +33 -0
- package/src/components/modals/AGConfirmModal.vue +9 -13
- package/src/components/modals/AGErrorReportModal.ts +46 -0
- package/src/components/modals/AGErrorReportModal.vue +54 -0
- package/src/components/modals/AGErrorReportModalButtons.vue +111 -0
- package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
- package/src/components/modals/AGLoadingModal.ts +23 -0
- package/src/components/modals/AGLoadingModal.vue +4 -8
- package/src/components/modals/AGModal.ts +1 -1
- package/src/components/modals/AGModal.vue +16 -13
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/AGPromptModal.ts +36 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +16 -7
- package/src/components/snackbars/AGSnackbar.vue +36 -0
- package/src/components/snackbars/index.ts +3 -0
- package/src/components/utils.ts +10 -0
- package/src/directives/index.ts +20 -3
- package/src/directives/measure.ts +21 -0
- package/src/errors/Errors.ts +65 -12
- package/src/errors/index.ts +26 -1
- package/src/errors/utils.ts +19 -0
- package/src/forms/Form.ts +57 -9
- package/src/forms/index.ts +1 -0
- package/src/forms/utils.ts +15 -0
- package/src/jobs/Job.ts +5 -0
- package/src/jobs/index.ts +7 -0
- package/src/lang/DefaultLangProvider.ts +43 -0
- package/src/lang/Lang.state.ts +11 -0
- package/src/lang/Lang.ts +41 -30
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +3 -2
- package/src/plugins/Plugin.ts +1 -0
- package/src/plugins/index.ts +19 -0
- package/src/services/App.state.ts +22 -4
- package/src/services/App.ts +38 -5
- package/src/services/Cache.ts +43 -0
- package/src/services/Events.test.ts +39 -0
- package/src/services/Events.ts +100 -30
- package/src/services/Service.ts +64 -17
- package/src/services/index.ts +11 -5
- package/src/services/store.ts +8 -5
- package/src/testing/index.ts +25 -0
- package/src/testing/setup.ts +19 -0
- package/src/ui/UI.state.ts +10 -1
- package/src/ui/UI.ts +168 -26
- package/src/ui/index.ts +12 -3
- package/src/utils/composition/events.ts +1 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/markdown.ts +11 -2
- package/src/utils/tailwindcss.test.ts +26 -0
- package/src/utils/tailwindcss.ts +7 -0
- package/src/utils/vue.ts +18 -5
- package/tailwind.config.js +4 -0
- package/tsconfig.json +1 -0
- package/vite.config.ts +6 -2
- package/.eslintrc.js +0 -3
- package/src/components/basic/index.ts +0 -3
- package/src/globals.ts +0 -6
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<AGHeadlessSnackbar class="flex flex-row items-center justify-center gap-3 p-4" :class="styleClasses">
|
|
3
|
+
<AGMarkdown :text="message" inline />
|
|
4
|
+
<AGButton
|
|
5
|
+
v-for="(action, i) of actions"
|
|
6
|
+
:key="i"
|
|
7
|
+
:color="color"
|
|
8
|
+
@click="activate(action)"
|
|
9
|
+
>
|
|
10
|
+
{{ action.text }}
|
|
11
|
+
</AGButton>
|
|
12
|
+
</AGHeadlessSnackbar>
|
|
13
|
+
</template>
|
|
14
|
+
|
|
15
|
+
<script setup lang="ts">
|
|
16
|
+
import { computed } from 'vue';
|
|
17
|
+
|
|
18
|
+
import { Colors } from '@/components/constants';
|
|
19
|
+
import { useSnackbar, useSnackbarProps } from '@/components/headless/snackbars';
|
|
20
|
+
|
|
21
|
+
import AGButton from '../forms/AGButton.vue';
|
|
22
|
+
import AGHeadlessSnackbar from '../headless/snackbars/AGHeadlessSnackbar.vue';
|
|
23
|
+
import AGMarkdown from '../lib/AGMarkdown.vue';
|
|
24
|
+
|
|
25
|
+
const props = defineProps(useSnackbarProps());
|
|
26
|
+
const { activate } = useSnackbar(props);
|
|
27
|
+
const styleClasses = computed(() => {
|
|
28
|
+
switch (props.color) {
|
|
29
|
+
case Colors.Danger:
|
|
30
|
+
return 'bg-red-200 text-red-900';
|
|
31
|
+
default:
|
|
32
|
+
case Colors.Secondary:
|
|
33
|
+
return 'bg-gray-900 text-white';
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
</script>
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function extractComponentProps<T extends Record<string, unknown>>(
|
|
2
|
+
values: Record<string, unknown>,
|
|
3
|
+
definitions: Record<string, unknown>,
|
|
4
|
+
): T {
|
|
5
|
+
return Object.keys(definitions).reduce((extracted, prop) => {
|
|
6
|
+
extracted[prop] = values[prop];
|
|
7
|
+
|
|
8
|
+
return extracted;
|
|
9
|
+
}, {} as Record<string, unknown>) as T;
|
|
10
|
+
}
|
package/src/directives/index.ts
CHANGED
|
@@ -3,13 +3,30 @@ import type { Directive } from 'vue';
|
|
|
3
3
|
import { definePlugin } from '@/plugins';
|
|
4
4
|
|
|
5
5
|
import initialFocus from './initial-focus';
|
|
6
|
+
import measure from './measure';
|
|
6
7
|
|
|
7
|
-
const
|
|
8
|
+
const builtInDirectives: Record<string, Directive> = {
|
|
8
9
|
'initial-focus': initialFocus,
|
|
10
|
+
'measure': measure,
|
|
9
11
|
};
|
|
10
12
|
|
|
13
|
+
export * from './measure';
|
|
14
|
+
|
|
11
15
|
export default definePlugin({
|
|
12
|
-
install(app) {
|
|
13
|
-
|
|
16
|
+
install(app, options) {
|
|
17
|
+
const directives = {
|
|
18
|
+
...builtInDirectives,
|
|
19
|
+
...options.directives,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
for (const [name, directive] of Object.entries(directives)) {
|
|
23
|
+
app.directive(name, directive);
|
|
24
|
+
}
|
|
14
25
|
},
|
|
15
26
|
});
|
|
27
|
+
|
|
28
|
+
declare module '@/bootstrap/options' {
|
|
29
|
+
export interface AerogelOptions {
|
|
30
|
+
directives?: Record<string, Directive>;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { defineDirective } from '@/utils/vue';
|
|
2
|
+
|
|
3
|
+
export interface ElementSize {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type MeasureDirectiveListener = (size: ElementSize) => unknown;
|
|
9
|
+
|
|
10
|
+
export default defineDirective({
|
|
11
|
+
mounted(element: HTMLElement, { value }) {
|
|
12
|
+
const listener = typeof value === 'function' ? (value as MeasureDirectiveListener) : null;
|
|
13
|
+
const sizes = element.getBoundingClientRect();
|
|
14
|
+
|
|
15
|
+
// TODO guard with modifiers.css once typed properly
|
|
16
|
+
element.style.setProperty('--width', `${sizes.width}px`);
|
|
17
|
+
element.style.setProperty('--height', `${sizes.height}px`);
|
|
18
|
+
|
|
19
|
+
listener?.({ width: sizes.width, height: sizes.height });
|
|
20
|
+
},
|
|
21
|
+
});
|
package/src/errors/Errors.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import { JSError, facade, isObject } from '@noeldemartin/utils';
|
|
1
|
+
import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
3
|
import App from '@/services/App';
|
|
4
4
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
5
|
-
import UI from '@/ui/UI';
|
|
6
|
-
import {
|
|
5
|
+
import UI, { UIComponents } from '@/ui/UI';
|
|
6
|
+
import { translateWithDefault } from '@/lang/utils';
|
|
7
7
|
|
|
8
8
|
import Service from './Errors.state';
|
|
9
|
+
import { Colors } from '@/components/constants';
|
|
10
|
+
import { Events } from '@/services';
|
|
11
|
+
import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
|
|
9
12
|
import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
13
|
+
import type { ModalComponent } from '@/ui/UI.state';
|
|
10
14
|
|
|
11
15
|
export class ErrorsService extends Service {
|
|
12
16
|
|
|
@@ -22,14 +26,27 @@ export class ErrorsService extends Service {
|
|
|
22
26
|
}
|
|
23
27
|
|
|
24
28
|
public async inspect(error: ErrorSource | ErrorReport[]): Promise<void> {
|
|
25
|
-
const reports = Array.isArray(error) ? error : [await this.createErrorReport(error)];
|
|
29
|
+
const reports = Array.isArray(error) ? (error as ErrorReport[]) : [await this.createErrorReport(error)];
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
if (reports.length === 0) {
|
|
32
|
+
UI.alert(translateWithDefault('errors.inspectEmpty', 'Nothing to inspect!'));
|
|
33
|
+
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
UI.openModal<ModalComponent<AGErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
|
|
38
|
+
reports,
|
|
39
|
+
});
|
|
29
40
|
}
|
|
30
41
|
|
|
31
42
|
public async report(error: ErrorSource, message?: string): Promise<void> {
|
|
32
|
-
|
|
43
|
+
await Events.emit('error', { error, message });
|
|
44
|
+
|
|
45
|
+
if (App.testing) {
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (App.development) {
|
|
33
50
|
this.logError(error);
|
|
34
51
|
}
|
|
35
52
|
|
|
@@ -37,7 +54,7 @@ export class ErrorsService extends Service {
|
|
|
37
54
|
throw error;
|
|
38
55
|
}
|
|
39
56
|
|
|
40
|
-
if (!App.isMounted) {
|
|
57
|
+
if (!App.isMounted()) {
|
|
41
58
|
const startupError = await this.createStartupErrorReport(error);
|
|
42
59
|
|
|
43
60
|
if (startupError) {
|
|
@@ -54,8 +71,24 @@ export class ErrorsService extends Service {
|
|
|
54
71
|
date: new Date(),
|
|
55
72
|
};
|
|
56
73
|
|
|
57
|
-
|
|
58
|
-
|
|
74
|
+
UI.showSnackbar(
|
|
75
|
+
message ??
|
|
76
|
+
translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
|
|
77
|
+
{
|
|
78
|
+
color: Colors.Danger,
|
|
79
|
+
actions: [
|
|
80
|
+
{
|
|
81
|
+
text: translateWithDefault('errors.viewDetails', 'View details'),
|
|
82
|
+
dismiss: true,
|
|
83
|
+
handler: () =>
|
|
84
|
+
UI.openModal<ModalComponent<AGErrorReportModalProps>>(
|
|
85
|
+
UI.requireComponent(UIComponents.ErrorReportModal),
|
|
86
|
+
{ reports: [report] },
|
|
87
|
+
),
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
);
|
|
59
92
|
|
|
60
93
|
this.setState({ logs: [log].concat(this.logs) });
|
|
61
94
|
}
|
|
@@ -102,8 +135,22 @@ export class ErrorsService extends Service {
|
|
|
102
135
|
return this.createErrorReportFromError(error);
|
|
103
136
|
}
|
|
104
137
|
|
|
138
|
+
if (isObject(error)) {
|
|
139
|
+
return objectWithoutEmpty({
|
|
140
|
+
title: toString(
|
|
141
|
+
error['name'] ?? error['title'] ?? translateWithDefault('errors.unknown', 'Unknown Error'),
|
|
142
|
+
),
|
|
143
|
+
description: toString(
|
|
144
|
+
error['message'] ??
|
|
145
|
+
error['description'] ??
|
|
146
|
+
translateWithDefault('errors.unknownDescription', 'Unknown error object'),
|
|
147
|
+
),
|
|
148
|
+
error,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
105
152
|
return {
|
|
106
|
-
title:
|
|
153
|
+
title: translateWithDefault('errors.unknown', 'Unknown Error'),
|
|
107
154
|
error,
|
|
108
155
|
};
|
|
109
156
|
}
|
|
@@ -129,4 +176,10 @@ export class ErrorsService extends Service {
|
|
|
129
176
|
|
|
130
177
|
}
|
|
131
178
|
|
|
132
|
-
export default facade(
|
|
179
|
+
export default facade(ErrorsService);
|
|
180
|
+
|
|
181
|
+
declare module '@/services/Events' {
|
|
182
|
+
export interface EventsPayload {
|
|
183
|
+
error: { error: ErrorSource; message?: string };
|
|
184
|
+
}
|
|
185
|
+
}
|
package/src/errors/index.ts
CHANGED
|
@@ -1,21 +1,46 @@
|
|
|
1
|
+
import type { App } from 'vue';
|
|
2
|
+
|
|
1
3
|
import { bootServices } from '@/services';
|
|
2
4
|
import { definePlugin } from '@/plugins';
|
|
3
5
|
|
|
4
6
|
import Errors from './Errors';
|
|
5
7
|
import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
|
|
6
8
|
|
|
9
|
+
export * from './utils';
|
|
7
10
|
export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
|
|
8
11
|
|
|
9
12
|
const services = { $errors: Errors };
|
|
13
|
+
const frameworkHandler: ErrorHandler = (error) => {
|
|
14
|
+
Errors.report(error);
|
|
15
|
+
|
|
16
|
+
return true;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function setUpErrorHandler(app: App, baseHandler: ErrorHandler = () => false): void {
|
|
20
|
+
const errorHandler: ErrorHandler = (error) => baseHandler(error) || frameworkHandler(error);
|
|
21
|
+
|
|
22
|
+
app.config.errorHandler = errorHandler;
|
|
23
|
+
globalThis.onerror = (event, _, __, ___, error) => errorHandler(error ?? event);
|
|
24
|
+
globalThis.onunhandledrejection = (event) => errorHandler(event.reason);
|
|
25
|
+
}
|
|
10
26
|
|
|
27
|
+
export type ErrorHandler = (error: ErrorSource) => boolean;
|
|
11
28
|
export type ErrorsServices = typeof services;
|
|
12
29
|
|
|
13
30
|
export default definePlugin({
|
|
14
|
-
async install(app) {
|
|
31
|
+
async install(app, options) {
|
|
32
|
+
setUpErrorHandler(app, options.handleError);
|
|
33
|
+
|
|
15
34
|
await bootServices(app, services);
|
|
16
35
|
},
|
|
17
36
|
});
|
|
18
37
|
|
|
38
|
+
declare module '@/bootstrap/options' {
|
|
39
|
+
export interface AerogelOptions {
|
|
40
|
+
handleError?(error: ErrorSource): boolean;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
19
44
|
declare module '@/services' {
|
|
20
45
|
export interface Services extends ErrorsServices {}
|
|
21
46
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { JSError, isObject, toString } from '@noeldemartin/utils';
|
|
2
|
+
import { translateWithDefault } from '@/lang/utils';
|
|
3
|
+
import type { ErrorSource } from './Errors.state';
|
|
4
|
+
|
|
5
|
+
export function getErrorMessage(error: ErrorSource): string {
|
|
6
|
+
if (typeof error === 'string') {
|
|
7
|
+
return error;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (error instanceof Error || error instanceof JSError) {
|
|
11
|
+
return error.message;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (isObject(error)) {
|
|
15
|
+
return toString(error['message'] ?? error['description'] ?? 'Unknown error object');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return translateWithDefault('errors.unknown', 'Unknown Error');
|
|
19
|
+
}
|
package/src/forms/Form.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { MagicObject } from '@noeldemartin/utils';
|
|
2
|
-
import { computed, reactive, readonly, ref } from 'vue';
|
|
1
|
+
import { MagicObject, arrayRemove } from '@noeldemartin/utils';
|
|
2
|
+
import { computed, nextTick, reactive, readonly, ref } from 'vue';
|
|
3
3
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
4
4
|
import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
5
5
|
|
|
@@ -7,6 +7,8 @@ export const FormFieldTypes = {
|
|
|
7
7
|
String: 'string',
|
|
8
8
|
Number: 'number',
|
|
9
9
|
Boolean: 'boolean',
|
|
10
|
+
Object: 'object',
|
|
11
|
+
Date: 'date',
|
|
10
12
|
} as const;
|
|
11
13
|
|
|
12
14
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
@@ -17,6 +19,7 @@ export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType
|
|
|
17
19
|
|
|
18
20
|
export type FormFieldDefinitions = Record<string, FormFieldDefinition>;
|
|
19
21
|
export type FormFieldType = ObjectValues<typeof FormFieldTypes>;
|
|
22
|
+
export type FormFieldValue = GetFormFieldValue<FormFieldType>;
|
|
20
23
|
|
|
21
24
|
export type FormData<T> = {
|
|
22
25
|
-readonly [k in keyof T]: T[k] extends FormFieldDefinition<infer TType, infer TRules>
|
|
@@ -36,17 +39,26 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
|
|
|
36
39
|
? number
|
|
37
40
|
: TType extends typeof FormFieldTypes.Boolean
|
|
38
41
|
? boolean
|
|
42
|
+
: TType extends typeof FormFieldTypes.Object
|
|
43
|
+
? object
|
|
44
|
+
: TType extends typeof FormFieldTypes.Date
|
|
45
|
+
? Date
|
|
39
46
|
: never;
|
|
40
47
|
|
|
48
|
+
const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
|
|
49
|
+
|
|
50
|
+
export type SubmitFormListener = () => unknown;
|
|
51
|
+
export type FocusFormListener = (input: string) => unknown;
|
|
52
|
+
|
|
41
53
|
export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
42
54
|
|
|
43
55
|
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
44
56
|
|
|
45
57
|
private _fields: Fields;
|
|
46
58
|
private _data: FormData<Fields>;
|
|
47
|
-
private _valid: ComputedRef<boolean>;
|
|
48
59
|
private _submitted: Ref<boolean>;
|
|
49
60
|
private _errors: FormErrors<Fields>;
|
|
61
|
+
private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
|
|
50
62
|
|
|
51
63
|
constructor(fields: Fields) {
|
|
52
64
|
super();
|
|
@@ -55,13 +67,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
55
67
|
this._submitted = ref(false);
|
|
56
68
|
this._data = this.getInitialData(fields);
|
|
57
69
|
this._errors = this.getInitialErrors(fields);
|
|
58
|
-
|
|
70
|
+
|
|
71
|
+
validForms.set(
|
|
72
|
+
this,
|
|
73
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
74
|
+
);
|
|
59
75
|
|
|
60
76
|
this.errors = readonly(this._errors);
|
|
61
77
|
}
|
|
62
78
|
|
|
63
79
|
public get valid(): boolean {
|
|
64
|
-
return this
|
|
80
|
+
return !!validForms.get(this)?.value;
|
|
65
81
|
}
|
|
66
82
|
|
|
67
83
|
public get submitted(): boolean {
|
|
@@ -80,6 +96,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
80
96
|
return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
|
|
81
97
|
}
|
|
82
98
|
|
|
99
|
+
public data(): FormData<Fields> {
|
|
100
|
+
return { ...this._data };
|
|
101
|
+
}
|
|
102
|
+
|
|
83
103
|
public validate(): boolean {
|
|
84
104
|
const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
|
|
85
105
|
formErrors[name] = this.getFieldErrors(name, definition);
|
|
@@ -92,17 +112,45 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
92
112
|
return this.valid;
|
|
93
113
|
}
|
|
94
114
|
|
|
95
|
-
public reset(): void {
|
|
115
|
+
public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
|
|
96
116
|
this._submitted.value = false;
|
|
97
117
|
|
|
98
|
-
this.resetData();
|
|
99
|
-
this.resetErrors();
|
|
118
|
+
options.keepData || this.resetData();
|
|
119
|
+
options.keepErrors || this.resetErrors();
|
|
100
120
|
}
|
|
101
121
|
|
|
102
122
|
public submit(): boolean {
|
|
103
123
|
this._submitted.value = true;
|
|
104
124
|
|
|
105
|
-
|
|
125
|
+
const valid = this.validate();
|
|
126
|
+
|
|
127
|
+
valid && this._listeners['submit']?.forEach((listener) => listener());
|
|
128
|
+
|
|
129
|
+
return valid;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public on(event: 'focus', listener: FocusFormListener): () => void;
|
|
133
|
+
public on(event: 'submit', listener: SubmitFormListener): () => void;
|
|
134
|
+
public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
|
|
135
|
+
this._listeners[event] ??= [];
|
|
136
|
+
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
138
|
+
this._listeners[event]?.push(listener as any);
|
|
139
|
+
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
141
|
+
return () => this.off(event as any, listener);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public off(event: 'focus', listener: FocusFormListener): void;
|
|
145
|
+
public off(event: 'submit', listener: SubmitFormListener): void;
|
|
146
|
+
public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
|
|
147
|
+
arrayRemove(this._listeners[event] ?? [], listener);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
public async focus(input: string): Promise<void> {
|
|
151
|
+
await nextTick();
|
|
152
|
+
|
|
153
|
+
this._listeners['focus']?.forEach((listener) => listener(input));
|
|
106
154
|
}
|
|
107
155
|
|
|
108
156
|
protected __get(property: string): unknown {
|
package/src/forms/index.ts
CHANGED
package/src/forms/utils.ts
CHANGED
|
@@ -8,6 +8,13 @@ export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
export function dateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
12
|
+
return {
|
|
13
|
+
default: defaultValue,
|
|
14
|
+
type: FormFieldTypes.Date,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
11
18
|
export function requiredBooleanInput(
|
|
12
19
|
defaultValue?: boolean,
|
|
13
20
|
): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
|
|
@@ -18,6 +25,14 @@ export function requiredBooleanInput(
|
|
|
18
25
|
};
|
|
19
26
|
}
|
|
20
27
|
|
|
28
|
+
export function requiredDateInput(defaultValue?: Date): FormFieldDefinition<typeof FormFieldTypes.Date> {
|
|
29
|
+
return {
|
|
30
|
+
default: defaultValue,
|
|
31
|
+
type: FormFieldTypes.Date,
|
|
32
|
+
rules: 'required',
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
21
36
|
export function requiredNumberInput(
|
|
22
37
|
defaultValue?: number,
|
|
23
38
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
package/src/jobs/Job.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import App from '@/services/App';
|
|
2
|
+
|
|
3
|
+
import type { LangProvider } from './Lang';
|
|
4
|
+
|
|
5
|
+
export default class DefaultLangProvider implements LangProvider {
|
|
6
|
+
|
|
7
|
+
constructor(private locale: string, private fallbackLocale: string) {}
|
|
8
|
+
|
|
9
|
+
public getLocale(): string {
|
|
10
|
+
return this.locale;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public async setLocale(locale: string): Promise<void> {
|
|
14
|
+
this.locale = locale;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public getFallbackLocale(): string {
|
|
18
|
+
return this.fallbackLocale;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async setFallbackLocale(fallbackLocale: string): Promise<void> {
|
|
22
|
+
this.fallbackLocale = fallbackLocale;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public getLocales(): string[] {
|
|
26
|
+
return ['en'];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public translate(key: string): string {
|
|
30
|
+
// eslint-disable-next-line no-console
|
|
31
|
+
App.development && console.warn('Lang provider is missing');
|
|
32
|
+
|
|
33
|
+
return key;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public translateWithDefault(_: string, defaultMessage: string): string {
|
|
37
|
+
// eslint-disable-next-line no-console
|
|
38
|
+
App.development && console.warn('Lang provider is missing');
|
|
39
|
+
|
|
40
|
+
return defaultMessage;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineServiceState } from '@/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
|
+
});
|
package/src/lang/Lang.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { facade
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import Service from '
|
|
3
|
+
import DefaultLangProvider from './DefaultLangProvider';
|
|
4
|
+
import Service from './Lang.state';
|
|
5
5
|
|
|
6
6
|
export interface LangProvider {
|
|
7
|
-
|
|
7
|
+
getLocale(): string;
|
|
8
|
+
setLocale(locale: string): Promise<void>;
|
|
9
|
+
getFallbackLocale(): string;
|
|
10
|
+
setFallbackLocale(fallbackLocale: string): Promise<void>;
|
|
11
|
+
getLocales(): string[];
|
|
12
|
+
translate(key: string, parameters?: Record<string, unknown> | number): string;
|
|
13
|
+
translateWithDefault(key: string, defaultMessage: string, parameters?: Record<string, unknown> | number): string;
|
|
8
14
|
}
|
|
9
15
|
|
|
10
16
|
export class LangService extends Service {
|
|
@@ -14,47 +20,52 @@ export class LangService extends Service {
|
|
|
14
20
|
constructor() {
|
|
15
21
|
super();
|
|
16
22
|
|
|
17
|
-
this.provider =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return key;
|
|
23
|
-
},
|
|
24
|
-
};
|
|
23
|
+
this.provider = new DefaultLangProvider(
|
|
24
|
+
this.getState('locale') ?? this.getBrowserLocale(),
|
|
25
|
+
this.getState('fallbackLocale'),
|
|
26
|
+
);
|
|
25
27
|
}
|
|
26
28
|
|
|
27
|
-
public setProvider(provider: LangProvider): void {
|
|
29
|
+
public async setProvider(provider: LangProvider): Promise<void> {
|
|
28
30
|
this.provider = provider;
|
|
31
|
+
this.locales = provider.getLocales();
|
|
32
|
+
|
|
33
|
+
await provider.setLocale(this.locale ?? this.getBrowserLocale());
|
|
34
|
+
await provider.setFallbackLocale(this.fallbackLocale);
|
|
29
35
|
}
|
|
30
36
|
|
|
31
|
-
public translate(key: string, parameters?: Record<string, unknown>): string {
|
|
37
|
+
public translate(key: string, parameters?: Record<string, unknown> | number): string {
|
|
32
38
|
return this.provider.translate(key, parameters) ?? key;
|
|
33
39
|
}
|
|
34
40
|
|
|
35
|
-
public translateWithDefault(key: string, defaultMessage: string): string;
|
|
36
|
-
public translateWithDefault(key: string, parameters: Record<string, unknown>, defaultMessage: string): string;
|
|
37
41
|
public translateWithDefault(
|
|
38
42
|
key: string,
|
|
39
|
-
|
|
40
|
-
|
|
43
|
+
defaultMessage: string,
|
|
44
|
+
parameters: Record<string, unknown> | number = {},
|
|
41
45
|
): string {
|
|
42
|
-
|
|
46
|
+
return this.provider.translateWithDefault(key, defaultMessage, parameters);
|
|
47
|
+
}
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
const
|
|
49
|
+
public getBrowserLocale(): string {
|
|
50
|
+
const locales = this.getState('locales');
|
|
46
51
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
)
|
|
53
|
-
|
|
52
|
+
return navigator.languages.find((locale) => locales.includes(locale)) ?? 'en';
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected async boot(): Promise<void> {
|
|
56
|
+
this.requireStore().$subscribe(
|
|
57
|
+
async () => {
|
|
58
|
+
await this.provider.setLocale(this.locale ?? this.getBrowserLocale());
|
|
59
|
+
await this.provider.setFallbackLocale(this.fallbackLocale);
|
|
54
60
|
|
|
55
|
-
|
|
61
|
+
this.locale
|
|
62
|
+
? document.querySelector('html')?.setAttribute('lang', this.locale)
|
|
63
|
+
: document.querySelector('html')?.removeAttribute('lang');
|
|
64
|
+
},
|
|
65
|
+
{ immediate: true },
|
|
66
|
+
);
|
|
56
67
|
}
|
|
57
68
|
|
|
58
69
|
}
|
|
59
70
|
|
|
60
|
-
export default facade(
|
|
71
|
+
export default facade(LangService);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './assets/histoire.css';
|
package/src/main.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import './globals';
|
|
2
|
-
|
|
3
1
|
export * from './bootstrap';
|
|
4
2
|
export * from './components';
|
|
3
|
+
export * from './directives';
|
|
5
4
|
export * from './errors';
|
|
6
5
|
export * from './forms';
|
|
6
|
+
export * from './jobs';
|
|
7
7
|
export * from './lang';
|
|
8
8
|
export * from './plugins';
|
|
9
9
|
export * from './services';
|
|
10
|
+
export * from './testing';
|
|
10
11
|
export * from './ui';
|
|
11
12
|
export * from './utils';
|