@aerogel/core 0.0.0-next.9a02fcd3bcf698211dd7a71d4c48257c96dd7832 → 0.0.0-next.9e0c0bbdcff5db68a1087ef53cbdc0f53299f6bb

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.
Files changed (198) hide show
  1. package/dist/aerogel-core.d.ts +2529 -1597
  2. package/dist/aerogel-core.js +3265 -0
  3. package/dist/aerogel-core.js.map +1 -0
  4. package/package.json +30 -37
  5. package/src/bootstrap/bootstrap.test.ts +4 -8
  6. package/src/bootstrap/index.ts +25 -16
  7. package/src/bootstrap/options.ts +1 -1
  8. package/src/components/{AGAppLayout.vue → AppLayout.vue} +4 -4
  9. package/src/components/{AGAppModals.vue → AppModals.vue} +3 -4
  10. package/src/components/{AGAppOverlays.vue → AppOverlays.vue} +5 -10
  11. package/src/components/AppToasts.vue +16 -0
  12. package/src/components/contracts/AlertModal.ts +4 -0
  13. package/src/components/contracts/Button.ts +16 -0
  14. package/src/components/contracts/ConfirmModal.ts +41 -0
  15. package/src/components/contracts/DropdownMenu.ts +20 -0
  16. package/src/components/contracts/ErrorReportModal.ts +29 -0
  17. package/src/components/contracts/Input.ts +26 -0
  18. package/src/components/contracts/LoadingModal.ts +22 -0
  19. package/src/components/contracts/Modal.ts +21 -0
  20. package/src/components/contracts/PromptModal.ts +30 -0
  21. package/src/components/contracts/Select.ts +44 -0
  22. package/src/components/contracts/Toast.ts +13 -0
  23. package/src/components/contracts/index.ts +11 -0
  24. package/src/components/headless/HeadlessButton.vue +51 -0
  25. package/src/components/headless/HeadlessInput.vue +59 -0
  26. package/src/components/headless/HeadlessInputDescription.vue +27 -0
  27. package/src/components/headless/{forms/AGHeadlessInputError.vue → HeadlessInputError.vue} +4 -8
  28. package/src/components/headless/HeadlessInputInput.vue +75 -0
  29. package/src/components/headless/{forms/AGHeadlessInputLabel.vue → HeadlessInputLabel.vue} +3 -7
  30. package/src/components/headless/HeadlessInputTextArea.vue +40 -0
  31. package/src/components/headless/HeadlessModal.vue +92 -0
  32. package/src/components/headless/HeadlessModalContent.vue +30 -0
  33. package/src/components/headless/HeadlessModalDescription.vue +12 -0
  34. package/src/components/headless/HeadlessModalOverlay.vue +12 -0
  35. package/src/components/headless/HeadlessModalTitle.vue +12 -0
  36. package/src/components/headless/HeadlessSelect.vue +113 -0
  37. package/src/components/headless/{forms/AGHeadlessSelectError.vue → HeadlessSelectError.vue} +5 -6
  38. package/src/components/headless/HeadlessSelectLabel.vue +25 -0
  39. package/src/components/headless/HeadlessSelectOption.vue +34 -0
  40. package/src/components/headless/HeadlessSelectOptions.vue +37 -0
  41. package/src/components/headless/HeadlessSelectTrigger.vue +22 -0
  42. package/src/components/headless/HeadlessSelectValue.vue +18 -0
  43. package/src/components/headless/HeadlessToast.vue +18 -0
  44. package/src/components/headless/HeadlessToastAction.vue +13 -0
  45. package/src/components/headless/index.ts +19 -3
  46. package/src/components/index.ts +6 -9
  47. package/src/components/ui/AdvancedOptions.vue +18 -0
  48. package/src/components/ui/AlertModal.vue +13 -0
  49. package/src/components/ui/Button.vue +98 -0
  50. package/src/components/ui/Checkbox.vue +56 -0
  51. package/src/components/ui/ConfirmModal.vue +42 -0
  52. package/src/components/ui/DropdownMenu.vue +32 -0
  53. package/src/components/ui/DropdownMenuOption.vue +14 -0
  54. package/src/components/ui/DropdownMenuOptions.vue +27 -0
  55. package/src/components/ui/EditableContent.vue +82 -0
  56. package/src/components/ui/ErrorMessage.vue +15 -0
  57. package/src/components/ui/ErrorReportModal.vue +62 -0
  58. package/src/components/{modals/AGErrorReportModalButtons.vue → ui/ErrorReportModalButtons.vue} +34 -27
  59. package/src/components/ui/ErrorReportModalTitle.vue +24 -0
  60. package/src/components/ui/Form.vue +24 -0
  61. package/src/components/ui/Input.vue +56 -0
  62. package/src/components/ui/Link.vue +12 -0
  63. package/src/components/ui/LoadingModal.vue +32 -0
  64. package/src/components/ui/Markdown.vue +69 -0
  65. package/src/components/ui/Modal.vue +91 -0
  66. package/src/components/ui/ModalContext.vue +30 -0
  67. package/src/components/ui/ProgressBar.vue +51 -0
  68. package/src/components/ui/PromptModal.vue +35 -0
  69. package/src/components/ui/Select.vue +25 -0
  70. package/src/components/ui/SelectLabel.vue +17 -0
  71. package/src/components/ui/SelectOption.vue +29 -0
  72. package/src/components/ui/SelectOptions.vue +30 -0
  73. package/src/components/ui/SelectTrigger.vue +29 -0
  74. package/src/components/ui/SettingsModal.vue +15 -0
  75. package/src/components/{lib/AGStartupCrash.vue → ui/StartupCrash.vue} +8 -8
  76. package/src/components/ui/Toast.vue +42 -0
  77. package/src/components/ui/index.ts +30 -0
  78. package/src/directives/index.ts +11 -5
  79. package/src/directives/measure.ts +34 -6
  80. package/src/errors/Errors.state.ts +1 -1
  81. package/src/errors/Errors.ts +17 -18
  82. package/src/errors/JobCancelledError.ts +3 -0
  83. package/src/errors/index.ts +9 -6
  84. package/src/errors/utils.ts +17 -1
  85. package/src/forms/{Form.test.ts → FormController.test.ts} +33 -4
  86. package/src/forms/{Form.ts → FormController.ts} +85 -25
  87. package/src/forms/composition.ts +4 -4
  88. package/src/forms/index.ts +3 -1
  89. package/src/forms/utils.ts +36 -5
  90. package/src/forms/validation.ts +19 -0
  91. package/src/index.css +54 -0
  92. package/src/{main.ts → index.ts} +1 -0
  93. package/src/jobs/Job.ts +144 -2
  94. package/src/jobs/index.ts +4 -1
  95. package/src/jobs/listeners.ts +3 -0
  96. package/src/jobs/status.ts +4 -0
  97. package/src/lang/DefaultLangProvider.ts +46 -0
  98. package/src/lang/Lang.state.ts +11 -0
  99. package/src/lang/Lang.ts +48 -21
  100. package/src/lang/index.ts +12 -6
  101. package/src/lang/settings/Language.vue +48 -0
  102. package/src/lang/settings/index.ts +10 -0
  103. package/src/plugins/Plugin.ts +1 -1
  104. package/src/plugins/index.ts +10 -7
  105. package/src/services/App.state.ts +36 -3
  106. package/src/services/App.ts +19 -3
  107. package/src/services/Cache.ts +43 -0
  108. package/src/services/Events.ts +15 -5
  109. package/src/services/Service.ts +125 -54
  110. package/src/services/Storage.ts +20 -0
  111. package/src/services/index.ts +16 -5
  112. package/src/services/utils.ts +18 -0
  113. package/src/testing/index.ts +4 -3
  114. package/src/testing/setup.ts +11 -0
  115. package/src/ui/UI.state.ts +19 -7
  116. package/src/ui/UI.ts +188 -77
  117. package/src/ui/index.ts +19 -18
  118. package/src/ui/utils.ts +16 -0
  119. package/src/utils/classes.ts +49 -0
  120. package/src/utils/composition/events.ts +2 -2
  121. package/src/utils/composition/forms.ts +14 -4
  122. package/src/utils/composition/persistent.test.ts +33 -0
  123. package/src/utils/composition/persistent.ts +11 -0
  124. package/src/utils/composition/state.test.ts +47 -0
  125. package/src/utils/composition/state.ts +33 -0
  126. package/src/utils/index.ts +5 -1
  127. package/src/utils/markdown.test.ts +50 -0
  128. package/src/utils/markdown.ts +19 -6
  129. package/src/utils/types.ts +3 -0
  130. package/src/utils/vue.ts +28 -127
  131. package/dist/aerogel-core.cjs.js +0 -2
  132. package/dist/aerogel-core.cjs.js.map +0 -1
  133. package/dist/aerogel-core.esm.js +0 -2
  134. package/dist/aerogel-core.esm.js.map +0 -1
  135. package/histoire.config.ts +0 -7
  136. package/noeldemartin.config.js +0 -5
  137. package/postcss.config.js +0 -6
  138. package/src/assets/histoire.css +0 -3
  139. package/src/components/AGAppSnackbars.vue +0 -13
  140. package/src/components/constants.ts +0 -8
  141. package/src/components/forms/AGButton.vue +0 -44
  142. package/src/components/forms/AGCheckbox.vue +0 -41
  143. package/src/components/forms/AGForm.vue +0 -26
  144. package/src/components/forms/AGInput.vue +0 -38
  145. package/src/components/forms/AGSelect.story.vue +0 -46
  146. package/src/components/forms/AGSelect.vue +0 -60
  147. package/src/components/forms/index.ts +0 -5
  148. package/src/components/headless/forms/AGHeadlessButton.vue +0 -51
  149. package/src/components/headless/forms/AGHeadlessInput.ts +0 -28
  150. package/src/components/headless/forms/AGHeadlessInput.vue +0 -57
  151. package/src/components/headless/forms/AGHeadlessInputInput.vue +0 -47
  152. package/src/components/headless/forms/AGHeadlessSelect.ts +0 -42
  153. package/src/components/headless/forms/AGHeadlessSelect.vue +0 -77
  154. package/src/components/headless/forms/AGHeadlessSelectButton.vue +0 -24
  155. package/src/components/headless/forms/AGHeadlessSelectLabel.vue +0 -24
  156. package/src/components/headless/forms/AGHeadlessSelectOption.ts +0 -4
  157. package/src/components/headless/forms/AGHeadlessSelectOption.vue +0 -39
  158. package/src/components/headless/forms/AGHeadlessSelectOptions.ts +0 -3
  159. package/src/components/headless/forms/index.ts +0 -14
  160. package/src/components/headless/modals/AGHeadlessModal.ts +0 -34
  161. package/src/components/headless/modals/AGHeadlessModal.vue +0 -86
  162. package/src/components/headless/modals/AGHeadlessModalPanel.vue +0 -28
  163. package/src/components/headless/modals/AGHeadlessModalTitle.vue +0 -13
  164. package/src/components/headless/modals/index.ts +0 -4
  165. package/src/components/headless/snackbars/AGHeadlessSnackbar.vue +0 -10
  166. package/src/components/headless/snackbars/index.ts +0 -40
  167. package/src/components/lib/AGErrorMessage.vue +0 -16
  168. package/src/components/lib/AGLink.vue +0 -9
  169. package/src/components/lib/AGMarkdown.vue +0 -36
  170. package/src/components/lib/AGMeasured.vue +0 -15
  171. package/src/components/lib/index.ts +0 -5
  172. package/src/components/modals/AGAlertModal.ts +0 -15
  173. package/src/components/modals/AGAlertModal.vue +0 -14
  174. package/src/components/modals/AGConfirmModal.ts +0 -27
  175. package/src/components/modals/AGConfirmModal.vue +0 -26
  176. package/src/components/modals/AGErrorReportModal.ts +0 -46
  177. package/src/components/modals/AGErrorReportModal.vue +0 -54
  178. package/src/components/modals/AGErrorReportModalTitle.vue +0 -25
  179. package/src/components/modals/AGLoadingModal.ts +0 -23
  180. package/src/components/modals/AGLoadingModal.vue +0 -15
  181. package/src/components/modals/AGModal.ts +0 -10
  182. package/src/components/modals/AGModal.vue +0 -39
  183. package/src/components/modals/AGModalContext.ts +0 -8
  184. package/src/components/modals/AGModalContext.vue +0 -22
  185. package/src/components/modals/AGModalTitle.vue +0 -9
  186. package/src/components/modals/AGPromptModal.ts +0 -30
  187. package/src/components/modals/AGPromptModal.vue +0 -34
  188. package/src/components/modals/index.ts +0 -17
  189. package/src/components/snackbars/AGSnackbar.vue +0 -36
  190. package/src/components/snackbars/index.ts +0 -3
  191. package/src/components/utils.ts +0 -10
  192. package/src/directives/initial-focus.ts +0 -11
  193. package/src/main.histoire.ts +0 -1
  194. package/src/utils/tailwindcss.test.ts +0 -26
  195. package/src/utils/tailwindcss.ts +0 -7
  196. package/tailwind.config.js +0 -4
  197. package/tsconfig.json +0 -11
  198. package/vite.config.ts +0 -14
