mirror of https://github.com/grafana/grafana
parent
0792c182cc
commit
637b91ab8d
@ -0,0 +1,41 @@ |
|||||||
|
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'; |
||||||
|
import DefaultTemplateQueryCtrl from '../templating/defaultTemplateQueryCtrl'; |
||||||
|
|
||||||
|
function WrapInProvider(Component, props) { |
||||||
|
return ( |
||||||
|
<Provider> |
||||||
|
<Component {...props} /> |
||||||
|
</Provider> |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
async function loadComponent(module) { |
||||||
|
const component = await importPluginModule(module); |
||||||
|
if (!component.TemplateQueryCtrl) { |
||||||
|
return DefaultTemplateQueryCtrl; |
||||||
|
} else { |
||||||
|
return component.TemplateQueryCtrl; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** @ngInject */ |
||||||
|
function pluginTemplateQueryComponentLoader(datasourceSrv) { |
||||||
|
return { |
||||||
|
restrict: 'E', |
||||||
|
link: async (scope, elem) => { |
||||||
|
const component = await loadComponent(scope.currentDatasource.meta.module); |
||||||
|
const props = { datasourceSrv, query: scope.current.query, isValid: scope.current.isValid }; |
||||||
|
ReactDOM.render(WrapInProvider(component, props), elem[0]); |
||||||
|
scope.$on('$destroy', () => { |
||||||
|
ReactDOM.unmountComponentAtNode(elem[0]); |
||||||
|
}); |
||||||
|
}, |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
coreModule.directive('pluginTemplateQueryComponent', pluginTemplateQueryComponentLoader); |
@ -1,47 +0,0 @@ |
|||||||
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); |
|
@ -0,0 +1,32 @@ |
|||||||
|
import React, { PureComponent } from 'react'; |
||||||
|
|
||||||
|
interface Props { |
||||||
|
query: string; |
||||||
|
} |
||||||
|
|
||||||
|
export default class DefaultTemplateQueryCtrl extends PureComponent<Props> { |
||||||
|
constructor(props) { |
||||||
|
super(props); |
||||||
|
} |
||||||
|
|
||||||
|
componentDidMount() { |
||||||
|
console.log('componentDidMount'); |
||||||
|
} |
||||||
|
|
||||||
|
render() { |
||||||
|
return ( |
||||||
|
<div className="gf-form"> |
||||||
|
<span className="gf-form-label width-7">Query</span> |
||||||
|
<input |
||||||
|
type="text" |
||||||
|
className="gf-form-input" |
||||||
|
ng-model="current.query" |
||||||
|
placeholder="metric name or tags query" |
||||||
|
ng-model-onblur |
||||||
|
ng-change="runQuery()" |
||||||
|
required |
||||||
|
/> |
||||||
|
</div> |
||||||
|
); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue