AddPanelWidget.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 = _.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: { x: gridPos.x, y: gridPos.y, w: gridPos.w, h: gridPos.h },
  85. };
  86. // apply panel template / defaults
  87. if (panelPluginInfo.defaults) {
  88. _.defaults(newPanel, panelPluginInfo.defaults);
  89. newPanel.title = panelPluginInfo.defaults.title;
  90. store.delete(LS_PANEL_COPY_KEY);
  91. }
  92. dashboard.addPanel(newPanel);
  93. dashboard.removePanel(this.props.panel);
  94. };
  95. onCreateNewRow = () => {
  96. const dashboard = this.props.dashboard;
  97. const newRow: any = {
  98. type: 'row',
  99. title: 'Row title',
  100. gridPos: { x: 0, y: 0 },
  101. };
  102. dashboard.addPanel(newRow);
  103. dashboard.removePanel(this.props.panel);
  104. };
  105. renderOptionLink = (icon, text, onClick) => {
  106. return (
  107. <div>
  108. <a href="#" onClick={onClick} className="add-panel-widget__link btn btn-inverse">
  109. <div className="add-panel-widget__icon">
  110. <i className={`gicon gicon-${icon}`} />
  111. </div>
  112. <span>{text}</span>
  113. </a>
  114. </div>
  115. );
  116. };
  117. render() {
  118. const { copiedPanelPlugins } = this.state;
  119. return (
  120. <div className="panel-container add-panel-widget-container">
  121. <div className="add-panel-widget">
  122. <div className="add-panel-widget__header grid-drag-handle">
  123. <i className="gicon gicon-add-panel" />
  124. <span className="add-panel-widget__title">New Panel</span>
  125. <button className="add-panel-widget__close" onClick={this.handleCloseAddPanel}>
  126. <i className="fa fa-close" />
  127. </button>
  128. </div>
  129. <div className="add-panel-widget__btn-container">
  130. <div className="add-panel-widget__create">
  131. {this.renderOptionLink('queries', 'Add Query', this.onCreateNewPanel)}
  132. {this.renderOptionLink('visualization', 'Choose Visualization', () =>
  133. this.onCreateNewPanel('visualization')
  134. )}
  135. </div>
  136. <div className="add-panel-widget__actions">
  137. <button className="btn btn-inverse add-panel-widget__action" onClick={this.onCreateNewRow}>
  138. Convert to row
  139. </button>
  140. {copiedPanelPlugins.length === 1 && (
  141. <button
  142. className="btn btn-inverse add-panel-widget__action"
  143. onClick={() => this.onPasteCopiedPanel(copiedPanelPlugins[0])}
  144. >
  145. Paste copied panel
  146. </button>
  147. )}
  148. </div>
  149. </div>
  150. </div>
  151. </div>
  152. );
  153. }
  154. }