@selfhelp/shared 1.21.3 → 1.21.6

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 (53) hide show
  1. package/CHANGELOG.md +106 -0
  2. package/README.md +2 -1
  3. package/dist/index.d.mts +519 -24
  4. package/dist/index.d.ts +519 -24
  5. package/dist/index.js +723 -18
  6. package/dist/index.mjs +674 -18
  7. package/dist/plugin-sdk/index.d.mts +1 -1
  8. package/dist/plugin-sdk/index.d.ts +1 -1
  9. package/dist/plugin-sdk/index.js +2 -1
  10. package/dist/plugin-sdk/index.mjs +2 -1
  11. package/dist/registry/index.d.mts +2 -2
  12. package/dist/registry/index.d.ts +2 -2
  13. package/dist/registry/index.js +2 -1
  14. package/dist/registry/index.mjs +2 -1
  15. package/dist/{styles.registry-DEDEZ8l3.d.ts → styles.registry-DX79oirw.d.ts} +93 -16
  16. package/dist/{styles.registry-DHM4M77N.d.mts → styles.registry-ffw0MH3r.d.mts} +93 -16
  17. package/package.json +1 -1
  18. package/src/api/__tests__/endpoints.test.ts +37 -12
  19. package/src/api/endpoints.ts +5 -5
  20. package/src/api/index.ts +1 -0
  21. package/src/api/pagesResolve.ts +78 -0
  22. package/src/content/__tests__/fieldsMapLabels.test.ts +55 -0
  23. package/src/content/__tests__/formRecordPrefill.test.ts +104 -0
  24. package/src/content/__tests__/optionLabels.test.ts +100 -0
  25. package/src/content/fieldsMapLabels.ts +103 -0
  26. package/src/content/formRecordPrefill.ts +191 -0
  27. package/src/content/index.ts +3 -0
  28. package/src/content/optionLabels.ts +200 -0
  29. package/src/navigation/__tests__/navigation.test.ts +296 -6
  30. package/src/navigation/activeTrail.ts +99 -0
  31. package/src/navigation/branchNav.ts +155 -6
  32. package/src/navigation/footerPreset.ts +109 -0
  33. package/src/navigation/headerLayers.ts +56 -0
  34. package/src/navigation/headerPreset.ts +9 -2
  35. package/src/navigation/index.ts +4 -0
  36. package/src/navigation/menuDepth.ts +73 -37
  37. package/src/navigation/mobileRoute.ts +25 -0
  38. package/src/navigation/navLinks.ts +0 -4
  39. package/src/navigation/navigationBundle.ts +81 -0
  40. package/src/navigation/navigationPayload.ts +96 -17
  41. package/src/registry/__tests__/platform.test.ts +11 -10
  42. package/src/registry/styles.registry.ts +2 -1
  43. package/src/types/__tests__/preview-bridge.test.ts +7 -0
  44. package/src/types/auth.ts +4 -0
  45. package/src/types/cms-app.ts +74 -0
  46. package/src/types/index.ts +1 -0
  47. package/src/types/mobile-adapters.ts +1 -0
  48. package/src/types/pages.ts +6 -0
  49. package/src/types/preview-bridge.ts +22 -2
  50. package/src/types/styles/composite.ts +21 -0
  51. package/src/types/styles/forms.ts +57 -10
  52. package/src/types/styles/interactive.ts +1 -1
  53. package/src/types/styles/unknown.ts +2 -2
@@ -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 type { INavigationMenuItem, INavigationPayload } from '../navigationPayload';
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
+ }
@@ -2,7 +2,12 @@
2
2
  SPDX-FileCopyrightText: 2026 Humdek, University of Bern
3
3
  SPDX-License-Identifier: MPL-2.0
4
4
  */
5
- import type { INavigationMenuItem, INavigationPayload } from './navigationPayload';
5
+ import type {
6
+ INavigationMenu,
7
+ INavigationMenuItem,
8
+ INavigationPayload,
9
+ TNavigationChildrenNavMode,
10
+ } from './navigationPayload';
6
11
  import { collectPageIdsFromMenuItems } from './menuMembership';
