panel_model.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { Emitter } from 'app/core/utils/emitter';
  2. import _ from 'lodash';
  3. export interface GridPos {
  4. x: number;
  5. y: number;
  6. w: number;
  7. h: number;
  8. static?: boolean;
  9. }
  10. const notPersistedProperties: { [str: string]: boolean } = {
  11. events: true,
  12. fullscreen: true,
  13. isEditing: true,
  14. hasRefreshed: true,
  15. };
  16. export class PanelModel {
  17. id: number;
  18. gridPos: GridPos;
  19. type: string;
  20. title: string;
  21. alert?: any;
  22. scopedVars?: any;
  23. repeat?: string;
  24. repeatIteration?: number;
  25. repeatPanelId?: number;
  26. repeatDirection?: string;
  27. repeatedByRow?: boolean;
  28. minSpan?: number;
  29. collapsed?: boolean;
  30. panels?: any;
  31. soloMode?: boolean;
  32. targets: any[];
  33. datasource: string;
  34. thresholds?: any;
  35. // non persisted
  36. fullscreen: boolean;
  37. isEditing: boolean;
  38. hasRefreshed: boolean;
  39. events: Emitter;
  40. constructor(model) {
  41. this.events = new Emitter();
  42. // copy properties from persisted model
  43. for (const property in model) {
  44. this[property] = model[property];
  45. }
  46. // defaults
  47. this.gridPos = this.gridPos || { x: 0, y: 0, h: 3, w: 6 };
  48. this.datasource = this.datasource || null;
  49. this.targets = this.targets || [{}];
  50. }
  51. getSaveModel() {
  52. const model: any = {};
  53. for (const property in this) {
  54. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  55. continue;
  56. }
  57. model[property] = _.cloneDeep(this[property]);
  58. }
  59. return model;
  60. }
  61. setViewMode(fullscreen: boolean, isEditing: boolean) {
  62. this.fullscreen = fullscreen;
  63. this.isEditing = isEditing;
  64. this.events.emit('panel-size-changed');
  65. }
  66. updateGridPos(newPos: GridPos) {
  67. let sizeChanged = false;
  68. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  69. sizeChanged = true;
  70. }
  71. this.gridPos.x = newPos.x;
  72. this.gridPos.y = newPos.y;
  73. this.gridPos.w = newPos.w;
  74. this.gridPos.h = newPos.h;
  75. if (sizeChanged) {
  76. this.events.emit('panel-size-changed');
  77. }
  78. }
  79. resizeDone() {
  80. this.events.emit('panel-size-changed');
  81. }
  82. refresh() {
  83. this.hasRefreshed = true;
  84. this.events.emit('refresh');
  85. }
  86. render() {
  87. if (!this.hasRefreshed) {
  88. this.refresh();
  89. } else {
  90. this.events.emit('render');
  91. }
  92. }
  93. panelInitialized() {
  94. this.events.emit('panel-initialized');
  95. }
  96. initEditMode() {
  97. this.events.emit('panel-init-edit-mode');
  98. }
  99. changeType(pluginId: string) {
  100. this.type = pluginId;
  101. delete this.thresholds;
  102. delete this.alert;
  103. }
  104. destroy() {
  105. this.events.removeAllListeners();
  106. }
  107. }