| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- // Libraries
- import React, { PureComponent } from 'react';
- // Utils & Services
- import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
- import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
- import { Emitter } from 'app/core/utils/emitter';
- // Types
- import { PanelModel } from '../panel_model';
- import { DataQuery, DataSourceApi } from 'app/types/series';
- interface Props {
- panel: PanelModel;
- query: DataQuery;
- onAddQuery: (query?: DataQuery) => void;
- onRemoveQuery: (query: DataQuery) => void;
- onMoveQuery: (query: DataQuery, direction: number) => void;
- datasourceName: string | null;
- }
- interface State {
- datasource: DataSourceApi | null;
- }
- export class QueryEditorRow extends PureComponent<Props, State> {
- element: HTMLElement | null = null;
- angularQueryEditor: AngularComponent | null = null;
- state: State = {
- datasource: null,
- };
- componentDidMount() {
- this.loadDatasource();
- }
- getAngularQueryComponentScope(): AngularQueryComponentScope {
- const { panel, onAddQuery, onMoveQuery, onRemoveQuery, query } = this.props;
- const { datasource } = this.state;
- return {
- datasource: datasource,
- target: query,
- panel: panel,
- refresh: () => panel.refresh(),
- render: () => panel.render,
- addQuery: onAddQuery,
- moveQuery: onMoveQuery,
- removeQuery: onRemoveQuery,
- events: panel.events,
- };
- }
- async loadDatasource() {
- const { query, panel } = this.props;
- const dataSourceSrv = getDatasourceSrv();
- const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
- this.setState({ datasource });
- }
- componentDidUpdate() {
- const { datasource } = this.state;
- // check if we need to load another datasource
- if (datasource && datasource.name !== this.props.datasourceName) {
- 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);
- }
- componentWillUnmount() {
- if (this.angularQueryEditor) {
- this.angularQueryEditor.destroy();
- }
- }
- render() {
- const { datasource } = this.state;
- if (!datasource) {
- return null;
- }
- if (datasource.pluginExports.QueryCtrl) {
- return <div ref={element => (this.element = element)} />;
- } else if (datasource.pluginExports.QueryEditor) {
- const QueryEditor = datasource.pluginExports.QueryEditor;
- return <QueryEditor />;
- }
- return <div>Data source plugin does not export any Query Editor component</div>;
- }
- }
- export interface AngularQueryComponentScope {
- target: DataQuery;
- panel: PanelModel;
- events: Emitter;
- refresh: () => void;
- render: () => void;
- removeQuery: (query: DataQuery) => void;
- addQuery: (query?: DataQuery) => void;
- moveQuery: (query: DataQuery, direction: number) => void;
- datasource: DataSourceApi;
- }
|