2019-01-14 08:44:58 -06:00
|
|
|
// Libraries
|
|
|
|
import React, { PureComponent } from 'react';
|
2019-01-16 07:00:29 -06:00
|
|
|
import classNames from 'classnames';
|
2019-01-17 03:26:08 -06:00
|
|
|
import _ from 'lodash';
|
2019-01-14 08:44:58 -06:00
|
|
|
|
|
|
|
// Utils & Services
|
2019-01-15 04:40:12 -06:00
|
|
|
import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2019-01-14 08:44:58 -06:00
|
|
|
import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
|
2019-01-15 04:40:12 -06:00
|
|
|
import { Emitter } from 'app/core/utils/emitter';
|
2019-01-14 08:44:58 -06:00
|
|
|
|
|
|
|
// Types
|
|
|
|
import { PanelModel } from '../panel_model';
|
2019-01-17 10:59:47 -06:00
|
|
|
import { DataQuery, DataSourceApi } from '@grafana/ui';
|
2019-01-14 08:44:58 -06:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
panel: PanelModel;
|
2019-01-15 04:40:12 -06:00
|
|
|
query: DataQuery;
|
|
|
|
onAddQuery: (query?: DataQuery) => void;
|
|
|
|
onRemoveQuery: (query: DataQuery) => void;
|
|
|
|
onMoveQuery: (query: DataQuery, direction: number) => void;
|
2019-01-21 11:54:57 -06:00
|
|
|
dataSourceValue: string | null;
|
2019-01-17 03:26:08 -06:00
|
|
|
inMixedMode: boolean;
|
2019-01-14 08:44:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2019-01-21 11:54:57 -06:00
|
|
|
loadedDataSourceValue: string | null | undefined;
|
2019-01-15 04:40:12 -06:00
|
|
|
datasource: DataSourceApi | null;
|
2019-01-16 07:00:29 -06:00
|
|
|
isCollapsed: boolean;
|
2019-01-16 10:53:40 -06:00
|
|
|
angularScope: AngularQueryComponentScope | null;
|
2019-01-14 08:44:58 -06:00
|
|
|
}
|
|
|
|
|
2019-01-15 04:40:12 -06:00
|
|
|
export class QueryEditorRow extends PureComponent<Props, State> {
|
|
|
|
element: HTMLElement | null = null;
|
|
|
|
angularQueryEditor: AngularComponent | null = null;
|
2019-01-14 08:44:58 -06:00
|
|
|
|
2019-01-15 04:40:12 -06:00
|
|
|
state: State = {
|
|
|
|
datasource: null,
|
2019-01-16 07:00:29 -06:00
|
|
|
isCollapsed: false,
|
2019-01-16 10:53:40 -06:00
|
|
|
angularScope: null,
|
2019-01-21 11:54:57 -06:00
|
|
|
loadedDataSourceValue: undefined,
|
2019-01-15 04:40:12 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.loadDatasource();
|
|
|
|
}
|
|
|
|
|
|
|
|
getAngularQueryComponentScope(): AngularQueryComponentScope {
|
2019-01-17 06:27:01 -06:00
|
|
|
const { panel, query } = this.props;
|
2019-01-15 04:40:12 -06:00
|
|
|
const { datasource } = this.state;
|
|
|
|
|
|
|
|
return {
|
|
|
|
datasource: datasource,
|
|
|
|
target: query,
|
|
|
|
panel: panel,
|
|
|
|
refresh: () => panel.refresh(),
|
2019-01-18 04:20:55 -06:00
|
|
|
render: () => panel.render(),
|
2019-01-15 04:40:12 -06:00
|
|
|
events: panel.events,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadDatasource() {
|
|
|
|
const { query, panel } = this.props;
|
|
|
|
const dataSourceSrv = getDatasourceSrv();
|
|
|
|
const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
|
|
|
|
|
2019-01-21 11:54:57 -06:00
|
|
|
this.setState({ datasource, loadedDataSourceValue: this.props.dataSourceValue });
|
2019-01-15 04:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2019-01-21 11:54:57 -06:00
|
|
|
const { loadedDataSourceValue } = this.state;
|
2019-01-15 04:40:12 -06:00
|
|
|
|
|
|
|
// check if we need to load another datasource
|
2019-01-21 11:54:57 -06:00
|
|
|
if (loadedDataSourceValue !== this.props.dataSourceValue) {
|
2019-01-15 04:40:12 -06:00
|
|
|
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);
|
2019-01-16 10:53:40 -06:00
|
|
|
|
|
|
|
// give angular time to compile
|
|
|
|
setTimeout(() => {
|
|
|
|
this.setState({ angularScope: scopeProps.ctrl });
|
|
|
|
}, 10);
|
2019-01-15 04:40:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.angularQueryEditor) {
|
|
|
|
this.angularQueryEditor.destroy();
|
|
|
|
}
|
2019-01-14 08:44:58 -06:00
|
|
|
}
|
|
|
|
|
2019-01-16 07:00:29 -06:00
|
|
|
onToggleCollapse = () => {
|
|
|
|
this.setState({ isCollapsed: !this.state.isCollapsed });
|
|
|
|
};
|
|
|
|
|
2019-01-17 13:02:43 -06:00
|
|
|
onQueryChange = (query: DataQuery) => {
|
|
|
|
Object.assign(this.props.query, query);
|
|
|
|
};
|
|
|
|
|
2019-01-23 10:44:22 -06:00
|
|
|
onRunQuery = () => {
|
2019-01-17 13:02:43 -06:00
|
|
|
this.props.panel.refresh();
|
|
|
|
};
|
|
|
|
|
2019-01-16 10:53:40 -06:00
|
|
|
renderPluginEditor() {
|
2019-01-17 13:02:43 -06:00
|
|
|
const { query } = this.props;
|
2019-01-16 10:53:40 -06:00
|
|
|
const { datasource } = this.state;
|
|
|
|
|
|
|
|
if (datasource.pluginExports.QueryCtrl) {
|
2019-01-17 06:08:20 -06:00
|
|
|
return <div ref={element => (this.element = element)} />;
|
2019-01-16 10:53:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (datasource.pluginExports.QueryEditor) {
|
|
|
|
const QueryEditor = datasource.pluginExports.QueryEditor;
|
2019-01-17 13:02:43 -06:00
|
|
|
return (
|
|
|
|
<QueryEditor
|
|
|
|
query={query}
|
|
|
|
datasource={datasource}
|
2019-01-23 10:44:22 -06:00
|
|
|
onChange={this.onQueryChange}
|
|
|
|
onRunQuery={this.onRunQuery}
|
2019-01-17 13:02:43 -06:00
|
|
|
/>
|
|
|
|
);
|
2019-01-16 10:53:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return <div>Data source plugin does not export any Query Editor component</div>;
|
|
|
|
}
|
|
|
|
|
|
|
|
onToggleEditMode = () => {
|
|
|
|
const { angularScope } = this.state;
|
|
|
|
|
|
|
|
if (angularScope && angularScope.toggleEditorMode) {
|
|
|
|
angularScope.toggleEditorMode();
|
|
|
|
this.angularQueryEditor.digest();
|
|
|
|
}
|
2019-01-17 06:15:25 -06:00
|
|
|
|
|
|
|
if (this.state.isCollapsed) {
|
|
|
|
this.setState({ isCollapsed: false });
|
|
|
|
}
|
2019-01-17 06:08:20 -06:00
|
|
|
};
|
2019-01-16 10:53:40 -06:00
|
|
|
|
|
|
|
get hasTextEditMode() {
|
|
|
|
const { angularScope } = this.state;
|
|
|
|
return angularScope && angularScope.toggleEditorMode;
|
|
|
|
}
|
|
|
|
|
2019-01-17 03:26:08 -06: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;
|
2019-01-23 12:34:56 -06:00
|
|
|
this.onExecuteQuery();
|
2019-01-17 03:26:08 -06:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2019-01-17 06:08:20 -06:00
|
|
|
renderCollapsedText(): string | null {
|
|
|
|
const { angularScope } = this.state;
|
|
|
|
|
|
|
|
if (angularScope && angularScope.getCollapsedText) {
|
|
|
|
return angularScope.getCollapsedText();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-14 08:44:58 -06:00
|
|
|
render() {
|
2019-01-21 11:54:57 -06:00
|
|
|
const { query, inMixedMode } = this.props;
|
2019-01-17 03:26:08 -06:00
|
|
|
const { datasource, isCollapsed } = 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,
|
|
|
|
});
|
2019-01-15 04:40:12 -06:00
|
|
|
|
|
|
|
if (!datasource) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-01-14 08:44:58 -06:00
|
|
|
|
2019-01-16 10:53:40 -06:00
|
|
|
return (
|
2019-01-17 03:26:08 -06:00
|
|
|
<div className={rowClasses}>
|
|
|
|
<div className="query-editor-row__header">
|
|
|
|
<div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
|
2019-01-16 10:53:40 -06:00
|
|
|
{isCollapsed && <i className="fa fa-caret-right" />}
|
|
|
|
{!isCollapsed && <i className="fa fa-caret-down" />}
|
|
|
|
<span>{query.refId}</span>
|
2019-01-21 11:54:57 -06:00
|
|
|
{inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
|
2019-01-17 03:26:08 -06:00
|
|
|
{isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
|
2019-01-16 07:00:29 -06:00
|
|
|
</div>
|
2019-01-19 01:22:56 -06:00
|
|
|
<div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
|
2019-01-17 06:08:20 -06:00
|
|
|
{isCollapsed && <div>{this.renderCollapsedText()}</div>}
|
|
|
|
</div>
|
2019-01-17 03:26:08 -06:00
|
|
|
<div className="query-editor-row__actions">
|
2019-01-16 10:53:40 -06:00
|
|
|
{this.hasTextEditMode && (
|
2019-01-17 06:08:20 -06:00
|
|
|
<button
|
|
|
|
className="query-editor-row__action"
|
|
|
|
onClick={this.onToggleEditMode}
|
|
|
|
title="Toggle text edit mode"
|
|
|
|
>
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-pencil" />
|
|
|
|
</button>
|
|
|
|
)}
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-arrow-down" />
|
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-arrow-up" />
|
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-copy" />
|
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<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-16 10:53:40 -06:00
|
|
|
</button>
|
2019-01-17 03:26:08 -06:00
|
|
|
<button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
|
2019-01-16 10:53:40 -06:00
|
|
|
<i className="fa fa-fw fa-trash" />
|
|
|
|
</button>
|
2019-01-16 07:00:29 -06:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-16 10:53:40 -06:00
|
|
|
<div className={bodyClasses}>{this.renderPluginEditor()}</div>
|
|
|
|
</div>
|
|
|
|
);
|
2019-01-14 08:44:58 -06:00
|
|
|
}
|
|
|
|
}
|
2019-01-15 04:40:12 -06:00
|
|
|
|
|
|
|
export interface AngularQueryComponentScope {
|
|
|
|
target: DataQuery;
|
|
|
|
panel: PanelModel;
|
|
|
|
events: Emitter;
|
|
|
|
refresh: () => void;
|
|
|
|
render: () => void;
|
|
|
|
datasource: DataSourceApi;
|
2019-01-16 10:53:40 -06:00
|
|
|
toggleEditorMode?: () => void;
|
2019-01-17 06:08:20 -06:00
|
|
|
getCollapsedText?: () => string;
|
2019-01-15 04:40:12 -06:00
|
|
|
}
|