2020-03-10 02:53:41 -05:00
|
|
|
import React, { MouseEvent, PureComponent } from 'react';
|
2020-04-27 02:09:05 -05:00
|
|
|
import { Icon } from '@grafana/ui';
|
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
|
|
|
|
2020-11-20 03:51:32 -06:00
|
|
|
import { toVariableIdentifier, toVariablePayload, VariableIdentifier } from '../state/types';
|
2020-03-10 02:53:41 -05:00
|
|
|
import { StoreState } from '../../../types';
|
|
|
|
import { VariableEditorEditor } from './VariableEditorEditor';
|
2021-02-17 23:21:35 -06:00
|
|
|
import { connect, ConnectedProps } from 'react-redux';
|
2020-06-25 00:38:55 -05:00
|
|
|
import { getEditorVariables } from '../state/selectors';
|
2020-03-10 02:53:41 -05:00
|
|
|
import { switchToEditMode, switchToListMode, switchToNewMode } from './actions';
|
|
|
|
import { changeVariableOrder, duplicateVariable, removeVariable } from '../state/sharedReducer';
|
2020-11-05 02:53:27 -06:00
|
|
|
import { VariableEditorList } from './VariableEditorList';
|
|
|
|
import { VariablesUnknownTable } from '../inspect/VariablesUnknownTable';
|
|
|
|
import { VariablesDependenciesButton } from '../inspect/VariablesDependenciesButton';
|
2020-03-10 02:53:41 -05:00
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
const mapStateToProps = (state: StoreState) => ({
|
|
|
|
variables: getEditorVariables(state),
|
|
|
|
idInEditor: state.templating.editor.id,
|
|
|
|
dashboard: state.dashboard.getModel(),
|
|
|
|
});
|
2020-03-10 02:53:41 -05:00
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
const mapDispatchToProps = {
|
|
|
|
changeVariableOrder,
|
|
|
|
duplicateVariable,
|
|
|
|
removeVariable,
|
|
|
|
switchToNewMode,
|
|
|
|
switchToEditMode,
|
|
|
|
switchToListMode,
|
|
|
|
};
|
2020-03-10 02:53:41 -05:00
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
interface OwnProps {}
|
2020-03-10 02:53:41 -05:00
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
const connector = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
|
|
|
type Props = OwnProps & ConnectedProps<typeof connector>;
|
2020-03-10 02:53:41 -05:00
|
|
|
|
|
|
|
class VariableEditorContainerUnconnected extends PureComponent<Props> {
|
|
|
|
componentDidMount(): void {
|
|
|
|
this.props.switchToListMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
onChangeToListMode = (event: MouseEvent<HTMLAnchorElement>) => {
|
|
|
|
event.preventDefault();
|
|
|
|
this.props.switchToListMode();
|
|
|
|
};
|
|
|
|
|
|
|
|
onEditVariable = (identifier: VariableIdentifier) => {
|
|
|
|
this.props.switchToEditMode(identifier);
|
|
|
|
};
|
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
onNewVariable = (event: MouseEvent) => {
|
2020-03-10 02:53:41 -05:00
|
|
|
event.preventDefault();
|
|
|
|
this.props.switchToNewMode();
|
|
|
|
};
|
|
|
|
|
|
|
|
onChangeVariableOrder = (identifier: VariableIdentifier, fromIndex: number, toIndex: number) => {
|
|
|
|
this.props.changeVariableOrder(toVariablePayload(identifier, { fromIndex, toIndex }));
|
|
|
|
};
|
|
|
|
|
|
|
|
onDuplicateVariable = (identifier: VariableIdentifier) => {
|
2020-03-23 07:45:08 -05:00
|
|
|
this.props.duplicateVariable(toVariablePayload(identifier, { newId: (undefined as unknown) as string }));
|
2020-03-10 02:53:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
onRemoveVariable = (identifier: VariableIdentifier) => {
|
|
|
|
this.props.removeVariable(toVariablePayload(identifier, { reIndex: true }));
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2021-01-20 00:59:48 -06:00
|
|
|
const variableToEdit = this.props.variables.find((s) => s.id === this.props.idInEditor) ?? null;
|
2020-03-10 02:53:41 -05:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="page-action-bar">
|
|
|
|
<h3 className="dashboard-settings__header">
|
|
|
|
<a
|
|
|
|
onClick={this.onChangeToListMode}
|
2020-04-27 02:09:05 -05:00
|
|
|
aria-label={selectors.pages.Dashboard.Settings.Variables.Edit.General.headerLink}
|
2020-03-10 02:53:41 -05:00
|
|
|
>
|
|
|
|
Variables
|
|
|
|
</a>
|
2020-11-20 03:51:32 -06:00
|
|
|
{this.props.idInEditor && (
|
2020-03-10 02:53:41 -05:00
|
|
|
<span>
|
2020-04-12 15:20:02 -05:00
|
|
|
<Icon
|
|
|
|
name="angle-right"
|
2020-04-27 02:09:05 -05:00
|
|
|
aria-label={selectors.pages.Dashboard.Settings.Variables.Edit.General.modeLabelEdit}
|
2020-03-10 02:53:41 -05:00
|
|
|
/>
|
|
|
|
Edit
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</h3>
|
|
|
|
|
|
|
|
<div className="page-action-bar__spacer" />
|
|
|
|
{this.props.variables.length > 0 && variableToEdit === null && (
|
2020-11-05 02:53:27 -06:00
|
|
|
<>
|
|
|
|
<VariablesDependenciesButton variables={this.props.variables} />
|
|
|
|
<a
|
|
|
|
type="button"
|
|
|
|
className="btn btn-primary"
|
|
|
|
onClick={this.onNewVariable}
|
|
|
|
aria-label={selectors.pages.Dashboard.Settings.Variables.List.newButton}
|
|
|
|
>
|
|
|
|
New
|
|
|
|
</a>
|
|
|
|
</>
|
2020-03-10 02:53:41 -05:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{!variableToEdit && (
|
2020-11-05 02:53:27 -06:00
|
|
|
<>
|
|
|
|
<VariableEditorList
|
|
|
|
dashboard={this.props.dashboard}
|
|
|
|
variables={this.props.variables}
|
|
|
|
onAddClick={this.onNewVariable}
|
|
|
|
onEditClick={this.onEditVariable}
|
|
|
|
onChangeVariableOrder={this.onChangeVariableOrder}
|
|
|
|
onDuplicateVariable={this.onDuplicateVariable}
|
|
|
|
onRemoveVariable={this.onRemoveVariable}
|
|
|
|
/>
|
|
|
|
<VariablesUnknownTable dashboard={this.props.dashboard} variables={this.props.variables} />
|
|
|
|
</>
|
2020-03-10 02:53:41 -05:00
|
|
|
)}
|
|
|
|
{variableToEdit && <VariableEditorEditor identifier={toVariableIdentifier(variableToEdit)} />}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 23:21:35 -06:00
|
|
|
export const VariableEditorContainer = connector(VariableEditorContainerUnconnected);
|