row_model.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. 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. appEvents.emit('confirm-modal', {
  77. title: 'Remove Panel',
  78. text: 'Are you sure you want to remove this panel?',
  79. icon: 'fa-trash',
  80. yesText: 'Remove',
  81. onConfirm: () => {
  82. this.removePanel(panel, false);
  83. }
  84. });
  85. return;
  86. }
  87. var index = _.indexOf(this.panels, panel);
  88. this.panels.splice(index, 1);
  89. this.events.emit('panel-removed', panel);
  90. this.panelSpanChanged();
  91. }
  92. movePanel(fromIndex, toIndex) {
  93. this.panels.splice(toIndex, 0, this.panels.splice(fromIndex, 1)[0]);
  94. }
  95. destroy() {
  96. this.events.removeAllListeners();
  97. }
  98. copyPropertiesFromRowSource(source) {
  99. this.height = source.height;
  100. this.title = source.title;
  101. this.showTitle = source.showTitle;
  102. this.titleSize = source.titleSize;
  103. }
  104. toggleCollapse() {
  105. this.collapse = !this.collapse;
  106. }
  107. }