QueryEditorRow.tsx 7.0 KB

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