@aerogel/core 0.0.0-next.926bde19326fe7b6b24b277666936862b64d8295 → 0.0.0-next.980a397d575dcb5ff8c5a0bff769d09f938ea03c
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 +958 -128
- 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 +14 -4
- package/postcss.config.js +6 -0
- package/src/assets/histoire.css +3 -0
- package/src/bootstrap/bootstrap.test.ts +1 -0
- package/src/bootstrap/index.ts +14 -2
- package/src/components/AGAppLayout.vue +6 -2
- package/src/components/AGAppModals.vue +15 -0
- package/src/components/AGAppOverlays.vue +5 -7
- package/src/components/AGAppSnackbars.vue +13 -0
- package/src/components/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +33 -10
- package/src/components/forms/AGCheckbox.vue +35 -0
- package/src/components/forms/AGInput.vue +8 -4
- package/src/components/forms/AGSelect.story.vue +28 -0
- package/src/components/forms/AGSelect.vue +53 -0
- package/src/components/forms/index.ts +5 -5
- package/src/components/headless/forms/AGHeadlessButton.vue +7 -7
- package/src/components/headless/forms/AGHeadlessInput.ts +27 -2
- package/src/components/headless/forms/AGHeadlessInput.vue +11 -8
- package/src/components/headless/forms/AGHeadlessInputError.vue +1 -1
- package/src/components/headless/forms/AGHeadlessInputInput.vue +15 -3
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +8 -2
- package/src/components/headless/forms/AGHeadlessSelect.ts +44 -0
- package/src/components/headless/forms/AGHeadlessSelect.vue +76 -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/index.ts +9 -1
- package/src/components/headless/index.ts +1 -0
- package/src/components/headless/modals/AGHeadlessModal.ts +9 -0
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +5 -1
- 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 +25 -0
- package/src/components/index.ts +3 -1
- package/src/components/lib/AGErrorMessage.vue +16 -0
- package/src/components/lib/AGLink.vue +9 -0
- package/src/components/lib/AGMarkdown.vue +36 -0
- package/src/components/lib/AGStartupCrash.vue +31 -0
- package/src/components/lib/index.ts +4 -0
- package/src/components/modals/AGAlertModal.vue +1 -2
- package/src/components/modals/AGConfirmModal.ts +10 -0
- package/src/components/modals/AGConfirmModal.vue +2 -2
- package/src/components/modals/AGErrorReportModal.ts +20 -0
- package/src/components/modals/AGErrorReportModal.vue +62 -0
- package/src/components/modals/AGErrorReportModalButtons.vue +109 -0
- package/src/components/modals/AGErrorReportModalTitle.vue +25 -0
- package/src/components/modals/AGLoadingModal.ts +9 -0
- package/src/components/modals/AGLoadingModal.vue +19 -0
- package/src/components/modals/AGModal.ts +1 -1
- package/src/components/modals/AGModal.vue +23 -4
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/index.ts +20 -2
- package/src/components/snackbars/AGSnackbar.vue +42 -0
- package/src/components/snackbars/index.ts +3 -0
- package/src/directives/index.ts +16 -3
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +183 -0
- package/src/errors/index.ts +55 -0
- package/src/forms/Form.ts +26 -15
- package/src/forms/utils.ts +17 -0
- package/src/lang/Lang.ts +12 -4
- package/src/lang/index.ts +3 -5
- package/src/lang/utils.ts +4 -0
- package/src/main.histoire.ts +1 -0
- package/src/main.ts +1 -2
- package/src/plugins/Plugin.ts +1 -0
- package/src/plugins/index.ts +19 -0
- package/src/services/App.state.ts +10 -2
- package/src/services/App.ts +24 -2
- package/src/services/Service.ts +136 -45
- package/src/services/index.ts +21 -4
- package/src/services/store.ts +27 -0
- package/src/ui/UI.state.ts +11 -1
- package/src/ui/UI.ts +75 -9
- package/src/ui/index.ts +7 -1
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/markdown.ts +11 -2
- package/src/utils/vue.ts +5 -3
- package/tailwind.config.js +4 -0
- package/tsconfig.json +1 -0
- package/vite.config.ts +2 -1
- package/.eslintrc.js +0 -3
- package/src/components/basic/AGMarkdown.vue +0 -35
- package/src/components/basic/index.ts +0 -3
- package/src/globals.ts +0 -6
package/src/forms/Form.ts
CHANGED
|
@@ -6,6 +6,8 @@ import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
|
|
|
6
6
|
export const FormFieldTypes = {
|
|
7
7
|
String: 'string',
|
|
8
8
|
Number: 'number',
|
|
9
|
+
Boolean: 'boolean',
|
|
10
|
+
Object: 'object',
|
|
9
11
|
} as const;
|
|
10
12
|
|
|
11
13
|
export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
|
|
@@ -33,15 +35,20 @@ export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.Strin
|
|
|
33
35
|
? string
|
|
34
36
|
: TType extends typeof FormFieldTypes.Number
|
|
35
37
|
? number
|
|
38
|
+
: TType extends typeof FormFieldTypes.Boolean
|
|
39
|
+
? boolean
|
|
40
|
+
: TType extends typeof FormFieldTypes.Object
|
|
41
|
+
? object
|
|
36
42
|
: never;
|
|
37
43
|
|
|
44
|
+
const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
|
|
45
|
+
|
|
38
46
|
export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
|
|
39
47
|
|
|
40
48
|
public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
|
|
41
49
|
|
|
42
50
|
private _fields: Fields;
|
|
43
51
|
private _data: FormData<Fields>;
|
|
44
|
-
private _valid: ComputedRef<boolean>;
|
|
45
52
|
private _submitted: Ref<boolean>;
|
|
46
53
|
private _errors: FormErrors<Fields>;
|
|
47
54
|
|
|
@@ -52,13 +59,17 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
52
59
|
this._submitted = ref(false);
|
|
53
60
|
this._data = this.getInitialData(fields);
|
|
54
61
|
this._errors = this.getInitialErrors(fields);
|
|
55
|
-
|
|
62
|
+
|
|
63
|
+
validForms.set(
|
|
64
|
+
this,
|
|
65
|
+
computed(() => !Object.values(this._errors).some((error) => error !== null)),
|
|
66
|
+
);
|
|
56
67
|
|
|
57
68
|
this.errors = readonly(this._errors);
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
public get valid(): boolean {
|
|
61
|
-
return this
|
|
72
|
+
return !!validForms.get(this)?.value;
|
|
62
73
|
}
|
|
63
74
|
|
|
64
75
|
public get submitted(): boolean {
|
|
@@ -78,10 +89,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
78
89
|
}
|
|
79
90
|
|
|
80
91
|
public validate(): boolean {
|
|
81
|
-
const errors = Object.entries(this._fields).reduce((
|
|
82
|
-
|
|
92
|
+
const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
|
|
93
|
+
formErrors[name] = this.getFieldErrors(name, definition);
|
|
83
94
|
|
|
84
|
-
return
|
|
95
|
+
return formErrors;
|
|
85
96
|
}, {} as Record<string, string[] | null>);
|
|
86
97
|
|
|
87
98
|
this.resetErrors(errors);
|
|
@@ -89,11 +100,11 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
89
100
|
return this.valid;
|
|
90
101
|
}
|
|
91
102
|
|
|
92
|
-
public reset(): void {
|
|
103
|
+
public reset(options: { keepData?: boolean; keepErrors?: boolean } = {}): void {
|
|
93
104
|
this._submitted.value = false;
|
|
94
105
|
|
|
95
|
-
this.resetData();
|
|
96
|
-
this.resetErrors();
|
|
106
|
+
options.keepData || this.resetData();
|
|
107
|
+
options.keepErrors || this.resetErrors();
|
|
97
108
|
}
|
|
98
109
|
|
|
99
110
|
public submit(): boolean {
|
|
@@ -135,10 +146,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
135
146
|
return {} as FormData<Fields>;
|
|
136
147
|
}
|
|
137
148
|
|
|
138
|
-
const data = Object.entries(fields).reduce((
|
|
139
|
-
|
|
149
|
+
const data = Object.entries(fields).reduce((formData, [name, definition]) => {
|
|
150
|
+
formData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
|
|
140
151
|
|
|
141
|
-
return
|
|
152
|
+
return formData;
|
|
142
153
|
}, {} as FormData<Fields>);
|
|
143
154
|
|
|
144
155
|
return reactive(data) as FormData<Fields>;
|
|
@@ -149,10 +160,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
|
|
|
149
160
|
return {} as FormErrors<Fields>;
|
|
150
161
|
}
|
|
151
162
|
|
|
152
|
-
const errors = Object.keys(fields).reduce((
|
|
153
|
-
|
|
163
|
+
const errors = Object.keys(fields).reduce((formErrors, name) => {
|
|
164
|
+
formErrors[name as keyof Fields] = null;
|
|
154
165
|
|
|
155
|
-
return
|
|
166
|
+
return formErrors;
|
|
156
167
|
}, {} as FormErrors<Fields>);
|
|
157
168
|
|
|
158
169
|
return reactive(errors) as FormErrors<Fields>;
|
package/src/forms/utils.ts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
import { FormFieldTypes } from './Form';
|
|
2
2
|
import type { FormFieldDefinition } from './Form';
|
|
3
3
|
|
|
4
|
+
export function booleanInput(defaultValue?: boolean): FormFieldDefinition<typeof FormFieldTypes.Boolean> {
|
|
5
|
+
return {
|
|
6
|
+
default: defaultValue,
|
|
7
|
+
type: FormFieldTypes.Boolean,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function requiredBooleanInput(
|
|
12
|
+
defaultValue?: boolean,
|
|
13
|
+
): FormFieldDefinition<typeof FormFieldTypes.Boolean, 'required'> {
|
|
14
|
+
return {
|
|
15
|
+
default: defaultValue,
|
|
16
|
+
type: FormFieldTypes.Boolean,
|
|
17
|
+
rules: 'required',
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
4
21
|
export function requiredNumberInput(
|
|
5
22
|
defaultValue?: number,
|
|
6
23
|
): FormFieldDefinition<typeof FormFieldTypes.Number, 'required'> {
|
package/src/lang/Lang.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { facade } from '@noeldemartin/utils';
|
|
1
|
+
import { facade, toString } from '@noeldemartin/utils';
|
|
2
2
|
|
|
3
3
|
import App from '@/services/App';
|
|
4
4
|
import Service from '@/services/Service';
|
|
@@ -17,7 +17,7 @@ export class LangService extends Service {
|
|
|
17
17
|
this.provider = {
|
|
18
18
|
translate: (key) => {
|
|
19
19
|
// eslint-disable-next-line no-console
|
|
20
|
-
App.
|
|
20
|
+
App.development && console.warn('Lang provider is missing');
|
|
21
21
|
|
|
22
22
|
return key;
|
|
23
23
|
},
|
|
@@ -41,10 +41,18 @@ export class LangService extends Service {
|
|
|
41
41
|
): string {
|
|
42
42
|
defaultMessage ??= defaultMessageOrParameters as string;
|
|
43
43
|
|
|
44
|
-
const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters;
|
|
44
|
+
const parameters = typeof defaultMessageOrParameters === 'string' ? {} : defaultMessageOrParameters ?? {};
|
|
45
45
|
const message = this.provider.translate(key, parameters) ?? key;
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
if (message === key) {
|
|
48
|
+
return Object.entries(parameters).reduce(
|
|
49
|
+
(renderedMessage, [name, value]) =>
|
|
50
|
+
renderedMessage.replace(new RegExp(`\\{\\s*${name}\\s*\\}`, 'g'), toString(value)),
|
|
51
|
+
defaultMessage,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return message;
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
}
|
package/src/lang/index.ts
CHANGED
|
@@ -2,16 +2,14 @@ import { bootServices } from '@/services';
|
|
|
2
2
|
import { definePlugin } from '@/plugins';
|
|
3
3
|
|
|
4
4
|
import Lang, { LangProvider } from './Lang';
|
|
5
|
+
import { translate, translateWithDefault } from './utils';
|
|
5
6
|
|
|
6
|
-
export { Lang, LangProvider };
|
|
7
|
+
export { Lang, LangProvider, translate, translateWithDefault };
|
|
7
8
|
|
|
8
9
|
const services = { $lang: Lang };
|
|
9
10
|
|
|
10
11
|
export type LangServices = typeof services;
|
|
11
12
|
|
|
12
|
-
export const translate = Lang.translate.bind(Lang);
|
|
13
|
-
export const translateWithDefault = Lang.translateWithDefault.bind(Lang);
|
|
14
|
-
|
|
15
13
|
export default definePlugin({
|
|
16
14
|
async install(app) {
|
|
17
15
|
app.config.globalProperties.$t ??= translate;
|
|
@@ -22,7 +20,7 @@ export default definePlugin({
|
|
|
22
20
|
});
|
|
23
21
|
|
|
24
22
|
declare module '@/services' {
|
|
25
|
-
interface Services extends LangServices {}
|
|
23
|
+
export interface Services extends LangServices {}
|
|
26
24
|
}
|
|
27
25
|
|
|
28
26
|
declare module '@vue/runtime-core' {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './assets/histoire.css';
|
package/src/main.ts
CHANGED
package/src/plugins/Plugin.ts
CHANGED
package/src/plugins/index.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import App from '@/services/App';
|
|
4
|
+
|
|
1
5
|
import type { Plugin } from './Plugin';
|
|
2
6
|
|
|
3
7
|
export * from './Plugin';
|
|
@@ -5,3 +9,18 @@ export * from './Plugin';
|
|
|
5
9
|
export function definePlugin<T extends Plugin>(plugin: T): T {
|
|
6
10
|
return plugin;
|
|
7
11
|
}
|
|
12
|
+
|
|
13
|
+
export async function installPlugins(plugins: Plugin[], ...args: GetClosureArgs<Plugin['install']>): Promise<void> {
|
|
14
|
+
App.setState(
|
|
15
|
+
'plugins',
|
|
16
|
+
plugins.reduce((pluginsMap, plugin) => {
|
|
17
|
+
if (plugin.name) {
|
|
18
|
+
pluginsMap[plugin.name] = plugin;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return pluginsMap;
|
|
22
|
+
}, {} as Record<string, Plugin>),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
await Promise.all(plugins.map((plugin) => plugin.install(...args)) ?? []);
|
|
26
|
+
}
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
+
import Aerogel from 'virtual:aerogel';
|
|
2
|
+
|
|
1
3
|
import { defineServiceState } from '@/services/Service';
|
|
4
|
+
import type { Plugin } from '@/plugins/Plugin';
|
|
2
5
|
|
|
3
6
|
export default defineServiceState({
|
|
7
|
+
name: 'app',
|
|
4
8
|
initialState: {
|
|
5
|
-
|
|
9
|
+
plugins: {} as Record<string, Plugin>,
|
|
10
|
+
environment: Aerogel.environment,
|
|
11
|
+
sourceUrl: Aerogel.sourceUrl,
|
|
12
|
+
isMounted: false,
|
|
6
13
|
},
|
|
7
14
|
computed: {
|
|
8
|
-
|
|
15
|
+
development: (state) => state.environment === 'development',
|
|
16
|
+
testing: (state) => state.environment === 'testing',
|
|
9
17
|
},
|
|
10
18
|
});
|
package/src/services/App.ts
CHANGED
|
@@ -1,7 +1,29 @@
|
|
|
1
|
-
import { facade } from '@noeldemartin/utils';
|
|
1
|
+
import { facade, forever, updateLocationQueryParameters } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
import type { Plugin } from '@/plugins';
|
|
2
5
|
|
|
3
6
|
import Service from './App.state';
|
|
4
7
|
|
|
5
|
-
export class AppService extends Service {
|
|
8
|
+
export class AppService extends Service {
|
|
9
|
+
|
|
10
|
+
public async reload(queryParameters?: Record<string, string | undefined>): Promise<void> {
|
|
11
|
+
queryParameters && updateLocationQueryParameters(queryParameters);
|
|
12
|
+
|
|
13
|
+
location.reload();
|
|
14
|
+
|
|
15
|
+
// Stall until the reload happens
|
|
16
|
+
await forever();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public plugin<T extends Plugin = Plugin>(name: string): T | null {
|
|
20
|
+
return (this.plugins[name] as T) ?? null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
protected async boot(): Promise<void> {
|
|
24
|
+
Events.once('application-mounted', () => this.setState({ isMounted: true }));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
}
|
|
6
28
|
|
|
7
29
|
export default facade(new AppService());
|
package/src/services/Service.ts
CHANGED
|
@@ -1,27 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MagicObject, PromisedValue } from '@noeldemartin/utils';
|
|
3
|
-
import type { ComputedRef } from 'vue';
|
|
1
|
+
import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
4
2
|
import type { Constructor } from '@noeldemartin/utils';
|
|
3
|
+
import type { Store } from 'pinia';
|
|
5
4
|
|
|
6
5
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
6
|
+
import { defineServiceStore } from '@/services/store';
|
|
7
7
|
|
|
8
8
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export type DefaultServiceState =
|
|
9
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
10
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
11
11
|
|
|
12
12
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
13
|
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
14
|
-
}
|
|
14
|
+
} & ThisType<{
|
|
15
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
|
+
}>;
|
|
15
17
|
|
|
16
18
|
export function defineServiceState<
|
|
17
19
|
State extends ServiceState = ServiceState,
|
|
18
20
|
ComputedState extends ServiceState = {}
|
|
19
21
|
>(options: {
|
|
22
|
+
name: string;
|
|
20
23
|
initialState: State;
|
|
24
|
+
persist?: (keyof State)[];
|
|
21
25
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
22
|
-
|
|
26
|
+
serialize?: (state: Partial<State>) => Partial<State>;
|
|
27
|
+
}): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
|
|
23
28
|
return class extends Service<State, ComputedState> {
|
|
24
29
|
|
|
30
|
+
public static persist = (options.persist as string[]) ?? [];
|
|
31
|
+
|
|
32
|
+
protected usesStore(): boolean {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
protected getName(): string | null {
|
|
37
|
+
return options.name ?? null;
|
|
38
|
+
}
|
|
39
|
+
|
|
25
40
|
protected getInitialState(): State {
|
|
26
41
|
return options.initialState;
|
|
27
42
|
}
|
|
@@ -29,47 +44,57 @@ export function defineServiceState<
|
|
|
29
44
|
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
30
45
|
return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
|
|
31
46
|
}
|
|
47
|
+
|
|
48
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
49
|
+
return options.serialize?.(state) ?? state;
|
|
50
|
+
}
|
|
32
51
|
|
|
33
|
-
} as unknown as Constructor<State> &
|
|
52
|
+
} as unknown as Constructor<State> &
|
|
53
|
+
Constructor<ComputedState> &
|
|
54
|
+
Constructor<Service<State, ComputedState, Partial<State>>>;
|
|
34
55
|
}
|
|
35
56
|
|
|
36
57
|
export default class Service<
|
|
37
58
|
State extends ServiceState = DefaultServiceState,
|
|
38
|
-
ComputedState extends ServiceState = {}
|
|
59
|
+
ComputedState extends ServiceState = {},
|
|
60
|
+
ServiceStorage extends Partial<State> = Partial<State>
|
|
39
61
|
> extends MagicObject {
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
public static persist: string[] = [];
|
|
64
|
+
|
|
65
|
+
protected _name: string;
|
|
42
66
|
private _booted: PromisedValue<void>;
|
|
43
|
-
private
|
|
44
|
-
private
|
|
67
|
+
private _computedStateKeys: Set<keyof State>;
|
|
68
|
+
private _store?: Store | false;
|
|
45
69
|
|
|
46
70
|
constructor() {
|
|
47
71
|
super();
|
|
48
72
|
|
|
49
|
-
|
|
50
|
-
this._booted = new PromisedValue();
|
|
51
|
-
this._state = reactive(this.getInitialState());
|
|
52
|
-
this._computedState = Object.entries(this.getComputedStateDefinition()).reduce(
|
|
53
|
-
(computedState, [name, method]) => {
|
|
54
|
-
computedState[name as keyof ComputedState] = computed(() => method(this._state));
|
|
73
|
+
const getters = this.getComputedStateDefinition();
|
|
55
74
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
75
|
+
this._name = this.getName() ?? new.target.name;
|
|
76
|
+
this._booted = new PromisedValue();
|
|
77
|
+
this._computedStateKeys = new Set(Object.keys(getters));
|
|
78
|
+
this._store =
|
|
79
|
+
this.usesStore() &&
|
|
80
|
+
defineServiceStore(this._name, {
|
|
81
|
+
state: () => this.getInitialState(),
|
|
82
|
+
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
getters: getters as any,
|
|
85
|
+
});
|
|
60
86
|
}
|
|
61
87
|
|
|
62
88
|
public get booted(): PromisedValue<void> {
|
|
63
89
|
return this._booted;
|
|
64
90
|
}
|
|
65
91
|
|
|
66
|
-
public launch(
|
|
67
|
-
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this.
|
|
68
|
-
|
|
69
|
-
this._namespace = namespace ?? this._namespace;
|
|
92
|
+
public launch(): Promise<void> {
|
|
93
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
70
94
|
|
|
71
95
|
try {
|
|
72
|
-
this.
|
|
96
|
+
this.frameworkBoot()
|
|
97
|
+
.then(() => this.boot())
|
|
73
98
|
.then(() => this._booted.resolve())
|
|
74
99
|
.catch(handleError);
|
|
75
100
|
} catch (error) {
|
|
@@ -79,15 +104,48 @@ export default class Service<
|
|
|
79
104
|
return this._booted;
|
|
80
105
|
}
|
|
81
106
|
|
|
107
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
108
|
+
if (!this._store) {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public getState(): State;
|
|
116
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
117
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
119
|
+
const store = this._store as any;
|
|
120
|
+
|
|
121
|
+
if (property) {
|
|
122
|
+
return store ? store[property] : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return store ? store : {};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
129
|
+
public setState(state: Partial<State>): void;
|
|
130
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
131
|
+
if (!this._store) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const state = (
|
|
136
|
+
typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty
|
|
137
|
+
) as Partial<State>;
|
|
138
|
+
|
|
139
|
+
Object.assign(this._store.$state, state);
|
|
140
|
+
|
|
141
|
+
this.onStateUpdated(state);
|
|
142
|
+
}
|
|
143
|
+
|
|
82
144
|
protected __get(property: string): unknown {
|
|
83
145
|
if (this.hasState(property)) {
|
|
84
146
|
return this.getState(property);
|
|
85
147
|
}
|
|
86
148
|
|
|
87
|
-
if (this.hasComputedState(property)) {
|
|
88
|
-
return this.getComputedState(property);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
149
|
return super.__get(property);
|
|
92
150
|
}
|
|
93
151
|
|
|
@@ -95,26 +153,33 @@ export default class Service<
|
|
|
95
153
|
this.setState({ [property]: value } as Partial<State>);
|
|
96
154
|
}
|
|
97
155
|
|
|
98
|
-
protected
|
|
99
|
-
|
|
100
|
-
|
|
156
|
+
protected onStateUpdated(state: Partial<State>): void {
|
|
157
|
+
// TODO fix this.static()
|
|
158
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
159
|
+
const persisted = objectOnly(state, persist);
|
|
101
160
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
161
|
+
if (isEmpty(persisted)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const storage = Storage.get<ServiceStorage>(this._name);
|
|
105
166
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
167
|
+
if (!storage) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
Storage.set(this._name, {
|
|
172
|
+
...storage,
|
|
173
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
174
|
+
});
|
|
110
175
|
}
|
|
111
176
|
|
|
112
|
-
protected
|
|
113
|
-
return
|
|
177
|
+
protected usesStore(): boolean {
|
|
178
|
+
return false;
|
|
114
179
|
}
|
|
115
180
|
|
|
116
|
-
protected
|
|
117
|
-
|
|
181
|
+
protected getName(): string | null {
|
|
182
|
+
return null;
|
|
118
183
|
}
|
|
119
184
|
|
|
120
185
|
protected getInitialState(): State {
|
|
@@ -125,8 +190,34 @@ export default class Service<
|
|
|
125
190
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
126
191
|
}
|
|
127
192
|
|
|
193
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
194
|
+
return state;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
protected async frameworkBoot(): Promise<void> {
|
|
198
|
+
this.initializePersistedState();
|
|
199
|
+
}
|
|
200
|
+
|
|
128
201
|
protected async boot(): Promise<void> {
|
|
129
|
-
//
|
|
202
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
protected initializePersistedState(): void {
|
|
206
|
+
// TODO fix this.static()
|
|
207
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
208
|
+
|
|
209
|
+
if (!this.usesStore() || isEmpty(persist)) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (Storage.has(this._name)) {
|
|
214
|
+
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
215
|
+
this.setState(persisted);
|
|
216
|
+
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
130
221
|
}
|
|
131
222
|
|
|
132
223
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { definePlugin } from '@/plugins';
|
|
|
5
5
|
import App from './App';
|
|
6
6
|
import Events from './Events';
|
|
7
7
|
import Service from './Service';
|
|
8
|
+
import { getPiniaStore } from './store';
|
|
8
9
|
|
|
9
10
|
export * from './App';
|
|
10
11
|
export * from './Events';
|
|
@@ -24,20 +25,36 @@ export interface Services extends DefaultServices {}
|
|
|
24
25
|
export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
|
|
25
26
|
await Promise.all(
|
|
26
27
|
Object.entries(services).map(async ([name, service]) => {
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
await service
|
|
29
|
+
.launch()
|
|
30
|
+
.catch((error) => app.config.errorHandler?.(error, null, `Failed launching ${name}.`));
|
|
29
31
|
}),
|
|
30
32
|
);
|
|
31
33
|
|
|
32
34
|
Object.assign(app.config.globalProperties, services);
|
|
35
|
+
|
|
36
|
+
App.development && Object.assign(window, services);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
export default definePlugin({
|
|
36
|
-
async install(app) {
|
|
37
|
-
|
|
40
|
+
async install(app, options) {
|
|
41
|
+
const services = {
|
|
42
|
+
...defaultServices,
|
|
43
|
+
...options.services,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
app.use(getPiniaStore());
|
|
47
|
+
|
|
48
|
+
await bootServices(app, services);
|
|
38
49
|
},
|
|
39
50
|
});
|
|
40
51
|
|
|
52
|
+
declare module '@/bootstrap/options' {
|
|
53
|
+
interface AerogelOptions {
|
|
54
|
+
services?: Record<string, Service>;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
41
58
|
declare module '@vue/runtime-core' {
|
|
42
59
|
interface ComponentCustomProperties extends Services {}
|
|
43
60
|
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
2
|
+
import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
|
|
3
|
+
|
|
4
|
+
let _store: Pinia | null = null;
|
|
5
|
+
|
|
6
|
+
function initializePiniaStore(): Pinia {
|
|
7
|
+
if (!_store) {
|
|
8
|
+
_store = createPinia();
|
|
9
|
+
|
|
10
|
+
setActivePinia(_store);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return _store;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getPiniaStore(): Pinia {
|
|
17
|
+
return _store ?? initializePiniaStore();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function defineServiceStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(
|
|
21
|
+
name: Id,
|
|
22
|
+
options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>,
|
|
23
|
+
): Store<Id, S, G, A> {
|
|
24
|
+
initializePiniaStore();
|
|
25
|
+
|
|
26
|
+
return defineStore(name, options)();
|
|
27
|
+
}
|
package/src/ui/UI.state.ts
CHANGED
|
@@ -17,6 +17,16 @@ export interface ModalComponent<
|
|
|
17
17
|
Result = unknown
|
|
18
18
|
> {}
|
|
19
19
|
|
|
20
|
+
export interface Snackbar {
|
|
21
|
+
id: string;
|
|
22
|
+
component: Component;
|
|
23
|
+
properties: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
20
26
|
export default defineServiceState({
|
|
21
|
-
|
|
27
|
+
name: 'ui',
|
|
28
|
+
initialState: {
|
|
29
|
+
modals: [] as Modal[],
|
|
30
|
+
snackbars: [] as Snackbar[],
|
|
31
|
+
},
|
|
22
32
|
});
|