2023-02-02 17:53:18 +00:00
|
|
|
import classnames from 'classnames';
|
2018-11-07 13:36:35 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2019-10-31 10:48:05 +01:00
|
|
|
import { PanelMenuItem } from '@grafana/data';
|
2023-03-28 10:20:35 +02:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
2023-02-24 04:23:56 +00:00
|
|
|
import { Menu } from '@grafana/ui';
|
2018-10-25 12:47:09 +02:00
|
|
|
|
2022-04-22 14:33:13 +01:00
|
|
|
import { PanelHeaderMenuItem } from './PanelHeaderMenuItem';
|
|
|
|
|
|
2018-11-07 13:36:35 +01:00
|
|
|
export interface Props {
|
2020-02-09 10:53:34 +01:00
|
|
|
items: PanelMenuItem[];
|
2023-01-12 11:10:09 +02:00
|
|
|
style?: React.CSSProperties;
|
2023-02-02 17:53:18 +00:00
|
|
|
itemsClassName?: string;
|
|
|
|
|
className?: string;
|
2018-10-25 12:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
2018-11-07 13:36:35 +01:00
|
|
|
export class PanelHeaderMenu extends PureComponent<Props> {
|
|
|
|
|
renderItems = (menu: PanelMenuItem[], isSubMenu = false) => {
|
2018-10-31 16:04:14 +01:00
|
|
|
return (
|
2023-01-12 11:10:09 +02:00
|
|
|
<ul
|
2023-02-02 17:53:18 +00:00
|
|
|
className={classnames('dropdown-menu', 'dropdown-menu--menu', 'panel-menu', this.props.itemsClassName)}
|
2023-01-12 11:10:09 +02:00
|
|
|
style={this.props.style}
|
|
|
|
|
role={isSubMenu ? '' : 'menu'}
|
|
|
|
|
>
|
2018-11-03 23:36:40 +01:00
|
|
|
{menu.map((menuItem, idx: number) => {
|
2018-10-31 16:04:14 +01:00
|
|
|
return (
|
2018-10-31 16:22:48 +01:00
|
|
|
<PanelHeaderMenuItem
|
2018-11-06 16:44:13 +01:00
|
|
|
key={`${menuItem.text}${idx}`}
|
2018-10-31 16:22:48 +01:00
|
|
|
type={menuItem.type}
|
|
|
|
|
text={menuItem.text}
|
|
|
|
|
iconClassName={menuItem.iconClassName}
|
2018-11-07 13:36:35 +01:00
|
|
|
onClick={menuItem.onClick}
|
2018-10-31 16:22:48 +01:00
|
|
|
shortcut={menuItem.shortcut}
|
2023-03-02 15:42:00 +01:00
|
|
|
href={menuItem.href}
|
2018-10-31 16:22:48 +01:00
|
|
|
>
|
|
|
|
|
{menuItem.subMenu && this.renderItems(menuItem.subMenu, true)}
|
|
|
|
|
</PanelHeaderMenuItem>
|
2018-10-31 16:04:14 +01:00
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</ul>
|
2018-10-25 12:47:09 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2023-02-02 17:53:18 +00:00
|
|
|
return (
|
|
|
|
|
<div className={classnames('panel-menu-container', 'dropdown', 'open', this.props.className)}>
|
2023-07-24 13:08:59 +02:00
|
|
|
{this.renderItems(flattenGroups(this.props.items))}
|
2023-02-02 17:53:18 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
2018-10-25 12:47:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-24 04:23:56 +00:00
|
|
|
|
2023-07-24 13:08:59 +02:00
|
|
|
function flattenGroups(items: PanelMenuItem[]): PanelMenuItem[] {
|
|
|
|
|
return items.reduce((all: PanelMenuItem[], item) => {
|
|
|
|
|
if (Array.isArray(item.subMenu) && item.type === 'submenu') {
|
|
|
|
|
all.push({
|
|
|
|
|
...item,
|
|
|
|
|
subMenu: flattenGroups(item.subMenu),
|
|
|
|
|
});
|
|
|
|
|
return all;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(item.subMenu) && item.type === 'group') {
|
|
|
|
|
const { subMenu, ...rest } = item;
|
|
|
|
|
all.push(rest);
|
|
|
|
|
all.push.apply(all, flattenGroups(subMenu));
|
|
|
|
|
return all;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
all.push(item);
|
|
|
|
|
return all;
|
|
|
|
|
}, []);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-24 04:23:56 +00:00
|
|
|
export function PanelHeaderMenuNew({ items }: Props) {
|
|
|
|
|
const renderItems = (items: PanelMenuItem[]) => {
|
2023-07-24 13:08:59 +02:00
|
|
|
return items.map((item) => {
|
|
|
|
|
switch (item.type) {
|
|
|
|
|
case 'divider':
|
|
|
|
|
return <Menu.Divider key={item.text} />;
|
|
|
|
|
case 'group':
|
|
|
|
|
return (
|
|
|
|
|
<Menu.Group key={item.text} label={item.text}>
|
|
|
|
|
{item.subMenu ? renderItems(item.subMenu) : undefined}
|
|
|
|
|
</Menu.Group>
|
|
|
|
|
);
|
|
|
|
|
default:
|
|
|
|
|
return (
|
|
|
|
|
<Menu.Item
|
|
|
|
|
key={item.text}
|
|
|
|
|
label={item.text}
|
|
|
|
|
icon={item.iconClassName}
|
|
|
|
|
childItems={item.subMenu ? renderItems(item.subMenu) : undefined}
|
|
|
|
|
url={item.href}
|
|
|
|
|
onClick={item.onClick}
|
|
|
|
|
shortcut={item.shortcut}
|
|
|
|
|
testId={selectors.components.Panels.Panel.menuItems(item.text)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-02-24 04:23:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return <Menu>{renderItems(items)}</Menu>;
|
|
|
|
|
}
|