All files / frontend/components/Docs docContextMenuState.ts

100% Statements 22/22
100% Branches 17/17
100% Functions 4/4
100% Lines 21/21

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                                                                                              5x                   10x 6x 6x 6x 6x 6x 6x 6x   6x           6x   6x 6x 14x 4x 4x   10x 10x           10x 10x   6x    
/*
  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.
*/
 
import { nextTick, reactive } from 'vue';
 
// Пункт меню. Формат { id, title, on } совместим с плагинами; icon и group необязательны.
export interface DocContextMenuItem {
  id: string;
  title: string;
  on: () => void;
  icon?: string;       // значок MDI, например mdi-download
  group?: string;      // подпись группы, например «Показать как»
}
 
// null в списке пунктов — разделитель
export type DocContextMenuEntry = DocContextMenuItem | null;
 
// Строка меню: пункт с подписью группы и разделителем перед ним
export interface DocContextMenuRow {
  key: string;
  item: DocContextMenuItem;
  caption: string | null;   // подпись группы — там, где group меняется
  divider: boolean;         // разделитель перед строкой; у первой строки его нет
}
 
export interface DocContextMenuState {
  show: boolean;
  x: number;
  y: number;
  items: DocContextMenuEntry[];
}
 
// Меню по правому клику у документов: одно на приложение, выводится в Root.vue (DocContextMenu) поверх
// страницы — Swagger UI и vis-network перерисовывают свой контейнер целиком.
export const docContextMenu = reactive<DocContextMenuState>({
  show: false,
  x: 0,
  y: 0,
  items: []
});
 
// Открывает меню с пунктами; без пунктов остается системное меню браузера.
// Вложенный документ или схема открывают свое меню первыми, внешний документ его не перебивает.
export function openDocContextMenu(event: MouseEvent, items?: DocContextMenuEntry[] | null): boolean {
  if (event.defaultPrevented || !items?.length) return false;
  event.preventDefault();
  docContextMenu.show = false;
  docContextMenu.x = event.clientX;
  docContextMenu.y = event.clientY;
  docContextMenu.items = items;
  nextTick(() => {
    docContextMenu.show = true;
  });
  return true;
}
 
// Плоский список строк меню. null в пунктах — разделитель; перед новой группой разделитель ставится сам.
// Подряд идущие разделители и разделители по краям схлопываются.
export function docContextMenuRows(items: DocContextMenuEntry[] | null | undefined): DocContextMenuRow[] {
  const rows: DocContextMenuRow[] = [];
  let group: string | undefined;
  let divider = false;
  (items || []).forEach((item, index) => {
    if (!item) {
      divider = true;
      return;
    }
    const caption = (item.group && (item.group !== group)) ? item.group : null;
    rows.push({
      key: item.id || `item-${index}`,
      item,
      caption,
      divider: !!rows.length && (divider || !!caption)
    });
    group = item.group;
    divider = false;
  });
  return rows;
}