panel_model.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. }
  49. getSaveModel() {
  50. const model: any = {};
  51. for (const property in this) {
  52. if (notPersistedProperties[property] || !this.hasOwnProperty(property)) {
  53. continue;
  54. }
  55. model[property] = _.cloneDeep(this[property]);
  56. }
  57. return model;
  58. }
  59. setViewMode(fullscreen: boolean, isEditing: boolean) {
  60. this.fullscreen = fullscreen;
  61. this.isEditing = isEditing;
  62. this.events.emit('panel-size-changed');
  63. }
  64. updateGridPos(newPos: GridPos) {
  65. let sizeChanged = false;
  66. if (this.gridPos.w !== newPos.w || this.gridPos.h !== newPos.h) {
  67. sizeChanged = true;
  68. }
  69. this.gridPos.x = newPos.x;
  70. this.gridPos.y = newPos.y;
  71. this.gridPos.w = newPos.w;
  72. this.gridPos.h = newPos.h;
  73. if (sizeChanged) {
  74. this.events.emit('panel-size-changed');
  75. }
  76. }
  77. resizeDone() {
  78. this.events.emit('panel-size-changed');
  79. }
  80. refresh() {
  81. this.hasRefreshed = true;
  82. this.events.emit('refresh');
  83. }
  84. render() {
  85. if (!this.hasRefreshed) {
  86. this.refresh();
  87. } else {
  88. this.events.emit('render');
  89. }
  90. }
  91. panelInitialized() {
  92. this.events.emit('panel-initialized');
  93. }
  94. initEditMode() {
  95. this.events.emit('panel-init-edit-mode');
  96. }
  97. changeType(pluginId: string) {
  98. this.type = pluginId;
  99. delete this.thresholds;
  100. delete this.alert;
  101. }
  102. destroy() {
  103. this.events.removeAllListeners();
  104. }
  105. }