PanelEditor.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { PureComponent } from 'react';
  2. import classNames from 'classnames';
  3. import { QueriesTab } from './QueriesTab';
  4. import { VisualizationTab } from './VisualizationTab';
  5. import { GeneralTab } from './GeneralTab';
  6. import { AlertTab } from './AlertTab';
  7. import config from 'app/core/config';
  8. import { store } from 'app/store/store';
  9. import { updateLocation } from 'app/core/actions';
  10. import { AngularComponent } from 'app/core/services/AngularLoader';
  11. import { PanelModel } from '../panel_model';
  12. import { DashboardModel } from '../dashboard_model';
  13. import { PanelPlugin } from 'app/types/plugins';
  14. interface PanelEditorProps {
  15. panel: PanelModel;
  16. dashboard: DashboardModel;
  17. plugin: PanelPlugin;
  18. angularPanel?: AngularComponent;
  19. onTypeChanged: (newType: PanelPlugin) => void;
  20. }
  21. interface PanelEditorTab {
  22. id: string;
  23. text: string;
  24. icon: string;
  25. }
  26. export class PanelEditor extends PureComponent<PanelEditorProps> {
  27. constructor(props) {
  28. super(props);
  29. }
  30. onChangeTab = (tab: PanelEditorTab) => {
  31. store.dispatch(
  32. updateLocation({
  33. query: { tab: tab.id },
  34. partial: true,
  35. })
  36. );
  37. this.forceUpdate();
  38. };
  39. renderCurrentTab(activeTab: string) {
  40. const { panel, dashboard, onTypeChanged, plugin, angularPanel } = this.props;
  41. switch (activeTab) {
  42. case 'general':
  43. return <GeneralTab panel={panel} />;
  44. case 'queries':
  45. return <QueriesTab panel={panel} dashboard={dashboard} />;
  46. case 'alert':
  47. return <AlertTab angularPanel={angularPanel} />;
  48. case 'visualization':
  49. return (
  50. <VisualizationTab
  51. panel={panel}
  52. dashboard={dashboard}
  53. plugin={plugin}
  54. onTypeChanged={onTypeChanged}
  55. angularPanel={angularPanel}
  56. />
  57. );
  58. default:
  59. return null;
  60. }
  61. }
  62. render() {
  63. const { plugin } = this.props;
  64. const activeTab = store.getState().location.query.tab || 'queries';
  65. const tabs = [
  66. { id: 'general', text: 'General', icon: 'gicon gicon-preferences' },
  67. { id: 'queries', text: 'Queries', icon: 'fa fa-database' },
  68. { id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
  69. ];
  70. if (config.alertingEnabled && plugin.id === 'graph') {
  71. tabs.push({
  72. id: 'alert',
  73. text: 'Alert',
  74. icon: 'gicon gicon-alert',
  75. });
  76. }
  77. return (
  78. <div className="panel-editor-container__editor">
  79. <div className="panel-editor-resizer">
  80. <div className="panel-editor-resizer__handle">
  81. <div className="panel-editor-resizer__handle-dots" />
  82. </div>
  83. </div>
  84. <div className="panel-editor-tabs">
  85. <ul className="gf-tabs">
  86. {tabs.map(tab => {
  87. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  88. })}
  89. </ul>
  90. </div>
  91. {this.renderCurrentTab(activeTab)}
  92. </div>
  93. );
  94. }
  95. }
  96. interface TabItemParams {
  97. tab: PanelEditorTab;
  98. activeTab: string;
  99. onClick: (tab: PanelEditorTab) => void;
  100. }
  101. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  102. const tabClasses = classNames({
  103. 'gf-tabs-link': true,
  104. active: activeTab === tab.id,
  105. });
  106. return (
  107. <li className="gf-tabs-item" onClick={() => onClick(tab)}>
  108. <a className={tabClasses}>
  109. <i className={tab.icon} /> {tab.text}
  110. </a>
  111. </li>
  112. );
  113. }