@aerogel/core 0.0.0-next.f1f5a990033d966dc0bb12d251110fbc9350dcc7 → 0.0.0-next.f86b4b09f066c4aef21796a37dbc8417b7dce3cd
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 +730 -139
- package/dist/aerogel-core.esm.js +1 -1
- package/dist/aerogel-core.esm.js.map +1 -1
- package/package.json +3 -3
- package/src/bootstrap/bootstrap.test.ts +0 -1
- package/src/bootstrap/index.ts +18 -4
- package/src/bootstrap/options.ts +3 -0
- package/src/components/AGAppSnackbars.vue +1 -1
- package/src/components/composition.ts +23 -0
- 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 +21 -3
- package/src/components/forms/AGSelect.vue +10 -3
- package/src/components/headless/forms/AGHeadlessButton.ts +3 -0
- package/src/components/headless/forms/AGHeadlessButton.vue +23 -12
- package/src/components/headless/forms/AGHeadlessInput.ts +10 -4
- package/src/components/headless/forms/AGHeadlessInput.vue +18 -5
- package/src/components/headless/forms/AGHeadlessInputDescription.vue +28 -0
- package/src/components/headless/forms/AGHeadlessInputInput.vue +44 -5
- package/src/components/headless/forms/AGHeadlessInputTextArea.vue +43 -0
- package/src/components/headless/forms/AGHeadlessSelect.ts +15 -12
- package/src/components/headless/forms/AGHeadlessSelect.vue +23 -22
- package/src/components/headless/forms/AGHeadlessSelectOption.vue +6 -6
- package/src/components/headless/forms/composition.ts +10 -0
- package/src/components/headless/forms/index.ts +4 -0
- package/src/components/index.ts +2 -0
- package/src/components/interfaces.ts +24 -0
- package/src/components/lib/AGErrorMessage.vue +2 -2
- package/src/components/lib/AGMarkdown.vue +9 -4
- package/src/components/lib/AGMeasured.vue +1 -0
- package/src/components/modals/AGConfirmModal.ts +11 -3
- package/src/components/modals/AGConfirmModal.vue +2 -2
- package/src/components/modals/AGPromptModal.ts +36 -0
- package/src/components/modals/AGPromptModal.vue +34 -0
- package/src/components/modals/index.ts +10 -19
- package/src/directives/index.ts +2 -0
- package/src/directives/measure.ts +33 -5
- package/src/errors/Errors.ts +16 -19
- package/src/errors/index.ts +1 -10
- package/src/errors/utils.ts +35 -0
- package/src/forms/Form.test.ts +28 -0
- package/src/forms/Form.ts +66 -8
- package/src/forms/index.ts +3 -1
- package/src/forms/utils.ts +34 -3
- package/src/forms/validation.ts +19 -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 +44 -29
- package/src/main.ts +3 -0
- package/src/services/App.state.ts +18 -2
- package/src/services/App.ts +29 -3
- 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 +51 -13
- package/src/services/index.ts +4 -1
- 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 +7 -0
- package/src/ui/UI.ts +124 -18
- package/src/ui/index.ts +3 -0
- package/src/ui/utils.ts +16 -0
- package/src/utils/composition/state.test.ts +47 -0
- package/src/utils/composition/state.ts +24 -0
- package/src/utils/vue.ts +11 -2
- package/vite.config.ts +4 -1
package/src/services/Events.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { FluentArray } from '@noeldemartin/utils';
|
|
1
|
+
import { arrayRemove, facade, fail, tap } from '@noeldemartin/utils';
|
|
3
2
|
|
|
4
3
|
import Service from '@/services/Service';
|
|
5
4
|
|
|
6
5
|
export interface EventsPayload {}
|
|
6
|
+
export interface EventListenerOptions {
|
|
7
|
+
priority: number;
|
|
8
|
+
}
|
|
9
|
+
export type AerogelGlobalEvents = Partial<{ [Event in EventWithoutPayload]: () => unknown }> &
|
|
10
|
+
Partial<{ [Event in EventWithPayload]: EventListener<EventsPayload[Event]> }>;
|
|
7
11
|
|
|
8
12
|
export type EventListener<T = unknown> = (payload: T) => unknown;
|
|
9
13
|
export type UnknownEvent<T> = T extends keyof EventsPayload ? never : T;
|
|
@@ -16,70 +20,136 @@ export type EventWithPayload = {
|
|
|
16
20
|
[K in keyof EventsPayload]: EventsPayload[K] extends void ? never : K;
|
|
17
21
|
}[keyof EventsPayload];
|
|
18
22
|
|
|
23
|
+
export const EventListenerPriorities = {
|
|
24
|
+
Low: -256,
|
|
25
|
+
Default: 0,
|
|
26
|
+
High: 256,
|
|
27
|
+
} as const;
|
|
28
|
+
|
|
19
29
|
export class EventsService extends Service {
|
|
20
30
|
|
|
21
|
-
private listeners: Record<string,
|
|
31
|
+
private listeners: Record<string, { priorities: number[]; handlers: Record<number, EventListener[]> }> = {};
|
|
32
|
+
|
|
33
|
+
protected async boot(): Promise<void> {
|
|
34
|
+
Object.entries(globalThis.__aerogelEvents__ ?? {}).forEach(([event, listener]) =>
|
|
35
|
+
this.on(event as string, listener as EventListener));
|
|
36
|
+
}
|
|
22
37
|
|
|
23
38
|
public emit<Event extends EventWithoutPayload>(event: Event): Promise<void>;
|
|
24
39
|
public emit<Event extends EventWithPayload>(event: Event, payload: EventsPayload[Event]): Promise<void>;
|
|
25
40
|
public emit<Event extends string>(event: UnknownEvent<Event>, payload?: unknown): Promise<void>;
|
|
26
41
|
public async emit(event: string, payload?: unknown): Promise<void> {
|
|
27
|
-
const listeners =
|
|
42
|
+
const listeners = this.listeners[event] ?? { priorities: [], handlers: {} };
|
|
28
43
|
|
|
29
|
-
|
|
44
|
+
for (const priority of listeners.priorities) {
|
|
45
|
+
await Promise.all(listeners.handlers[priority]?.map((listener) => listener(payload)) ?? []);
|
|
46
|
+
}
|
|
30
47
|
}
|
|
31
48
|
|
|
49
|
+
/* eslint-disable max-len */
|
|
32
50
|
public on<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
33
|
-
public on<Event extends
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
): () => void | void;
|
|
37
|
-
|
|
51
|
+
public on<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
52
|
+
public on<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
53
|
+
public on<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
38
54
|
public on<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
39
|
-
public on(event:
|
|
40
|
-
|
|
55
|
+
public on<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
56
|
+
/* eslint-enable max-len */
|
|
41
57
|
|
|
42
|
-
|
|
58
|
+
public on(
|
|
59
|
+
event: string,
|
|
60
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
61
|
+
listener?: EventListener,
|
|
62
|
+
): () => void {
|
|
63
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
64
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
65
|
+
|
|
66
|
+
this.registerListener(event, options, handler);
|
|
67
|
+
|
|
68
|
+
return () => this.off(event, handler);
|
|
43
69
|
}
|
|
44
70
|
|
|
71
|
+
/* eslint-disable max-len */
|
|
45
72
|
public once<Event extends EventWithoutPayload>(event: Event, listener: () => unknown): () => void;
|
|
46
|
-
public once<Event extends
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
): () => void | void;
|
|
50
|
-
|
|
73
|
+
public once<Event extends EventWithoutPayload>(event: Event, options: Partial<EventListenerOptions>, listener: () => unknown): () => void; // prettier-ignore
|
|
74
|
+
public once<Event extends EventWithPayload>(event: Event, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
75
|
+
public once<Event extends EventWithPayload>(event: Event, options: Partial<EventListenerOptions>, listener: EventListener<EventsPayload[Event]>): () => void | void; // prettier-ignore
|
|
51
76
|
public once<Event extends string>(event: UnknownEvent<Event>, listener: EventListener): () => void;
|
|
52
|
-
public once(event:
|
|
77
|
+
public once<Event extends string>(event: UnknownEvent<Event>, options: Partial<EventListenerOptions>, listener: EventListener): () => void; // prettier-ignore
|
|
78
|
+
/* eslint-enable max-len */
|
|
79
|
+
|
|
80
|
+
public once(
|
|
81
|
+
event: string,
|
|
82
|
+
optionsOrListener: Partial<EventListenerOptions> | EventListener,
|
|
83
|
+
listener?: EventListener,
|
|
84
|
+
): () => void {
|
|
53
85
|
let onceListener: EventListener | null = null;
|
|
86
|
+
const options = typeof optionsOrListener === 'function' ? {} : optionsOrListener;
|
|
87
|
+
const handler = typeof optionsOrListener === 'function' ? optionsOrListener : (listener as EventListener);
|
|
54
88
|
|
|
55
89
|
return tap(
|
|
56
90
|
() => onceListener && this.off(event, onceListener),
|
|
57
91
|
(off) => {
|
|
58
|
-
|
|
59
|
-
(
|
|
60
|
-
off();
|
|
92
|
+
onceListener = (...args) => {
|
|
93
|
+
off();
|
|
61
94
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
95
|
+
return handler(...args);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
this.registerListener(event, options, handler);
|
|
65
99
|
},
|
|
66
100
|
);
|
|
67
101
|
}
|
|
68
102
|
|
|
69
103
|
public off(event: string, listener: EventListener): void {
|
|
70
|
-
const
|
|
104
|
+
const listeners = this.listeners[event];
|
|
71
105
|
|
|
72
|
-
if (!
|
|
106
|
+
if (!listeners) {
|
|
73
107
|
return;
|
|
74
108
|
}
|
|
75
109
|
|
|
76
|
-
|
|
110
|
+
const priorities = [...listeners.priorities];
|
|
111
|
+
|
|
112
|
+
for (const priority of priorities) {
|
|
113
|
+
arrayRemove(listeners.handlers[priority] ?? [], listener);
|
|
77
114
|
|
|
78
|
-
|
|
115
|
+
if (listeners.handlers[priority]?.length === 0) {
|
|
116
|
+
delete listeners.handlers[priority];
|
|
117
|
+
arrayRemove(listeners.priorities, priority);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (listeners.priorities.length === 0) {
|
|
79
122
|
delete this.listeners[event];
|
|
80
123
|
}
|
|
81
124
|
}
|
|
82
125
|
|
|
126
|
+
protected registerListener(event: string, options: Partial<EventListenerOptions>, handler: EventListener): void {
|
|
127
|
+
const priority = options.priority ?? 0;
|
|
128
|
+
|
|
129
|
+
if (!(event in this.listeners)) {
|
|
130
|
+
this.listeners[event] = { priorities: [], handlers: {} };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const priorities =
|
|
134
|
+
this.listeners[event]?.priorities ?? fail<number[]>(`priorities missing for event '${event}'`);
|
|
135
|
+
const handlers =
|
|
136
|
+
this.listeners[event]?.handlers ??
|
|
137
|
+
fail<Record<number, EventListener[]>>(`handlers missing for event '${event}'`);
|
|
138
|
+
|
|
139
|
+
if (!priorities.includes(priority)) {
|
|
140
|
+
priorities.push(priority);
|
|
141
|
+
priorities.sort((a, b) => b - a);
|
|
142
|
+
handlers[priority] = [];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
handlers[priority]?.push(handler);
|
|
146
|
+
}
|
|
147
|
+
|
|
83
148
|
}
|
|
84
149
|
|
|
85
|
-
export default facade(
|
|
150
|
+
export default facade(EventsService);
|
|
151
|
+
|
|
152
|
+
declare global {
|
|
153
|
+
// eslint-disable-next-line no-var
|
|
154
|
+
var __aerogelEvents__: AerogelGlobalEvents | undefined;
|
|
155
|
+
}
|
package/src/services/Service.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { MagicObject, PromisedValue, Storage, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
1
|
+
import { MagicObject, PromisedValue, Storage, fail, isEmpty, objectDeepClone, objectOnly } from '@noeldemartin/utils';
|
|
2
2
|
import type { Constructor } from '@noeldemartin/utils';
|
|
3
|
+
import type { MaybeRef } from 'vue';
|
|
3
4
|
import type { Store } from 'pinia';
|
|
4
5
|
|
|
5
6
|
import ServiceBootError from '@/errors/ServiceBootError';
|
|
@@ -8,9 +9,12 @@ import { defineServiceStore } from '@/services/store';
|
|
|
8
9
|
export type ServiceState = Record<string, any>; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
9
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
|
-
[K in keyof TComputedState]: (state: TState) => TComputedState[K];
|
|
17
|
+
[K in keyof TComputedState]: (state: UnrefServiceState<TState>) => TComputedState[K];
|
|
14
18
|
} & ThisType<{
|
|
15
19
|
readonly [K in keyof TComputedState]: TComputedState[K];
|
|
16
20
|
}>;
|
|
@@ -20,12 +24,14 @@ export function defineServiceState<
|
|
|
20
24
|
ComputedState extends ServiceState = {}
|
|
21
25
|
>(options: {
|
|
22
26
|
name: string;
|
|
23
|
-
initialState: State;
|
|
27
|
+
initialState: State | (() => State);
|
|
24
28
|
persist?: (keyof State)[];
|
|
25
29
|
computed?: ComputedStateDefinition<State, ComputedState>;
|
|
26
30
|
serialize?: (state: Partial<State>) => Partial<State>;
|
|
27
|
-
}): Constructor<State
|
|
28
|
-
|
|
31
|
+
}): Constructor<UnrefServiceState<State>> &
|
|
32
|
+
Constructor<ComputedState> &
|
|
33
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>> {
|
|
34
|
+
return class extends Service<UnrefServiceState<State>, ComputedState> {
|
|
29
35
|
|
|
30
36
|
public static persist = (options.persist as string[]) ?? [];
|
|
31
37
|
|
|
@@ -37,21 +43,41 @@ export function defineServiceState<
|
|
|
37
43
|
return options.name ?? null;
|
|
38
44
|
}
|
|
39
45
|
|
|
40
|
-
protected getInitialState(): State {
|
|
41
|
-
|
|
46
|
+
protected getInitialState(): UnrefServiceState<State> {
|
|
47
|
+
if (typeof options.initialState === 'function') {
|
|
48
|
+
return options.initialState();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return Object.entries(options.initialState).reduce((state, [key, value]) => {
|
|
52
|
+
try {
|
|
53
|
+
value = structuredClone(value);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
// eslint-disable-next-line no-console
|
|
56
|
+
console.warn(
|
|
57
|
+
`Could not clone '${key}' state from ${this.getName()} service, ` +
|
|
58
|
+
'this may cause problems if you\'re using multiple instances of the service ' +
|
|
59
|
+
'(for example, in unit tests).\n' +
|
|
60
|
+
'To fix this problem, declare your initialState as a function instead.',
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
state[key as keyof State] = value;
|
|
65
|
+
|
|
66
|
+
return state;
|
|
67
|
+
}, {} as UnrefServiceState<State>);
|
|
42
68
|
}
|
|
43
69
|
|
|
44
|
-
protected getComputedStateDefinition(): ComputedStateDefinition<State
|
|
45
|
-
return options.computed ??
|
|
70
|
+
protected getComputedStateDefinition(): ComputedStateDefinition<UnrefServiceState<State>, ComputedState> {
|
|
71
|
+
return (options.computed ?? {}) as ComputedStateDefinition<UnrefServiceState<State>, ComputedState>;
|
|
46
72
|
}
|
|
47
73
|
|
|
48
74
|
protected serializePersistedState(state: Partial<State>): Partial<State> {
|
|
49
75
|
return options.serialize?.(state) ?? state;
|
|
50
76
|
}
|
|
51
|
-
|
|
52
|
-
} as unknown as Constructor<State
|
|
77
|
+
|
|
78
|
+
} as unknown as Constructor<UnrefServiceState<State>> &
|
|
53
79
|
Constructor<ComputedState> &
|
|
54
|
-
Constructor<Service<State
|
|
80
|
+
Constructor<Service<UnrefServiceState<State>, ComputedState, Partial<UnrefServiceState<State>>>>;
|
|
55
81
|
}
|
|
56
82
|
|
|
57
83
|
export default class Service<
|
|
@@ -65,7 +91,7 @@ export default class Service<
|
|
|
65
91
|
protected _name: string;
|
|
66
92
|
private _booted: PromisedValue<void>;
|
|
67
93
|
private _computedStateKeys: Set<keyof State>;
|
|
68
|
-
private _store
|
|
94
|
+
private _store: Store<string, State, ComputedState, {}> | false;
|
|
69
95
|
|
|
70
96
|
constructor() {
|
|
71
97
|
super();
|
|
@@ -104,6 +130,10 @@ export default class Service<
|
|
|
104
130
|
return this._booted;
|
|
105
131
|
}
|
|
106
132
|
|
|
133
|
+
public hasPersistedState(): boolean {
|
|
134
|
+
return Storage.has(this._name);
|
|
135
|
+
}
|
|
136
|
+
|
|
107
137
|
public hasState<P extends keyof State>(property: P): boolean {
|
|
108
138
|
if (!this._store) {
|
|
109
139
|
return false;
|
|
@@ -220,4 +250,12 @@ export default class Service<
|
|
|
220
250
|
Storage.set(this._name, objectOnly(this.getState(), persist));
|
|
221
251
|
}
|
|
222
252
|
|
|
253
|
+
protected requireStore(): Store<string, State, ComputedState, {}> {
|
|
254
|
+
if (!this._store) {
|
|
255
|
+
return fail(`Failed getting '${this._name}' store`);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return this._store;
|
|
259
|
+
}
|
|
260
|
+
|
|
223
261
|
}
|
package/src/services/index.ts
CHANGED
|
@@ -3,15 +3,18 @@ import type { App as VueApp } from 'vue';
|
|
|
3
3
|
import { definePlugin } from '@/plugins';
|
|
4
4
|
|
|
5
5
|
import App from './App';
|
|
6
|
+
import Cache from './Cache';
|
|
6
7
|
import Events from './Events';
|
|
7
8
|
import Service from './Service';
|
|
8
9
|
import { getPiniaStore } from './store';
|
|
9
10
|
|
|
10
11
|
export * from './App';
|
|
12
|
+
export * from './Cache';
|
|
11
13
|
export * from './Events';
|
|
12
14
|
export * from './Service';
|
|
15
|
+
export * from './store';
|
|
13
16
|
|
|
14
|
-
export { App, Events, Service };
|
|
17
|
+
export { App, Cache, Events, Service };
|
|
15
18
|
|
|
16
19
|
const defaultServices = {
|
|
17
20
|
$app: App,
|
package/src/services/store.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
+
import { tap } from '@noeldemartin/utils';
|
|
1
2
|
import { createPinia, defineStore, setActivePinia } from 'pinia';
|
|
2
3
|
import type { DefineStoreOptions, Pinia, StateTree, Store, _GettersTree } from 'pinia';
|
|
3
4
|
|
|
4
5
|
let _store: Pinia | null = null;
|
|
5
6
|
|
|
6
7
|
function initializePiniaStore(): Pinia {
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
return _store ?? resetPiniaStore();
|
|
9
|
+
}
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
export function resetPiniaStore(): Pinia {
|
|
12
|
+
return tap(createPinia(), (store) => {
|
|
13
|
+
_store = store;
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
setActivePinia(store);
|
|
16
|
+
});
|
|
14
17
|
}
|
|
15
18
|
|
|
16
19
|
export function getPiniaStore(): Pinia {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { GetClosureArgs } from '@noeldemartin/utils';
|
|
2
|
+
|
|
3
|
+
import Events from '@/services/Events';
|
|
4
|
+
import { definePlugin } from '@/plugins';
|
|
5
|
+
|
|
6
|
+
export interface AerogelTestingRuntime {
|
|
7
|
+
on: (typeof Events)['on'];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default definePlugin({
|
|
11
|
+
async install() {
|
|
12
|
+
if (import.meta.env.MODE !== 'testing') {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
globalThis.testingRuntime = {
|
|
17
|
+
on: ((...args: GetClosureArgs<(typeof Events)['on']>) => Events.on(...args)) as (typeof Events)['on'],
|
|
18
|
+
};
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
declare global {
|
|
23
|
+
// eslint-disable-next-line no-var
|
|
24
|
+
var testingRuntime: AerogelTestingRuntime | undefined;
|
|
25
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { mock, tap } from '@noeldemartin/utils';
|
|
2
|
+
import { beforeEach, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
+
tap(globalThis, (global: any) => {
|
|
6
|
+
global.jest = vi;
|
|
7
|
+
global.navigator = { languages: ['en'] };
|
|
8
|
+
global.localStorage = mock<Storage>({
|
|
9
|
+
getItem: () => null,
|
|
10
|
+
setItem: () => null,
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
vi.stubGlobal('document', {
|
|
16
|
+
querySelector: () => null,
|
|
17
|
+
getElementById: () => null,
|
|
18
|
+
});
|
|
19
|
+
});
|
package/src/ui/UI.state.ts
CHANGED
|
@@ -2,6 +2,8 @@ import type { Component } from 'vue';
|
|
|
2
2
|
|
|
3
3
|
import { defineServiceState } from '@/services/Service';
|
|
4
4
|
|
|
5
|
+
import { Layouts, getCurrentLayout } from './utils';
|
|
6
|
+
|
|
5
7
|
export interface Modal<T = unknown> {
|
|
6
8
|
id: string;
|
|
7
9
|
properties: Record<string, unknown>;
|
|
@@ -28,5 +30,10 @@ export default defineServiceState({
|
|
|
28
30
|
initialState: {
|
|
29
31
|
modals: [] as Modal[],
|
|
30
32
|
snackbars: [] as Snackbar[],
|
|
33
|
+
layout: getCurrentLayout(),
|
|
34
|
+
},
|
|
35
|
+
computed: {
|
|
36
|
+
mobile: ({ layout }) => layout === Layouts.Mobile,
|
|
37
|
+
desktop: ({ layout }) => layout === Layouts.Desktop,
|
|
31
38
|
},
|
|
32
39
|
});
|
package/src/ui/UI.ts
CHANGED
|
@@ -3,12 +3,15 @@ import { markRaw, nextTick } from 'vue';
|
|
|
3
3
|
import type { Component } from 'vue';
|
|
4
4
|
import type { ObjectValues } from '@noeldemartin/utils';
|
|
5
5
|
|
|
6
|
+
import App from '@/services/App';
|
|
6
7
|
import Events from '@/services/Events';
|
|
8
|
+
import type { Color } from '@/components/constants';
|
|
7
9
|
import type { SnackbarAction, SnackbarColor } from '@/components/headless/snackbars';
|
|
10
|
+
import type { AGAlertModalProps, AGConfirmModalProps, AGLoadingModalProps, AGPromptModalProps } from '@/components';
|
|
8
11
|
|
|
9
12
|
import Service from './UI.state';
|
|
13
|
+
import { MOBILE_BREAKPOINT, getCurrentLayout } from './utils';
|
|
10
14
|
import type { Modal, ModalComponent, Snackbar } from './UI.state';
|
|
11
|
-
import type { AGAlertModalProps, AGConfirmModalProps, AGLoadingModalProps } from '@/components';
|
|
12
15
|
|
|
13
16
|
interface ModalCallbacks<T = unknown> {
|
|
14
17
|
willClose(result: T | undefined): void;
|
|
@@ -25,15 +28,35 @@ export const UIComponents = {
|
|
|
25
28
|
ConfirmModal: 'confirm-modal',
|
|
26
29
|
ErrorReportModal: 'error-report-modal',
|
|
27
30
|
LoadingModal: 'loading-modal',
|
|
31
|
+
PromptModal: 'prompt-modal',
|
|
28
32
|
Snackbar: 'snackbar',
|
|
29
33
|
StartupCrash: 'startup-crash',
|
|
30
34
|
} as const;
|
|
31
35
|
|
|
32
36
|
export type UIComponent = ObjectValues<typeof UIComponents>;
|
|
33
37
|
|
|
38
|
+
export type ConfirmCheckboxes = Record<string, { label: string; default?: boolean; required?: boolean }>;
|
|
39
|
+
|
|
34
40
|
export interface ConfirmOptions {
|
|
35
41
|
acceptText?: string;
|
|
42
|
+
acceptColor?: Color;
|
|
43
|
+
cancelText?: string;
|
|
44
|
+
cancelColor?: Color;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ConfirmOptionsWithCheckboxes<T extends ConfirmCheckboxes = ConfirmCheckboxes> extends ConfirmOptions {
|
|
48
|
+
checkboxes?: T;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface PromptOptions {
|
|
52
|
+
label?: string;
|
|
53
|
+
defaultValue?: string;
|
|
54
|
+
placeholder?: string;
|
|
55
|
+
acceptText?: string;
|
|
56
|
+
acceptColor?: Color;
|
|
36
57
|
cancelText?: string;
|
|
58
|
+
cancelColor?: Color;
|
|
59
|
+
trim?: boolean;
|
|
37
60
|
}
|
|
38
61
|
|
|
39
62
|
export interface ShowSnackbarOptions {
|
|
@@ -68,13 +91,18 @@ export class UIService extends Service {
|
|
|
68
91
|
this.openModal(this.requireComponent(UIComponents.AlertModal), getProperties());
|
|
69
92
|
}
|
|
70
93
|
|
|
94
|
+
/* eslint-disable max-len */
|
|
71
95
|
public async confirm(message: string, options?: ConfirmOptions): Promise<boolean>;
|
|
72
96
|
public async confirm(title: string, message: string, options?: ConfirmOptions): Promise<boolean>;
|
|
97
|
+
public async confirm<T extends ConfirmCheckboxes>(message: string, options?: ConfirmOptionsWithCheckboxes<T>): Promise<[boolean, Record<keyof T, boolean>]>; // prettier-ignore
|
|
98
|
+
public async confirm<T extends ConfirmCheckboxes>(title: string, message: string, options?: ConfirmOptionsWithCheckboxes<T>): Promise<[boolean, Record<keyof T, boolean>]>; // prettier-ignore
|
|
99
|
+
/* eslint-enable max-len */
|
|
100
|
+
|
|
73
101
|
public async confirm(
|
|
74
102
|
messageOrTitle: string,
|
|
75
|
-
messageOrOptions?: string | ConfirmOptions,
|
|
76
|
-
options?: ConfirmOptions,
|
|
77
|
-
): Promise<boolean> {
|
|
103
|
+
messageOrOptions?: string | ConfirmOptions | ConfirmOptionsWithCheckboxes,
|
|
104
|
+
options?: ConfirmOptions | ConfirmOptionsWithCheckboxes,
|
|
105
|
+
): Promise<boolean | [boolean, Record<string, boolean>]> {
|
|
78
106
|
const getProperties = (): AGConfirmModalProps => {
|
|
79
107
|
if (typeof messageOrOptions !== 'string') {
|
|
80
108
|
return {
|
|
@@ -89,19 +117,79 @@ export class UIService extends Service {
|
|
|
89
117
|
...(options ?? {}),
|
|
90
118
|
};
|
|
91
119
|
};
|
|
120
|
+
const properties = getProperties();
|
|
121
|
+
const modal = await this.openModal<
|
|
122
|
+
ModalComponent<AGConfirmModalProps, boolean | [boolean, Record<string, boolean>]>
|
|
123
|
+
>(this.requireComponent(UIComponents.ConfirmModal), properties);
|
|
124
|
+
const result = await modal.beforeClose;
|
|
92
125
|
|
|
93
|
-
const
|
|
94
|
-
|
|
126
|
+
const confirmed = typeof result === 'object' ? result[0] : result ?? false;
|
|
127
|
+
const checkboxes =
|
|
128
|
+
typeof result === 'object'
|
|
129
|
+
? result[1]
|
|
130
|
+
: Object.entries(properties.checkboxes ?? {}).reduce(
|
|
131
|
+
(values, [checkbox, { default: defaultValue }]) => ({
|
|
132
|
+
[checkbox]: defaultValue ?? false,
|
|
133
|
+
...values,
|
|
134
|
+
}),
|
|
135
|
+
{} as Record<string, boolean>,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
for (const [name, checkbox] of Object.entries(properties.checkboxes ?? {})) {
|
|
139
|
+
if (!checkbox.required || checkboxes[name]) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (confirmed && App.development) {
|
|
144
|
+
// eslint-disable-next-line no-console
|
|
145
|
+
console.warn(`Confirmed confirm modal was suppressed because required '${name}' checkbox was missing`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return [false, checkboxes];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return 'checkboxes' in properties ? [confirmed, checkboxes] : confirmed;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public async prompt(message: string, options?: PromptOptions): Promise<string | null>;
|
|
155
|
+
public async prompt(title: string, message: string, options?: PromptOptions): Promise<string | null>;
|
|
156
|
+
public async prompt(
|
|
157
|
+
messageOrTitle: string,
|
|
158
|
+
messageOrOptions?: string | PromptOptions,
|
|
159
|
+
options?: PromptOptions,
|
|
160
|
+
): Promise<string | null> {
|
|
161
|
+
const trim = options?.trim ?? true;
|
|
162
|
+
const getProperties = (): AGPromptModalProps => {
|
|
163
|
+
if (typeof messageOrOptions !== 'string') {
|
|
164
|
+
return {
|
|
165
|
+
message: messageOrTitle,
|
|
166
|
+
...(messageOrOptions ?? {}),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
title: messageOrTitle,
|
|
172
|
+
message: messageOrOptions,
|
|
173
|
+
...(options ?? {}),
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const modal = await this.openModal<ModalComponent<AGPromptModalProps, string | null>>(
|
|
178
|
+
this.requireComponent(UIComponents.PromptModal),
|
|
95
179
|
getProperties(),
|
|
96
180
|
);
|
|
97
|
-
const
|
|
181
|
+
const rawResult = await modal.beforeClose;
|
|
182
|
+
const result = trim && typeof rawResult === 'string' ? rawResult?.trim() : rawResult;
|
|
98
183
|
|
|
99
|
-
return result ??
|
|
184
|
+
return result ?? null;
|
|
100
185
|
}
|
|
101
186
|
|
|
102
|
-
public async loading<T>(operation: Promise<T>): Promise<T>;
|
|
103
|
-
public async loading<T>(message: string, operation: Promise<T>): Promise<T>;
|
|
104
|
-
public async loading<T>(
|
|
187
|
+
public async loading<T>(operation: Promise<T> | (() => T)): Promise<T>;
|
|
188
|
+
public async loading<T>(message: string, operation: Promise<T> | (() => T)): Promise<T>;
|
|
189
|
+
public async loading<T>(
|
|
190
|
+
messageOrOperation: string | Promise<T> | (() => T),
|
|
191
|
+
operation?: Promise<T> | (() => T),
|
|
192
|
+
): Promise<T> {
|
|
105
193
|
const getProperties = (): AGLoadingModalProps => {
|
|
106
194
|
if (typeof messageOrOperation !== 'string') {
|
|
107
195
|
return {};
|
|
@@ -113,7 +201,8 @@ export class UIService extends Service {
|
|
|
113
201
|
const modal = await this.openModal(this.requireComponent(UIComponents.LoadingModal), getProperties());
|
|
114
202
|
|
|
115
203
|
try {
|
|
116
|
-
operation = typeof messageOrOperation === 'string' ? (operation as
|
|
204
|
+
operation = typeof messageOrOperation === 'string' ? (operation as () => T) : messageOrOperation;
|
|
205
|
+
operation = typeof operation === 'function' ? Promise.resolve(operation()) : operation;
|
|
117
206
|
|
|
118
207
|
const [result] = await Promise.all([operation, after({ seconds: 1 })]);
|
|
119
208
|
|
|
@@ -127,7 +216,7 @@ export class UIService extends Service {
|
|
|
127
216
|
const snackbar: Snackbar = {
|
|
128
217
|
id: uuid(),
|
|
129
218
|
properties: { message, ...options },
|
|
130
|
-
component: options.component ??
|
|
219
|
+
component: markRaw(options.component ?? this.requireComponent(UIComponents.Snackbar)),
|
|
131
220
|
};
|
|
132
221
|
|
|
133
222
|
this.setState('snackbars', this.snackbars.concat(snackbar));
|
|
@@ -183,6 +272,7 @@ export class UIService extends Service {
|
|
|
183
272
|
protected async boot(): Promise<void> {
|
|
184
273
|
this.watchModalEvents();
|
|
185
274
|
this.watchMountedEvent();
|
|
275
|
+
this.watchViewportBreakpoints();
|
|
186
276
|
}
|
|
187
277
|
|
|
188
278
|
private watchModalEvents(): void {
|
|
@@ -212,13 +302,17 @@ export class UIService extends Service {
|
|
|
212
302
|
|
|
213
303
|
private watchMountedEvent(): void {
|
|
214
304
|
Events.once('application-mounted', async () => {
|
|
215
|
-
|
|
305
|
+
if (!globalThis.document || !globalThis.getComputedStyle) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const splash = globalThis.document.getElementById('splash');
|
|
216
310
|
|
|
217
311
|
if (!splash) {
|
|
218
312
|
return;
|
|
219
313
|
}
|
|
220
314
|
|
|
221
|
-
if (
|
|
315
|
+
if (globalThis.getComputedStyle(splash).opacity !== '0') {
|
|
222
316
|
splash.style.opacity = '0';
|
|
223
317
|
|
|
224
318
|
await after({ ms: 600 });
|
|
@@ -228,16 +322,28 @@ export class UIService extends Service {
|
|
|
228
322
|
});
|
|
229
323
|
}
|
|
230
324
|
|
|
325
|
+
private watchViewportBreakpoints(): void {
|
|
326
|
+
if (!globalThis.matchMedia) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const media = globalThis.matchMedia(`(min-width: ${MOBILE_BREAKPOINT}px)`);
|
|
331
|
+
|
|
332
|
+
media.addEventListener('change', () => this.setState({ layout: getCurrentLayout() }));
|
|
333
|
+
}
|
|
334
|
+
|
|
231
335
|
}
|
|
232
336
|
|
|
233
|
-
export default facade(
|
|
337
|
+
export default facade(UIService);
|
|
234
338
|
|
|
235
339
|
declare module '@/services/Events' {
|
|
236
340
|
export interface EventsPayload {
|
|
237
|
-
'modal-will-close': { modal: Modal; result?: unknown };
|
|
238
|
-
'modal-closed': { modal: Modal; result?: unknown };
|
|
239
341
|
'close-modal': { id: string; result?: unknown };
|
|
240
342
|
'hide-modal': { id: string };
|
|
343
|
+
'hide-overlays-backdrop': void;
|
|
344
|
+
'modal-closed': { modal: Modal; result?: unknown };
|
|
345
|
+
'modal-will-close': { modal: Modal; result?: unknown };
|
|
241
346
|
'show-modal': { id: string };
|
|
347
|
+
'show-overlays-backdrop': void;
|
|
242
348
|
}
|
|
243
349
|
}
|