All files / core/blong-browser/src/model menu.ts

96.07% Statements 49/51
90% Branches 54/60
100% Functions 14/14
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                    1x                                                         12x                           22x 22x 21x 6x             15x 13x         18x 18x                                 16x 16x 16x 16x 16x 12x 12x   4x   2x   4x 3x               3x   4x                     32x       10x         12x 16x 16x 16x 11x 11x 7x   12x 21x           17x 14x 14x               7x 9x 9x           7x 9x 9x      
/**
 * menu.ts — how a model spec becomes a menubar entry.
 *
 * The aggregator used to push every page into one group titled after the subject,
 * which works for four pages and not for twenty: the menubar grows past the height
 * of a laptop screen and nothing is findable. A model now says where it belongs
 * (`IModelSpec.menu`) or marks itself as reference data (`classifier`), and this
 * file turns that into the group/leaf shape the portal renders.
 *
 * The rules are stated once, here, and are pure so they can be tested without a
 * port:
 *
 * - default: flat group titled `subjectTitle` (the subject, capitalized);
 * - `classifier`: the shared `CLASSIFIER_ROOT` group, with one submenu per realm
 *   named after its `subjectTitle` — the same pickers every realm's forms use;
 * - `menu.group`: a flat group with that title (a realm splitting its pages);
 * - `menu.root` + `menu.group`: nested;
 * - `menu.hidden`: no menu entry at all.
 *
 * Group ids are derived from the title and stay language-independent, which is
 * what the Playwright selectors and the portal's `data-testid` rely on.
 */
import type {IModelMenu, IResolvedModelSpec} from '@feasibleone/blong';
import type {IMenuItem} from '../types/portal.js';
 
/** The shared root every `classifier: true` page lands in. */
export const CLASSIFIER_ROOT = 'Classifiers';
 
/**
 * A leaf: the page's method, the label it sorts by, and where the model put it.
 *
 * `title` travels with the entry as the sort key and as the label a menu without a
 * component slice would fall back on; the portal still takes the rendered title
 * from the slice, so a translated page keeps its translated name.
 */
export interface IMenuLeaf {
    /** English label — `objectTitle` unless the model names one. */
    title: string;
    method: string;
    icon?: string;
    /** Position inside the group; absent means "after the ordered ones, alphabetically". */
    order?: number;
}
 
/** A group of entries — the portal renders this as a submenu. */
export interface IMenuGroup {
    id?: string;
    title: string;
    items: IMenuNode[];
}
 
export type IMenuNode = IMenuLeaf | IMenuGroup;
 
/** Stable, language-independent identifier for a group title. */
export function menuGroupId(title: string): string {
    return title
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, '-')
        .replace(/^-|-$/g, '');
}
 
/** Resolved placement of one page: `undefined` means "no menu entry". */
export interface IMenuPlacement {
    root: string;
    group?: string;
}
 
/** Where a model's browse page belongs in the menubar. */
export function menuPlacement(model: IResolvedModelSpec): IMenuPlacement | undefined {
    const menu: IModelMenu = model.menu ?? {};
    if (menu.hidden) return undefined;
    if (model.classifier) {
        return {
            root: menu.root ?? CLASSIFIER_ROOT,
            group: menu.group ?? model.subjectTitle,
        };
    }
    // No `root` means the named group is the top level, so `group` alone moves a
    // page into another flat group instead of nesting it one level down.
    if (menu.group && !menu.root) return {root: menu.group};
    return {root: menu.root ?? model.subjectTitle, ...(menu.group && {group: menu.group})};
}
 
/** The menubar leaf for a model's browse page. */
export function menuLeaf(model: IResolvedModelSpec): IMenuLeaf {
    const menu: IModelMenu = model.menu ?? {};
    return {
        title: menu.title ?? model.objectTitle,
        method: `${model.subject}.${model.object}.browse`,
        ...(menu.icon && {icon: menu.icon}),
        ...(menu.order !== undefined && {order: menu.order}),
    };
}
 
/**
 * Add one page to the menu tree in place.
 *
 * `menus` is the aggregator's accumulated map — root title → its entries — so a
 * second realm contributing to `Classifiers` finds the root already there and
 * merges into its own submenu, and a suite that adds a group of its own does not
 * lose the generated ones.
 */
export function addMenuEntry(menus: Record<string, IMenuNode[]>, model: IResolvedModelSpec): void {
    const placement = menuPlacement(model);
    Iif (!placement) return;
    const root = (menus[placement.root] ||= []);
    const leaf = menuLeaf(model);
    if (!placement.group) {
        root.push(leaf);
        return;
    }
    let group = root.find(
        (entry): entry is IMenuGroup =>
            typeof entry === 'object' && 'items' in entry && entry.title === placement.group,
    );
    if (!group) {
        group = {
            // Qualified by the root: the classifier submenu is titled `Sport`, as is
            // the realm's own group, and two groups sharing an id makes the portal's
            // `data-testid` ambiguous.
            id: menuGroupId(`${placement.root}-${placement.group}`),
            title: placement.group,
            items: [],
        };
        root.push(group);
    }
    group.items.push(leaf);
}
 
/**
 * The order an entry asks for, or `undefined` when the model named none.
 *
 * A group sorts by this first and then by label, so a realm that orders two of
 * its pages gets those two at the top and the rest alphabetically, and a realm
 * that orders none gets an alphabetically sorted menu without doing anything.
 */
function ordered(entry: IMenuNode): boolean {
    return typeof entry === 'object' && 'method' in entry && entry.order !== undefined;
}
 
function orderValue(entry: IMenuNode): number {
    return typeof entry === 'object' && 'method' in entry ? (entry.order ?? 0) : 0;
}
 
/** Sort one group's entries in place: explicit order first, then alphabetical. */
function sortEntries(items: IMenuNode[]): void {
    items.sort((left, right) => {
        const leftOrdered = ordered(left);
        const rightOrdered = ordered(right);
        if (leftOrdered && rightOrdered) return orderValue(left) - orderValue(right);
        Iif (leftOrdered) return -1;
        if (rightOrdered) return 1;
        return left.title.localeCompare(right.title, undefined, {sensitivity: 'base'});
    });
    for (const entry of items) {
        if (typeof entry === 'object' && 'items' in entry) sortEntries(entry.items);
    }
}
 
/** Drop the sort key the menu model does not carry (`IAction` has no `order`). */
function emitted(entry: IMenuNode): IMenuNode {
    if (typeof entry !== 'object' || !('method' in entry)) return entry;
    const {order: _order, ...leaf} = entry;
    return leaf;
}
 
/**
 * The generated menu, with the shared classifier root last: a realm's own pages
 * are what its people open all day, and reference data is looked up by exception.
 */
export function menuGroups(menus: Record<string, IMenuNode[]>): IMenuItem[] {
    const groups = Object.entries(menus).map(([title, items]) => {
        sortEntries(items);
        return {
            id: menuGroupId(title),
            title,
            items: items.map(emitted),
        };
    });
    return [
        ...groups.filter(group => group.title !== CLASSIFIER_ROOT),
        ...groups.filter(group => group.title === CLASSIFIER_ROOT),
    ] as IMenuItem[];
}