row_model.ts 2.7 KB

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