Files
wekan/models/lib/leftMenu.js
T
Lauri Ojansivu 1c1aefbd16 Admin Panel / Attachments: render its side menu from the shared left menu.
The last of the seven. 2525 characters of menu markup became one
+leftMenu(menuItems); Attachments already tracked the open pane in activeSection,
so nothing had to be derived, and its single click handler is untouched - the
shared template puts js-attachments-menu on every entry alongside the shared
class.

One template addition: the Sandstorm entry is a proper noun, not a translated
string, so an item may now carry a literal `label` where the others carry a
`labelKey`. The Sandstorm-only condition survives as data, a null the builder
drops, rather than markup wrapped in `if isSandstorm`.

All seven Admin Panel pages now render the same menu from a data list. Across
them: 8393 characters of duplicated markup gone, 44 entries described as data,
and 19 near-identical click handlers collapsed to 7 - one per page.

Tests: a check that ALL seven render the shared menu and none keeps .side-menu
markup of its own - the design is only worth having if nothing is left outside it
- plus the Sandstorm entry and the literal-label fallback.

Remaining: People's seven pane tables still need converting to Table.md.

Thanks to xet7 !
2026-07-25 18:51:28 +03:00

57 lines
2.5 KiB
JavaScript

// Pure helpers behind the shared left menu (docs/Design/Page/Left-Menu.md).
//
// Every Admin Panel page — Settings, People, Features, Attachments, Problems,
// Translation, Info — has the same menu beside its content: a list of entries,
// each an icon and a label, with the current one highlighted. That markup was
// retyped 44 times across seven templates. It is now built from a plain item
// list, here, so a menu is data rather than markup.
//
// An item is:
// { id, icon, labelKey, cls } a normal entry
// { separator: true } a horizontal rule between groups
//
// `id` is what the page's click handler reads from data-id; `icon` is the Font
// Awesome class; `labelKey` is an i18n key.
// Normalize a menu definition into what the template iterates. `activeId` marks
// exactly one entry active — the page tells us which, so the menu never has to
// know how the page stores its state.
export function buildMenuItems(items, activeId, jsClass = '') {
const list = Array.isArray(items) ? items : [];
return list
// A page may build its list with conditional entries (`isSandstorm ? null :
// {...}`); drop the holes instead of rendering an empty <li>.
.filter(Boolean)
.map(item => {
if (item.separator) {
return { separator: true };
}
return {
id: item.id || '',
icon: item.icon || '',
labelKey: item.labelKey || '',
// A literal label, for the rare entry that is a proper noun rather than a
// translated string (Admin Panel / Attachments -> Sandstorm).
label: item.label || '',
// Only one entry can be active. Compared as strings so a page storing its
// active id as a number still highlights the right row.
active: !!item.id && String(item.id) === String(activeId),
// The page's own handler class, kept alongside the shared one so an
// existing `click a.js-<page>-menu` handler goes on working.
jsClass: item.jsClass || jsClass || '',
cls: item.cls || '',
// Icon shape, reproducing exactly what each page already rendered so a
// conversion changes no pixel. See leftMenu.jade.
emoji: !!item.emoji,
iconWrapCls: item.iconWrapCls || '',
};
});
}
// True when exactly one entry is active. Used by the test to prove a menu can
// never highlight two rows at once, which is what happens when each page keeps
// its own `isXActive` helper per entry.
export function activeCount(items) {
return (Array.isArray(items) ? items : []).filter(i => i && i.active).length;
}