PanelEditor.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import { PanelModel } from '../panel_model';
  4. import { DashboardModel } from '../dashboard_model';
  5. import { store } from 'app/store/configureStore';
  6. import { QueriesTab } from './QueriesTab';
  7. import { PanelPlugin, PluginExports } from 'app/types/plugins';
  8. import { VizTypePicker } from './VizTypePicker';
  9. import { updateLocation } from 'app/core/actions';
  10. import CustomScrollbar from 'app/core/components/CustomScrollbar/CustomScrollbar';
  11. interface PanelEditorProps {
  12. panel: PanelModel;
  13. dashboard: DashboardModel;
  14. panelType: string;
  15. pluginExports: PluginExports;
  16. onTypeChanged: (newType: PanelPlugin) => void;
  17. }
  18. interface PanelEditorTab {
  19. id: string;
  20. text: string;
  21. icon: string;
  22. }
  23. export class PanelEditor extends React.Component<PanelEditorProps, any> {
  24. tabs: PanelEditorTab[];
  25. constructor(props) {
  26. super(props);
  27. this.tabs = [
  28. { id: 'queries', text: 'Queries', icon: 'fa fa-database' },
  29. { id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
  30. { id: 'alert', text: 'Alert', icon: 'gicon gicon-alert' },
  31. ];
  32. }
  33. renderQueriesTab() {
  34. return <QueriesTab panel={this.props.panel} dashboard={this.props.dashboard} />;
  35. }
  36. renderPanelOptions() {
  37. const { pluginExports } = this.props;
  38. if (pluginExports.PanelOptions) {
  39. const PanelOptions = pluginExports.PanelOptions;
  40. return <PanelOptions />;
  41. } else {
  42. return <p>Visualization has no options</p>;
  43. }
  44. }
  45. renderVizTab() {
  46. return (
  47. <div className="viz-editor">
  48. <VizTypePicker currentType={this.props.panel.type} onTypeChanged={this.props.onTypeChanged} />
  49. <h5 className="page-heading p-t-2">Options</h5>
  50. {this.renderPanelOptions()}
  51. </div>
  52. );
  53. }
  54. onChangeTab = (tab: PanelEditorTab) => {
  55. store.dispatch(
  56. updateLocation({
  57. query: { tab: tab.id },
  58. partial: true,
  59. })
  60. );
  61. };
  62. render() {
  63. const { location } = store.getState();
  64. const activeTab = location.query.tab || 'queries';
  65. return (
  66. <div className="panel-editor-container__editor">
  67. <div className="panel-editor__aside">
  68. <h2 className="panel-editor__aside-header">
  69. <i className="fa fa-cog" />
  70. Edit Panel
  71. </h2>
  72. {this.tabs.map(tab => {
  73. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  74. })}
  75. <div className="dashboard-settings__aside-actions">
  76. <button className="btn btn-inverse" ng-click="ctrl.exitFullscreen();">
  77. <i className="fa fa-remove" /> Close
  78. </button>
  79. </div>
  80. </div>
  81. <div className="panel-editor__content">
  82. <CustomScrollbar>
  83. {activeTab === 'queries' && this.renderQueriesTab()}
  84. {activeTab === 'visualization' && this.renderVizTab()}
  85. </CustomScrollbar>
  86. </div>
  87. </div>
  88. );
  89. }
  90. }
  91. interface TabItemParams {
  92. tab: PanelEditorTab;
  93. activeTab: string;
  94. onClick: (tab: PanelEditorTab) => void;
  95. }
  96. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  97. const tabClasses = classNames({
  98. 'dashboard-settings__nav-item': true,
  99. active: activeTab === tab.id,
  100. });
  101. return (
  102. <a className={tabClasses} onClick={() => onClick(tab)}>
  103. <i className={tab.icon} /> {tab.text}
  104. </a>
  105. );
  106. }