DashboardRow.tsx 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import { PanelModel } from '../panel_model';
  4. import { PanelContainer } from './PanelContainer';
  5. import templateSrv from 'app/features/templating/template_srv';
  6. import appEvents from 'app/core/app_events';
  7. export interface DashboardRowProps {
  8. panel: PanelModel;
  9. getPanelContainer: () => PanelContainer;
  10. }
  11. export class DashboardRow extends React.Component<DashboardRowProps, any> {
  12. dashboard: any;
  13. panelContainer: any;
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. collapsed: this.props.panel.collapsed,
  18. };
  19. this.panelContainer = this.props.getPanelContainer();
  20. this.dashboard = this.panelContainer.getDashboard();
  21. this.toggle = this.toggle.bind(this);
  22. this.openSettings = this.openSettings.bind(this);
  23. }
  24. toggle() {
  25. this.dashboard.toggleRow(this.props.panel);
  26. this.setState(prevState => {
  27. return { collapsed: !prevState.collapsed };
  28. });
  29. }
  30. openSettings() {
  31. appEvents.emit('show-modal', {
  32. templateHtml: `<row-options row="model.row" on-updated="model.onUpdated()" on-delete="model.onDelete()" dismiss="dismiss()"></row-options>`,
  33. modalClass: 'modal--narrow',
  34. model: {
  35. row: this.props.panel,
  36. onUpdated: this.forceUpdate.bind(this),
  37. onDelete: this.onDelete.bind(this),
  38. },
  39. });
  40. }
  41. onDelete() {
  42. let text2 = '';
  43. if (this.props.panel.panels.length) {
  44. text2 = `This will also remove the row's ${this.props.panel.panels.length} panels`;
  45. }
  46. appEvents.emit('confirm-modal', {
  47. title: 'Delete Row',
  48. text: 'Are you sure you want to remove this row?',
  49. text2: text2,
  50. icon: 'fa-trash',
  51. onConfirm: () => {
  52. const panelContainer = this.props.getPanelContainer();
  53. const dashboard = panelContainer.getDashboard();
  54. dashboard.removePanel(this.props.panel);
  55. },
  56. });
  57. }
  58. render() {
  59. const classes = classNames({ 'dashboard-row': true, 'dashboard-row--collapsed': this.state.collapsed });
  60. const chevronClass = classNames({
  61. fa: true,
  62. 'fa-chevron-down': !this.state.collapsed,
  63. 'fa-chevron-right': this.state.collapsed,
  64. });
  65. let title = templateSrv.replaceWithText(this.props.panel.title, this.props.panel.scopedVars);
  66. const hiddenPanels = this.props.panel.panels ? this.props.panel.panels.length : 0;
  67. return (
  68. <div className={classes}>
  69. <a className="dashboard-row__title pointer" onClick={this.toggle}>
  70. <i className={chevronClass} />
  71. {title}
  72. <span className="dashboard-row__panel_count">({hiddenPanels} hidden panels)</span>
  73. </a>
  74. <div className="dashboard-row__actions">
  75. <a className="pointer" onClick={this.openSettings}>
  76. <i className="fa fa-cog" />
  77. </a>
  78. </div>
  79. <div className="dashboard-row__drag grid-drag-handle" />
  80. </div>
  81. );
  82. }
  83. }