row_model.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. defaults = {
  13. title: 'Dashboard Row',
  14. panels: [],
  15. showTitle: false,
  16. titleSize: 'h6',
  17. height: 250,
  18. isNew: false,
  19. repeat: null,
  20. repeatRowId: null,
  21. repeatIteration: null,
  22. };
  23. constructor(private model) {
  24. assignModelProperties(this, model, this.defaults);
  25. this.events = new Emitter();
  26. this.updateRowSpan();
  27. }
  28. getSaveModel() {
  29. assignModelProperties(this.model, this, this.defaults);
  30. return this.model;
  31. }
  32. updateRowSpan() {
  33. this.span = 0;
  34. for (let panel of this.panels) {
  35. this.span += panel.span;
  36. }
  37. }
  38. panelSpanChanged() {
  39. var oldSpan = this.span;
  40. this.updateRowSpan();
  41. if (oldSpan !== this.span) {
  42. this.events.emit('span-changed');
  43. }
  44. }
  45. addPanel(panel) {
  46. var rowSpan = this.span;
  47. var panelCount = this.panels.length;
  48. var space = (12 - rowSpan) - panel.span;
  49. // try to make room of there is no space left
  50. if (space <= 0) {
  51. if (panelCount === 1) {
  52. this.panels[0].span = 6;
  53. panel.span = 6;
  54. } else if (panelCount === 2) {
  55. this.panels[0].span = 4;
  56. this.panels[1].span = 4;
  57. panel.span = 4;
  58. } else if (panelCount === 3) {
  59. this.panels[0].span = 3;
  60. this.panels[1].span = 3;
  61. this.panels[2].span = 3;
  62. panel.span = 3;
  63. }
  64. }
  65. this.panels.push(panel);
  66. this.events.emit('panel-added', panel);
  67. this.panelSpanChanged();
  68. }
  69. removePanel(panel, ask?) {
  70. console.log('remove panel');
  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. }