7
12
  import { pageUrlToMobileRoute } from './mobileRoute';
8
13
 
@@ -28,8 +33,8 @@ function toSegment(item: INavigationMenuItem): IBranchNavSegment | null {
28
33
  keyword: item.page.keyword,
29
34
  url: item.page.url,
30
35
  label: itemLabel(item),
31
- icon: item.icon ?? item.page.icon,
32
- mobile_icon: item.page.mobile_icon,
36
+ icon: item.icon ?? null,
37
+ mobile_icon: item.mobile_icon ?? null,
33
38
  };
34
39
  }
35
40
 
@@ -106,9 +111,9 @@ export function resolveBranchNavGroup(
106
111
  }
107
112
  const located = findParentItem([root], currentPageId);
108
113
  if (located) {
109
- const directChildren = menuVisibleChildItems(
110
- located.parent ?? { id: 0, item_type: 'group', label: '', position: 0, children: located.siblings },
111
- );
114
+ const directChildren = located.parent
115
+ ? menuVisibleChildItems(located.parent)
116
+ : located.siblings.filter((sibling) => sibling.page != null && sibling.is_active !== false);
112
117
  if (located.parent && located.parent.page?.id === currentPageId && directChildren.length > 0) {
113
118
  const segments = directChildren.map(toSegment).filter((s): s is IBranchNavSegment => s != null);
114
119
  return segments.length > 0 ? segments : null;
@@ -163,6 +168,150 @@ export function resolveWebBranchNavGroup(
163
168
  return resolveBranchNavGroup(payload, currentPageId, ['web_header', 'web_footer']);
164
169
  }
165
170
 
171
+ /** One breadcrumb step; group items produce unlinked crumbs (`pageId === null`). */
172
+ export interface IBreadcrumbEntry {
173
+ label: string;
174
+ pageId: number | null;
175
+ keyword: string | null;
176
+ url: string | null;
177
+ }
178
+
179
+ /**
180
+ * Everything a web renderer needs to present the branch of the current page:
181
+ * resolved presentation mode, the sidebar group, breadcrumbs, and the
182
+ * prev/next pager. `null` when the page is a top-level leaf (no branch UI).
183
+ */
184
+ export interface IBranchNavContext {
185
+ /** `sidebar` | `pills` | `none` — parent-item override, else menu default, else `sidebar`. */
186
+ mode: TNavigationChildrenNavMode;
187
+ /** Menu-level toggle for the breadcrumb trail. */
188
+ showBreadcrumbs: boolean;
189
+ /** Prev/next pager — parent-item override, else menu default, else on. */
190
+ showPager: boolean;
191
+ /** Label of the branch parent (page or group), used as the sidebar heading. */
192
+ heading: string | null;
193
+ /** Branch parent as a navigable segment when the parent is a page. */
194
+ parent: IBranchNavSegment | null;
195
+ /** Ordered current-level group: children of the branch parent. */
196
+ segments: IBranchNavSegment[];
197
+ /** Menu path root → current page (current page is the last entry). */
198
+ breadcrumbs: IBreadcrumbEntry[];
199
+ /** Neighbours of the current page inside `segments`. */
200
+ pager: { prev: IBranchNavSegment | null; next: IBranchNavSegment | null };
201
+ }
202
+
203
+ function findMenuItemPath(
204
+ items: INavigationMenuItem[],
205
+ pageId: number,
206
+ trail: INavigationMenuItem[] = [],
207
+ ): INavigationMenuItem[] | null {
208
+ for (const item of items) {
209
+ const nextTrail = [...trail, item];
210
+ if (item.page?.id === pageId) {
211
+ return nextTrail;
212
+ }
213
+ if (item.children?.length) {
214
+ const found = findMenuItemPath(item.children, pageId, nextTrail);
215
+ if (found) {
216
+ return found;
217
+ }
218
+ }
219
+ }
220
+ return null;
221
+ }
222
+
223
+ function toBreadcrumb(item: INavigationMenuItem): IBreadcrumbEntry {
224
+ return {
225
+ label: itemLabel(item),
226
+ pageId: item.page?.id ?? null,
227
+ keyword: item.page?.keyword ?? null,
228
+ url: item.page?.url ?? null,
229
+ };
230
+ }
231
+
232
+ function resolveModeForBranch(
233
+ branchParent: INavigationMenuItem | null,
234
+ menu: INavigationMenu,
235
+ ): TNavigationChildrenNavMode {
236
+ return branchParent?.children_nav ?? menu.children_nav ?? 'sidebar';
237
+ }
238
+
239
+ function resolvePagerForBranch(
240
+ branchParent: INavigationMenuItem | null,
241
+ menu: INavigationMenu,
242
+ ): boolean {
243
+ return branchParent?.show_pager ?? menu.show_pager ?? true;
244
+ }
245
+
246
+ /**
247
+ * Resolve the full branch-navigation context of a web page: which presentation
248
+ * to use (menu default + per-parent override), the sidebar group, breadcrumbs,
249
+ * and prev/next neighbours.
250
+ *
251
+ * - Page with menu-visible children → the page is the branch parent, its
252
+ * children are the group (no pager: the parent is the overview).
253
+ * - Nested page → the parent item owns the branch, the siblings are the group,
254
+ * the pager walks the group.
255
+ * - Top-level leaf → `null` (no generated branch UI).
256
+ */
257
+ export function resolveWebBranchNavContext(
258
+ payload: INavigationPayload,
259
+ currentPageId: number,
260
+ ): IBranchNavContext | null {
261
+ for (const key of ['web_header', 'web_footer'] as const) {
262
+ const menu = payload.menus[key];
263
+ if (!menu?.items?.length) {
264
+ continue;
265
+ }
266
+ const path = findMenuItemPath(menu.items, currentPageId);
267
+ if (!path) {
268
+ continue;
269
+ }
270
+
271
+ const currentItem = path[path.length - 1];
272
+ const ownChildren = menuVisibleChildItems(currentItem);
273
+
274
+ let branchParent: INavigationMenuItem | null = null;
275
+ let group: INavigationMenuItem[];
276
+ if (ownChildren.length > 0) {
277
+ branchParent = currentItem;
278
+ group = ownChildren;
279
+ } else if (path.length >= 2) {
280
+ branchParent = path[path.length - 2];
281
+ group = menuVisibleChildItems(branchParent);
282
+ } else {
283
+ // Top-level leaf: the header already covers this level.
284
+ return null;
285
+ }
286
+
287
+ const segments = group
288
+ .map(toSegment)
289
+ .filter((segment): segment is IBranchNavSegment => segment != null);
290
+ if (segments.length === 0) {
291
+ return null;
292
+ }
293
+
294
+ const currentIndex = segments.findIndex((segment) => segment.pageId === currentPageId);
295
+ const pager = {
296
+ prev: currentIndex > 0 ? segments[currentIndex - 1] : null,
297
+ next: currentIndex >= 0 && currentIndex < segments.length - 1 ? segments[currentIndex + 1] : null,
298
+ };
299
+
300
+ return {
301
+ mode: resolveModeForBranch(branchParent, menu),
302
+ showBreadcrumbs: menu.show_breadcrumbs ?? false,
303
+ showPager: resolvePagerForBranch(branchParent, menu),
304
+ heading: branchParent ? itemLabel(branchParent) : null,
305
+ parent: branchParent ? toSegment(branchParent) : null,
306
+ segments,
307
+ breadcrumbs: path.map(toBreadcrumb),
308
+ pager,
309
+ };
310
+ }
311
+
312
+ return null;
313
+ }
314
+
166
315
  export type TMobilePagePresentation = 'route' | 'modal';
167
316
 
168
317
  export function resolveMobilePagePresentation(