QueryEditorRow.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import classNames from 'classnames';
  4. import _ from 'lodash';
  5. // Utils & Services
  6. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  7. import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
  8. import { Emitter } from 'app/core/utils/emitter';
  9. import { getTimeSrv } from 'app/features/dashboard/services/TimeSrv';
  10. // Types
  11. import { PanelModel } from '../state/PanelModel';
  12. import { DataQuery, DataSourceApi, TimeRange } from '@grafana/ui';
  13. interface Props {
  14. panel: PanelModel;
  15. query: DataQuery;
  16. onAddQuery: (query?: DataQuery) => void;
  17. onRemoveQuery: (query: DataQuery) => void;
  18. onMoveQuery: (query: DataQuery, direction: number) => void;
  19. onChange: (query: DataQuery) => void;
  20. dataSourceValue: string | null;
  21. inMixedMode: boolean;
  22. }
  23. interface State {
  24. loadedDataSourceValue: string | null | undefined;
  25. datasource: DataSourceApi | null;
  26. isCollapsed: boolean;
  27. angularScope: AngularQueryComponentScope | null;
  28. }
  29. export class QueryEditorRow extends PureComponent<Props, State> {
  30. element: HTMLElement | null = null;
  31. angularQueryEditor: AngularComponent | null = null;
  32. state: State = {
  33. datasource: null,
  34. isCollapsed: false,
  35. angularScope: null,
  36. loadedDataSourceValue: undefined,
  37. };
  38. componentDidMount() {
  39. this.loadDatasource();
  40. this.props.panel.events.on('refresh', this.onPanelRefresh);
  41. }
  42. onPanelRefresh = () => {
  43. if (this.state.angularScope) {
  44. this.state.angularScope.range = getTimeSrv().timeRange();
  45. }
  46. };
  47. getAngularQueryComponentScope(): AngularQueryComponentScope {
  48. const { panel, query } = this.props;
  49. const { datasource } = this.state;
  50. return {
  51. datasource: datasource,
  52. target: query,
  53. panel: panel,
  54. refresh: () => panel.refresh(),
  55. render: () => panel.render(),
  56. events: panel.events,
  57. range: getTimeSrv().timeRange(),
  58. };
  59. }
  60. async loadDatasource() {
  61. const { query, panel } = this.props;
  62. const dataSourceSrv = getDatasourceSrv();
  63. const datasource = await dataSourceSrv.get(query.datasource || panel.datasource);
  64. this.setState({ datasource, loadedDataSourceValue: this.props.dataSourceValue });
  65. }
  66. componentDidUpdate() {
  67. const { loadedDataSourceValue } = this.state;
  68. // check if we need to load another datasource
  69. if (loadedDataSourceValue !== this.props.dataSourceValue) {
  70. if (this.angularQueryEditor) {
  71. this.angularQueryEditor.destroy();
  72. this.angularQueryEditor = null;
  73. }
  74. this.loadDatasource();
  75. return;
  76. }
  77. if (!this.element || this.angularQueryEditor) {
  78. return;
  79. }
  80. const loader = getAngularLoader();
  81. const template = '<plugin-component type="query-ctrl" />';
  82. const scopeProps = { ctrl: this.getAngularQueryComponentScope() };
  83. this.angularQueryEditor = loader.load(this.element, scopeProps, template);
  84. // give angular time to compile
  85. setTimeout(() => {
  86. this.setState({ angularScope: scopeProps.ctrl });
  87. }, 10);
  88. }
  89. componentWillUnmount() {
  90. this.props.panel.events.off('refresh', this.onPanelRefresh);
  91. if (this.angularQueryEditor) {
  92. this.angularQueryEditor.destroy();
  93. }
  94. }
  95. onToggleCollapse = () => {
  96. this.setState({ isCollapsed: !this.state.isCollapsed });
  97. };
  98. onRunQuery = () => {
  99. this.props.panel.refresh();
  100. };
  101. renderPluginEditor() {
  102. const { query, onChange } = this.props;
  103. const { datasource } = this.state;
  104. if (datasource.pluginExports.QueryCtrl) {
  105. return <div ref={element => (this.element = element)} />;
  106. }
  107. if (datasource.pluginExports.QueryEditor) {
  108. const QueryEditor = datasource.pluginExports.QueryEditor;
  109. return <QueryEditor query={query} datasource={datasource} onChange={onChange} onRunQuery={this.onRunQuery} />;
  110. }
  111. return <div>Data source plugin does not export any Query Editor component</div>;
  112. }
  113. onToggleEditMode = () => {
  114. const { angularScope } = this.state;
  115. if (angularScope && angularScope.toggleEditorMode) {
  116. angularScope.toggleEditorMode();
  117. this.angularQueryEditor.digest();
  118. }
  119. if (this.state.isCollapsed) {
  120. this.setState({ isCollapsed: false });
  121. }
  122. };
  123. get hasTextEditMode() {
  124. const { angularScope } = this.state;
  125. return angularScope && angularScope.toggleEditorMode;
  126. }
  127. onRemoveQuery = () => {
  128. this.props.onRemoveQuery(this.props.query);
  129. };
  130. onCopyQuery = () => {
  131. const copy = _.cloneDeep(this.props.query);
  132. this.props.onAddQuery(copy);
  133. };
  134. onDisableQuery = () => {
  135. this.props.query.hide = !this.props.query.hide;
  136. this.onRunQuery();
  137. this.forceUpdate();
  138. };
  139. renderCollapsedText(): string | null {
  140. const { angularScope } = this.state;
  141. if (angularScope && angularScope.getCollapsedText) {
  142. return angularScope.getCollapsedText();
  143. }
  144. return null;
  145. }
  146. render() {
  147. const { query, inMixedMode } = this.props;
  148. const { datasource, isCollapsed } = this.state;
  149. const isDisabled = query.hide;
  150. const bodyClasses = classNames('query-editor-row__body gf-form-query', {
  151. 'query-editor-row__body--collapsed': isCollapsed,
  152. });
  153. const rowClasses = classNames('query-editor-row', {
  154. 'query-editor-row--disabled': isDisabled,
  155. 'gf-form-disabled': isDisabled,
  156. });
  157. if (!datasource) {
  158. return null;
  159. }
  160. return (
  161. <div className={rowClasses}>
  162. <div className="query-editor-row__header">
  163. <div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
  164. {isCollapsed && <i className="fa fa-caret-right" />}
  165. {!isCollapsed && <i className="fa fa-caret-down" />}
  166. <span>{query.refId}</span>
  167. {inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
  168. {isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
  169. </div>
  170. <div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
  171. {isCollapsed && <div>{this.renderCollapsedText()}</div>}
  172. </div>
  173. <div className="query-editor-row__actions">
  174. {this.hasTextEditMode && (
  175. <button
  176. className="query-editor-row__action"
  177. onClick={this.onToggleEditMode}
  178. title="Toggle text edit mode"
  179. >
  180. <i className="fa fa-fw fa-pencil" />
  181. </button>
  182. )}
  183. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
  184. <i className="fa fa-fw fa-arrow-down" />
  185. </button>
  186. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
  187. <i className="fa fa-fw fa-arrow-up" />
  188. </button>
  189. <button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
  190. <i className="fa fa-fw fa-copy" />
  191. </button>
  192. <button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
  193. {isDisabled && <i className="fa fa-fw fa-eye-slash" />}
  194. {!isDisabled && <i className="fa fa-fw fa-eye" />}
  195. </button>
  196. <button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
  197. <i className="fa fa-fw fa-trash" />
  198. </button>
  199. </div>
  200. </div>
  201. <div className={bodyClasses}>{this.renderPluginEditor()}</div>
  202. </div>
  203. );
  204. }
  205. }
  206. export interface AngularQueryComponentScope {
  207. target: DataQuery;
  208. panel: PanelModel;
  209. events: Emitter;
  210. refresh: () => void;
  211. render: () => void;
  212. datasource: DataSourceApi;
  213. toggleEditorMode?: () => void;
  214. getCollapsedText?: () => string;
  215. range: TimeRange;
  216. }