QueryEditorRow.tsx 7.4 KB

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