@selfhelp/shared 1.21.2 → 1.21.5
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/CHANGELOG.md +92 -0
- package/README.md +2 -1
- package/dist/index.d.mts +427 -20
- package/dist/index.d.ts +427 -20
- package/dist/index.js +560 -13
- package/dist/index.mjs +518 -13
- package/dist/plugin-sdk/index.d.mts +1 -1
- package/dist/plugin-sdk/index.d.ts +1 -1
- package/dist/plugin-sdk/index.js +2 -1
- package/dist/plugin-sdk/index.mjs +2 -1
- package/dist/registry/index.d.mts +2 -2
- package/dist/registry/index.d.ts +2 -2
- package/dist/registry/index.js +2 -1
- package/dist/registry/index.mjs +2 -1
- package/dist/{styles.registry-DEDEZ8l3.d.ts → styles.registry-DX79oirw.d.ts} +93 -16
- package/dist/{styles.registry-DHM4M77N.d.mts → styles.registry-ffw0MH3r.d.mts} +93 -16
- package/package.json +1 -1
- package/src/content/__tests__/fieldsMapLabels.test.ts +55 -0
- package/src/content/__tests__/optionLabels.test.ts +100 -0
- package/src/content/fieldsMapLabels.ts +103 -0
- package/src/content/index.ts +2 -0
- package/src/content/optionLabels.ts +200 -0
- package/src/navigation/__tests__/navigation.test.ts +296 -6
- package/src/navigation/activeTrail.ts +99 -0
- package/src/navigation/branchNav.ts +155 -6
- package/src/navigation/footerPreset.ts +109 -0
- package/src/navigation/headerLayers.ts +56 -0
- package/src/navigation/headerPreset.ts +9 -2
- package/src/navigation/index.ts +4 -0
- package/src/navigation/menuDepth.ts +73 -37
- package/src/navigation/mobileRoute.ts +25 -0
- package/src/navigation/navLinks.ts +0 -4
- package/src/navigation/navigationBundle.ts +81 -0
- package/src/navigation/navigationPayload.ts +96 -17
- package/src/registry/__tests__/platform.test.ts +11 -10
- package/src/registry/styles.registry.ts +2 -1
- package/src/types/__tests__/preview-bridge.test.ts +7 -0
- package/src/types/auth.ts +4 -0
- package/src/types/cms-app.ts +74 -0
- package/src/types/index.ts +1 -0
- package/src/types/mobile-adapters.ts +1 -0
- package/src/types/pages.ts +6 -0
- package/src/types/preview-bridge.ts +12 -1
- package/src/types/styles/composite.ts +21 -0
- package/src/types/styles/forms.ts +57 -10
- package/src/types/styles/interactive.ts +1 -1
- package/src/types/styles/unknown.ts +2 -2
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Cross-platform helpers for user-owned option catalogs in CMS styles.
|
|
7
|
+
*
|
|
8
|
+
* The selected data row stores only stable option codes; labels are resolved
|
|
9
|
+
* per language from translatable style config fields.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export interface IOptionCatalogEntry {
|
|
13
|
+
value: string;
|
|
14
|
+
sort?: number;
|
|
15
|
+
disabled?: boolean;
|
|
16
|
+
meta?: Record<string, unknown>;
|
|
17
|
+
/**
|
|
18
|
+
* Compatibility-only label carried by legacy `{ value, text|label }`
|
|
19
|
+
* catalogs. New catalogs keep labels in the translatable `option_labels`
|
|
20
|
+
* field instead.
|
|
21
|
+
*/
|
|
22
|
+
legacyLabel?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface IOptionLabelMap {
|
|
26
|
+
[code: string]: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface IResolvedOption {
|
|
30
|
+
value: string;
|
|
31
|
+
label: string;
|
|
32
|
+
sort?: number;
|
|
33
|
+
disabled?: boolean;
|
|
34
|
+
meta?: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface IOptionStyleConfig {
|
|
38
|
+
catalogField: 'options' | 'radio_options' | 'combobox_options' | 'segmented_control_data';
|
|
39
|
+
multipleField?: 'is_multiple' | 'web_combobox_multi_select';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Option-bearing core styles and the fields that define their catalogs.
|
|
44
|
+
* Boolean `checkbox` is intentionally absent because it is not an enum group.
|
|
45
|
+
*/
|
|
46
|
+
export const OPTION_STYLE_CONFIGS = {
|
|
47
|
+
select: {
|
|
48
|
+
catalogField: 'options',
|
|
49
|
+
multipleField: 'is_multiple',
|
|
50
|
+
},
|
|
51
|
+
radio: {
|
|
52
|
+
catalogField: 'radio_options',
|
|
53
|
+
},
|
|
54
|
+
combobox: {
|
|
55
|
+
catalogField: 'combobox_options',
|
|
56
|
+
multipleField: 'web_combobox_multi_select',
|
|
57
|
+
},
|
|
58
|
+
'segmented-control': {
|
|
59
|
+
catalogField: 'segmented_control_data',
|
|
60
|
+
},
|
|
61
|
+
} as const satisfies Record<string, IOptionStyleConfig>;
|
|
62
|
+
|
|
63
|
+
export type TOptionStyleName = keyof typeof OPTION_STYLE_CONFIGS;
|
|
64
|
+
|
|
65
|
+
function parseJson(raw: unknown): unknown {
|
|
66
|
+
if (typeof raw !== 'string' || raw.trim() === '') {
|
|
67
|
+
return raw;
|
|
68
|
+
}
|
|
69
|
+
try {
|
|
70
|
+
return JSON.parse(raw);
|
|
71
|
+
} catch {
|
|
72
|
+
return raw;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function toString(value: unknown): string | null {
|
|
77
|
+
if (typeof value !== 'string') return null;
|
|
78
|
+
const trimmed = value.trim();
|
|
79
|
+
return trimmed === '' ? null : trimmed;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Parse option catalog from style field content.
|
|
84
|
+
*
|
|
85
|
+
* Supports both current legacy shape (`[{ value, text|label }]`) and the new
|
|
86
|
+
* language-neutral base shape (`[{ value, sort, disabled, meta }]`).
|
|
87
|
+
*/
|
|
88
|
+
export function parseOptionCatalog(raw: unknown): IOptionCatalogEntry[] {
|
|
89
|
+
const parsed = parseJson(raw);
|
|
90
|
+
if (!Array.isArray(parsed)) return [];
|
|
91
|
+
|
|
92
|
+
const entries: IOptionCatalogEntry[] = [];
|
|
93
|
+
for (const item of parsed) {
|
|
94
|
+
if (item == null || typeof item !== 'object') continue;
|
|
95
|
+
const record = item as Record<string, unknown>;
|
|
96
|
+
const value = toString(record.value);
|
|
97
|
+
if (!value) continue;
|
|
98
|
+
const explicitMeta = record.meta
|
|
99
|
+
&& typeof record.meta === 'object'
|
|
100
|
+
&& !Array.isArray(record.meta)
|
|
101
|
+
? record.meta as Record<string, unknown>
|
|
102
|
+
: {};
|
|
103
|
+
const legacyDescription = toString(record.description);
|
|
104
|
+
const meta = legacyDescription && explicitMeta.description === undefined
|
|
105
|
+
? { ...explicitMeta, description: legacyDescription }
|
|
106
|
+
: explicitMeta;
|
|
107
|
+
entries.push({
|
|
108
|
+
value,
|
|
109
|
+
...(typeof record.sort === 'number' ? { sort: record.sort } : {}),
|
|
110
|
+
...(typeof record.disabled === 'boolean' ? { disabled: record.disabled } : {}),
|
|
111
|
+
...(Object.keys(meta).length > 0 ? { meta } : {}),
|
|
112
|
+
...(toString(record.label) ?? toString(record.text)
|
|
113
|
+
? { legacyLabel: toString(record.label) ?? toString(record.text) ?? undefined }
|
|
114
|
+
: {}),
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return entries;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Parse per-language labels map (`code -> label`) from a translatable style
|
|
122
|
+
* field value.
|
|
123
|
+
*/
|
|
124
|
+
export function parseOptionLabels(raw: unknown): IOptionLabelMap {
|
|
125
|
+
const parsed = parseJson(raw);
|
|
126
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
127
|
+
return {};
|
|
128
|
+
}
|
|
129
|
+
const labels: IOptionLabelMap = {};
|
|
130
|
+
for (const [key, value] of Object.entries(parsed)) {
|
|
131
|
+
if (!key || typeof value !== 'string') continue;
|
|
132
|
+
const trimmed = value.trim();
|
|
133
|
+
if (trimmed !== '') {
|
|
134
|
+
labels[key] = trimmed;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return labels;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function resolveOptionLabel(
|
|
141
|
+
code: string | null | undefined,
|
|
142
|
+
labels: IOptionLabelMap,
|
|
143
|
+
fallbackLabels: IOptionLabelMap = {},
|
|
144
|
+
legacyLabel?: string,
|
|
145
|
+
): string {
|
|
146
|
+
if (!code) return '';
|
|
147
|
+
return labels[code] ?? fallbackLabels[code] ?? legacyLabel ?? code;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function resolveOptions(
|
|
151
|
+
catalogRaw: unknown,
|
|
152
|
+
labelsRaw: unknown,
|
|
153
|
+
fallbackLabelsRaw?: unknown,
|
|
154
|
+
): IResolvedOption[] {
|
|
155
|
+
const catalog = parseOptionCatalog(catalogRaw);
|
|
156
|
+
const labels = parseOptionLabels(labelsRaw);
|
|
157
|
+
const fallbackLabels = parseOptionLabels(fallbackLabelsRaw);
|
|
158
|
+
|
|
159
|
+
return catalog.map((entry) => ({
|
|
160
|
+
value: entry.value,
|
|
161
|
+
label: resolveOptionLabel(entry.value, labels, fallbackLabels, entry.legacyLabel),
|
|
162
|
+
...(entry.sort !== undefined ? { sort: entry.sort } : {}),
|
|
163
|
+
...(entry.disabled !== undefined ? { disabled: entry.disabled } : {}),
|
|
164
|
+
...(entry.meta ? { meta: entry.meta } : {}),
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function resolveOptionCodesLabels(
|
|
169
|
+
codes: string[],
|
|
170
|
+
labels: IOptionLabelMap,
|
|
171
|
+
fallbackLabels: IOptionLabelMap = {},
|
|
172
|
+
): string[] {
|
|
173
|
+
return codes
|
|
174
|
+
.map((code) => resolveOptionLabel(code, labels, fallbackLabels))
|
|
175
|
+
.filter((label) => label !== '');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Normalize a stored single value, comma-separated multi value, or array. */
|
|
179
|
+
export function parseOptionCodes(value: unknown): string[] {
|
|
180
|
+
const rawCodes = Array.isArray(value)
|
|
181
|
+
? value
|
|
182
|
+
: (typeof value === 'string' ? value.split(',') : []);
|
|
183
|
+
|
|
184
|
+
return rawCodes
|
|
185
|
+
.filter((code): code is string => typeof code === 'string')
|
|
186
|
+
.map((code) => code.trim())
|
|
187
|
+
.filter((code) => code !== '');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export function resolveOptionValueLabels(
|
|
191
|
+
value: unknown,
|
|
192
|
+
labelsRaw: unknown,
|
|
193
|
+
fallbackLabelsRaw?: unknown,
|
|
194
|
+
): string[] {
|
|
195
|
+
return resolveOptionCodesLabels(
|
|
196
|
+
parseOptionCodes(value),
|
|
197
|
+
parseOptionLabels(labelsRaw),
|
|
198
|
+
parseOptionLabels(fallbackLabelsRaw),
|
|
199
|
+
);
|
|
200
|
+
}
|
|
@@ -3,15 +3,20 @@ SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
|
3
3
|
SPDX-License-Identifier: MPL-2.0
|
|
4
4
|
*/
|
|
5
5
|
import { describe, it, expect } from 'vitest';
|
|
6
|
-
import
|
|
6
|
+
import {
|
|
7
|
+
resolveBrandingPresentation,
|
|
8
|
+
type INavigationMenuItem,
|
|
9
|
+
type INavigationPayload,
|
|
10
|
+
} from '../navigationPayload';
|
|
7
11
|
import {
|
|
8
12
|
getNavigationItemHref,
|
|
9
13
|
getNavigationItemLabel,
|
|
10
14
|
flattenNavigationMenuItems,
|
|
11
15
|
} from '../navLinks';
|
|
12
|
-
import { pageUrlToMobileRoute } from '../mobileRoute';
|
|
16
|
+
import { buildPublicPathFromRoute, pageUrlToMobileRoute } from '../mobileRoute';
|
|
13
17
|
import {
|
|
14
18
|
DEFAULT_WEB_HEADER_PRESET,
|
|
19
|
+
isDoubleWebHeaderPreset,
|
|
15
20
|
isWebHeaderPreset,
|
|
16
21
|
resolveWebHeaderPreset,
|
|
17
22
|
WEB_HEADER_PRESET_VALUES,
|
|
@@ -28,23 +33,53 @@ import {
|
|
|
28
33
|
isBottomTabMenuItemActive,
|
|
29
34
|
resolveHolderRedirectPath,
|
|
30
35
|
resolveMobileSegmentGroup,
|
|
36
|
+
resolveWebBranchNavContext,
|
|
31
37
|
resolveWebBranchNavGroup,
|
|
32
38
|
} from '../branchNav';
|
|
33
39
|
import { searchMenuPagesInPayload } from '../menuSearch';
|
|
34
40
|
import { clampMenuItemsAtDepth, resolveMenuMaxDepth } from '../menuDepth';
|
|
41
|
+
import { mergeHeaderLayers, splitHeaderLayers } from '../headerLayers';
|
|
42
|
+
import {
|
|
43
|
+
DEFAULT_WEB_FOOTER_PRESET,
|
|
44
|
+
flattenFooterItems,
|
|
45
|
+
footerColumnItems,
|
|
46
|
+
footerGroupLinks,
|
|
47
|
+
footerStandaloneItems,
|
|
48
|
+
isWebFooterPreset,
|
|
49
|
+
resolveWebFooterPreset,
|
|
50
|
+
} from '../footerPreset';
|
|
51
|
+
import {
|
|
52
|
+
expandedIdsForActiveTrail,
|
|
53
|
+
getNavigationItemMobileHref,
|
|
54
|
+
isMenuItemActiveOnMobile,
|
|
55
|
+
isMenuItemActiveOnWeb,
|
|
56
|
+
} from '../activeTrail';
|
|
57
|
+
import {
|
|
58
|
+
NAVIGATION_BUNDLE_VERSION,
|
|
59
|
+
isNavigationBundleVersionSupported,
|
|
60
|
+
} from '../navigationBundle';
|
|
35
61
|
|
|
36
62
|
function menuItem(partial: Partial<INavigationMenuItem>): INavigationMenuItem {
|
|
37
63
|
return {
|
|
38
64
|
id: 1,
|
|
39
65
|
item_type: 'page',
|
|
40
66
|
label: 'Team',
|
|
67
|
+
description: null,
|
|
68
|
+
aria_label: null,
|
|
69
|
+
icon: null,
|
|
70
|
+
mobile_icon: null,
|
|
41
71
|
position: 10,
|
|
72
|
+
layer: null,
|
|
73
|
+
children_nav: null,
|
|
74
|
+
external_url: null,
|
|
42
75
|
page: {
|
|
43
76
|
id: 9,
|
|
44
77
|
keyword: 'team',
|
|
45
78
|
url: '/team',
|
|
46
79
|
title: 'Team',
|
|
47
80
|
},
|
|
81
|
+
is_active: true,
|
|
82
|
+
children: [],
|
|
48
83
|
...partial,
|
|
49
84
|
};
|
|
50
85
|
}
|
|
@@ -65,6 +100,13 @@ describe('navLinks', () => {
|
|
|
65
100
|
});
|
|
66
101
|
|
|
67
102
|
describe('mobileRoute', () => {
|
|
103
|
+
it('builds a concrete public path from a parameterized URL template', () => {
|
|
104
|
+
expect(
|
|
105
|
+
buildPublicPathFromRoute('/team-members/{record_id}', { record_id: '5' }),
|
|
106
|
+
).toBe('/team-members/5');
|
|
107
|
+
expect(buildPublicPathFromRoute('/team-members/{record_id}', {})).toBeNull();
|
|
108
|
+
});
|
|
109
|
+
|
|
68
110
|
it('maps canonical urls to expo keyword routes', () => {
|
|
69
111
|
expect(pageUrlToMobileRoute('/', 'home')).toBe('/index');
|
|
70
112
|
expect(pageUrlToMobileRoute('/about', 'about')).toBe('/about');
|
|
@@ -102,10 +144,10 @@ describe('mobileIcons', () => {
|
|
|
102
144
|
function navigationPayload(items: INavigationMenuItem[]): INavigationPayload {
|
|
103
145
|
return {
|
|
104
146
|
menus: {
|
|
105
|
-
web_header: { key: 'web_header', platform: 'web', surface: 'header', items },
|
|
106
|
-
web_footer: { key: 'web_footer', platform: 'web', surface: 'footer', items: [] },
|
|
107
|
-
mobile_drawer: { key: 'mobile_drawer', platform: 'mobile', surface: 'drawer', items },
|
|
108
|
-
mobile_bottom_tabs: { key: 'mobile_bottom_tabs', platform: 'mobile', surface: 'bottom_tabs', items: [] },
|
|
147
|
+
web_header: { key: 'web_header', platform: 'web', surface: 'header', preset: 'dropdown', max_depth: 2, item_limit: null, children_nav: 'sidebar', show_breadcrumbs: true, items },
|
|
148
|
+
web_footer: { key: 'web_footer', platform: 'web', surface: 'footer', preset: 'columns', max_depth: 2, item_limit: null, children_nav: 'sidebar', show_breadcrumbs: true, items: [] },
|
|
149
|
+
mobile_drawer: { key: 'mobile_drawer', platform: 'mobile', surface: 'drawer', preset: null, max_depth: 2, item_limit: null, children_nav: null, show_breadcrumbs: false, items },
|
|
150
|
+
mobile_bottom_tabs: { key: 'mobile_bottom_tabs', platform: 'mobile', surface: 'bottom_tabs', preset: null, max_depth: 2, item_limit: 5, children_nav: null, show_breadcrumbs: false, items: [] },
|
|
109
151
|
},
|
|
110
152
|
startup: {
|
|
111
153
|
web_guest_start_page: { keyword: 'home', url: '/', title: 'Home' },
|
|
@@ -160,6 +202,134 @@ describe('menu membership + branch nav', () => {
|
|
|
160
202
|
});
|
|
161
203
|
});
|
|
162
204
|
|
|
205
|
+
describe('web branch nav context (sidebar + breadcrumbs + pager)', () => {
|
|
206
|
+
const branchParent = menuItem({
|
|
207
|
+
id: 10,
|
|
208
|
+
label: 'Modules',
|
|
209
|
+
page: { id: 100, keyword: 'parent', url: '/parent', title: 'Modules' },
|
|
210
|
+
children: [
|
|
211
|
+
menuItem({ id: 11, label: 'Module 1', page: { id: 101, keyword: 'child-a', url: '/parent/child-a', title: 'Module 1' } }),
|
|
212
|
+
menuItem({ id: 12, label: 'Module 2', page: { id: 102, keyword: 'child-b', url: '/parent/child-b', title: 'Module 2' } }),
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
const topLevelLeaf = menuItem({
|
|
216
|
+
id: 20,
|
|
217
|
+
page: { id: 200, keyword: 'contact', url: '/contact', title: 'Contact' },
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('builds sibling group, breadcrumbs and pager for a nested page', () => {
|
|
221
|
+
const payload = navigationPayload([branchParent, topLevelLeaf]);
|
|
222
|
+
const context = resolveWebBranchNavContext(payload, 101);
|
|
223
|
+
expect(context).not.toBeNull();
|
|
224
|
+
expect(context?.mode).toBe('sidebar');
|
|
225
|
+
expect(context?.showBreadcrumbs).toBe(true);
|
|
226
|
+
expect(context?.heading).toBe('Modules');
|
|
227
|
+
expect(context?.parent?.pageId).toBe(100);
|
|
228
|
+
expect(context?.segments.map((s) => s.keyword)).toEqual(['child-a', 'child-b']);
|
|
229
|
+
expect(context?.breadcrumbs.map((b) => b.label)).toEqual(['Modules', 'Module 1']);
|
|
230
|
+
expect(context?.pager.prev).toBeNull();
|
|
231
|
+
expect(context?.pager.next?.keyword).toBe('child-b');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('walks the pager from the middle of the group', () => {
|
|
235
|
+
const payload = navigationPayload([branchParent]);
|
|
236
|
+
const context = resolveWebBranchNavContext(payload, 102);
|
|
237
|
+
expect(context?.pager.prev?.keyword).toBe('child-a');
|
|
238
|
+
expect(context?.pager.next).toBeNull();
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('treats a page with children as the branch parent (overview, no pager)', () => {
|
|
242
|
+
const payload = navigationPayload([branchParent]);
|
|
243
|
+
const context = resolveWebBranchNavContext(payload, 100);
|
|
244
|
+
expect(context?.heading).toBe('Modules');
|
|
245
|
+
expect(context?.segments.map((s) => s.keyword)).toEqual(['child-a', 'child-b']);
|
|
246
|
+
expect(context?.breadcrumbs.map((b) => b.label)).toEqual(['Modules']);
|
|
247
|
+
expect(context?.pager.prev).toBeNull();
|
|
248
|
+
expect(context?.pager.next).toBeNull();
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('returns null for a top-level leaf (header already covers it)', () => {
|
|
252
|
+
const payload = navigationPayload([branchParent, topLevelLeaf]);
|
|
253
|
+
expect(resolveWebBranchNavContext(payload, 200)).toBeNull();
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('honours the per-parent children_nav override above the menu default', () => {
|
|
257
|
+
const overridden = { ...branchParent, children_nav: 'none' as const };
|
|
258
|
+
const payload = navigationPayload([overridden]);
|
|
259
|
+
const context = resolveWebBranchNavContext(payload, 101);
|
|
260
|
+
expect(context?.mode).toBe('none');
|
|
261
|
+
|
|
262
|
+
// Menu default applies when the parent has no override.
|
|
263
|
+
const fallbackPayload = navigationPayload([branchParent]);
|
|
264
|
+
fallbackPayload.menus.web_header.children_nav = 'pills';
|
|
265
|
+
const fallback = resolveWebBranchNavContext(fallbackPayload, 101);
|
|
266
|
+
expect(fallback?.mode).toBe('pills');
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('resolves branding presentation with defaults and text fallback', () => {
|
|
270
|
+
// Older backends: no size/variant → md + logo-and-name, but no logo image degrades to name-only.
|
|
271
|
+
const legacy = resolveBrandingPresentation({ logo_url: null, logo_alt: 'Site', link_url: null });
|
|
272
|
+
expect(legacy).toMatchObject({ size: 'md', variant: 'name-only', showLogo: false, showName: true });
|
|
273
|
+
|
|
274
|
+
// Full block: xl logo-only shows just the image at 56px.
|
|
275
|
+
const logoOnly = resolveBrandingPresentation({
|
|
276
|
+
logo_url: '/assets/logo.svg',
|
|
277
|
+
logo_alt: 'Site',
|
|
278
|
+
link_url: '/home',
|
|
279
|
+
logo_size: 'xl',
|
|
280
|
+
logo_variant: 'logo-only',
|
|
281
|
+
});
|
|
282
|
+
expect(logoOnly).toMatchObject({ size: 'xl', variant: 'logo-only', logoHeight: 56, showLogo: true, showName: false });
|
|
283
|
+
|
|
284
|
+
// Missing branding entirely → safe text fallback.
|
|
285
|
+
expect(resolveBrandingPresentation(undefined).variant).toBe('name-only');
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it('resolves the pager toggle: parent override, else menu default, else on', () => {
|
|
289
|
+
// No menu value + no override → default on.
|
|
290
|
+
const payload = navigationPayload([branchParent]);
|
|
291
|
+
expect(resolveWebBranchNavContext(payload, 101)?.showPager).toBe(true);
|
|
292
|
+
|
|
293
|
+
// Menu-level off applies to branches without an override.
|
|
294
|
+
const menuOffPayload = navigationPayload([branchParent]);
|
|
295
|
+
menuOffPayload.menus.web_header.show_pager = false;
|
|
296
|
+
expect(resolveWebBranchNavContext(menuOffPayload, 101)?.showPager).toBe(false);
|
|
297
|
+
|
|
298
|
+
// Parent override wins over the menu default.
|
|
299
|
+
const overridden = { ...branchParent, show_pager: true };
|
|
300
|
+
const overridePayload = navigationPayload([overridden]);
|
|
301
|
+
overridePayload.menus.web_header.show_pager = false;
|
|
302
|
+
expect(resolveWebBranchNavContext(overridePayload, 101)?.showPager).toBe(true);
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('surfaces grandchild breadcrumbs through the full path', () => {
|
|
306
|
+
const deepTree = menuItem({
|
|
307
|
+
id: 30,
|
|
308
|
+
label: 'Modules',
|
|
309
|
+
page: { id: 300, keyword: 'modules', url: '/modules', title: 'Modules' },
|
|
310
|
+
children: [
|
|
311
|
+
menuItem({
|
|
312
|
+
id: 31,
|
|
313
|
+
label: 'Module 1',
|
|
314
|
+
page: { id: 301, keyword: 'module-1', url: '/modules/module-1', title: 'Module 1' },
|
|
315
|
+
children: [
|
|
316
|
+
menuItem({
|
|
317
|
+
id: 32,
|
|
318
|
+
label: 'Lesson A',
|
|
319
|
+
page: { id: 302, keyword: 'lesson-a', url: '/modules/module-1/lesson-a', title: 'Lesson A' },
|
|
320
|
+
}),
|
|
321
|
+
],
|
|
322
|
+
}),
|
|
323
|
+
],
|
|
324
|
+
});
|
|
325
|
+
const payload = navigationPayload([deepTree]);
|
|
326
|
+
const context = resolveWebBranchNavContext(payload, 302);
|
|
327
|
+
expect(context?.breadcrumbs.map((b) => b.label)).toEqual(['Modules', 'Module 1', 'Lesson A']);
|
|
328
|
+
expect(context?.heading).toBe('Module 1');
|
|
329
|
+
expect(context?.segments.map((s) => s.keyword)).toEqual(['lesson-a']);
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
163
333
|
describe('mobile segment group', () => {
|
|
164
334
|
it('prepends self segment when tab page has content and children', () => {
|
|
165
335
|
const payload = navigationPayload([
|
|
@@ -277,3 +447,123 @@ describe('bottom tab active state', () => {
|
|
|
277
447
|
expect(isBottomTabMenuItemActive(tabs[0], '/other')).toBe(false);
|
|
278
448
|
});
|
|
279
449
|
});
|
|
450
|
+
|
|
451
|
+
describe('header layers', () => {
|
|
452
|
+
const topContact = menuItem({
|
|
453
|
+
id: 1,
|
|
454
|
+
layer: 'top',
|
|
455
|
+
position: 20,
|
|
456
|
+
page: { id: 101, keyword: 'contact', url: '/contact', title: 'Contact' },
|
|
457
|
+
});
|
|
458
|
+
const topAbout = menuItem({
|
|
459
|
+
id: 2,
|
|
460
|
+
layer: 'top',
|
|
461
|
+
position: 10,
|
|
462
|
+
page: { id: 102, keyword: 'about', url: '/about', title: 'About' },
|
|
463
|
+
});
|
|
464
|
+
const mainHome = menuItem({
|
|
465
|
+
id: 3,
|
|
466
|
+
position: 10,
|
|
467
|
+
page: { id: 103, keyword: 'home', url: '/', title: 'Home' },
|
|
468
|
+
});
|
|
469
|
+
const mainServices = menuItem({
|
|
470
|
+
id: 4,
|
|
471
|
+
position: 20,
|
|
472
|
+
page: { id: 104, keyword: 'services', url: '/services', title: 'Services' },
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
it('splits root items into position-ordered top and main rows', () => {
|
|
476
|
+
const { top, main } = splitHeaderLayers([topContact, mainServices, topAbout, mainHome]);
|
|
477
|
+
expect(top.map((i) => i.id)).toEqual([2, 1]);
|
|
478
|
+
expect(main.map((i) => i.id)).toEqual([3, 4]);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it('merges for single presets: main row first, then top row appended', () => {
|
|
482
|
+
const merged = mergeHeaderLayers([topContact, mainServices, topAbout, mainHome]);
|
|
483
|
+
expect(merged.map((i) => i.id)).toEqual([3, 4, 2, 1]);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it('detects double header presets', () => {
|
|
487
|
+
expect(isDoubleWebHeaderPreset('double-dropdown')).toBe(true);
|
|
488
|
+
expect(isDoubleWebHeaderPreset('double-mega-menu')).toBe(true);
|
|
489
|
+
expect(isDoubleWebHeaderPreset('dropdown')).toBe(false);
|
|
490
|
+
expect(isDoubleWebHeaderPreset(null)).toBe(false);
|
|
491
|
+
});
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
describe('footer presets', () => {
|
|
495
|
+
const group = menuItem({
|
|
496
|
+
id: 1,
|
|
497
|
+
item_type: 'group',
|
|
498
|
+
label: 'Company',
|
|
499
|
+
page: null,
|
|
500
|
+
children: [
|
|
501
|
+
menuItem({ id: 2, page: { id: 201, keyword: 'about', url: '/about', title: 'About' } }),
|
|
502
|
+
menuItem({ id: 3, is_active: false, page: { id: 202, keyword: 'hidden', url: '/hidden', title: 'Hidden' } }),
|
|
503
|
+
menuItem({ id: 4, item_type: 'external_url', page: null, external_url: 'https://example.org' }),
|
|
504
|
+
],
|
|
505
|
+
});
|
|
506
|
+
const emptyGroup = menuItem({ id: 5, item_type: 'group', label: 'Empty', page: null, children: [] });
|
|
507
|
+
const standalone = menuItem({ id: 6, page: { id: 203, keyword: 'imprint', url: '/imprint', title: 'Imprint' } });
|
|
508
|
+
|
|
509
|
+
it('validates and resolves footer presets', () => {
|
|
510
|
+
expect(isWebFooterPreset('columns')).toBe(true);
|
|
511
|
+
expect(isWebFooterPreset('inline')).toBe(true);
|
|
512
|
+
expect(isWebFooterPreset('grid')).toBe(false);
|
|
513
|
+
expect(resolveWebFooterPreset(null)).toBe(DEFAULT_WEB_FOOTER_PRESET);
|
|
514
|
+
expect(resolveWebFooterPreset('inline')).toBe('inline');
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it('selects renderable columns and standalone links for the columns preset', () => {
|
|
518
|
+
const items = [group, emptyGroup, standalone];
|
|
519
|
+
expect(footerColumnItems(items).map((i) => i.id)).toEqual([1]);
|
|
520
|
+
expect(footerStandaloneItems(items).map((i) => i.id)).toEqual([6]);
|
|
521
|
+
expect(footerGroupLinks(group).map((i) => i.id)).toEqual([2, 4]);
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
it('flattens groups for the inline preset without mutating data', () => {
|
|
525
|
+
const items = [group, emptyGroup, standalone];
|
|
526
|
+
const flat = flattenFooterItems(items);
|
|
527
|
+
expect(flat.map((i) => i.id)).toEqual([2, 4, 6]);
|
|
528
|
+
expect(group.children).toHaveLength(3);
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
describe('active trail', () => {
|
|
533
|
+
const drawer = [
|
|
534
|
+
menuItem({
|
|
535
|
+
id: 1,
|
|
536
|
+
page: { id: 301, keyword: 'services', url: '/services', title: 'Services' },
|
|
537
|
+
children: [
|
|
538
|
+
menuItem({ id: 2, page: { id: 302, keyword: 'consulting', url: '/services/consulting', title: 'Consulting' } }),
|
|
539
|
+
],
|
|
540
|
+
}),
|
|
541
|
+
menuItem({ id: 3, page: { id: 303, keyword: 'contact', url: '/contact', title: 'Contact' } }),
|
|
542
|
+
];
|
|
543
|
+
|
|
544
|
+
it('resolves mobile hrefs including root aliases', () => {
|
|
545
|
+
expect(getNavigationItemMobileHref(menuItem({ page: { id: 1, keyword: 'home', url: '/', title: 'Home' } }))).toBe('/index');
|
|
546
|
+
expect(getNavigationItemMobileHref(menuItem({ page: { id: 2, keyword: 'consulting', url: '/services/consulting', title: 'C' } }))).toBe('/consulting');
|
|
547
|
+
expect(getNavigationItemMobileHref(menuItem({ item_type: 'external_url', page: null, external_url: 'https://x.org' }))).toBe('https://x.org');
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
it('marks ancestors active when a descendant matches (web + mobile)', () => {
|
|
551
|
+
expect(isMenuItemActiveOnWeb(drawer[0], '/services/consulting')).toBe(true);
|
|
552
|
+
expect(isMenuItemActiveOnWeb(drawer[1], '/services/consulting')).toBe(false);
|
|
553
|
+
expect(isMenuItemActiveOnMobile(drawer[0], '/consulting')).toBe(true);
|
|
554
|
+
expect(isMenuItemActiveOnMobile(drawer[1], '/consulting')).toBe(false);
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
it('collects the expanded id set for the active trail only', () => {
|
|
558
|
+
expect([...expandedIdsForActiveTrail(drawer, '/consulting', 'mobile')]).toEqual([1]);
|
|
559
|
+
expect([...expandedIdsForActiveTrail(drawer, '/contact', 'mobile')]).toEqual([]);
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
describe('navigation bundle contract', () => {
|
|
564
|
+
it('accepts only the current bundle version', () => {
|
|
565
|
+
expect(isNavigationBundleVersionSupported(NAVIGATION_BUNDLE_VERSION)).toBe(true);
|
|
566
|
+
expect(isNavigationBundleVersionSupported('1.0')).toBe(false);
|
|
567
|
+
expect(isNavigationBundleVersionSupported(null)).toBe(false);
|
|
568
|
+
});
|
|
569
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Single active-state/active-trail implementation for every menu renderer:
|
|
7
|
+
* web burger drawer, mobile drawer, and mobile bottom tabs all resolve
|
|
8
|
+
* "is this item (or one of its descendants) the current page?" here.
|
|
9
|
+
*/
|
|
10
|
+
import type { INavigationMenuItem } from './navigationPayload';
|
|
11
|
+
import { getNavigationItemHref } from './navLinks';
|
|
12
|
+
import { pageUrlToMobileRoute } from './mobileRoute';
|
|
13
|
+
|
|
14
|
+
/** Expo Router href for a resolved menu item (mobile keyword routes). */
|
|
15
|
+
export function getNavigationItemMobileHref(item: INavigationMenuItem): string {
|
|
16
|
+
if (item.item_type === 'external_url' && item.external_url) {
|
|
17
|
+
return item.external_url;
|
|
18
|
+
}
|
|
19
|
+
if (!item.page) {
|
|
20
|
+
return '/index';
|
|
21
|
+
}
|
|
22
|
+
return pageUrlToMobileRoute(item.page.url, item.page.keyword);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Web pathname match: exact or a nested path under the item href. */
|
|
26
|
+
export function matchesWebPath(href: string, pathname: string): boolean {
|
|
27
|
+
if (href === '' || href === '#') {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (pathname === href) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return href !== '/' && pathname.startsWith(`${href}/`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Mobile pathname match: exact, `/index` root aliases, or nested path. */
|
|
37
|
+
export function matchesMobilePath(href: string, pathname: string): boolean {
|
|
38
|
+
if (pathname === href) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
if (href === '/index' && (pathname === '' || pathname === '/' || pathname === '/index')) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return href !== '/' && pathname.startsWith(`${href}/`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function isActive(
|
|
48
|
+
item: INavigationMenuItem,
|
|
49
|
+
pathname: string,
|
|
50
|
+
getHref: (item: INavigationMenuItem) => string,
|
|
51
|
+
matches: (href: string, pathname: string) => boolean,
|
|
52
|
+
): boolean {
|
|
53
|
+
if (matches(getHref(item), pathname)) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return item.children.some((child) => isActive(child, pathname, getHref, matches));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** True when the item or any descendant matches the current web pathname. */
|
|
60
|
+
export function isMenuItemActiveOnWeb(item: INavigationMenuItem, pathname: string): boolean {
|
|
61
|
+
return isActive(item, pathname, getNavigationItemHref, matchesWebPath);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** True when the item or any descendant matches the current mobile pathname. */
|
|
65
|
+
export function isMenuItemActiveOnMobile(item: INavigationMenuItem, pathname: string): boolean {
|
|
66
|
+
return isActive(item, pathname, getNavigationItemMobileHref, matchesMobilePath);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Ids of every item whose subtree contains the active page — the set of
|
|
71
|
+
* parents a collapsible drawer should auto-expand for the current route.
|
|
72
|
+
*/
|
|
73
|
+
export function expandedIdsForActiveTrail(
|
|
74
|
+
items: INavigationMenuItem[],
|
|
75
|
+
pathname: string,
|
|
76
|
+
platform: 'web' | 'mobile',
|
|
77
|
+
): Set<number> {
|
|
78
|
+
const getHref = platform === 'mobile' ? getNavigationItemMobileHref : getNavigationItemHref;
|
|
79
|
+
const matches = platform === 'mobile' ? matchesMobilePath : matchesWebPath;
|
|
80
|
+
const expanded = new Set<number>();
|
|
81
|
+
|
|
82
|
+
const visit = (item: INavigationMenuItem): boolean => {
|
|
83
|
+
let childActive = false;
|
|
84
|
+
for (const child of item.children) {
|
|
85
|
+
if (visit(child)) {
|
|
86
|
+
childActive = true;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (childActive) {
|
|
90
|
+
expanded.add(item.id);
|
|
91
|
+
}
|
|
92
|
+
return childActive || matches(getHref(item), pathname);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
for (const item of items) {
|
|
96
|
+
visit(item);
|
|
97
|
+
}
|
|
98
|
+
return expanded;
|
|
99
|
+
}
|