VariableQueryComponentLoader.tsx 1.1 KB

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