@aerogel/core 0.0.0-next.926bde19326fe7b6b24b277666936862b64d8295 → 0.0.0-next.97312fd206b83ac5ae520da32b1bb3f12fb55969
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 +1436 -232
- 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 +4 -3
- package/src/bootstrap/index.ts +26 -4
- 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/constants.ts +8 -0
- package/src/components/forms/AGButton.vue +33 -10
- package/src/components/forms/AGCheckbox.vue +41 -0
- package/src/components/forms/AGInput.vue +15 -9
- package/src/components/forms/AGSelect.story.vue +46 -0
- package/src/components/forms/AGSelect.vue +60 -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 +23 -3
- 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 +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/index.ts +9 -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/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 +40 -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/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 +27 -0
- package/src/components/modals/AGConfirmModal.vue +8 -12
- 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 +15 -0
- package/src/components/modals/AGModal.ts +1 -1
- package/src/components/modals/AGModal.vue +26 -5
- package/src/components/modals/AGModalTitle.vue +9 -0
- package/src/components/modals/AGPromptModal.ts +30 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +16 -6
- 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 +18 -3
- package/src/directives/measure.ts +12 -0
- package/src/errors/Errors.state.ts +31 -0
- package/src/errors/Errors.ts +188 -0
- package/src/errors/index.ts +55 -0
- package/src/forms/Form.ts +27 -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 +9 -2
- package/src/services/App.ts +36 -2
- package/src/services/Service.ts +146 -49
- 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 +164 -17
- package/src/ui/index.ts +15 -4
- package/src/utils/composition/events.ts +1 -0
- package/src/utils/composition/forms.ts +11 -0
- package/src/utils/index.ts +2 -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 +15 -4
- 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/services/Service.ts
CHANGED
|
@@ -1,75 +1,106 @@
|
|
|
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 { MaybeRef } from 'vue';
|
|
4
|
+
import type { Store } from 'pinia';
|
|
5
5
|
|
|
6
6
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
7
|
+
import { defineServiceStore } from '@/services/store';
|
|
7
8
|
|
|
8
9
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export type DefaultServiceState =
|
|
10
|
+
export type DefaultServiceState = any; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
11
|
export type ServiceConstructor<T extends Service = Service> = Constructor<T> & typeof Service;
|
|
12
|
+
export type UnrefServiceState<State extends ServiceState> = {
|
|
13
|
+
[K in keyof State]: State[K] extends MaybeRef<infer T> ? T : State[K];
|
|
14
|
+
};
|
|
11
15
|
|
|
12
16
|
export type ComputedStateDefinition<TState extends ServiceState, TComputedState extends ServiceState> = {
|
|
13
17
|
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
14
|
-
}
|
|
18
|
+
} & ThisType<{
|
|
19
|
+
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
20
|
+
}>;
|
|
15
21
|
|
|
16
22
|
export function defineServiceState<
|
|
17
23
|
State extends ServiceState = ServiceState,
|
|
18
24
|
ComputedState extends ServiceState = {}
|
|
19
25
|
>(options: {
|
|
26
|
+
name: string;
|
|
20
27
|
initialState: State;
|
|
28
|
+
persist?: (keyof State)[];
|
|
21
29
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
22
|
-
|
|
23
|
-
|
|
30
|
+
serialize?: (state: Partial<State>) => Partial<State>;
|
|
31
|
+
}): Constructor<UnrefServiceState<State>> &
|
|
32
|
+
Constructor<ComputedState> &
|
|
33
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>> {
|
|
34
|
+
return class extends Service<UnrefServiceState<State>, ComputedState> {
|
|
35
|
+
|
|
36
|
+
public static persist = (options.persist as string[]) ?? [];
|
|
37
|
+
|
|
38
|
+
protected usesStore(): boolean {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected getName(): string | null {
|
|
43
|
+
return options.name ?? null;
|
|
44
|
+
}
|
|
24
45
|
|
|
25
|
-
protected getInitialState(): State {
|
|
46
|
+
protected getInitialState(): UnrefServiceState<State> {
|
|
26
47
|
return options.initialState;
|
|
27
48
|
}
|
|
28
49
|
|
|
29
|
-
protected getComputedStateDefinition(): ComputedStateDefinition<State
|
|
30
|
-
return options.computed ??
|
|
50
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<UnrefServiceState<State>, ComputedState> {
|
|
51
|
+
return (options.computed ?? {}) as ComputedStateDefinition<UnrefServiceState<State>, ComputedState>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
55
|
+
return options.serialize?.(state) ?? state;
|
|
31
56
|
}
|
|
32
57
|
|
|
33
|
-
} as unknown as Constructor<State
|
|
58
|
+
} as unknown as Constructor<UnrefServiceState<State>> &
|
|
59
|
+
Constructor<ComputedState> &
|
|
60
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>>;
|
|
34
61
|
}
|
|
35
62
|
|
|
36
63
|
export default class Service<
|
|
37
64
|
State extends ServiceState = DefaultServiceState,
|
|
38
|
-
ComputedState extends ServiceState = {}
|
|
65
|
+
ComputedState extends ServiceState = {},
|
|
66
|
+
ServiceStorage extends Partial<State> = Partial<State>
|
|
39
67
|
> extends MagicObject {
|
|
40
68
|
|
|
41
|
-
|
|
69
|
+
public static persist: string[] = [];
|
|
70
|
+
|
|
71
|
+
protected _name: string;
|
|
42
72
|
private _booted: PromisedValue<void>;
|
|
43
|
-
private
|
|
44
|
-
private
|
|
73
|
+
private _computedStateKeys: Set<keyof State>;
|
|
74
|
+
private _store?: Store | false;
|
|
45
75
|
|
|
46
76
|
constructor() {
|
|
47
77
|
super();
|
|
48
78
|
|
|
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));
|
|
79
|
+
const getters = this.getComputedStateDefinition();
|
|
55
80
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
81
|
+
this._name = this.getName() ?? new.target.name;
|
|
82
|
+
this._booted = new PromisedValue();
|
|
83
|
+
this._computedStateKeys = new Set(Object.keys(getters));
|
|
84
|
+
this._store =
|
|
85
|
+
this.usesStore() &&
|
|
86
|
+
defineServiceStore(this._name, {
|
|
87
|
+
state: () => this.getInitialState(),
|
|
88
|
+
|
|
89
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
90
|
+
getters: getters as any,
|
|
91
|
+
});
|
|
60
92
|
}
|
|
61
93
|
|
|
62
94
|
public get booted(): PromisedValue<void> {
|
|
63
95
|
return this._booted;
|
|
64
96
|
}
|
|
65
97
|
|
|
66
|
-
public launch(
|
|
67
|
-
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this.
|
|
68
|
-
|
|
69
|
-
this._namespace = namespace ?? this._namespace;
|
|
98
|
+
public launch(): Promise<void> {
|
|
99
|
+
const handleError = (error: unknown) => this._booted.reject(new ServiceBootError(this._name, error));
|
|
70
100
|
|
|
71
101
|
try {
|
|
72
|
-
this.
|
|
102
|
+
this.frameworkBoot()
|
|
103
|
+
.then(() => this.boot())
|
|
73
104
|
.then(() => this._booted.resolve())
|
|
74
105
|
.catch(handleError);
|
|
75
106
|
} catch (error) {
|
|
@@ -79,15 +110,48 @@ export default class Service<
|
|
|
79
110
|
return this._booted;
|
|
80
111
|
}
|
|
81
112
|
|
|
113
|
+
public hasState<P extends keyof State>(property: P): boolean {
|
|
114
|
+
if (!this._store) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return property in this._store.$state || this._computedStateKeys.has(property);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public getState(): State;
|
|
122
|
+
public getState<P extends keyof State>(property: P): State[P];
|
|
123
|
+
public getState<P extends keyof State>(property?: P): State | State[P] {
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
125
|
+
const store = this._store as any;
|
|
126
|
+
|
|
127
|
+
if (property) {
|
|
128
|
+
return store ? store[property] : undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return store ? store : {};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
public setState<P extends keyof State>(property: P, value: State[P]): void;
|
|
135
|
+
public setState(state: Partial<State>): void;
|
|
136
|
+
public setState<P extends keyof State>(stateOrProperty: P | Partial<State>, value?: State[P]): void {
|
|
137
|
+
if (!this._store) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const state = (
|
|
142
|
+
typeof stateOrProperty === 'string' ? { [stateOrProperty]: value } : stateOrProperty
|
|
143
|
+
) as Partial<State>;
|
|
144
|
+
|
|
145
|
+
Object.assign(this._store.$state, state);
|
|
146
|
+
|
|
147
|
+
this.onStateUpdated(state);
|
|
148
|
+
}
|
|
149
|
+
|
|
82
150
|
protected __get(property: string): unknown {
|
|
83
151
|
if (this.hasState(property)) {
|
|
84
152
|
return this.getState(property);
|
|
85
153
|
}
|
|
86
154
|
|
|
87
|
-
if (this.hasComputedState(property)) {
|
|
88
|
-
return this.getComputedState(property);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
155
|
return super.__get(property);
|
|
92
156
|
}
|
|
93
157
|
|
|
@@ -95,26 +159,33 @@ export default class Service<
|
|
|
95
159
|
this.setState({ [property]: value } as Partial<State>);
|
|
96
160
|
}
|
|
97
161
|
|
|
98
|
-
protected
|
|
99
|
-
|
|
100
|
-
|
|
162
|
+
protected onStateUpdated(state: Partial<State>): void {
|
|
163
|
+
// TODO fix this.static()
|
|
164
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
165
|
+
const persisted = objectOnly(state, persist);
|
|
101
166
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
167
|
+
if (isEmpty(persisted)) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const storage = Storage.get<ServiceStorage>(this._name);
|
|
172
|
+
|
|
173
|
+
if (!storage) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
105
176
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
177
|
+
Storage.set(this._name, {
|
|
178
|
+
...storage,
|
|
179
|
+
...this.serializePersistedState(objectDeepClone(persisted) as Partial<State>),
|
|
180
|
+
});
|
|
110
181
|
}
|
|
111
182
|
|
|
112
|
-
protected
|
|
113
|
-
return
|
|
183
|
+
protected usesStore(): boolean {
|
|
184
|
+
return false;
|
|
114
185
|
}
|
|
115
186
|
|
|
116
|
-
protected
|
|
117
|
-
|
|
187
|
+
protected getName(): string | null {
|
|
188
|
+
return null;
|
|
118
189
|
}
|
|
119
190
|
|
|
120
191
|
protected getInitialState(): State {
|
|
@@ -125,8 +196,34 @@ export default class Service<
|
|
|
125
196
|
return {} as ComputedStateDefinition<State, ComputedState>;
|
|
126
197
|
}
|
|
127
198
|
|
|
199
|
+
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
200
|
+
return state;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
protected async frameworkBoot(): Promise<void> {
|
|
204
|
+
this.initializePersistedState();
|
|
205
|
+
}
|
|
206
|
+
|
|
128
207
|
protected async boot(): Promise<void> {
|
|
129
|
-
//
|
|
208
|
+
// Placeholder for overrides, don't place any functionality here.
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
protected initializePersistedState(): void {
|
|
212
|
+
// TODO fix this.static()
|
|
213
|
+
const persist = (this.constructor as unknown as { persist: string[] }).persist;
|
|
214
|
+
|
|
215
|
+
if (!this.usesStore() || isEmpty(persist)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (Storage.has(this._name)) {
|
|
220
|
+
const persisted = Storage.require<ServiceStorage>(this._name);
|
|
221
|
+
this.setState(persisted);
|
|
222
|
+
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
130
227
|
}
|
|
131
228
|
|
|
132
229
|
}
|
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
|
+
export 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
|
});
|
package/src/ui/UI.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import { facade, fail, uuid } from '@noeldemartin/utils';
|
|
1
|
+
import { after, facade, fail, uuid } from '@noeldemartin/utils';
|
|
2
2
|
import { markRaw, nextTick } from 'vue';
|
|
3
3
|
import type { Component } from 'vue';
|
|
4
4
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
5
5
|
|
|
6
6
|
import Events from '@/services/Events';
|
|
7
|
+
import type { SnackbarAction, SnackbarColor } from '@/components/headless/snackbars';
|
|
8
|
+
import type { AGAlertModalProps, AGConfirmModalProps, AGLoadingModalProps, AGPromptModalProps } from '@/components';
|
|
7
9
|
|
|
8
10
|
import Service from './UI.state';
|
|
9
|
-
import type { Modal, ModalComponent } from './UI.state';
|
|
11
|
+
import type { Modal, ModalComponent, Snackbar } from './UI.state';
|
|
10
12
|
|
|
11
13
|
interface ModalCallbacks<T = unknown> {
|
|
12
14
|
willClose(result: T | undefined): void;
|
|
@@ -21,36 +23,165 @@ type ModalResult<TComponent> = TComponent extends ModalComponent<Record<string,
|
|
|
21
23
|
export const UIComponents = {
|
|
22
24
|
AlertModal: 'alert-modal',
|
|
23
25
|
ConfirmModal: 'confirm-modal',
|
|
26
|
+
ErrorReportModal: 'error-report-modal',
|
|
27
|
+
LoadingModal: 'loading-modal',
|
|
28
|
+
PromptModal: 'prompt-modal',
|
|
29
|
+
Snackbar: 'snackbar',
|
|
30
|
+
StartupCrash: 'startup-crash',
|
|
24
31
|
} as const;
|
|
25
32
|
|
|
26
33
|
export type UIComponent = ObjectValues<typeof UIComponents>;
|
|
27
34
|
|
|
35
|
+
export interface ConfirmOptions {
|
|
36
|
+
acceptText?: string;
|
|
37
|
+
cancelText?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface PromptOptions {
|
|
41
|
+
label?: string;
|
|
42
|
+
defaultValue?: string;
|
|
43
|
+
placeholder?: string;
|
|
44
|
+
acceptText?: string;
|
|
45
|
+
cancelText?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ShowSnackbarOptions {
|
|
49
|
+
component?: Component;
|
|
50
|
+
color?: SnackbarColor;
|
|
51
|
+
actions?: SnackbarAction[];
|
|
52
|
+
}
|
|
53
|
+
|
|
28
54
|
export class UIService extends Service {
|
|
29
55
|
|
|
30
56
|
private modalCallbacks: Record<string, Partial<ModalCallbacks>> = {};
|
|
31
57
|
private components: Partial<Record<UIComponent, Component>> = {};
|
|
32
58
|
|
|
59
|
+
public requireComponent(name: UIComponent): Component {
|
|
60
|
+
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
61
|
+
}
|
|
62
|
+
|
|
33
63
|
public alert(message: string): void;
|
|
34
64
|
public alert(title: string, message: string): void;
|
|
35
65
|
public alert(messageOrTitle: string, message?: string): void {
|
|
36
|
-
const
|
|
66
|
+
const getProperties = (): AGAlertModalProps => {
|
|
67
|
+
if (typeof message !== 'string') {
|
|
68
|
+
return { message: messageOrTitle };
|
|
69
|
+
}
|
|
37
70
|
|
|
38
|
-
|
|
71
|
+
return {
|
|
72
|
+
title: messageOrTitle,
|
|
73
|
+
message,
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
this.openModal(this.requireComponent(UIComponents.AlertModal), getProperties());
|
|
39
78
|
}
|
|
40
79
|
|
|
41
|
-
public async confirm(message: string): Promise<boolean>;
|
|
42
|
-
public async confirm(title: string, message: string): Promise<boolean>;
|
|
43
|
-
public async confirm(
|
|
44
|
-
|
|
45
|
-
|
|
80
|
+
public async confirm(message: string, options?: ConfirmOptions): Promise<boolean>;
|
|
81
|
+
public async confirm(title: string, message: string, options?: ConfirmOptions): Promise<boolean>;
|
|
82
|
+
public async confirm(
|
|
83
|
+
messageOrTitle: string,
|
|
84
|
+
messageOrOptions?: string | ConfirmOptions,
|
|
85
|
+
options?: ConfirmOptions,
|
|
86
|
+
): Promise<boolean> {
|
|
87
|
+
const getProperties = (): AGConfirmModalProps => {
|
|
88
|
+
if (typeof messageOrOptions !== 'string') {
|
|
89
|
+
return {
|
|
90
|
+
message: messageOrTitle,
|
|
91
|
+
...(messageOrOptions ?? {}),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
title: messageOrTitle,
|
|
97
|
+
message: messageOrOptions,
|
|
98
|
+
...(options ?? {}),
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const modal = await this.openModal<ModalComponent<AGConfirmModalProps, boolean>>(
|
|
46
103
|
this.requireComponent(UIComponents.ConfirmModal),
|
|
47
|
-
|
|
104
|
+
getProperties(),
|
|
48
105
|
);
|
|
49
106
|
const result = await modal.beforeClose;
|
|
50
107
|
|
|
51
108
|
return result ?? false;
|
|
52
109
|
}
|
|
53
110
|
|
|
111
|
+
public async prompt(message: string, options?: PromptOptions): Promise<string | null>;
|
|
112
|
+
public async prompt(title: string, message: string, options?: PromptOptions): Promise<string | null>;
|
|
113
|
+
public async prompt(
|
|
114
|
+
messageOrTitle: string,
|
|
115
|
+
messageOrOptions?: string | PromptOptions,
|
|
116
|
+
options?: PromptOptions,
|
|
117
|
+
): Promise<string | null> {
|
|
118
|
+
const getProperties = (): AGPromptModalProps => {
|
|
119
|
+
if (typeof messageOrOptions !== 'string') {
|
|
120
|
+
return {
|
|
121
|
+
message: messageOrTitle,
|
|
122
|
+
...(messageOrOptions ?? {}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
title: messageOrTitle,
|
|
128
|
+
message: messageOrOptions,
|
|
129
|
+
...(options ?? {}),
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const modal = await this.openModal<ModalComponent<AGPromptModalProps, string | null>>(
|
|
134
|
+
this.requireComponent(UIComponents.PromptModal),
|
|
135
|
+
getProperties(),
|
|
136
|
+
);
|
|
137
|
+
const result = await modal.beforeClose;
|
|
138
|
+
|
|
139
|
+
return result ?? null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
public async loading<T>(operation: Promise<T>): Promise<T>;
|
|
143
|
+
public async loading<T>(message: string, operation: Promise<T>): Promise<T>;
|
|
144
|
+
public async loading<T>(messageOrOperation: string | Promise<T>, operation?: Promise<T>): Promise<T> {
|
|
145
|
+
const getProperties = (): AGLoadingModalProps => {
|
|
146
|
+
if (typeof messageOrOperation !== 'string') {
|
|
147
|
+
return {};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return { message: messageOrOperation };
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const modal = await this.openModal(this.requireComponent(UIComponents.LoadingModal), getProperties());
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
operation = typeof messageOrOperation === 'string' ? (operation as Promise<T>) : messageOrOperation;
|
|
157
|
+
|
|
158
|
+
const [result] = await Promise.all([operation, after({ seconds: 1 })]);
|
|
159
|
+
|
|
160
|
+
return result;
|
|
161
|
+
} finally {
|
|
162
|
+
await this.closeModal(modal.id);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
public showSnackbar(message: string, options: ShowSnackbarOptions = {}): void {
|
|
167
|
+
const snackbar: Snackbar = {
|
|
168
|
+
id: uuid(),
|
|
169
|
+
properties: { message, ...options },
|
|
170
|
+
component: options.component ?? markRaw(this.requireComponent(UIComponents.Snackbar)),
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
this.setState('snackbars', this.snackbars.concat(snackbar));
|
|
174
|
+
|
|
175
|
+
setTimeout(() => this.hideSnackbar(snackbar.id), 5000);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public hideSnackbar(id: string): void {
|
|
179
|
+
this.setState(
|
|
180
|
+
'snackbars',
|
|
181
|
+
this.snackbars.filter((snackbar) => snackbar.id !== id),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
54
185
|
public registerComponent(name: UIComponent, component: Component): void {
|
|
55
186
|
this.components[name] = component;
|
|
56
187
|
}
|
|
@@ -90,13 +221,8 @@ export class UIService extends Service {
|
|
|
90
221
|
}
|
|
91
222
|
|
|
92
223
|
protected async boot(): Promise<void> {
|
|
93
|
-
await super.boot();
|
|
94
|
-
|
|
95
224
|
this.watchModalEvents();
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
private requireComponent(name: UIComponent): Component {
|
|
99
|
-
return this.components[name] ?? fail(`UI Component '${name}' is not defined!`);
|
|
225
|
+
this.watchMountedEvent();
|
|
100
226
|
}
|
|
101
227
|
|
|
102
228
|
private watchModalEvents(): void {
|
|
@@ -109,7 +235,10 @@ export class UIService extends Service {
|
|
|
109
235
|
});
|
|
110
236
|
|
|
111
237
|
Events.on('modal-closed', async ({ modal, result }) => {
|
|
112
|
-
this.setState(
|
|
238
|
+
this.setState(
|
|
239
|
+
'modals',
|
|
240
|
+
this.modals.filter((m) => m.id !== modal.id),
|
|
241
|
+
);
|
|
113
242
|
|
|
114
243
|
this.modalCallbacks[modal.id]?.closed?.(result);
|
|
115
244
|
|
|
@@ -121,6 +250,24 @@ export class UIService extends Service {
|
|
|
121
250
|
});
|
|
122
251
|
}
|
|
123
252
|
|
|
253
|
+
private watchMountedEvent(): void {
|
|
254
|
+
Events.once('application-mounted', async () => {
|
|
255
|
+
const splash = document.getElementById('splash');
|
|
256
|
+
|
|
257
|
+
if (!splash) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (window.getComputedStyle(splash).opacity !== '0') {
|
|
262
|
+
splash.style.opacity = '0';
|
|
263
|
+
|
|
264
|
+
await after({ ms: 600 });
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
splash.remove();
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
124
271
|
}
|
|
125
272
|
|
|
126
273
|
export default facade(new UIService());
|
package/src/ui/index.ts
CHANGED
|
@@ -6,12 +6,18 @@ import { definePlugin } from '@/plugins';
|
|
|
6
6
|
import UI, { UIComponents } from './UI';
|
|
7
7
|
import AGAlertModal from '../components/modals/AGAlertModal.vue';
|
|
8
8
|
import AGConfirmModal from '../components/modals/AGConfirmModal.vue';
|
|
9
|
+
import AGErrorReportModal from '../components/modals/AGErrorReportModal.vue';
|
|
10
|
+
import AGLoadingModal from '../components/modals/AGLoadingModal.vue';
|
|
11
|
+
import AGPromptModal from '../components/modals/AGPromptModal.vue';
|
|
12
|
+
import AGSnackbar from '../components/snackbars/AGSnackbar.vue';
|
|
13
|
+
import AGStartupCrash from '../components/lib/AGStartupCrash.vue';
|
|
9
14
|
import type { UIComponent } from './UI';
|
|
10
15
|
|
|
11
|
-
export { UI, UIComponents, UIComponent };
|
|
12
|
-
|
|
13
16
|
const services = { $ui: UI };
|
|
14
17
|
|
|
18
|
+
export * from './UI';
|
|
19
|
+
export { default as UI } from './UI';
|
|
20
|
+
|
|
15
21
|
export type UIServices = typeof services;
|
|
16
22
|
|
|
17
23
|
export default definePlugin({
|
|
@@ -19,6 +25,11 @@ export default definePlugin({
|
|
|
19
25
|
const defaultComponents = {
|
|
20
26
|
[UIComponents.AlertModal]: AGAlertModal,
|
|
21
27
|
[UIComponents.ConfirmModal]: AGConfirmModal,
|
|
28
|
+
[UIComponents.ErrorReportModal]: AGErrorReportModal,
|
|
29
|
+
[UIComponents.LoadingModal]: AGLoadingModal,
|
|
30
|
+
[UIComponents.PromptModal]: AGPromptModal,
|
|
31
|
+
[UIComponents.Snackbar]: AGSnackbar,
|
|
32
|
+
[UIComponents.StartupCrash]: AGStartupCrash,
|
|
22
33
|
};
|
|
23
34
|
|
|
24
35
|
Object.entries({
|
|
@@ -31,11 +42,11 @@ export default definePlugin({
|
|
|
31
42
|
});
|
|
32
43
|
|
|
33
44
|
declare module '@/bootstrap/options' {
|
|
34
|
-
interface AerogelOptions {
|
|
45
|
+
export interface AerogelOptions {
|
|
35
46
|
components?: Partial<Record<UIComponent, Component>>;
|
|
36
47
|
}
|
|
37
48
|
}
|
|
38
49
|
|
|
39
50
|
declare module '@/services' {
|
|
40
|
-
interface Services extends UIServices {}
|
|
51
|
+
export interface Services extends UIServices {}
|
|
41
52
|
}
|
|
@@ -14,6 +14,7 @@ export function useEvent<Event extends EventWithPayload>(
|
|
|
14
14
|
event: Event,
|
|
15
15
|
listener: EventListener<EventsPayload[Event]>
|
|
16
16
|
): void;
|
|
17
|
+
export function useEvent<Payload>(event: string, listener: (payload: Payload) => unknown): void;
|
|
17
18
|
export function useEvent<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): void;
|
|
18
19
|
|
|
19
20
|
export function useEvent(event: string, listener: EventListener): void {
|
|
@@ -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
|
+
}
|