row_model.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import {Emitter, contextSrv, appEvents, assignModelProperties} from 'app/core/core';
  4. export class DashboardRow {
  5. panels: any;
  6. title: any;
  7. showTitle: any;
  8. titleSize: any;
  9. events: Emitter;
  10. span: number;
  11. height: number;
  12. collapse: boolean;
  13. defaults = {
  14. title: 'Dashboard Row',
  15. panels: [],
  16. showTitle: false,
  17. titleSize: 'h6',
  18. height: 250,
  19. repeat: null,
  20. repeatRowId: null,
  21. repeatIteration: null,
  22. collapse: false,
  23. };
  24. constructor(private model) {
  25. assignModelProperties(this, model, this.defaults);
  26. this.events = new Emitter();
  27. this.updateRowSpan();
  28. }
  29. getSaveModel() {
  30. assignModelProperties(this.model, this, this.defaults);
  31. return this.model;
  32. }
  33. updateRowSpan() {
  34. this.span = 0;
  35. for (let panel of this.panels) {
  36. this.span += panel.span;
  37. }
  38. }
  39. panelSpanChanged(alwaysSendEvent?) {
  40. var oldSpan = this.span;
  41. this.updateRowSpan();
  42. if (alwaysSendEvent || oldSpan !== this.span) {
  43. this.events.emit('span-changed');
  44. }
  45. }
  46. addPanel(panel) {
  47. var rowSpan = this.span;
  48. var panelCount = this.panels.length;
  49. var space = (12 - rowSpan) - panel.span;
  50. // try to make room of there is no space left
  51. if (space <= 0) {
  52. if (panelCount === 1) {
  53. this.panels[0].span = 6;
  54. panel.span = 6;
  55. } else if (panelCount === 2) {
  56. this.panels[0].span = 4;
  57. this.panels[1].span = 4;
  58. panel.span = 4;
  59. } else if (panelCount === 3) {
  60. this.panels[0].span = 3;
  61. this.panels[1].span = 3;
  62. this.panels[2].span = 3;
  63. panel.span = 3;
  64. }
  65. }
  66. this.panels.push(panel);
  67. this.events.emit('panel-added', panel);
  68. this.panelSpanChanged();
  69. }
  70. removePanel(panel, ask?) {
  71. if (ask !== false) {
  72. appEvents.emit('confirm-modal', {
  73. title: 'Remove Panel',
  74. text: 'Are you sure you want to remove this panel?',
  75. icon: 'fa-trash',
  76. yesText: 'Remove',
  77. onConfirm: () => {
  78. this.removePanel(panel, false);
  79. }
  80. });
  81. return;
  82. }
  83. var index = _.indexOf(this.panels, panel);
  84. this.panels.splice(index, 1);
  85. this.events.emit('panel-removed', panel);
  86. this.panelSpanChanged();
  87. }
  88. movePanel(fromIndex, toIndex) {
  89. this.panels.splice(toIndex, 0, this.panels.splice(fromIndex, 1)[0]);
  90. }
  91. destroy() {
  92. this.events.removeAllListeners();
  93. }
  94. copyPropertiesFromRowSource(source) {
  95. this.height = source.height;
  96. this.title = source.title;
  97. this.showTitle = source.showTitle;
  98. this.titleSize = source.titleSize;
  99. }
  100. toggleCollapse() {
  101. this.collapse = !this.collapse;
  102. }
  103. }