QueryEditorRow.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 (
  110. <QueryEditor
  111. query={query}
  112. datasource={datasource}
  113. onChange={onChange}
  114. onRunQuery={this.onRunQuery}
  115. />
  116. );
  117. }
  118. return <div>Data source plugin does not export any Query Editor component</div>;
  119. }
  120. onToggleEditMode = () => {
  121. const { angularScope } = this.state;
  122. if (angularScope && angularScope.toggleEditorMode) {
  123. angularScope.toggleEditorMode();
  124. this.angularQueryEditor.digest();
  125. }
  126. if (this.state.isCollapsed) {
  127. this.setState({ isCollapsed: false });
  128. }
  129. };
  130. get hasTextEditMode() {
  131. const { angularScope } = this.state;
  132. return angularScope && angularScope.toggleEditorMode;
  133. }
  134. onRemoveQuery = () => {
  135. this.props.onRemoveQuery(this.props.query);
  136. };
  137. onCopyQuery = () => {
  138. const copy = _.cloneDeep(this.props.query);
  139. this.props.onAddQuery(copy);
  140. };
  141. onDisableQuery = () => {
  142. this.props.query.hide = !this.props.query.hide;
  143. this.onRunQuery();
  144. this.forceUpdate();
  145. };
  146. renderCollapsedText(): string | null {
  147. const { angularScope } = this.state;
  148. if (angularScope && angularScope.getCollapsedText) {
  149. return angularScope.getCollapsedText();
  150. }
  151. return null;
  152. }
  153. render() {
  154. const { query, inMixedMode } = this.props;
  155. const { datasource, isCollapsed } = this.state;
  156. const isDisabled = query.hide;
  157. const bodyClasses = classNames('query-editor-row__body gf-form-query', {
  158. 'query-editor-row__body--collapsed': isCollapsed,
  159. });
  160. const rowClasses = classNames('query-editor-row', {
  161. 'query-editor-row--disabled': isDisabled,
  162. 'gf-form-disabled': isDisabled,
  163. });
  164. if (!datasource) {
  165. return null;
  166. }
  167. return (
  168. <div className={rowClasses}>
  169. <div className="query-editor-row__header">
  170. <div className="query-editor-row__ref-id" onClick={this.onToggleCollapse}>
  171. {isCollapsed && <i className="fa fa-caret-right" />}
  172. {!isCollapsed && <i className="fa fa-caret-down" />}
  173. <span>{query.refId}</span>
  174. {inMixedMode && <em className="query-editor-row__context-info"> ({datasource.name})</em>}
  175. {isDisabled && <em className="query-editor-row__context-info"> Disabled</em>}
  176. </div>
  177. <div className="query-editor-row__collapsed-text" onClick={this.onToggleEditMode}>
  178. {isCollapsed && <div>{this.renderCollapsedText()}</div>}
  179. </div>
  180. <div className="query-editor-row__actions">
  181. {this.hasTextEditMode && (
  182. <button
  183. className="query-editor-row__action"
  184. onClick={this.onToggleEditMode}
  185. title="Toggle text edit mode"
  186. >
  187. <i className="fa fa-fw fa-pencil" />
  188. </button>
  189. )}
  190. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, 1)}>
  191. <i className="fa fa-fw fa-arrow-down" />
  192. </button>
  193. <button className="query-editor-row__action" onClick={() => this.props.onMoveQuery(query, -1)}>
  194. <i className="fa fa-fw fa-arrow-up" />
  195. </button>
  196. <button className="query-editor-row__action" onClick={this.onCopyQuery} title="Duplicate query">
  197. <i className="fa fa-fw fa-copy" />
  198. </button>
  199. <button className="query-editor-row__action" onClick={this.onDisableQuery} title="Disable/enable query">
  200. {isDisabled && <i className="fa fa-fw fa-eye-slash" />}
  201. {!isDisabled && <i className="fa fa-fw fa-eye" />}
  202. </button>
  203. <button className="query-editor-row__action" onClick={this.onRemoveQuery} title="Remove query">
  204. <i className="fa fa-fw fa-trash" />
  205. </button>
  206. </div>
  207. </div>
  208. <div className={bodyClasses}>{this.renderPluginEditor()}</div>
  209. </div>
  210. );
  211. }
  212. }
  213. export interface AngularQueryComponentScope {
  214. target: DataQuery;
  215. panel: PanelModel;
  216. events: Emitter;
  217. refresh: () => void;
  218. render: () => void;
  219. datasource: DataSourceApi;
  220. toggleEditorMode?: () => void;
  221. getCollapsedText?: () => string;
  222. range: TimeRange;
  223. }