PanelEditor.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. return this.renderAsTabs();
  75. // return this.renderAsBoxes();
  76. // const { location } = store.getState();
  77. // const activeTab = location.query.tab || 'queries';
  78. //
  79. // return (
  80. // <div className="panel-editor-container__editor">
  81. // <div className="panel-editor-resizer">
  82. // <div className="panel-editor-resizer__handle">
  83. // <div className="panel-editor-resizer__handle-dots" />
  84. // </div>
  85. // </div>
  86. // <div className="panel-editor__aside">
  87. // <h2 className="panel-editor__aside-header">
  88. // <i className="fa fa-cog" />
  89. // Edit Panel
  90. // </h2>
  91. //
  92. // {this.tabs.map(tab => {
  93. // return <TabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  94. // })}
  95. //
  96. // <div className="panel-editor__aside-actions">
  97. // <button className="btn btn-secondary" onClick={this.onClose}>
  98. // Back to dashboard
  99. // </button>
  100. // <button className="btn btn-inverse" onClick={this.onClose}>
  101. // Discard changes
  102. // </button>
  103. // </div>
  104. // </div>
  105. // <div className="panel-editor__content">
  106. // <CustomScrollbar>
  107. // {activeTab === 'queries' && this.renderQueriesTab()}
  108. // {activeTab === 'visualization' && this.renderVizTab()}
  109. // </CustomScrollbar>
  110. // </div>
  111. // </div>
  112. // );
  113. }
  114. renderAsTabs() {
  115. const { location } = store.getState();
  116. const { panel } = this.props;
  117. const activeTab = location.query.tab || 'queries';
  118. return (
  119. <div className="panel-editor-container__editor">
  120. <div className="panel-editor-resizer">
  121. <div className="panel-editor-resizer__handle">
  122. <div className="panel-editor-resizer__handle-dots" />
  123. </div>
  124. </div>
  125. <div className="tabbed-view tabbed-view--new">
  126. <div className="tabbed-view-header">
  127. <h3 className="tabbed-view-panel-title">{panel.title}</h3>
  128. <ul className="gf-tabs">
  129. {this.tabs.map(tab => {
  130. return <OldTabItem tab={tab} activeTab={activeTab} onClick={this.onChangeTab} key={tab.id} />;
  131. })}
  132. </ul>
  133. <button className="tabbed-view-close-btn" onClick={this.onClose}>
  134. <i className="fa fa-remove" />
  135. </button>
  136. </div>
  137. <div className="tabbed-view-body">
  138. <CustomScrollbar>
  139. {activeTab === 'queries' && this.renderQueriesTab()}
  140. {activeTab === 'visualization' && this.renderVizTab()}
  141. </CustomScrollbar>
  142. </div>
  143. </div>
  144. </div>
  145. );
  146. }
  147. renderAsBoxes() {
  148. const { location } = store.getState();
  149. const { panel } = this.props;
  150. const activeTab = location.query.tab || 'queries';
  151. return (
  152. <div className="panel-editor-container__editor">
  153. <div className="panel-editor-resizer">
  154. <div className="panel-editor-resizer__handle">
  155. <div className="panel-editor-resizer__handle-dots" />
  156. </div>
  157. </div>
  158. <CustomScrollbar>
  159. {this.renderQueriesTab()}
  160. {this.renderVizTab()}
  161. </CustomScrollbar>
  162. </div>
  163. );
  164. }
  165. }
  166. interface TabItemParams {
  167. tab: PanelEditorTab;
  168. activeTab: string;
  169. onClick: (tab: PanelEditorTab) => void;
  170. }
  171. function TabItem({ tab, activeTab, onClick }: TabItemParams) {
  172. const tabClasses = classNames({
  173. 'panel-editor__aside-item': true,
  174. active: activeTab === tab.id,
  175. });
  176. return (
  177. <a className={tabClasses} onClick={() => onClick(tab)}>
  178. <i className={tab.icon} /> {tab.text}
  179. </a>
  180. );
  181. }
  182. function OldTabItem({ tab, activeTab, onClick }: TabItemParams) {
  183. const tabClasses = classNames({
  184. 'gf-tabs-link': true,
  185. active: activeTab === tab.id,
  186. });
  187. return (
  188. <li className="gf-tabs-item" onClick={() => onClick(tab)}>
  189. <a className={tabClasses}>{tab.text}</a>
  190. </li>
  191. );
  192. }