grafana/public/app/features/dashboard/panel_editor/QueryEditorRow.tsx

276 lines
8.5 KiB
TypeScript
Raw Normal View History

2019-01-14 22:44:58 +08:00
// Libraries
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import _ from 'lodash';
2019-01-14 22:44:58 +08:00
// Utils & Services
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
2019-01-14 22:44:58 +08:00
import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
import { Emitter } from 'app/core/utils/emitter';
import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
2019-01-14 22:44:58 +08:00
// Types
import { PanelModel } from '../state/PanelModel';
import { DataQuery, DataSourceApi, TimeRange } from '@grafana/ui';
2019-01-14 22:44:58 +08:00
interface Props {
panel: PanelModel;
query: DataQuery;
onAddQuery: (query?: DataQuery) => void;
onRemoveQuery: (query: DataQuery) => void;
onMoveQuery: (query: DataQuery, direction: number) => void;
onChange: (query: DataQuery) => void;
2019-01-22 01:54:57 +08:00
dataSourceValue: string | null;
inMixedMode: boolean;
2019-01-14 22:44:58 +08:00
}
interface State {
2019-01-22 01:54:57 +08:00
loadedDataSourceValue: string | null | undefined;
datasource: DataSourceApi | null;
isCollapsed: boolean;
hasTextEditMode: boolean;
2019-01-14 22:44:58 +08:00
}
export class QueryEditorRow extends PureComponent<Props, State> {
element: HTMLElement | null = null;
angularScope: AngularQueryComponentScope | null;
angularQueryEditor: AngularComponent | null = null;
2019-01-14 22:44:58 +08:00
state: State = {
datasource: null,
isCollapsed: false,
2019-01-22 01:54:57 +08:00
loadedDataSourceValue: undefined,
hasTextEditMode: false,
};
componentDidMount() {
this.loadDatasource();
this.props.panel.events.on('refresh', this.onPanelRefresh);
this.props.panel.events.on('data-error', this.onPanelDataError);
this.props.panel.events.on('data-received', this.onPanelDataReceived);
}
componentWillUnmount() {
this.props.panel.events.off('refresh', this.onPanelRefresh);
this.props.panel.events.off('data-error', this.onPanelDataError);
this.props.panel.events.off('data-received', this.onPanelDataReceived);
if (this.angularQueryEditor) {
this.angularQueryEditor.destroy();
}
}
onPanelDataError = () => {
// Some query controllers listen to data error events and need a digest
if (this.angularQueryEditor) {
// for some reason this needs to be done in next tick
setTimeout(this.angularQueryEditor.digest);
}
};
onPanelDataReceived = () => {
// Some query controllers listen to data error events and need a digest
if (this.angularQueryEditor) {
// for some reason this needs to be done in next tick
setTimeout(this.angularQueryEditor.digest);
}
};
onPanelRefresh = () => {
if (this.angularScope) {
this.angularScope.range = getTimeSrv().timeRange();
}
};
getAngularQueryComponentScope(): AngularQueryComponentScope {
const { panel, query } = this.props;
const { datasource } = this.state;
return {
datasource: datasource,
target: query,
panel: panel,
refresh: () => panel.refresh(),
render: () => panel.render(),
events: panel.events,
range: getTimeSrv().timeRange(),
};
}
async loadDatasource() {
const { query, panel } = this.props;
const dataSourceSrv = getDatasourceSrv();
const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
this.setState({
datasource,
loadedDataSourceValue: this.props.dataSourceValue,
hasTextEditMode: false,
});
}
componentDidUpdate() {
2019-01-22 01:54:57 +08:00
const { loadedDataSourceValue } = this.state;
// check if we need to load another datasource
2019-01-22 01:54:57 +08:00
if (loadedDataSourceValue !== this.props.dataSourceValue) {
if (this.angularQueryEditor) {
this.angularQueryEditor.destroy();
this.angularQueryEditor = null;
}
this.loadDatasource();
return;
}
if (!this.element || this.angularQueryEditor) {
return;
}
const loader = getAngularLoader();
const template = '<plugin-component type="query-ctrl" />';
const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
this.angularQueryEditor = loader.load(this.element, scopeProps, template);
this.angularScope = scopeProps.ctrl;
2019-01-17 00:53:40 +08:00
// give angular time to compile
setTimeout(() => {
this.setState({ hasTextEditMode: !!this.angularScope.toggleEditorMode });
2019-01-17 00:53:40 +08:00
}, 10);
}
onToggleCollapse = () => {
this.setState({ isCollapsed: !this.state.isCollapsed });
};
onRunQuery = () => {
this.props.panel.refresh();
};
2019-01-17 00:53:40 +08:00
renderPluginEditor() {
const { query, onChange } = this.props;
2019-01-17 00:53:40 +08:00
const { datasource } = this.state;
if (datasource.pluginExports.QueryCtrl) {
2019-01-17 20:08:20 +08:00
return <div ref={element => (this.element = element)} />;
2019-01-17 00:53:40 +08:00
}
if (datasource.pluginExports.QueryEditor) {
const QueryEditor = datasource.pluginExports.QueryEditor;
return <QueryEditor query={query} datasource={datasource} onChange={onChange} onRunQuery={this.onRunQuery} />;
2019-01-17 00:53:40 +08:00
}
return <div>Data source plugin does not export any Query Editor component</div>;
}
onToggleEditMode = () => {
if (this.angularScope && this.angularScope.toggleEditorMode) {
this.angularScope.toggleEditorMode();
2019-01-17 00:53:40 +08:00
this.angularQueryEditor.digest();
}
2019-01-17 20:15:25 +08:00
if (this.state.isCollapsed) {
this.setState({ isCollapsed: false });
}
2019-01-17 20:08:20 +08:00
};
2019-01-17 00:53:40 +08:00
onRemoveQuery = () => {
this.props.onRemoveQuery(this.props.query);
};
onCopyQuery = () => {
const copy = _.cloneDeep(this.props.query);
this.props.onAddQuery(copy);
};
onDisableQuery = () => {
this.props.query.hide = !this.props.query.hide;
this.onRunQuery();
this.forceUpdate();
};
2019-01-17 20:08:20 +08:00
renderCollapsedText(): string | null {
if (this.angularScope && this.angularScope.getCollapsedText) {
return this.angularScope.getCollapsedText();
2019-01-17 20:08:20 +08:00
}
return null;
}
2019-01-14 22:44:58 +08:00
render() {
2019-01-22 01:54:57 +08:00
const { query, inMixedMode } = this.props;
const { datasource, isCollapsed, hasTextEditMode } = this.state;
const isDisabled = query.hide;
const bodyClasses = classNames('query-editor-row__body gf-form-query', {
'query-editor-row__body--collapsed': isCollapsed,
});
const rowClasses = classNames('query-editor-row', {
'query-editor-row--disabled': isDisabled,
'gf-form-disabled': isDisabled,
});
if (!datasource) {
return null;
}
2019-01-14 22:44:58 +08:00
2019-01-17 00:53:40 +08:00
return (
<div className={rowClasses}>
<div className="query-editor-row__header">
<div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
2019-01-17 00:53:40 +08:00
{isCollapsed && <i className="fa fa-caret-right" />}
{!isCollapsed && <i className="fa fa-caret-down" />}
<span>{query.refId}</span>
2019-01-22 01:54:57 +08:00
{inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
{isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
</div>
<div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
2019-01-17 20:08:20 +08:00
{isCollapsed && <div>{this.renderCollapsedText()}</div>}
</div>
<div className="query-editor-row__actions">
{hasTextEditMode && (
2019-01-17 20:08:20 +08:00
<button
className="query-editor-row__action"
onClick={this.onToggleEditMode}
title="Toggle text edit mode"
>
2019-01-17 00:53:40 +08:00
<i className="fa fa-fw fa-pencil" />
</button>
)}
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
2019-01-17 00:53:40 +08:00
<i className="fa fa-fw fa-arrow-down" />
</button>
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
2019-01-17 00:53:40 +08:00
<i className="fa fa-fw fa-arrow-up" />
</button>
<button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
2019-01-17 00:53:40 +08:00
<i className="fa fa-fw fa-copy" />
</button>
<button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
{isDisabled && <i className="fa fa-fw fa-eye-slash" />}
{!isDisabled && <i className="fa fa-fw fa-eye" />}
2019-01-17 00:53:40 +08:00
</button>
<button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
2019-01-17 00:53:40 +08:00
<i className="fa fa-fw fa-trash" />
</button>
</div>
</div>
2019-01-17 00:53:40 +08:00
<div className={bodyClasses}>{this.renderPluginEditor()}</div>
</div>
);
2019-01-14 22:44:58 +08:00
}
}
export interface AngularQueryComponentScope {
target: DataQuery;
panel: PanelModel;
events: Emitter;
refresh: () => void;
render: () => void;
datasource: DataSourceApi;
2019-01-17 00:53:40 +08:00
toggleEditorMode?: () => void;
2019-01-17 20:08:20 +08:00
getCollapsedText?: () => string;
range: TimeRange;
}