AddPanelWidget.tsx 5.2 KB

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