@@ -0,0 +1,29 @@
1
+ <template>
2
+ <HeadlessSelectTrigger :class="renderedClasses">
3
+ <HeadlessSelectValue class="col-start-1 row-start-1 truncate pr-6" />
4
+ <IconCheveronDown class="col-start-1 row-start-1 size-5 self-center justify-self-end text-gray-500 sm:size-4" />
5
+ </HeadlessSelectTrigger>
6
+ </template>
7
+
8
+ <script setup lang="ts">
9
+ import IconCheveronDown from '~icons/zondicons/cheveron-down';
10
+
11
+ import { computed } from 'vue';
12
+ import type { HTMLAttributes } from 'vue';
13
+
14
+ import HeadlessSelectTrigger from '@aerogel/core/components/headless/HeadlessSelectTrigger.vue';
15
+ import HeadlessSelectValue from '@aerogel/core/components/headless/HeadlessSelectValue.vue';
16
+ import { injectReactiveOrFail } from '@aerogel/core/utils';
17
+ import { classes } from '@aerogel/core/utils/classes';
18
+ import type { SelectExpose } from '@aerogel/core/components/contracts/Select';
19
+
20
+ const { class: rootClasses } = defineProps<{ class?: HTMLAttributes['class'] }>();
21
+ const select = injectReactiveOrFail<SelectExpose>('select', '<SelectTrigger> must be a child of a <Select>');
22
+ const renderedClasses = computed(() =>
23
+ classes(
24
+ // eslint-disable-next-line vue/max-len
25
+ 'focus:outline-primary-600 grid w-full cursor-default grid-cols-1 rounded-md bg-white py-1.5 pr-2 pl-3 text-left text-gray-900 outline-1 -outline-offset-1 outline-gray-300 focus:outline-2 focus:-outline-offset-2 sm:text-sm/6',
26
+ { 'mt-1': select.label },
27
+ rootClasses,
28
+ ));
29
+ </script>
@@ -0,0 +1,15 @@
1
+ <template>
2
+ <Modal :title="$td('settings.title', 'Settings')">
3
+ <component :is="setting.component" v-for="(setting, key) in settings" :key />
4
+ </Modal>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import { arraySorted } from '@noeldemartin/utils';
9
+ import { computed } from 'vue';
10
+
11
+ import App from '@aerogel/core/services/App';
12
+ import Modal from '@aerogel/core/components/ui/Modal.vue';
13
+
14
+ const settings = computed(() => arraySorted(App.settings, 'priority', 'desc'));
15
+ </script>
@@ -1,10 +1,10 @@
1
1
  <template>
