PanelEditor.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. {this.renderPanelOptions()}
  50. </div>
  51. );
  52. }
  53. onChangeTab = (tab: PanelEditorTab) => {
  54. store.dispatch(
  55. updateLocation({
  56. query: { tab: tab.id },
  57. partial: true,
  58. })
  59. );
  60. };
  61. onClose = () => {
  62. store.dispatch(
  63. updateLocation({
  64. query: { tab: null, fullscreen: null, edit: null },
  65. partial: true,
  66. })
  67. );
  68. };
  69. render() {
  70. const { location } = store.getState();
  71. const activeTab = location.query.tab || 'queries';
  72. return (
  73. <div className="panel-editor-container__editor">
  74. <div className="panel-editor-resizer">
  75. <div className="panel-editor-resizer__handle">
  76. <div className="panel-editor-resizer__handle-dots" />
  77. </div>
  78. </div>
  79. <div className="panel-editor__aside">
  80. <h2 className="panel-editor__aside-header">
  81. <i className="fa fa-cog" />
  82. Edit Panel
  83. </h2>
  84. {this.tabs.map(tab => {
  85. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  86. })}
  87. <div className="panel-editor__aside-actions">
  88. <button className="btn btn-success" onClick={this.onClose}>
  89. Back to dashboard
  90. </button>
  91. <button className="btn btn-inverse" onClick={this.onClose}>
  92. Discard changes
  93. </button>
  94. </div>
  95. </div>
  96. <div className="panel-editor__content">
  97. <CustomScrollbar>
  98. {activeTab === 'queries' && this.renderQueriesTab()}
  99. {activeTab === 'visualization' && this.renderVizTab()}
  100. </CustomScrollbar>
  101. </div>
  102. </div>
  103. );
  104. }
  105. }
  106. interface TabItemParams {
  107. tab: PanelEditorTab;
  108. activeTab: string;
  109. onClick: (tab: PanelEditorTab) => void;
  110. }
  111. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  112. const tabClasses = classNames({
  113. 'panel-editor__aside-item': true,
  114. active: activeTab === tab.id,
  115. });
  116. return (
  117. <a className={tabClasses} onClick={() => onClick(tab)}>
  118. <i className={tab.icon} /> {tab.text}
  119. </a>
  120. );
  121. }