2017-12-21 11:56:45 +01:00
|
|
|
import React from 'react';
|
|
|
|
|
import ReactDOM from 'react-dom';
|
2018-09-14 09:41:37 +02:00
|
|
|
import { Provider } from 'react-redux';
|
2018-04-26 11:58:42 +02:00
|
|
|
|
2017-12-21 11:56:45 +01:00
|
|
|
import coreModule from 'app/core/core_module';
|
2018-11-05 16:54:48 +01:00
|
|
|
import { store } from 'app/store/store';
|
2018-04-26 11:58:42 +02:00
|
|
|
import { BackendSrv } from 'app/core/services/backend_srv';
|
|
|
|
|
import { DatasourceSrv } from 'app/features/plugins/datasource_srv';
|
2018-05-30 13:13:29 +02:00
|
|
|
import { ContextSrv } from 'app/core/services/context_srv';
|
2017-12-21 11:56:45 +01:00
|
|
|
|
|
|
|
|
function WrapInProvider(store, Component, props) {
|
|
|
|
|
return (
|
2018-09-14 09:41:37 +02:00
|
|
|
<Provider store={store}>
|
|
|
|
|
<Component {...props} />
|
|
|
|
|
</Provider>
|
2017-12-21 11:56:45 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @ngInject */
|
2018-05-30 13:13:29 +02:00
|
|
|
export function reactContainer(
|
|
|
|
|
$route,
|
|
|
|
|
$location,
|
|
|
|
|
backendSrv: BackendSrv,
|
|
|
|
|
datasourceSrv: DatasourceSrv,
|
|
|
|
|
contextSrv: ContextSrv
|
|
|
|
|
) {
|
2017-12-21 11:56:45 +01:00
|
|
|
return {
|
|
|
|
|
restrict: 'E',
|
2017-12-27 13:15:27 +01:00
|
|
|
template: '',
|
2017-12-21 11:56:45 +01:00
|
|
|
link(scope, elem) {
|
2018-05-30 13:13:29 +02:00
|
|
|
// Check permissions for this component
|
|
|
|
|
const { roles } = $route.current.locals;
|
|
|
|
|
if (roles && roles.length) {
|
|
|
|
|
if (!roles.some(r => contextSrv.hasRole(r))) {
|
|
|
|
|
$location.url('/');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let { component } = $route.current.locals;
|
2018-04-27 11:49:11 +02:00
|
|
|
// Dynamic imports return whole module, need to extract default export
|
|
|
|
|
if (component.default) {
|
|
|
|
|
component = component.default;
|
|
|
|
|
}
|
2018-05-30 13:13:29 +02:00
|
|
|
|
2018-04-27 11:49:11 +02:00
|
|
|
const props = {
|
2018-01-17 16:11:34 +01:00
|
|
|
backendSrv: backendSrv,
|
2018-04-26 11:58:42 +02:00
|
|
|
datasourceSrv: datasourceSrv,
|
2018-04-27 18:21:20 +02:00
|
|
|
routeParams: $route.current.params,
|
2018-01-17 16:11:34 +01:00
|
|
|
};
|
2017-12-21 11:56:45 +01:00
|
|
|
|
|
|
|
|
ReactDOM.render(WrapInProvider(store, component, props), elem[0]);
|
|
|
|
|
|
2018-09-05 07:47:30 +02:00
|
|
|
scope.$on('$destroy', () => {
|
2017-12-21 11:56:45 +01:00
|
|
|
ReactDOM.unmountComponentAtNode(elem[0]);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
coreModule.directive('reactContainer', reactContainer);
|