| 1 |
import { computed, type ComputedRef } from 'vue' |
| 2 |
import type { Component } from 'vue' |
| 3 |
import type { IvyFormAPIHooks } from '@/composables/IvyFormAPI' |
| 4 |
|
| 5 |
export type MultiPageComponentKey = |
| 6 |
| 'progressIndicator' |
| 7 |
| 'pageNavigation' |
| 8 |
| 'progressSettings' |
| 9 |
| 'navigationSettings' |
| 10 |
|
| 11 |
const MULTI_PAGE_COMPONENT_HOOKS: Record<MultiPageComponentKey, string> = { |
| 12 |
progressIndicator: 'ivyforms/multi_page/filter/progress_indicator_component', |
| 13 |
pageNavigation: 'ivyforms/multi_page/filter/page_navigation_component', |
| 14 |
progressSettings: 'ivyforms/multi_page/filter/progress_settings_component', |
| 15 |
navigationSettings: 'ivyforms/multi_page/filter/navigation_settings_component', |
| 16 |
} |
| 17 |
|
| 18 |
const MULTI_PAGE_COMPONENT_REGISTRY_KEYS: Record<MultiPageComponentKey, string> = { |
| 19 |
progressIndicator: 'IvyFormProgressIndicator', |
| 20 |
pageNavigation: 'IvyFormPageNavigation', |
| 21 |
progressSettings: 'ProgressSettings', |
| 22 |
navigationSettings: 'NavigationSettings', |
| 23 |
} |
| 24 |
|
| 25 |
const isVueComponent = (value: unknown): value is Component => |
| 26 |
!!value && |
| 27 |
(typeof value === 'function' || typeof value === 'object') && |
| 28 |
('render' in (value as Record<string, unknown>) || |
| 29 |
'setup' in (value as Record<string, unknown>) || |
| 30 |
'__hmrId' in (value as Record<string, unknown>) || |
| 31 |
'__name' in (value as Record<string, unknown>)) |
| 32 |
|
| 33 |
const getHooks = (): IvyFormAPIHooks | undefined => { |
| 34 |
const liteHooks = window.IvyForms?.hooks |
| 35 |
const apiHooks = window.IvyForms?.api?.hooks |
| 36 |
|
| 37 |
if (liteHooks?.applyFilters) { |
| 38 |
return liteHooks |
| 39 |
} |
| 40 |
|
| 41 |
if (apiHooks?.applyFilters) { |
| 42 |
return apiHooks |
| 43 |
} |
| 44 |
|
| 45 |
return undefined |
| 46 |
} |
| 47 |
|
| 48 |
const resolveFromRegistry = (key: MultiPageComponentKey): Component | null => { |
| 49 |
const registry = window.IvyForms?.components |
| 50 |
const name = MULTI_PAGE_COMPONENT_REGISTRY_KEYS[key] |
| 51 |
|
| 52 |
if (!registry || !name) { |
| 53 |
return null |
| 54 |
} |
| 55 |
|
| 56 |
if (typeof registry === 'function') { |
| 57 |
try { |
| 58 |
const res = registry(name) |
| 59 |
if (isVueComponent(res)) return res |
| 60 |
if (res && typeof res === 'object') { |
| 61 |
const c = (res as Record<string, unknown>)[name] |
| 62 |
if (isVueComponent(c)) return c |
| 63 |
} |
| 64 |
} catch { |
| 65 |
// noop |
| 66 |
} |
| 67 |
|
| 68 |
try { |
| 69 |
const map = registry() |
| 70 |
if (map && typeof map === 'object') { |
| 71 |
const c = (map as Record<string, unknown>)[name] |
| 72 |
if (isVueComponent(c)) return c |
| 73 |
} |
| 74 |
} catch { |
| 75 |
// noop |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (typeof registry === 'object') { |
| 80 |
const c = (registry as Record<string, unknown>)[name] |
| 81 |
if (isVueComponent(c)) return c |
| 82 |
} |
| 83 |
|
| 84 |
return null |
| 85 |
} |
| 86 |
|
| 87 |
export const resolveMultiPageComponent = (key: MultiPageComponentKey): Component | null => { |
| 88 |
const hooks = getHooks() |
| 89 |
if (hooks?.applyFilters) { |
| 90 |
const resolved = hooks.applyFilters<Component | null>(MULTI_PAGE_COMPONENT_HOOKS[key], null) |
| 91 |
if (isVueComponent(resolved)) { |
| 92 |
return resolved |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return resolveFromRegistry(key) |
| 97 |
} |
| 98 |
|
| 99 |
const useResolvedComponent = (key: MultiPageComponentKey): ComputedRef<Component | null> => { |
| 100 |
return computed(() => { |
| 101 |
const hooks = getHooks() |
| 102 |
|
| 103 |
// Establish reactive dependency so components update when Pro registers filters later. |
| 104 |
void hooks?.updateCounter?.value |
| 105 |
|
| 106 |
return resolveMultiPageComponent(key) |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
export function useMultiPageComponents() { |
| 111 |
return { |
| 112 |
progressIndicator: useResolvedComponent('progressIndicator'), |
| 113 |
pageNavigation: useResolvedComponent('pageNavigation'), |
| 114 |
progressSettings: useResolvedComponent('progressSettings'), |
| 115 |
navigationSettings: useResolvedComponent('navigationSettings'), |
| 116 |
} |
| 117 |
} |
| 118 |
|