variableQueryEditorLoader.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import coreModule from 'app/core/core_module';
  2. import { importDataSourcePlugin } from './plugin_loader';
  3. import React from 'react';
  4. import ReactDOM from 'react-dom';
  5. import DefaultVariableQueryEditor from '../templating/DefaultVariableQueryEditor';
  6. import { DataSourcePluginMeta } from '@grafana/ui';
  7. import { TemplateSrv } from '../templating/template_srv';
  8. async function loadComponent(meta: DataSourcePluginMeta) {
  9. const dsPlugin = await importDataSourcePlugin(meta);
  10. if (dsPlugin.components.VariableQueryEditor) {
  11. return dsPlugin.components.VariableQueryEditor;
  12. } else {
  13. return DefaultVariableQueryEditor;
  14. }
  15. }
  16. /** @ngInject */
  17. function variableQueryEditorLoader(templateSrv: TemplateSrv) {
  18. return {
  19. restrict: 'E',
  20. link: async (scope: any, elem: JQuery) => {
  21. const Component = await loadComponent(scope.currentDatasource.meta);
  22. const props = {
  23. datasource: scope.currentDatasource,
  24. query: scope.current.query,
  25. onChange: scope.onQueryChange,
  26. templateSrv,
  27. };
  28. ReactDOM.render(<Component {...props} />, elem[0]);
  29. scope.$on('$destroy', () => {
  30. ReactDOM.unmountComponentAtNode(elem[0]);
  31. });
  32. },
  33. };
  34. }
  35. coreModule.directive('variableQueryEditorLoader', variableQueryEditorLoader);