plugin_react_component.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import _ from 'lodash';
  2. import coreModule from 'app/core/core_module';
  3. import { importPluginModule } from './plugin_loader';
  4. import React from 'react';
  5. import ReactDOM from 'react-dom';
  6. import { Provider } from 'react-redux';
  7. function WrapInProvider(Component, props) {
  8. return (
  9. <Provider>
  10. <Component {...props} />
  11. </Provider>
  12. );
  13. }
  14. /** @ngInject */
  15. function pluginReactDirectiveLoader($compile, datasourceSrv, $rootScope, $q, $http, $templateCache, $timeout) {
  16. async function getModule(scope, attrs) {
  17. switch (attrs.type) {
  18. case 'template-query-ctrl': {
  19. const dsModule = await importPluginModule(scope.currentDatasource.meta.module);
  20. console.log(dsModule);
  21. return dsModule.TemplateQueryCtrl;
  22. }
  23. default: {
  24. return $q.reject({
  25. message: 'Could not find component type: ' + attrs.type,
  26. });
  27. }
  28. }
  29. }
  30. return {
  31. restrict: 'E',
  32. link: async (scope, elem, attrs) => {
  33. const component = await getModule(scope, attrs);
  34. const props = { datasourceSrv };
  35. ReactDOM.render(WrapInProvider(component, props), elem[0]);
  36. scope.$on('$destroy', () => {
  37. ReactDOM.unmountComponentAtNode(elem[0]);
  38. });
  39. },
  40. };
  41. }
  42. coreModule.directive('pluginReactComponent', pluginReactDirectiveLoader);