QueryEditorRow.tsx 8.5 KB

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