PanelEditor.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { PureComponent } from 'react';
  2. import classNames from 'classnames';
  3. import { QueriesTab } from './QueriesTab';
  4. import { VizTypePicker } from './VizTypePicker';
  5. import CustomScrollbar from 'app/core/components/CustomScrollbar/CustomScrollbar';
  6. import { store } from 'app/store/configureStore';
  7. import { updateLocation } from 'app/core/actions';
  8. import { PanelModel } from '../panel_model';
  9. import { DashboardModel } from '../dashboard_model';
  10. import { PanelPlugin } from 'app/types/plugins';
  11. interface PanelEditorProps {
  12. panel: PanelModel;
  13. dashboard: DashboardModel;
  14. plugin: PanelPlugin;
  15. onTypeChanged: (newType: PanelPlugin) => void;
  16. }
  17. interface PanelEditorTab {
  18. id: string;
  19. text: string;
  20. icon: string;
  21. }
  22. export class PanelEditor extends PureComponent<PanelEditorProps> {
  23. tabs: PanelEditorTab[];
  24. constructor(props) {
  25. super(props);
  26. this.tabs = [
  27. { id: 'queries', text: 'Queries', icon: 'fa fa-database' },
  28. { id: 'visualization', text: 'Visualization', icon: 'fa fa-line-chart' },
  29. { id: 'alert', text: 'Alert', icon: 'gicon gicon-alert' },
  30. ];
  31. }
  32. renderQueriesTab() {
  33. return <QueriesTab panel={this.props.panel} dashboard={this.props.dashboard} />;
  34. }
  35. renderPanelOptions() {
  36. const { plugin, panel } = this.props;
  37. const { PanelOptionsComponent } = plugin.exports;
  38. if (PanelOptionsComponent) {
  39. return <PanelOptionsComponent options={panel.getOptions()} onChange={this.onPanelOptionsChanged} />;
  40. } else {
  41. return <p>Visualization has no options</p>;
  42. }
  43. }
  44. onPanelOptionsChanged = (options: any) => {
  45. this.props.panel.updateOptions(options);
  46. this.forceUpdate();
  47. };
  48. renderVizTab() {
  49. return (
  50. <div className="viz-editor">
  51. <VizTypePicker current={this.props.plugin} onTypeChanged={this.props.onTypeChanged} />
  52. {this.renderPanelOptions()}
  53. </div>
  54. );
  55. }
  56. onChangeTab = (tab: PanelEditorTab) => {
  57. store.dispatch(
  58. updateLocation({
  59. query: { tab: tab.id },
  60. partial: true,
  61. })
  62. );
  63. this.forceUpdate();
  64. };
  65. onClose = () => {
  66. store.dispatch(
  67. updateLocation({
  68. query: { tab: null, fullscreen: null, edit: null },
  69. partial: true,
  70. })
  71. );
  72. };
  73. render() {
  74. const { location } = store.getState();
  75. const activeTab = location.query.tab || 'queries';
  76. return (
  77. <div className="panel-editor-container__editor">
  78. <div className="panel-editor-resizer">
  79. <div className="panel-editor-resizer__handle">
  80. <div className="panel-editor-resizer__handle-dots" />
  81. </div>
  82. </div>
  83. <div className="panel-editor__aside">
  84. <h2 className="panel-editor__aside-header">
  85. <i className="fa fa-cog" />
  86. Edit Panel
  87. </h2>
  88. {this.tabs.map(tab => {
  89. return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  90. })}
  91. <div className="panel-editor__aside-actions">
  92. <button className="btn btn-secondary" onClick={this.onClose}>
  93. Back to dashboard
  94. </button>
  95. <button className="btn btn-inverse" onClick={this.onClose}>
  96. Discard changes
  97. </button>
  98. </div>
  99. </div>
  100. <div className="panel-editor__content">
  101. <CustomScrollbar>
  102. {activeTab === 'queries' && this.renderQueriesTab()}
  103. {activeTab === 'visualization' && this.renderVizTab()}
  104. </CustomScrollbar>
  105. </div>
  106. </div>
  107. );
  108. }
  109. }
  110. interface TabItemParams {
  111. tab: PanelEditorTab;
  112. activeTab: string;
  113. onClick: (tab: PanelEditorTab) => void;
  114. }
  115. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  116. const tabClasses = classNames({
  117. 'panel-editor__aside-item': true,
  118. active: activeTab === tab.id,
  119. });
  120. return (
  121. <a className={tabClasses} onClick={() => onClick(tab)}>
  122. <i className={tab.icon} /> {tab.text}
  123. </a>
  124. );
  125. }