mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
stackdriver: add basic directive for loading react plugin components
This commit is contained in:
parent
16bf269c31
commit
efd3343d19
@ -4,3 +4,4 @@ import './import_list/import_list';
|
||||
import './ds_edit_ctrl';
|
||||
import './datasource_srv';
|
||||
import './plugin_component';
|
||||
import './plugin_react_component';
|
||||
|
47
public/app/features/plugins/plugin_react_component.tsx
Normal file
47
public/app/features/plugins/plugin_react_component.tsx
Normal file
@ -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 (
|
||||
<Provider>
|
||||
<Component {...props} />
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
|
||||
/** @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);
|
Loading…
Reference in New Issue
Block a user