2
- <div class="grid flex-grow place-items-center">
2
+ <div class="grid grow place-items-center">
3
3
  <div class="flex flex-col items-center space-y-6 p-8">
4
4
  <h1 class="mt-2 text-center text-4xl font-medium text-red-600">
5
5
  {{ $td('startupCrash.title', 'Something went wrong!') }}
6
6
  </h1>
7
- <AGMarkdown
7
+ <Markdown
8
8
  :text="
9
9
  $td(
10
10
  'startupCrash.message',
@@ -14,18 +14,18 @@
14
14
  class="mt-4 text-center"
15
15
  />
16
16
  <div class="mt-4 flex flex-col space-y-4">
17
- <AGButton color="danger" @click="$app.reload()">
17
+ <Button variant="danger" @click="$app.reload()">
18
18
  {{ $td('startupCrash.reload', 'Try again') }}
19
- </AGButton>
20
- <AGButton color="danger" @click="$errors.inspect($errors.startupErrors)">
19
+ </Button>
20
+ <Button variant="danger" @click="$errors.inspect($errors.startupErrors)">
21
21
  {{ $td('startupCrash.inspect', 'View error details') }}
22
- </AGButton>
22
+ </Button>
23
23
  </div>
24
24
  </div>
25
25
  </div>
26
26
  </template>
27
27
 
28
28
  <script setup lang="ts">
29
- import AGMarkdown from './AGMarkdown.vue';
30
- import AGButton from '../forms/AGButton.vue';
29
+ import Markdown from '@aerogel/core/components/ui/Markdown.vue';
30
+ import Button from '@aerogel/core/components/ui/Button.vue';
31
31
  </script>
@@ -0,0 +1,42 @@
1
+ <template>
2
+ <HeadlessToast :class="renderedClasses">
3
+ <Markdown v-if="message" :text="message" inline />
4
+
5
+ <Button
6
+ v-for="(action, key) of actions"
7
+ :key
8
+ :action
9
+ :variant
10
+ :as="HeadlessToastAction"
11
+ />
12
+ </HeadlessToast>
13
+ </template>
14
+
15
+ <script setup lang="ts">
16
+ import type { HTMLAttributes } from 'vue';
17
+
18
+ import Button from '@aerogel/core/components/ui/Button.vue';
19
+ import Markdown from '@aerogel/core/components/ui/Markdown.vue';
20
+ import HeadlessToast from '@aerogel/core/components/headless/HeadlessToast.vue';
21
+ import HeadlessToastAction from '@aerogel/core/components/headless/HeadlessToastAction.vue';
22
+ import { computedVariantClasses } from '@aerogel/core/utils/classes';
23
+ import type { ToastProps } from '@aerogel/core/components/contracts/Toast';
24
+ import type { Variants } from '@aerogel/core/utils/classes';
25
+
26
+ const { class: baseClasses, variant = 'secondary' } = defineProps<ToastProps & { class?: HTMLAttributes['class'] }>();
27
+ const renderedClasses = computedVariantClasses<Variants<Pick<ToastProps, 'variant'>>>(
28
+ { baseClasses, variant },
29
+ {
30
+ baseClasses: 'flex items-center gap-2 rounded-md p-2 ring-1 shadow-lg border-gray-200',
31
+ variants: {
32
+ variant: {
33
+ secondary: 'bg-gray-900 text-white ring-black',
34
+ danger: 'bg-red-50 text-red-900 ring-red-100',
35
+ },
36
+ },
37
+ defaultVariants: {
38
+ variant: 'secondary',
39
+ },
40
+ },
41
+ );
42
+ </script>
@@ -0,0 +1,30 @@
1
+ export { default as AdvancedOptions } from './AdvancedOptions.vue';
2
+ export { default as AlertModal } from './AlertModal.vue';
3
+ export { default as Button } from './Button.vue';
4
+ export { default as Checkbox } from './Checkbox.vue';
5
+ export { default as ConfirmModal } from './ConfirmModal.vue';
6
+ export { default as DropdownMenu } from './DropdownMenu.vue';
7
+ export { default as DropdownMenuOption } from './DropdownMenuOption.vue';
8
+ export { default as DropdownMenuOptions } from './DropdownMenuOptions.vue';
9
+ export { default as EditableContent } from './EditableContent.vue';
10
+ export { default as ErrorMessage } from './ErrorMessage.vue';
11
+ export { default as ErrorReportModal } from './ErrorReportModal.vue';
12
+ export { default as ErrorReportModalButtons } from './ErrorReportModalButtons.vue';
13
+ export { default as ErrorReportModalTitle } from './ErrorReportModalTitle.vue';
14
+ export { default as Form } from './Form.vue';
15
+ export { default as Input } from './Input.vue';
16
+ export { default as Link } from './Link.vue';
17
+ export { default as LoadingModal } from './LoadingModal.vue';
18
+ export { default as Markdown } from './Markdown.vue';
19
+ export { default as Modal } from './Modal.vue';
20
+ export { default as ModalContext } from './ModalContext.vue';
21
+ export { default as ProgressBar } from './ProgressBar.vue';
22
+ export { default as PromptModal } from './PromptModal.vue';
23
+ export { default as Select } from './Select.vue';
24
+ export { default as SelectLabel } from './SelectLabel.vue';
25
+ export { default as SelectOption } from './SelectOption.vue';
26
+ export { default as SelectOptions } from './SelectOptions.vue';
27
+ export { default as SelectTrigger } from './SelectTrigger.vue';
28
+ export { default as SettingsModal } from './SettingsModal.vue';
29
+ export { default as StartupCrash } from './StartupCrash.vue';
30
+ export { default as Toast } from './Toast.vue';
@@ -1,15 +1,15 @@
1
1
  import type { Directive } from 'vue';
2
2
 
3
- import { definePlugin } from '@/plugins';
3
+ import { definePlugin } from '@aerogel/core/plugins';
4
4
 
5
- import initialFocus from './initial-focus';
6
5
  import measure from './measure';
7
6
 
8
7
  const builtInDirectives: Record<string, Directive> = {
9
- 'initial-focus': initialFocus,
10
- 'measure': measure,
8
+ measure: measure,
11
9
  };
12
10
 
11
+ export * from './measure';
12
+
13
13
  export default definePlugin({
14
14
  install(app, options) {
15
15
  const directives = {
@@ -23,8 +23,14 @@ export default definePlugin({
23
23
  },
24
24
  });
25
25
 
26
- declare module '@/bootstrap/options' {
26
+ declare module '@aerogel/core/bootstrap/options' {
27
27
  export interface AerogelOptions {
28
28
  directives?: Record<string, Directive>;
29
29
  }
30
30
  }
31
+
32
+ declare module 'vue' {
33
+ interface ComponentCustomDirectives {
34
+ measure: Directive<string, string>;
35
+ }
36
+ }
@@ -1,12 +1,40 @@
1
- import { defineDirective } from '@/utils/vue';
1
+ import { defineDirective } from '@aerogel/core/utils/vue';
2
+ import { tap } from '@noeldemartin/utils';
3
+
4
+ const resizeObservers: WeakMap<HTMLElement, ResizeObserver> = new WeakMap();
5
+
6
+ export interface ElementSize {
7
+ width: number;
8
+ height: number;
9
+ }
10
+
11
+ export type MeasureDirectiveListener = (size: ElementSize) => unknown;
2
12
 
3
13
  export default defineDirective({
4
- mounted(element: HTMLElement, { value }: { value?: () => unknown }) {
5
- const sizes = element.getBoundingClientRect();
14
+ mounted(element: HTMLElement, { value }) {
15
+ // TODO replace with argument when typed properly
16
+ const modifiers = { css: true, watch: true };
17
+
18
+ const listener = typeof value === 'function' ? (value as MeasureDirectiveListener) : null;
19
+ const update = () => {
20
+ const sizes = element.getBoundingClientRect();
6
21
 
7
- element.style.setProperty('--width', `${sizes.width}px`);
8
- element.style.setProperty('--height', `${sizes.height}px`);
22
+ if (modifiers.css) {
23
+ element.style.setProperty('--width', `${sizes.width}px`);
24
+ element.style.setProperty('--height', `${sizes.height}px`);
25
+ }
9
26
 
10
- value?.();
27
+ listener?.({ width: sizes.width, height: sizes.height });
28
+ };
29
+
30
+ if (modifiers.watch) {
31
+ resizeObservers.set(element, tap(new ResizeObserver(update)).observe(element));
32
+ }
33
+
34
+ update();
35
+ },
36
+ unmounted(element) {
37
+ resizeObservers.get(element)?.unobserve(element);
38
+ resizeObservers.delete(element);
11
39
  },
12
40
  });
@@ -1,6 +1,6 @@
1
1
  import type { JSError } from '@noeldemartin/utils';
2
2
 
3
- import { defineServiceState } from '@/services';
3
+ import { defineServiceState } from '@aerogel/core/services';
4
4
 
5
5
  export type ErrorSource = string | Error | JSError | unknown;
6
6
 
@@ -1,16 +1,15 @@
1
- import { JSError, facade, isObject, objectWithoutEmpty, toString } from '@noeldemartin/utils';
1
+ import { JSError, facade, isDevelopment, isObject, isTesting, objectWithoutEmpty, toString } from '@noeldemartin/utils';
2
2
 
3
- import App from '@/services/App';
4
- import ServiceBootError from '@/errors/ServiceBootError';
5
- import UI, { UIComponents } from '@/ui/UI';
6
- import { translateWithDefault } from '@/lang/utils';
3
+ import App from '@aerogel/core/services/App';
4
+ import ServiceBootError from '@aerogel/core/errors/ServiceBootError';
5
+ import UI, { UIComponents } from '@aerogel/core/ui/UI';
6
+ import { translateWithDefault } from '@aerogel/core/lang/utils';
7
+ import { Events } from '@aerogel/core/services';
8
+ import type { ErrorReportModalProps } from '@aerogel/core/components/contracts/ErrorReportModal';
9
+ import type { ModalComponent } from '@aerogel/core/ui/UI.state';
7
10
 
8
11
  import Service from './Errors.state';
9
- import { Colors } from '@/components/constants';
10
- import { Events } from '@/services';
11
- import type { AGErrorReportModalProps } from '@/components/modals/AGErrorReportModal';
12
12
  import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
13
- import type { ModalComponent } from '@/ui/UI.state';
14
13
 
15
14
  export class ErrorsService extends Service {
16
15
 
@@ -34,7 +33,7 @@ export class ErrorsService extends Service {
34
33
  return;
35
34
  }
36
35
 
37
- UI.openModal<ModalComponent<AGErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
36
+ UI.openModal<ModalComponent<ErrorReportModalProps>>(UI.requireComponent(UIComponents.ErrorReportModal), {
38
37
  reports,
39
38
  });
40
39
  }
@@ -42,11 +41,11 @@ export class ErrorsService extends Service {
42
41
  public async report(error: ErrorSource, message?: string): Promise<void> {
43
42
  await Events.emit('error', { error, message });
44
43
 
45
- if (App.testing) {
44
+ if (isTesting('unit')) {
46
45
  throw error;
47
46
  }
48
47
 
49
- if (App.development) {
48
+ if (isDevelopment()) {
50
49
  this.logError(error);
51
50
  }
52
51
 
@@ -71,17 +70,17 @@ export class ErrorsService extends Service {
71
70
  date: new Date(),
72
71
  };
73
72
 
74
- UI.showSnackbar(
73
+ UI.toast(
75
74
  message ??
76
75
  translateWithDefault('errors.notice', 'Something went wrong, but it\'s not your fault. Try again!'),
77
76
  {
78
- color: Colors.Danger,
77
+ variant: 'danger',
79
78
  actions: [
80
79
  {
81
- text: translateWithDefault('errors.viewDetails', 'View details'),
80
+ label: translateWithDefault('errors.viewDetails', 'View details'),
82
81
  dismiss: true,
83
- handler: () =>
84
- UI.openModal<ModalComponent<AGErrorReportModalProps>>(
82
+ click: () =>
83
+ UI.openModal<ModalComponent<ErrorReportModalProps>>(
85
84
  UI.requireComponent(UIComponents.ErrorReportModal),
86
85
  { reports: [report] },
87
86
  ),
@@ -178,7 +177,7 @@ export class ErrorsService extends Service {
178
177
 
179
178
  export default facade(ErrorsService);
180
179
 
181
- declare module '@/services/Events' {
180
+ declare module '@aerogel/core/services/Events' {
182
181
  export interface EventsPayload {
183
182
  error: { error: ErrorSource; message?: string };
184
183
  }
@@ -0,0 +1,3 @@
1
+ import { JSError } from '@noeldemartin/utils';
2
+
3
+ export default class JobCancelledError extends JSError {}
@@ -1,13 +1,16 @@
1
1
  import type { App } from 'vue';
2
2
 
3
- import { bootServices } from '@/services';
4
- import { definePlugin } from '@/plugins';
3
+ import { bootServices } from '@aerogel/core/services';
4
+ import { definePlugin } from '@aerogel/core/plugins';
5
5
 
6
6
  import Errors from './Errors';
7
- import { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
7
+ import type { ErrorReport, ErrorReportLog, ErrorSource } from './Errors.state';
8
8
 
9
9
  export * from './utils';
10
- export { Errors, ErrorSource, ErrorReport, ErrorReportLog };
10
+ export { Errors };
11
+ export { default as JobCancelledError } from './JobCancelledError';
12
+ export { default as ServiceBootError } from './ServiceBootError';
13
+ export type { ErrorSource, ErrorReport, ErrorReportLog };
11
14
 
12
15
  const services = { $errors: Errors };
13
16
  const frameworkHandler: ErrorHandler = (error) => {
@@ -35,12 +38,12 @@ export default definePlugin({
35
38
  },
36
39
  });
37
40
 
38
- declare module '@/bootstrap/options' {
41
+ declare module '@aerogel/core/bootstrap/options' {
39
42
  export interface AerogelOptions {
40
43
  handleError?(error: ErrorSource): boolean;
41
44
  }
42
45
  }
43
46
 
44
- declare module '@/services' {
47
+ declare module '@aerogel/core/services' {
45
48
  export interface Services extends ErrorsServices {}
46
49
  }
@@ -1,8 +1,24 @@
1
1
  import { JSError, isObject, toString } from '@noeldemartin/utils';
2
- import { translateWithDefault } from '@/lang/utils';
2
+ import { translateWithDefault } from '@aerogel/core/lang/utils';
3
3
  import type { ErrorSource } from './Errors.state';
4
4
 
5
+ const handlers: ErrorHandler[] = [];
6
+
7
+ export type ErrorHandler = (error: ErrorSource) => string | undefined;
8
+
9
+ export function registerErrorHandler(handler: ErrorHandler): void {
10
+ handlers.push(handler);
11
+ }
12
+
5
13
  export function getErrorMessage(error: ErrorSource): string {
14
+ for (const handler of handlers) {
15
+ const result = handler(error);
16
+
17
+ if (result) {
18
+ return result;
19
+ }
20
+ }
21
+
6
22
  if (typeof error === 'string') {
7
23
  return error;
8
24
  }
@@ -1,10 +1,11 @@
1
1
  import { describe, expect, expectTypeOf, it } from 'vitest';
2
2
 
3
- import { useForm } from './composition';
4
- import { FormFieldTypes } from '@/main';
5
- import { numberInput, requiredStringInput } from '@/forms/utils';
3
+ import { useForm } from '@aerogel/core/forms/composition';
4
+ import { numberInput, requiredStringInput } from '@aerogel/core/forms/utils';
6
5
 
7
- describe('Form', () => {
6
+ import { FormFieldTypes } from './FormController';
7
+
8
+ describe('FormController', () => {
8
9
 
9
10
  it('defines magic fields', () => {
10
11
  const form = useForm({
@@ -55,4 +56,32 @@ describe('Form', () => {
55
56
  expect(form.name).toBeNull();
56
57
  });
57
58
 
59
+ it('trims values', () => {
60
+ // Arrange
61
+ const form = useForm({
62
+ trimmed: {
63
+ type: FormFieldTypes.String,
64
+ rules: 'required',
65
+ },
66
+ untrimmed: {
67
+ type: FormFieldTypes.String,
68
+ rules: 'required',
69
+ trim: false,
70
+ },
71
+ });
72
+
73
+ // Act
74
+ form.trimmed = ' ';
75
+ form.untrimmed = ' ';
76
+
77
+ form.submit();
78
+
79
+ // Assert
80
+ expect(form.valid).toBe(false);
81
+ expect(form.submitted).toBe(true);
82
+ expect(form.trimmed).toEqual('');
83
+ expect(form.untrimmed).toEqual(' ');
84
+ expect(form.errors).toEqual({ trimmed: ['required'], untrimmed: null });
85
+ });
86
+
58
87
  });
@@ -1,5 +1,6 @@
1
- import { MagicObject } from '@noeldemartin/utils';
2
- import { computed, reactive, readonly, ref } from 'vue';
1
+ import { computed, nextTick, reactive, readonly, ref } from 'vue';
2
+ import { MagicObject, arrayRemove, fail, toString } from '@noeldemartin/utils';
3
+ import { validate } from './validation';
3
4
  import type { ObjectValues } from '@noeldemartin/utils';
4
5
  import type { ComputedRef, DeepReadonly, Ref, UnwrapNestedRefs } from 'vue';
5
6
 
@@ -8,10 +9,12 @@ export const FormFieldTypes = {
8
9
  Number: 'number',
9
10
  Boolean: 'boolean',
10
11
  Object: 'object',
12
+ Date: 'date',
11
13
  } as const;
12
14
 
13
15
  export interface FormFieldDefinition<TType extends FormFieldType = FormFieldType, TRules extends string = string> {
14
16
  type: TType;
17
+ trim?: boolean;
15
18
  default?: GetFormFieldValue<TType>;
16
19
  rules?: TRules;
17
20
  }
@@ -35,16 +38,21 @@ export type FormErrors<T> = {
35
38
  export type GetFormFieldValue<TType> = TType extends typeof FormFieldTypes.String
36
39
  ? string
37
40
  : TType extends typeof FormFieldTypes.Number
38
- ? number
39
- : TType extends typeof FormFieldTypes.Boolean
40
- ? boolean
41
- : TType extends typeof FormFieldTypes.Object
42
- ? object
43
- : never;
41
+ ? number
42
+ : TType extends typeof FormFieldTypes.Boolean
43
+ ? boolean
44
+ : TType extends typeof FormFieldTypes.Object
45
+ ? object
46
+ : TType extends typeof FormFieldTypes.Date
47
+ ? Date
48
+ : never;
44
49
 
45
- const validForms: WeakMap<Form, ComputedRef<boolean>> = new WeakMap();
50
+ const validForms: WeakMap<FormController, ComputedRef<boolean>> = new WeakMap();
46
51
 
47
- export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
52
+ export type SubmitFormListener = () => unknown;
53
+ export type FocusFormListener = (input: string) => unknown;
54
+
55
+ export default class FormController<Fields extends FormFieldDefinitions = FormFieldDefinitions> extends MagicObject {
48
56
 
49
57
  public errors: DeepReadonly<UnwrapNestedRefs<FormErrors<Fields>>>;
50
58
 
@@ -52,6 +60,7 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
52
60
  private _data: FormData<Fields>;
53
61
  private _submitted: Ref<boolean>;
54
62
  private _errors: FormErrors<Fields>;
63
+ private _listeners: { focus?: FocusFormListener[]; submit?: SubmitFormListener[] } = {};
55
64
 
56
65
  constructor(fields: Fields) {
57
66
  super();
@@ -78,7 +87,13 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
78
87
  }
79
88
 
80
89
  public setFieldValue<T extends keyof Fields>(field: T, value: FormData<Fields>[T]): void {
81
- this._data[field] = value;
90
+ const definition =
91
+ this._fields[field] ?? fail<FormFieldDefinition>(`Trying to set undefined '${toString(field)}' field`);
92
+
93
+ this._data[field] =
94
+ definition.type === FormFieldTypes.String && (definition.trim ?? true)
95
+ ? (toString(value).trim() as FormData<Fields>[T])
96
+ : value;
82
97
 
83
98
  if (this._submitted.value) {
84
99
  this.validate();
@@ -89,12 +104,23 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
89
104
  return this._data[field] as unknown as GetFormFieldValue<Fields[T]['type']>;
90
105
  }
91
106
 
107
+ public getFieldRules<T extends keyof Fields>(field: T): string[] {
108
+ return this._fields[field]?.rules?.split('|') ?? [];
109
+ }
110
+
111
+ public data(): FormData<Fields> {
112
+ return { ...this._data };
113
+ }
114
+
92
115
  public validate(): boolean {
93
- const errors = Object.entries(this._fields).reduce((formErrors, [name, definition]) => {
94
- formErrors[name] = this.getFieldErrors(name, definition);
116
+ const errors = Object.entries(this._fields).reduce(
117
+ (formErrors, [name, definition]) => {
118
+ formErrors[name] = this.getFieldErrors(name, definition);
95
119
 
96
- return formErrors;
97
- }, {} as Record<string, string[] | null>);
120
+ return formErrors;
121
+ },
122
+ {} as Record<string, string[] | null>,
123
+ );
98
124
 
99
125
  this.resetErrors(errors);
100
126
 
@@ -111,32 +137,66 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
111
137
  public submit(): boolean {
112
138
  this._submitted.value = true;
113
139
 
114
- return this.validate();
140
+ const valid = this.validate();
141
+
142
+ valid && this._listeners['submit']?.forEach((listener) => listener());
143
+
144
+ return valid;
115
145
  }
116
146
 
117
- protected __get(property: string): unknown {
147
+ public on(event: 'focus', listener: FocusFormListener): () => void;
148
+ public on(event: 'submit', listener: SubmitFormListener): () => void;
149
+ public on(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): () => void {
150
+ this._listeners[event] ??= [];
151
+
152
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
153
+ this._listeners[event]?.push(listener as any);
154
+
155
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
156
+ return () => this.off(event as any, listener);
157
+ }
158
+
159
+ public off(event: 'focus', listener: FocusFormListener): void;
160
+ public off(event: 'submit', listener: SubmitFormListener): void;
161
+ public off(event: 'focus' | 'submit', listener: FocusFormListener | SubmitFormListener): void {
162
+ arrayRemove(this._listeners[event] ?? [], listener);
163
+ }
164
+
165
+ public async focus(input: string): Promise<void> {
166
+ await nextTick();
167
+
168
+ this._listeners['focus']?.forEach((listener) => listener(input));
169
+ }
170
+
171
+ protected override __get(property: string): unknown {
118
172
  if (!(property in this._fields)) {
119
173
  return super.__get(property);
120
174
  }
121
175
 
122
- return this._data[property];
176
+ return this.getFieldValue(property);
123
177
  }
124
178
 
125
- protected __set(property: string, value: unknown): void {
179
+ protected override __set(property: string, value: unknown): void {
126
180
  if (!(property in this._fields)) {
127
181
  super.__set(property, value);
128
182
 
129
183
  return;
130
184
  }
131
185
 
132
- Object.assign(this._data, { [property]: value });
186
+ this.setFieldValue(property, value as FormData<Fields>[string]);
133
187
  }
134
188
 
135
189
  private getFieldErrors(name: keyof Fields, definition: FormFieldDefinition): string[] | null {
136
190
  const errors = [];
191
+ const value = this._data[name];
192
+ const rules = definition.rules?.split('|') ?? [];
193
+
194
+ for (const rule of rules) {
195
+ if (rule !== 'required' && (value === null || value === undefined)) {
196
+ continue;
197
+ }
137
198
 
138
- if (definition.rules?.includes('required') && !this._data[name]) {
139
- errors.push('required');
199
+ errors.push(...validate(value, rule));
140
200
  }
141
201
 
142
202
  return errors.length > 0 ? errors : null;
@@ -147,10 +207,10 @@ export default class Form<Fields extends FormFieldDefinitions = FormFieldDefinit
147
207
  return {} as FormData<Fields>;
148
208
  }
149
209
 
150
- const data = Object.entries(fields).reduce((formData, [name, definition]) => {
151
- formData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
210
+ const data = Object.entries(fields).reduce((initialData, [name, definition]) => {
211
+ initialData[name as keyof Fields] = (definition.default ?? null) as FormData<Fields>[keyof Fields];
152
212
 
153
- return formData;
213
+ return initialData;
154
214
  }, {} as FormData<Fields>);
155
215
 
156
216
  return reactive(data) as FormData<Fields>;
@@ -1,6 +1,6 @@
1
- import Form from '@/forms/Form';
2
- import type { FormData, FormFieldDefinitions } from '@/forms/Form';
1
+ import FormController from '@aerogel/core/forms/FormController';
2
+ import type { FormData, FormFieldDefinitions } from '@aerogel/core/forms/FormController';
3
3
 
4
- export function useForm<const T extends FormFieldDefinitions>(fields: T): Form<T> & FormData<T> {
5
- return new Form(fields) as Form<T> & FormData<T>;
4
+ export function useForm<const T extends FormFieldDefinitions>(fields: T): FormController<T> & FormData<T> {
5
+ return new FormController(fields) as FormController<T> & FormData<T>;
6
6
  }
@@ -1,3 +1,5 @@
1
- export * from './Form';
2
1
  export * from './composition';
2
+ export * from './FormController';
3
3
  export * from './utils';
4
+ export * from './validation';
5
+ export { default as FormController } from './FormController';