All files / frontend/helpers menuTree.ts

100% Statements 47/47
96.66% Branches 29/30
100% Functions 14/14
100% Lines 42/42

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                                        3x 8x         3x 4x     3x 73x                                                                       3x 102x 102x 210x 102x         3x   8x 9x   57x 57x 57x 127x 123x 123x 123x 218x 218x   123x 123x 123x 123x 123x 92x     57x       3x 23x 23x 26x 47x 47x             47x 47x 12x 12x       23x 23x    
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
// Логика главного меню (Layouts/Menu.vue): слова поиска, значки пунктов, путь к текущей странице.
// Подсветка совпадений — общая, в highlightMatches.ts.
 
// Слова запроса поиска по меню; запрос уже приведен к нижнему регистру
export const menuFilterTerms = (query: string | null | undefined): string[] =>
  query ? query.split(' ').filter(Boolean) : [];
 
// Метамодели задают значки в двух форматах: MDI (mdi-*) и Material Icons (description,
// location_searching). Второй Vuetify понимает только с префиксом набора md:.
// Значок с явным набором (md:..., mdi:...) не меняется.
export const menuIconName = (icon: string): string =>
  (icon.startsWith('mdi-') || icon.includes(':')) ? icon : `md:${icon}`;
 
// Пункт лежит на пути к текущей странице: он предок выбранного пункта (сам выбранный — нет)
export const isOnMenuTrail = (location: string, selectedLocation: string | null | undefined): boolean =>
  !!selectedLocation && selectedLocation.startsWith(`${location}/`);
 
// Пункт меню из сборщика (menu-builder.mjs): пункты метамодели и служебные пункты ядра (service)
export interface MenuDatasetItem {
  title?: string;
  route?: string;
  icon?: string;
  location?: string;
  service?: boolean;
}
 
// Узел дерева метамодели; ключ узла — сегмент location
export interface MenuNode {
  title: string;
  route?: string;
  icon?: string;
  items: Record<string, MenuNode>;
}
 
export interface MenuTree {
  items: Record<string, MenuNode>;
}
 
// Строка дерева в меню
export interface MenuRow {
  title: string;
  route?: string;
  icon?: string;
  level: number;
  location: string;
  isGroup?: boolean;
}
 
// Служебные пункты ядра выводятся отдельно, внизу меню. Пункты делятся до построения дерева, поэтому пункт
// метамодели с тем же location («Поиск», «Проблемы», devtool) остается в дереве отдельным узлом
// со всеми вложенными пунктами.
export const splitMenuItems = (items: MenuDatasetItem[] | null | undefined): { tree: MenuDatasetItem[]; service: MenuDatasetItem[] } => {
  const tree: MenuDatasetItem[] = [];
  const service: MenuDatasetItem[] = [];
  (items || []).forEach((item) => (item.service ? service : tree).push(item));
  return { tree, service };
};
 
// Дерево из пунктов метамодели. accept — фильтр поиска по location; isCurrent — пункт текущей страницы.
// expand — разделы, которые нужно раскрыть, чтобы был виден пункт текущей страницы.
export const buildMenuTree = (
  items: MenuDatasetItem[],
  accept: (location: string) => boolean = () => true,
  isCurrent: (route: string | undefined, item: MenuDatasetItem) => boolean = () => false
): { tree: MenuTree; expand: string[] } => {
  const tree: MenuTree = { items: {} };
  const expand: string[] = [];
  items.forEach((item) => {
    if (!item.location || !accept(item.location)) return;
    const location = item.location.split('/');
    let node: MenuTree | MenuNode = tree;
    location.forEach((key) => {
      if (!node.items[key]) node.items[key] = { title: key, items: {} };
      node = node.items[key];
    });
    const leaf = node as MenuNode;
    leaf.title = item.title ?? leaf.title;
    leaf.route = item.route;
    leaf.icon = item.icon;
    if (isCurrent(item.route, item)) {
      location.forEach((_, i) => expand.push(location.slice(0, i + 1).join('/')));
    }
  });
  return { tree, expand };
};
 
// Строки меню по дереву: раздел разворачивается, если isExpanded для его location вернул true
export const menuRows = (tree: MenuTree | null | undefined, isExpanded: (location: string) => boolean): MenuRow[] => {
  const rows: MenuRow[] = [];
  const walk = (node: MenuTree | MenuNode, path: string[]) => {
    Object.entries(node.items).forEach(([key, item]) => {
      const itemPath = path.concat(key);
      const row: MenuRow = {
        title: item.title,
        route: item.route,
        icon: item.icon,
        level: itemPath.length - 1,
        location: itemPath.join('/')
      };
      rows.push(row);
      if (Object.keys(item.items).length) {
        row.isGroup = true;
        if (isExpanded(row.location)) walk(item, itemPath);
      }
    });
  };
  tree && walk(tree, []);
  return rows;
};