@aerogel/core 0.0.0-next.7f6ed5a1f91688a86bf5ede2adc465e4fd6cfdea → 0.0.0-next.b85327579d32f21c6a9fa21142f0165cdd320d7e
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.d.ts +426 -82
- package/dist/aerogel-core.esm.js +1 -1
- package/package.json +6 -8
- package/src/bootstrap/bootstrap.test.ts +0 -56
- package/src/bootstrap/index.ts +9 -25
- package/src/bootstrap/options.ts +5 -1
- package/src/components/basic/AGMarkdown.vue +20 -5
- package/src/components/forms/AGButton.vue +26 -3
- package/src/components/forms/AGCheckbox.vue +35 -0
- package/src/components/forms/AGInput.vue +8 -4
- package/src/components/forms/index.ts +2 -1
- package/src/components/headless/forms/AGHeadlessButton.vue +3 -4
- package/src/components/headless/forms/AGHeadlessInput.ts +2 -2
- package/src/components/headless/forms/AGHeadlessInput.vue +5 -5
- package/src/components/headless/forms/AGHeadlessInputError.vue +9 -5
- package/src/components/headless/forms/AGHeadlessInputInput.vue +20 -4
- package/src/components/headless/forms/AGHeadlessInputLabel.vue +16 -0
- package/src/components/headless/forms/index.ts +6 -4
- package/src/components/headless/modals/AGHeadlessModal.vue +5 -1
- package/src/components/headless/modals/AGHeadlessModalPanel.vue +10 -2
- package/src/components/index.ts +2 -1
- package/src/components/modals/AGAlertModal.vue +13 -2
- package/src/components/modals/AGConfirmModal.vue +30 -0
- package/src/components/modals/AGLoadingModal.vue +19 -0
- package/src/components/modals/AGModal.ts +4 -0
- package/src/components/modals/AGModal.vue +20 -2
- package/src/components/modals/index.ts +4 -1
- package/src/directives/index.ts +5 -3
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +132 -0
- package/src/errors/index.ts +21 -0
- package/src/forms/Form.test.ts +21 -0
- package/src/forms/Form.ts +38 -16
- package/src/forms/utils.ts +17 -0
- package/src/globals.ts +6 -0
- package/src/lang/Lang.ts +47 -8
- package/src/lang/index.ts +17 -76
- package/src/lang/utils.ts +4 -0
- package/src/main.ts +4 -0
- package/src/plugins/Plugin.ts +7 -0
- package/src/plugins/index.ts +7 -0
- package/src/services/App.state.ts +13 -0
- package/src/services/App.ts +17 -0
- package/src/services/Service.ts +151 -28
- package/src/services/index.ts +29 -7
- package/src/services/store.ts +27 -0
- package/src/types/vite.d.ts +0 -2
- package/src/ui/UI.state.ts +3 -6
- package/src/ui/UI.ts +35 -2
- package/src/ui/index.ts +20 -14
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/composition/hooks.ts +9 -0
- package/src/utils/index.ts +3 -0
- package/tsconfig.json +1 -10
- package/vite.config.ts +2 -6
- package/src/bootstrap/hooks.ts +0 -19
- package/src/lang/helpers.ts +0 -5
- package/src/models/index.ts +0 -18
- package/src/routing/index.ts +0 -33
- package/src/testing/stubs/lang/en.yaml +0 -1
- package/src/testing/stubs/models/User.ts +0 -3
package/src/lang/index.ts
CHANGED
|
@@ -1,89 +1,30 @@
|
|
|
1
|
-
import { createI18n } from 'vue-i18n';
|
|
2
|
-
import { fail, stringMatch } from '@noeldemartin/utils';
|
|
3
|
-
import type { I18nOptions } from 'vue-i18n';
|
|
4
|
-
import type { Plugin } from 'vue';
|
|
5
|
-
|
|
6
|
-
import { defineBootstrapHook, onAppMounted } from '@/bootstrap/hooks';
|
|
7
1
|
import { bootServices } from '@/services';
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
import Lang from './Lang';
|
|
11
|
-
|
|
12
|
-
const services = { $lang: Lang };
|
|
13
|
-
|
|
14
|
-
function getLangOptions(options: BootstrapOptions): LangOptions | null {
|
|
15
|
-
if (options.lang) {
|
|
16
|
-
return options.lang;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (options.langMessages) {
|
|
20
|
-
return { messages: options.langMessages };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
2
|
+
import { definePlugin } from '@/plugins';
|
|
25
3
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const locale = stringMatch<2>(fileName, /.*\/lang\/(.+)\.yaml/)?.[1];
|
|
4
|
+
import Lang, { LangProvider } from './Lang';
|
|
5
|
+
import { translate, translateWithDefault } from './utils';
|
|
29
6
|
|
|
30
|
-
|
|
31
|
-
loaders[locale] = () =>
|
|
32
|
-
(loader as () => Promise<{ default: Record<string, unknown> }>)().then(
|
|
33
|
-
({ default: messages }) => messages,
|
|
34
|
-
);
|
|
35
|
-
}
|
|
7
|
+
export { Lang, LangProvider, translate, translateWithDefault };
|
|
36
8
|
|
|
37
|
-
|
|
38
|
-
}, {} as Record<string, LazyMessages>);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function createAppI18n(options: LangOptions): Promise<Plugin> {
|
|
42
|
-
const locale = options.defaultLocale ?? 'en';
|
|
43
|
-
const fallbackLocale = options.fallbackLocale ?? 'en';
|
|
44
|
-
const messageLoaders = getMessageLoaders(options.messages);
|
|
45
|
-
const lazyMessages = messageLoaders[locale] ?? fail<LazyMessages>(`Missing messages for '${locale}' locale`);
|
|
46
|
-
const messages = { [locale]: await lazyMessages() } as I18nOptions['messages'];
|
|
47
|
-
|
|
48
|
-
return createI18n({ locale, fallbackLocale, messages });
|
|
49
|
-
}
|
|
9
|
+
const services = { $lang: Lang };
|
|
50
10
|
|
|
51
11
|
export type LangServices = typeof services;
|
|
52
12
|
|
|
53
|
-
export
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
defaultLocale?: string;
|
|
58
|
-
fallbackLocale?: string;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export * from './helpers';
|
|
62
|
-
export { Lang };
|
|
63
|
-
|
|
64
|
-
export default defineBootstrapHook(async (app, options) => {
|
|
65
|
-
const langOptions = getLangOptions(options);
|
|
66
|
-
|
|
67
|
-
if (!langOptions) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
onAppMounted(() => Lang.setup());
|
|
72
|
-
|
|
73
|
-
const plugin = await createAppI18n(langOptions);
|
|
13
|
+
export default definePlugin({
|
|
14
|
+
async install(app) {
|
|
15
|
+
app.config.globalProperties.$t ??= translate;
|
|
16
|
+
app.config.globalProperties.$td = translateWithDefault;
|
|
74
17
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
await bootServices(app, services);
|
|
18
|
+
await bootServices(app, services);
|
|
19
|
+
},
|
|
78
20
|
});
|
|
79
21
|
|
|
80
|
-
declare module '@/
|
|
81
|
-
interface
|
|
82
|
-
lang?: LangOptions;
|
|
83
|
-
langMessages?: Record<string, unknown>;
|
|
84
|
-
}
|
|
22
|
+
declare module '@/services' {
|
|
23
|
+
export interface Services extends LangServices {}
|
|
85
24
|
}
|
|
86
25
|
|
|
87
|
-
declare module '
|
|
88
|
-
interface
|
|
26
|
+
declare module '@vue/runtime-core' {
|
|
27
|
+
interface ComponentCustomProperties {
|
|
28
|
+
$td: typeof translateWithDefault;
|
|
29
|
+
}
|
|
89
30
|
}
|
package/src/main.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import './globals';
|
|
2
|
+
|
|
1
3
|
export * from './bootstrap';
|
|
2
4
|
export * from './components';
|
|
5
|
+
export * from './errors';
|
|
3
6
|
export * from './forms';
|
|
4
7
|
export * from './lang';
|
|
8
|
+
export * from './plugins';
|
|
5
9
|
export * from './services';
|
|
6
10
|
export * from './ui';
|
|
7
11
|
export * from './utils';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { defineServiceState } from '@/services/Service';
|
|
2
|
+
|
|
3
|
+
export default defineServiceState({
|
|
4
|
+
name: 'app',
|
|
5
|
+
initialState: {
|
|
6
|
+
environment: __AG_ENV,
|
|
7
|
+
isMounted: false,
|
|
8
|
+
},
|
|
9
|
+
computed: {
|
|
10
|
+
isDevelopment: (state) => state.environment === 'development',
|
|
11
|
+
isTesting: (state) => state.environment === 'testing',
|
|
12
|
+
},
|
|
13
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { facade } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
|
|
5
|
+
import Service from './App.state';
|
|
6
|
+
|
|
7
|
+
export class AppService extends Service {
|
|
8
|
+
|
|
9
|
+
protected async boot(): Promise<void> {
|
|
10
|
+
await super.boot();
|
|
11
|
+
|
|
12
|
+
Events.once('application-mounted', () => this.setState({ isMounted: true }));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default facade(new AppService());
|
package/src/services/Service.ts
CHANGED
|
@@ -1,47 +1,96 @@
|
|
|
1
|
-
import { MagicObject, PromisedValue } from '@noeldemartin/utils';
|
|
2
|
-
import { reactive } from 'vue';
|
|
1
|
+
import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
3
2
|
import type { Constructor } from '@noeldemartin/utils';
|
|
3
|
+
import type { Store } from 'pinia';
|
|
4
4
|
|
|
5
5
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
6
|
+
import { defineServiceStore } from '@/services/store';
|
|
6
7
|
|
|
7
8
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
8
|
-
export type DefaultServiceState =
|
|
9
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
10
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
10
11
|
|
|
11
|
-
export
|
|
12
|
+
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
|
+
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
14
|
+
} & ThisType<{
|
|
15
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
|
+
}>;
|
|
17
|
+
|
|
18
|
+
export function defineServiceState<
|
|
19
|
+
State extends ServiceState = ServiceState,
|
|
20
|
+
ComputedState extends ServiceState = {}
|
|
21
|
+
>(options: {
|
|
22
|
+
name: string;
|
|
12
23
|
initialState: State;
|
|
13
|
-
|
|
14
|
-
|
|
24
|
+
persist?: (keyof State)[];
|
|
25
|
+
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
26
|
+
serialize?: (state: Partial<State>) => Partial<State>;
|
|
27
|
+
}): Constructor<State> & Constructor<ComputedState> & Constructor<Service<State, ComputedState, Partial<State>>> {
|
|
28
|
+
return class extends Service<State, ComputedState> {
|
|
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
|
+
}
|
|
15
39
|
|
|
16
40
|
protected getInitialState(): State {
|
|
17
41
|
return options.initialState;
|
|
18
42
|
}
|
|
43
|
+
|
|
44
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
45
|
+
return options.computed ?? ({} as ComputedStateDefinition<State, ComputedState>);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
49
|
+
return options.serialize?.(state) ?? state;
|
|
50
|
+
}
|
|
19
51
|
|
|
20
|
-
} as unknown as Constructor<State> &
|
|
52
|
+
} as unknown as Constructor<State> &
|
|
53
|
+
Constructor<ComputedState> &
|
|
54
|
+
Constructor<Service<State, ComputedState, Partial<State>>>;
|
|
21
55
|
}
|
|
22
56
|
|
|
23
|
-
export default class Service<
|
|
57
|
+
export default class Service<
|
|
58
|
+
State extends ServiceState = DefaultServiceState,
|
|
59
|
+
ComputedState extends ServiceState = {},
|
|
60
|
+
ServiceStorage extends Partial<State> = Partial<State>
|
|
61
|
+
> extends MagicObject {
|
|
24
62
|
|
|
25
|
-
|
|
63
|
+
public static persist: string[] = [];
|
|
64
|
+
|
|
65
|
+
protected _name: string;
|
|
26
66
|
private _booted: PromisedValue<void>;
|
|
27
|
-
private
|
|
67
|
+
private _computedStateKeys: Set<keyof State>;
|
|
68
|
+
private _store?: Store | false;
|
|
28
69
|
|
|
29
70
|
constructor() {
|
|
30
71
|
super();
|
|
31
72
|
|
|
32
|
-
|
|
73
|
+
const getters = this.getComputedStateDefinition();
|
|
74
|
+
|
|
75
|
+
this._name = this.getName() ?? new.target.name;
|
|
33
76
|
this._booted = new PromisedValue();
|
|
34
|
-
this.
|
|
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
|
+
});
|
|
35
86
|
}
|
|
36
87
|
|
|
37
88
|
public get booted(): PromisedValue<void> {
|
|
38
89
|
return this._booted;
|
|
39
90
|
}
|
|
40
91
|
|
|
41
|
-
public launch(
|
|
42
|
-
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this.
|
|
43
|
-
|
|
44
|
-
this._namespace = namespace ?? this._namespace;
|
|
92
|
+
public launch(): Promise<void> {
|
|
93
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
45
94
|
|
|
46
95
|
try {
|
|
47
96
|
this.boot()
|
|
@@ -54,38 +103,112 @@ export default class Service<State extends ServiceState = DefaultServiceState> e
|
|
|
54
103
|
return this._booted;
|
|
55
104
|
}
|
|
56
105
|
|
|
106
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
107
|
+
if (!this._store) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public getState(): State;
|
|
115
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
116
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
118
|
+
const store = this._store as any;
|
|
119
|
+
|
|
120
|
+
if (property) {
|
|
121
|
+
return store ? store[property] : undefined;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return store ? store : {};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
128
|
+
public setState(state: Partial<State>): void;
|
|
129
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
130
|
+
if (!this._store) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const state = (
|
|
135
|
+
typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty
|
|
136
|
+
) as Partial<State>;
|
|
137
|
+
|
|
138
|
+
Object.assign(this._store.$state, state);
|
|
139
|
+
|
|
140
|
+
this.onStateUpdated(state);
|
|
141
|
+
}
|
|
142
|
+
|
|
57
143
|
protected __get(property: string): unknown {
|
|
58
|
-
if (
|
|
59
|
-
return
|
|
144
|
+
if (this.hasState(property)) {
|
|
145
|
+
return this.getState(property);
|
|
60
146
|
}
|
|
61
147
|
|
|
62
|
-
return
|
|
148
|
+
return super.__get(property);
|
|
63
149
|
}
|
|
64
150
|
|
|
65
151
|
protected __set(property: string, value: unknown): void {
|
|
66
152
|
this.setState({ [property]: value } as Partial<State>);
|
|
67
153
|
}
|
|
68
154
|
|
|
69
|
-
protected
|
|
70
|
-
|
|
155
|
+
protected onStateUpdated(state: Partial<State>): void {
|
|
156
|
+
// TODO fix this.static()
|
|
157
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
158
|
+
const persisted = objectOnly(state, persist);
|
|
159
|
+
|
|
160
|
+
if (isEmpty(persisted)) {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const storage = Storage.require<ServiceStorage>(this._name);
|
|
165
|
+
|
|
166
|
+
Storage.set(this._name, {
|
|
167
|
+
...storage,
|
|
168
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
169
|
+
});
|
|
71
170
|
}
|
|
72
171
|
|
|
73
|
-
protected
|
|
74
|
-
|
|
75
|
-
protected getState<P extends keyof State>(property?: P): State | State[P] {
|
|
76
|
-
return property ? this._state[property] : this._state;
|
|
172
|
+
protected usesStore(): boolean {
|
|
173
|
+
return false;
|
|
77
174
|
}
|
|
78
175
|
|
|
79
|
-
protected
|
|
80
|
-
|
|
176
|
+
protected getName(): string | null {
|
|
177
|
+
return null;
|
|
81
178
|
}
|
|
82
179
|
|
|
83
180
|
protected getInitialState(): State {
|
|
84
181
|
return {} as State;
|
|
85
182
|
}
|
|
86
183
|
|
|
184
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<State, ComputedState> {
|
|
185
|
+
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
189
|
+
return state;
|
|
190
|
+
}
|
|
191
|
+
|
|
87
192
|
protected async boot(): Promise<void> {
|
|
88
|
-
|
|
193
|
+
this.restorePersistedState();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
protected restorePersistedState(): void {
|
|
197
|
+
// TODO fix this.static()
|
|
198
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
199
|
+
|
|
200
|
+
if (!this.usesStore() || isEmpty(persist)) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
if (Storage.has(this._name)) {
|
|
205
|
+
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
206
|
+
this.setState(persisted);
|
|
207
|
+
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
89
212
|
}
|
|
90
213
|
|
|
91
214
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import type { App } from 'vue';
|
|
1
|
+
import type { App as VueApp } from 'vue';
|
|
2
2
|
|
|
3
|
+
import { definePlugin } from '@/plugins';
|
|
4
|
+
|
|
5
|
+
import App from './App';
|
|
3
6
|
import Events from './Events';
|
|
4
7
|
import Service from './Service';
|
|
5
|
-
import {
|
|
8
|
+
import { getPiniaStore } from './store';
|
|
6
9
|
|
|
10
|
+
export * from './App';
|
|
7
11
|
export * from './Events';
|
|
8
12
|
export * from './Service';
|
|
9
13
|
|
|
10
|
-
export { Events, Service };
|
|
14
|
+
export { App, Events, Service };
|
|
11
15
|
|
|
12
16
|
const defaultServices = {
|
|
17
|
+
$app: App,
|
|
13
18
|
$events: Events,
|
|
14
19
|
};
|
|
15
20
|
|
|
@@ -17,18 +22,35 @@ export type DefaultServices = typeof defaultServices;
|
|
|
17
22
|
|
|
18
23
|
export interface Services extends DefaultServices {}
|
|
19
24
|
|
|
20
|
-
export async function bootServices(app:
|
|
25
|
+
export async function bootServices(app: VueApp, services: Record<string, Service>): Promise<void> {
|
|
21
26
|
await Promise.all(
|
|
22
|
-
Object.entries(services).map(async ([
|
|
27
|
+
Object.entries(services).map(async ([_, service]) => {
|
|
23
28
|
// eslint-disable-next-line no-console
|
|
24
|
-
await service.launch(
|
|
29
|
+
await service.launch().catch((error) => console.error(error));
|
|
25
30
|
}),
|
|
26
31
|
);
|
|
27
32
|
|
|
28
33
|
Object.assign(app.config.globalProperties, services);
|
|
29
34
|
}
|
|
30
35
|
|
|
31
|
-
export default
|
|
36
|
+
export default definePlugin({
|
|
37
|
+
async install(app, options) {
|
|
38
|
+
const services = {
|
|
39
|
+
...defaultServices,
|
|
40
|
+
...options.services,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
app.use(getPiniaStore());
|
|
44
|
+
|
|
45
|
+
await bootServices(app, services);
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
declare module '@/bootstrap/options' {
|
|
50
|
+
interface AerogelOptions {
|
|
51
|
+
services?: Record<string, Service>;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
32
54
|
|
|
33
55
|
declare module '@vue/runtime-core' {
|
|
34
56
|
interface ComponentCustomProperties extends Services {}
|
|
@@ -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/types/vite.d.ts
CHANGED
package/src/ui/UI.state.ts
CHANGED
|
@@ -2,10 +2,6 @@ import type { Component } from 'vue';
|
|
|
2
2
|
|
|
3
3
|
import { defineServiceState } from '@/services/Service';
|
|
4
4
|
|
|
5
|
-
export interface State {
|
|
6
|
-
modals: Modal[];
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
export interface Modal<T = unknown> {
|
|
10
6
|
id: string;
|
|
11
7
|
properties: Record<string, unknown>;
|
|
@@ -21,6 +17,7 @@ export interface ModalComponent<
|
|
|
21
17
|
Result = unknown
|
|
22
18
|
> {}
|
|
23
19
|
|
|
24
|
-
export default defineServiceState
|
|
25
|
-
|
|
20
|
+
export default defineServiceState({
|
|
21
|
+
name: 'ui',
|
|
22
|
+
initialState: { modals: [] as Modal[] },
|
|
26
23
|
});
|
package/src/ui/UI.ts
CHANGED
|
@@ -20,6 +20,8 @@ type ModalResult<TComponent> = TComponent extends ModalComponent<Record<string,
|
|
|
20
20
|
|
|
21
21
|
export const UIComponents = {
|
|
22
22
|
AlertModal: 'alert-modal',
|
|
23
|
+
ConfirmModal: 'confirm-modal',
|
|
24
|
+
LoadingModal: 'loading-modal',
|
|
23
25
|
} as const;
|
|
24
26
|
|
|
25
27
|
export type UIComponent = ObjectValues<typeof UIComponents>;
|
|
@@ -29,8 +31,39 @@ export class UIService extends Service {
|
|
|
29
31
|
private modalCallbacks: Record<string, Partial<ModalCallbacks>> = {};
|
|
30
32
|
private components: Partial<Record<UIComponent, Component>> = {};
|
|
31
33
|
|
|
32
|
-
public alert(message: string): void
|
|
33
|
-
|
|
34
|
+
public alert(message: string): void;
|
|
35
|
+
public alert(title: string, message: string): void;
|
|
36
|
+
public alert(messageOrTitle: string, message?: string): void {
|
|
37
|
+
const options = typeof message === 'string' ? { title: messageOrTitle, message } : { message: messageOrTitle };
|
|
38
|
+
|
|
39
|
+
this.openModal(this.requireComponent(UIComponents.AlertModal), options);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async confirm(message: string): Promise<boolean>;
|
|
43
|
+
public async confirm(title: string, message: string): Promise<boolean>;
|
|
44
|
+
public async confirm(messageOrTitle: string, message?: string): Promise<boolean> {
|
|
45
|
+
const options = typeof message === 'string' ? { title: messageOrTitle, message } : { message: messageOrTitle };
|
|
46
|
+
const modal = await this.openModal<ModalComponent<{ message: string }, boolean>>(
|
|
47
|
+
this.requireComponent(UIComponents.ConfirmModal),
|
|
48
|
+
options,
|
|
49
|
+
);
|
|
50
|
+
const result = await modal.beforeClose;
|
|
51
|
+
|
|
52
|
+
return result ?? false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public async loading<T>(operation: Promise<T>): Promise<T>;
|
|
56
|
+
public async loading<T>(message: string, operation: Promise<T>): Promise<T>;
|
|
57
|
+
public async loading<T>(messageOrOperation: string | Promise<T>, operation?: Promise<T>): Promise<T> {
|
|
58
|
+
operation = typeof messageOrOperation === 'string' ? (operation as Promise<T>) : messageOrOperation;
|
|
59
|
+
|
|
60
|
+
const message = typeof messageOrOperation === 'string' ? messageOrOperation : undefined;
|
|
61
|
+
const modal = await this.openModal(this.requireComponent(UIComponents.LoadingModal), { message });
|
|
62
|
+
const result = await operation;
|
|
63
|
+
|
|
64
|
+
await this.closeModal(modal.id);
|
|
65
|
+
|
|
66
|
+
return result;
|
|
34
67
|
}
|
|
35
68
|
|
|
36
69
|
public registerComponent(name: UIComponent, component: Component): void {
|
package/src/ui/index.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import type { Component } from 'vue';
|
|
2
2
|
|
|
3
3
|
import { bootServices } from '@/services';
|
|
4
|
-
import {
|
|
4
|
+
import { definePlugin } from '@/plugins';
|
|
5
5
|
|
|
6
6
|
import UI, { UIComponents } from './UI';
|
|
7
7
|
import AGAlertModal from '../components/modals/AGAlertModal.vue';
|
|
8
|
+
import AGConfirmModal from '../components/modals/AGConfirmModal.vue';
|
|
9
|
+
import AGLoadingModal from '../components/modals/AGLoadingModal.vue';
|
|
8
10
|
import type { UIComponent } from './UI';
|
|
9
11
|
|
|
10
12
|
export { UI, UIComponents, UIComponent };
|
|
@@ -13,25 +15,29 @@ const services = { $ui: UI };
|
|
|
13
15
|
|
|
14
16
|
export type UIServices = typeof services;
|
|
15
17
|
|
|
16
|
-
export default
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
export default definePlugin({
|
|
19
|
+
async install(app, options) {
|
|
20
|
+
const defaultComponents = {
|
|
21
|
+
[UIComponents.AlertModal]: AGAlertModal,
|
|
22
|
+
[UIComponents.ConfirmModal]: AGConfirmModal,
|
|
23
|
+
[UIComponents.LoadingModal]: AGLoadingModal,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
Object.entries({
|
|
27
|
+
...defaultComponents,
|
|
28
|
+
...options.components,
|
|
29
|
+
}).forEach(([name, component]) => UI.registerComponent(name as UIComponent, component));
|
|
30
|
+
|
|
31
|
+
await bootServices(app, services);
|
|
32
|
+
},
|
|
27
33
|
});
|
|
28
34
|
|
|
29
35
|
declare module '@/bootstrap/options' {
|
|
30
|
-
interface
|
|
36
|
+
interface AerogelOptions {
|
|
31
37
|
components?: Partial<Record<UIComponent, Component>>;
|
|
32
38
|
}
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
declare module '@/services' {
|
|
36
|
-
interface Services extends UIServices {}
|
|
42
|
+
export interface Services extends UIServices {}
|
|
37
43
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { objectWithout } from '@noeldemartin/utils';
|
|
2
|
+
import { computed, useAttrs } from 'vue';
|
|
3
|
+
import type { ComputedRef } from 'vue';
|
|
4
|
+
|
|
5
|
+
export function useInputAttrs(): [ComputedRef<{}>, ComputedRef<unknown>] {
|
|
6
|
+
const attrs = useAttrs();
|
|
7
|
+
const className = computed(() => attrs.class);
|
|
8
|
+
const inputAttrs = computed(() => objectWithout(attrs, 'class'));
|
|
9
|
+
|
|
10
|
+
return [inputAttrs, className];
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { noop } from '@noeldemartin/utils';
|
|
2
|
+
import { onMounted, onUnmounted } from 'vue';
|
|
3
|
+
|
|
4
|
+
export function onCleanMounted(operation: () => Function): void {
|
|
5
|
+
let cleanUp: Function = noop;
|
|
6
|
+
|
|
7
|
+
onMounted(() => (cleanUp = operation()));
|
|
8
|
+
onUnmounted(() => cleanUp());
|
|
9
|
+
}
|
package/src/utils/index.ts
CHANGED