import type { TLocalizationToken } from '@cloudbeaver/core-localization' ; import { OrderedMap } from '@cloudbeaver/core-utils' ; import type { IMenuPanel } from '../IMenuPanel' ; const DEFAULT_ITEM_ORDER = 100 ; export type MenuItemType = 'checkbox' | 'radio' ; export interface IMenuItemOptions { id : string ; title ? : TLocalizationToken ; titleGetter ? : ( ... args : any [ ] ) => TLocalizationToken | undefined ; icon ? : string ; iconGetter ? : ( ) => string | undefined ; tooltip ? : string ; tooltipGetter ? : ( ) => TLocalizationToken | undefined ; order ? : number ; isPanel ? : boolean ; keepMenuOpen ? : boolean ; panel ? : IMenuPanel ; type ? : MenuItemType ; separator ? : boolean ; rtl ? : boolean ; } /**
 * This class store IMenuItemOptions in a set of trees.
 * Items on a certain level of the tree are always ordered.
 * to show menu you need to convert menuItemOptions to MenuItemModels
 */ export class MenuOptionsStore < T extends IMenuItemOptions > { private panelsMap : Map < string , MenuItemOptionsList < T > > = new Map ( ) ; addRootPanel ( panelId ) { this . createPanelIfNotExists ( panelId ) ; } addMenuItem ( panelId , params ) { const panel = this . createPanelIfNotExists ( panelId ) ; if ( panel . menuItems . has ( params . id ) ) { throw new Error ( `Panel "${ panelId }" already has item ${ params . id }` ) ; } this . putItemInPanel ( panel , params ) ; if ( params . isPanel ) { this . createPanelIfNotExists ( params . id ) ; } } getPanel ( panelId ) { const panel = this . panelsMap . get ( panelId ) ; if ( ! panel ) { throw new Error ( `Menu panel "${ panelId }" is missing` ) ; } return panel ; } private putItemInPanel ( panel , params ) { panel . menuItems . addValue ( params ) ; panel . menuItems . sort ( ( a , b ) => this . compare ( a , b ) ) ; } private createPanelIfNotExists ( panelId ) { if ( this . panelsMap . has ( panelId ) ) { return this . panelsMap . get ( panelId ) ! ; } const panel = new MenuItemOptionsList < T > ( panelId ) ; this . panelsMap . set ( panelId , panel ) ; return panel ; } private compare ( a , b ) { const orderA = a . order !== undefined ? a . order : DEFAULT_ITEM_ORDER ; const orderB = b . order !== undefined ? b . order : DEFAULT_ITEM_ORDER ; return orderA - orderB ; } } class MenuItemOptionsList < T extends IMenuItemOptions > { readonly id : string ; menuItems = new OrderedMap < string , T > ( option => option . id ) ; constructor ( id ) { this . id = id ; } }