diff --git a/public/app/features/plugins/all.ts b/public/app/features/plugins/all.ts index c9fb250266c..bd94a82b31e 100644 --- a/public/app/features/plugins/all.ts +++ b/public/app/features/plugins/all.ts @@ -4,3 +4,4 @@ import './import_list/import_list'; import './ds_edit_ctrl'; import './datasource_srv'; import './plugin_component'; +import './plugin_react_component'; diff --git a/public/app/features/plugins/plugin_react_component.tsx b/public/app/features/plugins/plugin_react_component.tsx new file mode 100644 index 00000000000..d20679c9681 --- /dev/null +++ b/public/app/features/plugins/plugin_react_component.tsx @@ -0,0 +1,47 @@ +import _ from 'lodash'; +import coreModule from 'app/core/core_module'; +import { importPluginModule } from './plugin_loader'; +import React from 'react'; +import ReactDOM from 'react-dom'; +import { Provider } from 'react-redux'; + +function WrapInProvider(Component, props) { + return ( + + + + ); +} + +/** @ngInject */ +function pluginReactDirectiveLoader($compile, datasourceSrv, $rootScope, $q, $http, $templateCache, $timeout) { + async function getModule(scope, attrs) { + switch (attrs.type) { + case 'template-query-ctrl': { + const dsModule = await importPluginModule(scope.currentDatasource.meta.module); + console.log(dsModule); + return dsModule.TemplateQueryCtrl; + } + default: { + return $q.reject({ + message: 'Could not find component type: ' + attrs.type, + }); + } + } + } + + return { + restrict: 'E', + link: async (scope, elem, attrs) => { + const component = await getModule(scope, attrs); + const props = { datasourceSrv }; + ReactDOM.render(WrapInProvider(component, props), elem[0]); + + scope.$on('$destroy', () => { + ReactDOM.unmountComponentAtNode(elem[0]); + }); + }, + }; +} + +coreModule.directive('pluginReactComponent', pluginReactDirectiveLoader);