2022-04-22 21:33:13 +08:00
|
|
|
import { css } from '@emotion/css';
|
2019-02-04 18:19:45 +08:00
|
|
|
import React, { PureComponent } from 'react';
|
2020-03-10 15:53:41 +08:00
|
|
|
import { connect, MapStateToProps } from 'react-redux';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2023-06-12 23:25:38 +08:00
|
|
|
import { AnnotationQuery, DataQuery, GrafanaTheme2 } from '@grafana/data';
|
|
|
|
import { stylesFactory, Themeable2, withTheme2 } from '@grafana/ui';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
import { StoreState } from '../../../../types';
|
2022-02-18 13:06:04 +08:00
|
|
|
import { getSubMenuVariables, getVariablesState } from '../../../variables/state/selectors';
|
2021-04-12 15:41:07 +08:00
|
|
|
import { VariableModel } from '../../../variables/types';
|
2020-03-10 15:53:41 +08:00
|
|
|
import { DashboardModel } from '../../state';
|
2022-04-22 21:33:13 +08:00
|
|
|
import { DashboardLink } from '../../state/DashboardModel';
|
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
import { Annotations } from './Annotations';
|
2022-04-22 21:33:13 +08:00
|
|
|
import { DashboardLinks } from './DashboardLinks';
|
2020-03-10 15:53:41 +08:00
|
|
|
import { SubMenuItems } from './SubMenuItems';
|
2019-02-04 18:19:45 +08:00
|
|
|
|
2023-06-12 23:25:38 +08:00
|
|
|
interface OwnProps extends Themeable2 {
|
2020-03-10 15:53:41 +08:00
|
|
|
dashboard: DashboardModel;
|
2020-07-15 15:18:35 +08:00
|
|
|
links: DashboardLink[];
|
2020-11-21 19:35:18 +08:00
|
|
|
annotations: AnnotationQuery[];
|
2020-03-10 15:53:41 +08:00
|
|
|
}
|
2019-02-04 18:19:45 +08:00
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
interface ConnectedProps {
|
|
|
|
variables: VariableModel[];
|
2019-02-04 18:19:45 +08:00
|
|
|
}
|
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
interface DispatchProps {}
|
2019-02-04 18:19:45 +08:00
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
type Props = OwnProps & ConnectedProps & DispatchProps;
|
2019-02-04 18:19:45 +08:00
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
class SubMenuUnConnected extends PureComponent<Props> {
|
2022-08-22 23:51:33 +08:00
|
|
|
onAnnotationStateChanged = (updatedAnnotation: AnnotationQuery<DataQuery>) => {
|
2020-03-10 15:53:41 +08:00
|
|
|
// we're mutating dashboard state directly here until annotations are in Redux.
|
|
|
|
for (let index = 0; index < this.props.dashboard.annotations.list.length; index++) {
|
|
|
|
const annotation = this.props.dashboard.annotations.list[index];
|
|
|
|
if (annotation.name === updatedAnnotation.name) {
|
|
|
|
annotation.enable = !annotation.enable;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.props.dashboard.startRefresh();
|
|
|
|
this.forceUpdate();
|
|
|
|
};
|
2019-02-04 18:19:45 +08:00
|
|
|
|
2023-07-11 18:30:08 +08:00
|
|
|
disableSubmitOnEnter = (e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
2019-02-04 18:19:45 +08:00
|
|
|
render() {
|
2023-06-12 23:25:38 +08:00
|
|
|
const { dashboard, variables, links, annotations, theme } = this.props;
|
|
|
|
|
|
|
|
const styles = getStyles(theme);
|
2020-07-15 15:18:35 +08:00
|
|
|
|
2021-04-12 15:41:07 +08:00
|
|
|
if (!dashboard.isSubMenuVisible()) {
|
2020-03-10 15:53:41 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-07-29 23:29:55 +08:00
|
|
|
const readOnlyVariables = dashboard.meta.isSnapshot ?? false;
|
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
return (
|
2023-06-12 23:25:38 +08:00
|
|
|
<div className={styles.submenu}>
|
2023-07-11 18:30:08 +08:00
|
|
|
<form aria-label="Template variables" className={styles.formStyles} onSubmit={this.disableSubmitOnEnter}>
|
2022-07-29 23:29:55 +08:00
|
|
|
<SubMenuItems variables={variables} readOnly={readOnlyVariables} />
|
2021-08-19 22:28:25 +08:00
|
|
|
</form>
|
2021-04-27 16:03:52 +08:00
|
|
|
<Annotations
|
|
|
|
annotations={annotations}
|
|
|
|
onAnnotationChanged={this.onAnnotationStateChanged}
|
|
|
|
events={dashboard.events}
|
|
|
|
/>
|
2023-06-12 23:25:38 +08:00
|
|
|
<div className={styles.spacer} />
|
2020-07-15 15:18:35 +08:00
|
|
|
{dashboard && <DashboardLinks dashboard={dashboard} links={links} />}
|
2020-03-10 15:53:41 +08:00
|
|
|
</div>
|
|
|
|
);
|
2019-02-04 18:19:45 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 15:53:41 +08:00
|
|
|
|
2022-02-18 13:06:04 +08:00
|
|
|
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, ownProps) => {
|
|
|
|
const { uid } = ownProps.dashboard;
|
|
|
|
const templatingState = getVariablesState(uid, state);
|
2020-07-15 15:18:35 +08:00
|
|
|
return {
|
2022-02-18 13:06:04 +08:00
|
|
|
variables: getSubMenuVariables(uid, templatingState.variables),
|
2020-07-15 15:18:35 +08:00
|
|
|
};
|
|
|
|
};
|
2020-03-10 15:53:41 +08:00
|
|
|
|
2023-06-12 23:25:38 +08:00
|
|
|
const getStyles = stylesFactory((theme: GrafanaTheme2) => {
|
|
|
|
return {
|
|
|
|
formStyles: css`
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
display: contents;
|
|
|
|
`,
|
|
|
|
submenu: css`
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
align-content: flex-start;
|
|
|
|
align-items: flex-start;
|
|
|
|
gap: ${theme.spacing(1)} ${theme.spacing(2)};
|
|
|
|
padding: 0 0 ${theme.spacing(1)} 0;
|
|
|
|
`,
|
|
|
|
spacer: css({
|
|
|
|
flexGrow: 1,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
2021-08-19 22:28:25 +08:00
|
|
|
|
2023-06-12 23:25:38 +08:00
|
|
|
export const SubMenu = withTheme2(connect(mapStateToProps)(SubMenuUnConnected));
|
2021-04-12 15:41:07 +08:00
|
|
|
|
2020-03-10 15:53:41 +08:00
|
|
|
SubMenu.displayName = 'SubMenu';
|