row_model.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import {Emitter, 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. this.model = {};
  32. assignModelProperties(this.model, this, this.defaults);
  33. // remove properties that dont server persisted purpose
  34. delete this.model.isNew;
  35. return this.model;
  36. }
  37. updateRowSpan() {
  38. this.span = 0;
  39. for (let panel of this.panels) {
  40. this.span += panel.span;
  41. }
  42. }
  43. panelSpanChanged(alwaysSendEvent?) {
  44. var oldSpan = this.span;
  45. this.updateRowSpan();
  46. if (alwaysSendEvent || oldSpan !== this.span) {
  47. this.events.emit('span-changed');
  48. }
  49. }
  50. addPanel(panel) {
  51. var rowSpan = this.span;
  52. var panelCount = this.panels.length;
  53. var space = (12 - rowSpan) - panel.span;
  54. // try to make room of there is no space left
  55. if (space <= 0) {
  56. if (panelCount === 1) {
  57. this.panels[0].span = 6;
  58. panel.span = 6;
  59. } else if (panelCount === 2) {
  60. this.panels[0].span = 4;
  61. this.panels[1].span = 4;
  62. panel.span = 4;
  63. } else if (panelCount === 3) {
  64. this.panels[0].span = 3;
  65. this.panels[1].span = 3;
  66. this.panels[2].span = 3;
  67. panel.span = 3;
  68. }
  69. }
  70. this.panels.push(panel);
  71. this.events.emit('panel-added', panel);
  72. this.panelSpanChanged();
  73. }
  74. removePanel(panel, ask?) {
  75. if (ask !== false) {
  76. var text2, confirmText;
  77. if (panel.alert) {
  78. text2 = "Panel includes an alert rule, removing panel will also remove alert rule";
  79. confirmText = "YES";
  80. }
  81. appEvents.emit('confirm-modal', {
  82. title: 'Remove Panel',
  83. text: 'Are you sure you want to remove this panel?',
  84. text2: text2,
  85. icon: 'fa-trash',
  86. confirmText: confirmText,
  87. yesText: 'Remove',
  88. onConfirm: () => {
  89. this.removePanel(panel, false);
  90. }
  91. });
  92. return;
  93. }
  94. var index = _.indexOf(this.panels, panel);
  95. this.panels.splice(index, 1);
  96. this.events.emit('panel-removed', panel);
  97. this.panelSpanChanged();
  98. }
  99. movePanel(fromIndex, toIndex) {
  100. this.panels.splice(toIndex, 0, this.panels.splice(fromIndex, 1)[0]);
  101. }
  102. destroy() {
  103. this.events.removeAllListeners();
  104. }
  105. copyPropertiesFromRowSource(source) {
  106. this.height = source.height;
  107. this.title = source.title;
  108. this.showTitle = source.showTitle;
  109. this.titleSize = source.titleSize;
  110. }
  111. toggleCollapse() {
  112. this.collapse = !this.collapse;
  113. }
  114. }