AddPanelWidget.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Libraries
  2. import React from 'react';
  3. import _ from 'lodash';
  4. // Utils
  5. import config from 'app/core/config';
  6. import store from 'app/core/store';
  7. // Store
  8. import { store as reduxStore } from 'app/store/store';
  9. import { updateLocation } from 'app/core/actions';
  10. // Types
  11. import { PanelModel } from '../../state';
  12. import { DashboardModel } from '../../state';
  13. import { LS_PANEL_COPY_KEY } from 'app/core/constants';
  14. import { LocationUpdate } from 'app/types';
  15. export interface Props {
  16. panel: PanelModel;
  17. dashboard: DashboardModel;
  18. }
  19. export interface State {
  20. copiedPanelPlugins: any[];
  21. }
  22. export class AddPanelWidget extends React.Component<Props, State> {
  23. constructor(props) {
  24. super(props);
  25. this.handleCloseAddPanel = this.handleCloseAddPanel.bind(this);
  26. this.state = {
  27. copiedPanelPlugins: this.getCopiedPanelPlugins(),
  28. };
  29. }
  30. getCopiedPanelPlugins() {
  31. const panels = _.chain(config.panels)
  32. .filter({ hideFromList: false })
  33. .map(item => item)
  34. .value();
  35. const copiedPanels = [];
  36. const copiedPanelJson = store.get(LS_PANEL_COPY_KEY);
  37. if (copiedPanelJson) {
  38. const copiedPanel = JSON.parse(copiedPanelJson);
  39. const pluginInfo: any = _.find(panels, { id: copiedPanel.type });
  40. if (pluginInfo) {
  41. const pluginCopy = _.cloneDeep(pluginInfo);
  42. pluginCopy.name = copiedPanel.title;
  43. pluginCopy.sort = -1;
  44. pluginCopy.defaults = copiedPanel;
  45. copiedPanels.push(pluginCopy);
  46. }
  47. }
  48. return _.sortBy(copiedPanels, 'sort');
  49. }
  50. handleCloseAddPanel(evt) {
  51. evt.preventDefault();
  52. this.props.dashboard.removePanel(this.props.dashboard.panels[0]);
  53. }
  54. onCreateNewPanel = (tab = 'queries') => {
  55. const dashboard = this.props.dashboard;
  56. const { gridPos } = this.props.panel;
  57. const newPanel: any = {
  58. type: 'graph',
  59. title: 'Panel Title',
  60. gridPos: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
  61. };
  62. dashboard.addPanel(newPanel);
  63. dashboard.removePanel(this.props.panel);
  64. const location: LocationUpdate = {
  65. query: {
  66. panelId: newPanel.id,
  67. edit: true,
  68. fullscreen: true,
  69. },
  70. partial: true,
  71. };
  72. if (tab === 'visualization') {
  73. location.query.tab = 'visualization';
  74. location.query.openVizPicker = true;
  75. }
  76. reduxStore.dispatch(updateLocation(location));
  77. };
  78. onPasteCopiedPanel = panelPluginInfo => {
  79. const dashboard = this.props.dashboard;
  80. const { gridPos } = this.props.panel;
  81. const newPanel: any = {
  82. type: panelPluginInfo.id,
  83. title: 'Panel Title',
  84. gridPos: {
  85. x: gridPos.x,
  86. y: gridPos.y,
  87. w: panelPluginInfo.defaults.gridPos.w,
  88. h: panelPluginInfo.defaults.gridPos.h,
  89. },
  90. };
  91. // apply panel template / defaults
  92. if (panelPluginInfo.defaults) {
  93. _.defaults(newPanel, panelPluginInfo.defaults);
  94. newPanel.title = panelPluginInfo.defaults.title;
  95. store.delete(LS_PANEL_COPY_KEY);
  96. }
  97. dashboard.addPanel(newPanel);
  98. dashboard.removePanel(this.props.panel);
  99. };
  100. onCreateNewRow = () => {
  101. const dashboard = this.props.dashboard;
  102. const newRow: any = {
  103. type: 'row',
  104. title: 'Row title',
  105. gridPos: { x: 0, y: 0 },
  106. };
  107. dashboard.addPanel(newRow);
  108. dashboard.removePanel(this.props.panel);
  109. };
  110. renderOptionLink = (icon, text, onClick) => {
  111. return (
  112. <div>
  113. <a href="#" onClick={onClick} className="add-panel-widget__link btn btn-inverse">
  114. <div className="add-panel-widget__icon">
  115. <i className={`gicon gicon-${icon}`} />
  116. </div>
  117. <span>{text}</span>
  118. </a>
  119. </div>
  120. );
  121. };
  122. render() {
  123. const { copiedPanelPlugins } = this.state;
  124. return (
  125. <div className="panel-container add-panel-widget-container">
  126. <div className="add-panel-widget">
  127. <div className="add-panel-widget__header grid-drag-handle">
  128. <i className="gicon gicon-add-panel" />
  129. <span className="add-panel-widget__title">New Panel</span>
  130. <button className="add-panel-widget__close" onClick={this.handleCloseAddPanel}>
  131. <i className="fa fa-close" />
  132. </button>
  133. </div>
  134. <div className="add-panel-widget__btn-container">
  135. <div className="add-panel-widget__create">
  136. {this.renderOptionLink('queries', 'Add Query', this.onCreateNewPanel)}
  137. {this.renderOptionLink('visualization', 'Choose Visualization', () =>
  138. this.onCreateNewPanel('visualization')
  139. )}
  140. </div>
  141. <div className="add-panel-widget__actions">
  142. <button className="btn btn-inverse add-panel-widget__action" onClick={this.onCreateNewRow}>
  143. Convert to row
  144. </button>
  145. {copiedPanelPlugins.length === 1 && (
  146. <button
  147. className="btn btn-inverse add-panel-widget__action"
  148. onClick={() => this.onPasteCopiedPanel(copiedPanelPlugins[0])}
  149. >
  150. Paste copied panel
  151. </button>
  152. )}
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. );
  158. }
  159. }