row_model.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import {Emitter, contextSrv} from 'app/core/core';
  4. import {assignModelProperties} from 'app/core/core';
  5. export class DashboardRow {
  6. panels: any;
  7. title: any;
  8. showTitle: any;
  9. titleSize: any;
  10. events: Emitter;
  11. span: number;
  12. defaults = {
  13. title: 'Dashboard Row',
  14. panels: [],
  15. showTitle: false,
  16. titleSize: 'h6',
  17. height: 250,
  18. isNew: false,
  19. };
  20. constructor(private model) {
  21. console.log(model.isNew);
  22. assignModelProperties(this, model, this.defaults);
  23. this.events = new Emitter();
  24. this.updateRowSpan();
  25. }
  26. getSaveModel() {
  27. assignModelProperties(this.model, this, this.defaults);
  28. return this.model;
  29. }
  30. updateRowSpan() {
  31. this.span = 0;
  32. for (let panel of this.panels) {
  33. this.span += panel.span;
  34. }
  35. }
  36. panelSpanChanged() {
  37. var oldSpan = this.span;
  38. this.updateRowSpan();
  39. if (oldSpan !== this.span) {
  40. this.events.emit('span-changed');
  41. }
  42. }
  43. addPanel(panel) {
  44. var rowSpan = this.span;
  45. var panelCount = this.panels.length;
  46. var space = (12 - rowSpan) - panel.span;
  47. // try to make room of there is no space left
  48. if (space <= 0) {
  49. if (panelCount === 1) {
  50. this.panels[0].span = 6;
  51. panel.span = 6;
  52. } else if (panelCount === 2) {
  53. this.panels[0].span = 4;
  54. this.panels[1].span = 4;
  55. panel.span = 4;
  56. }
  57. }
  58. this.panels.push(panel);
  59. this.events.emit('panel-added', panel);
  60. this.panelSpanChanged();
  61. }
  62. removePanel(panel) {
  63. var index = _.indexOf(this.panels, panel);
  64. this.panels.splice(index, 1);
  65. this.events.emit('panel-removed', panel);
  66. this.panelSpanChanged();
  67. }
  68. }