GrafanaRoute.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React from 'react';
  2. // @ts-ignore
  3. import Drop from 'tether-drop';
  4. import { locationSearchToObject, navigationLogger, reportPageview } from '@grafana/runtime';
  5. import { keybindingSrv } from '../services/keybindingSrv';
  6. import { GrafanaRouteComponentProps } from './types';
  7. export interface Props extends Omit<GrafanaRouteComponentProps, 'queryParams'> {}
  8. export class GrafanaRoute extends React.Component<Props> {
  9. componentDidMount() {
  10. this.updateBodyClassNames();
  11. this.cleanupDOM();
  12. // unbinds all and re-bind global keybindins
  13. keybindingSrv.reset();
  14. keybindingSrv.initGlobals();
  15. reportPageview();
  16. navigationLogger('GrafanaRoute', false, 'Mounted', this.props.match);
  17. }
  18. componentDidUpdate(prevProps: Props) {
  19. this.cleanupDOM();
  20. reportPageview();
  21. navigationLogger('GrafanaRoute', false, 'Updated', this.props, prevProps);
  22. }
  23. componentWillUnmount() {
  24. this.updateBodyClassNames(true);
  25. navigationLogger('GrafanaRoute', false, 'Unmounted', this.props.route);
  26. }
  27. getPageClasses() {
  28. return this.props.route.pageClass ? this.props.route.pageClass.split(' ') : [];
  29. }
  30. updateBodyClassNames(clear = false) {
  31. for (const cls of this.getPageClasses()) {
  32. if (clear) {
  33. document.body.classList.remove(cls);
  34. } else {
  35. document.body.classList.add(cls);
  36. }
  37. }
  38. }
  39. cleanupDOM() {
  40. document.body.classList.remove('sidemenu-open--xs');
  41. // cleanup tooltips
  42. const tooltipById = document.getElementById('tooltip');
  43. tooltipById?.parentElement?.removeChild(tooltipById);
  44. const tooltipsByClass = document.querySelectorAll('.tooltip');
  45. for (let i = 0; i < tooltipsByClass.length; i++) {
  46. const tooltip = tooltipsByClass[i];
  47. tooltip.parentElement?.removeChild(tooltip);
  48. }
  49. // cleanup tether-drop
  50. for (const drop of Drop.drops) {
  51. drop.destroy();
  52. }
  53. }
  54. render() {
  55. const { props } = this;
  56. navigationLogger('GrafanaRoute', false, 'Rendered', props.route);
  57. const RouteComponent = props.route.component;
  58. return <RouteComponent {...props} queryParams={locationSearchToObject(props.location.search)} />;
  59. }
  60. }